-
Notifications
You must be signed in to change notification settings - Fork 1
/
SSL.py
128 lines (107 loc) · 5.05 KB
/
SSL.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import torch.nn as nn
from torch.autograd import Variable
from torch.utils import data
from PIL import Image
from tqdm import tqdm
import os
import torch
import numpy as np
from dataset.cityscapes_dataset import cityscapesDataSet
from model.build_BiSeNet import BiSeNet
IMG_MEAN = np.array((104.00698793, 116.66876762, 122.67891434), dtype=np.float32)
def ssl(model, save_path, num_classes, batch_size, num_workers, crop_size, fixed_threshold=True):
"""
Save pseudo-labels for target images in a folder. The labels can be chosen either with a
fixed or a variable threshold
"""
if not os.path.exists(save_path):
os.makedirs(save_path)
model.eval()
model.cuda(0)
targetloader = data.DataLoader(
cityscapesDataSet("Cityscapes", "Cityscapes/train.txt", mean=IMG_MEAN, crop_size=crop_size),
batch_size=batch_size, shuffle=True, num_workers=num_workers,
pin_memory=True)
predicted_label = np.zeros((len(targetloader), 512, 1024), dtype=np.uint8)
image_name = []
images = []
if fixed_threshold:
classified_pixels = 0.0
fixed_thres = 0.9
for index, batch in enumerate(tqdm(targetloader)):
image, _, name = batch
output = model(Variable(image).cuda())
output = nn.functional.softmax(output, dim=1)
output = nn.functional.upsample(output, (512, 1024), mode='bilinear', align_corners=True).cpu().data[
0].numpy()
output = output.transpose(1, 2, 0)
label, prob = np.argmax(output, axis=2), np.max(output, axis=2)
# Remove pixels whose confidence is lower than the threshold