-
Notifications
You must be signed in to change notification settings - Fork 0
/
synthetic_inference_real_data.py
216 lines (150 loc) · 9.22 KB
/
synthetic_inference_real_data.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import argparse
import yaml
import sys
from tqdm import tqdm
import os
from PIL import Image
from torchvision import transforms
import torch
sys.path.append(".")
from utils import util
from dataset import make_data_loader
from models import build_model
from utils import settings
import numpy as np
def convert_labels(label_tensor):
label_tensor[label_tensor == 0] = 1
label_tensor[label_tensor == 255] = 2
label_tensor[label_tensor == 125] = 3
return label_tensor
def preprocess_input(cfg, image, label):
image = transforms.functional.resize(image, (cfg['TRAINING']['IMAGE_SIZE_W'], cfg['TRAINING']['IMAGE_SIZE_H']),
Image.BICUBIC)
label = transforms.functional.resize(label,
(cfg['TRAINING']['IMAGE_SIZE_W'], cfg['TRAINING']['IMAGE_SIZE_H']),
Image.NEAREST)
label = convert_labels(np.array(label))
image = transforms.functional.to_tensor(np.array(image))
image_tensor = transforms.functional.normalize(image, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
label = torch.from_numpy(label).unsqueeze(0)
return image_tensor, label
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Inference for synthetic data - SPADE model')
parser.add_argument('--path_ymlfile', type=str, default='configs/inference.yaml', help='Path to yaml file.')
opt = parser.parse_args()
with open(opt.path_ymlfile, 'r') as ymlfile:
cfg = yaml.load(ymlfile)
_device = settings.initialize_cuda_and_logging(cfg)
train_loader, val_loader = make_data_loader(cfg)
model = build_model(cfg)
input_folders = ['/cvlabdata2/cvlab/datasets_anastasiia/Datasets/Dislocations/Synthetic_Dislocations_V3/dislocations_dataset/dislocations/train/segmentation_left_image/',
'/cvlabdata2/cvlab/datasets_anastasiia/Datasets/Dislocations/Synthetic_Dislocations_V3/dislocations_dataset/dislocations/train/segmentation_right_image/']
input_folders_test = ['/cvlabdata2/cvlab/datasets_anastasiia/Datasets/Dislocations/Synthetic_Dislocations_V3/dislocations_dataset/dislocations/test/segmentation_left_image/',
'/cvlabdata2/cvlab/datasets_anastasiia/Datasets/Dislocations/Synthetic_Dislocations_V3/dislocations_dataset/dislocations/test/segmentation_right_image/']
input_folders_val = ['/cvlabdata2/cvlab/datasets_anastasiia/Datasets/Dislocations/Synthetic_Dislocations_V3/dislocations_dataset/dislocations/val/segmentation_left_image/',
'/cvlabdata2/cvlab/datasets_anastasiia/Datasets/Dislocations/Synthetic_Dislocations_V3/dislocations_dataset/dislocations/val/segmentation_right_image/']
output_folders = ['/cvlabdata2/cvlab/datasets_anastasiia/Datasets/Dislocations/Synthetic_Dislocations_V3/dislocations_dataset/dislocations/train/left_image/',
'/cvlabdata2/cvlab/datasets_anastasiia/Datasets/Dislocations/Synthetic_Dislocations_V3/dislocations_dataset/dislocations/train/right_image/']
output_folders_test = ['/cvlabdata2/cvlab/datasets_anastasiia/Datasets/Dislocations/Synthetic_Dislocations_V3/dislocations_dataset/dislocations/test/left_image/',
'/cvlabdata2/cvlab/datasets_anastasiia/Datasets/Dislocations/Synthetic_Dislocations_V3/dislocations_dataset/dislocations/test/right_image/']
output_folders_val = ['/cvlabdata2/cvlab/datasets_anastasiia/Datasets/Dislocations/Synthetic_Dislocations_V3/dislocations_dataset/dislocations/val/left_image/',
'/cvlabdata2/cvlab/datasets_anastasiia/Datasets/Dislocations/Synthetic_Dislocations_V3/dislocations_dataset/dislocations/val/right_image/']
for folder_ in output_folders_test:
if not os.path.exists(folder_):
os.makedirs(folder_)
for folder_ in output_folders_val:
if not os.path.exists(folder_):
os.makedirs(folder_)
for folder_ in output_folders:
if not os.path.exists(folder_):
os.makedirs(folder_)
style_directory_train = '/cvlabdata2/cvlab/datasets_anastasiia/Datasets/Dislocations/dislocations_segmentation_dataset/train_img/'
style_directory_test = '/cvlabdata2/cvlab/datasets_anastasiia/Datasets/Dislocations/dislocations_segmentation_dataset/val_img_test/'
style_images_train = os.listdir(style_directory_train)
style_images_test = os.listdir(style_directory_test)
# test
labels = os.listdir(input_folders_test[0])
for label_ in tqdm(labels):
img_name = np.random.choice(style_images_test, 1)[0]
image = Image.open(os.path.join(style_directory_test, img_name))
image = image.convert('RGB')
label_left = Image.open(os.path.join(input_folders_test[0], label_)).convert('L')
label_right = Image.open(os.path.join(input_folders_test[1], label_.replace('LEFT','RIGHT'))).convert('L')
image_tensor, label_tensor_left = preprocess_input(cfg, image, label_left)
_, label_tensor_right = preprocess_input(cfg, image, label_right)
seed = np.random.randint(1,100000)
data = {'label': label_tensor_left.unsqueeze(0),
'image': image_tensor.unsqueeze(0),
'path': os.path.join(input_folders_test[0], label_),
'seed':seed}
fake_image_left = model.forward(data, 'inference')
fake_image_left = util.tensor2im(fake_image_left, tile=False)
Image.fromarray(fake_image_left[0]).save(output_folders_test[0]+label_)
data = {'label': label_tensor_right.unsqueeze(0),
'image': image_tensor.unsqueeze(0),
'path': os.path.join(input_folders_test[1], label_.replace('LEFT','RIGHT')),
'seed':seed }
fake_image_right = model.forward(data, 'inference')
fake_image_right = util.tensor2im(fake_image_right, tile=False)
Image.fromarray(fake_image_right[0]).save(output_folders_test[1]+label_.replace('LEFT','RIGHT'))
# train
labels = os.listdir(input_folders[0])
for label_ in tqdm(labels):
img_name = np.random.choice(style_images_train, 1)[0]
label_left = Image.open(os.path.join(input_folders[0], label_)).convert('L')
label_right = Image.open(os.path.join(input_folders[1], label_.replace('LEFT','RIGHT'))).convert('L')
image = Image.open(os.path.join(style_directory_train, img_name))
image = image.convert('RGB')
image_tensor, label_tensor_left = preprocess_input(cfg, image, label_left)
_, label_tensor_right = preprocess_input(cfg, image, label_right)
seed = np.random.randint(1,100000)
data = {'label': label_tensor_left.unsqueeze(0),
'image': image_tensor.unsqueeze(0),
'path': os.path.join(input_folders[0], label_),
'seed':seed}
fake_image_left = model.forward(data, 'inference')
fake_image_left = util.tensor2im(fake_image_left, tile=False)
Image.fromarray(fake_image_left[0]).save(output_folders[0]+label_)
data = {'label': label_tensor_right.unsqueeze(0),
'image': image_tensor.unsqueeze(0),
'path': os.path.join(input_folders[1], label_.replace('LEFT','RIGHT')),
'seed':seed }
fake_image_right = model.forward(data, 'inference')
fake_image_right = util.tensor2im(fake_image_right, tile=False)
Image.fromarray(fake_image_right[0]).save(output_folders[1]+label_.replace('LEFT','RIGHT'))
# labels = os.listdir(input_folders_val[0])
# for label_ in tqdm(labels):
#
# img_name = np.random.choice(style_images_test, 1)[0]
#
# image = Image.open(os.path.join(style_directory_test, img_name))
# image = image.convert('RGB')
#
# label_left = Image.open(os.path.join(input_folders_val[0], label_)).convert('L')
# label_right = Image.open(os.path.join(input_folders_val[1], label_.replace('LEFT','RIGHT'))).convert('L')
#
# image_tensor, label_tensor_left = preprocess_input(cfg, image, label_left)
# _, label_tensor_right = preprocess_input(cfg, image, label_right)
#
# seed = np.random.randint(1,100000)
# data = {'label': label_tensor_left.unsqueeze(0),
# 'image': image_tensor.unsqueeze(0),
# 'path': os.path.join(input_folders_test[0], label_),
#
# 'seed':seed}
#
# fake_image_left = model.forward(data, 'inference')
#
# fake_image_left = util.tensor2im(fake_image_left, tile=False)
#
# Image.fromarray(fake_image_left[0]).save(output_folders_val[0]+label_)
#
# data = {'label': label_tensor_right.unsqueeze(0),
# 'image': image_tensor.unsqueeze(0),
# 'path': os.path.join(output_folders_val[1], label_.replace('LEFT','RIGHT')),
#
# 'seed':seed }
#
# fake_image_right = model.forward(data, 'inference')
# fake_image_right = util.tensor2im(fake_image_right, tile=False)
# Image.fromarray(fake_image_right[0]).save(output_folders_val[1]+label_.replace('LEFT','RIGHT'))