-
Notifications
You must be signed in to change notification settings - Fork 0
/
perform_tests_AE_mysemanticUI_gogui.py
416 lines (338 loc) · 15.1 KB
/
perform_tests_AE_mysemanticUI_gogui.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 20 11:01:07 2019
similar to perform_test_2.py
for the new rico split sets..
add argparse...
@author: dipu
"""
import os
import torch
from torchvision import transforms
import glob
import json
from collections import defaultdict
from PIL import Image
import pickle
from scipy.spatial.distance import cdist
import numpy as np
import argparse
import _init_paths
from BoundingBox import BoundingBox
from BoundingBoxes import BoundingBoxes
#from eval_metrics.get_overall_IOU import get_overall_IOU
from eval_metric_gogui.get_overall_Classwise_IOU import get_overall_Classwise_IOU
from eval_metric_gogui.get_overall_pix_acc import get_overall_pix_acc
from RICO_Dataset import RICO_Dataset
import models
from utils import mkdir_if_missing, load_checkpoint
import torch.nn as nn
h = 48
w = 24
class Autoencoder(nn.Module):
def __init__(self):
super(Autoencoder, self).__init__()
self.encoder = nn.Sequential(
# nn.Linear(w * h * 3, 11200),
# nn.ReLU(True),
nn.Linear(w * h * 3, 2048),
nn.ReLU(True),
nn.Linear(2048, 256),
nn.ReLU(True),
nn.Linear(256, 64))
self.decoder = nn.Sequential(
nn.Linear(64, 256),
nn.ReLU(True),
nn.Linear(256, 2048),
nn.ReLU(True),
# nn.Linear(2048, 11200),
# nn.ReLU(True),
nn.Linear(2048, w * h * 3),
nn.Tanh()
)
def forward(self, x):
x = x.view(x.size(0), -1)
x_enc = self.encoder(x)
x_rec = self.decoder(x_enc)
return x_enc, x_rec
def extract_features(data_loader, model):
model.eval()
torch.set_grad_enabled(False)
features = []
labels = []
for i, (imgs, im_fn) in enumerate(data_loader):
#for i, (imgs, im_fn, img25Chan) in enumerate(data_loader):
imgs = imgs.view(imgs.size(0), -1)
imgs = imgs.cuda()
x_enc, out = model(imgs)
outputs = x_enc.detach().cpu().numpy()
features.append(outputs)
labels += list(im_fn)
# print(i)
return features, labels
#data_dir = '/mnt/amber/scratch/Dipu/RICO/MySemanticUI/'
#data_dir = '/mnt/amber/scratch/Dipu/RICO/MySemanticUIDefault/'
data_dir = '/home/dipu/dipu_ps/codes/GraphEncoding-GoogleUI/SemanticGogUIs_Default/'
#UI_data = pickle.load(open('/mnt/amber/scratch/Dipu/RICO/UI_data.p', 'rb'))
#train_uis = UI_data['train_uis']
#
#UI_test_data = pickle.load(open("/mnt/amber/scratch/Dipu/RICO/UI_test_data.p", "rb"))
#query_uis = UI_test_data['query_uis']
#gallery_uis = UI_test_data['gallery_uis']
#
#rico_info = pickle.load(open('/home/dipu/codes/GraphEncoding-RICO/data/rico_box_info.pkl', 'rb'))
#rico_ids = list(rico_info.keys())
#
#rico_ids = [x+'.png' for x in rico_ids]
#gallery_uis = list(set(rico_ids) & set(gallery_uis))
split_data = pickle.load(open('/home/dipu/dipu_ps/codes/GraphEncoding-GoogleUI/gogui_split42.pkl', 'rb'))
query_uis = split_data['query_uis']
gallery_uis = split_data['gallery_uis']
#data_dir = '/mnt/amber/scratch/Dipu/RICO/semantic_annotations'
#split_set_file = '/mnt/amber/scratch/Dipu/RICO/rico_split_set2.pkl'
#rico_split_set2 = pickle.load(open(split_set_file, 'rb'))
#
#train_uis = rico_split_set2['train_uis']
#query_uis = rico_split_set2['query_uis']
#gallery_uis = rico_split_set2['gallery_uis']
#
#train_uis = [x + '.png' for x in train_uis]
#query_uis = [x + '.png' for x in query_uis]
#gallery_uis = [x + '.png' for x in gallery_uis]
def main(args):
BATCH_SIZE = args.batch_size
if args.model_name == 'strided_512' or args.model_name == 'strided':
resize_shape = [255,127]
else:
resize_shape = [254,126]
data_transform = transforms.Compose([
transforms.Resize([h,w]), #transforms.Resize([255,127]) # transforms.Resize([254,126])
transforms.ToTensor(),
transforms.Normalize([0.5,0.5,0.5], [0.5,0.5,0.5])
])
# train_dataset = RICO_Dataset(train_uis, data_dir, transform= data_transform)
# train_loader = torch.utils.data.DataLoader(train_dataset, batch_size= BATCH_SIZE,
# drop_last = True, pin_memory=True, num_workers=16)
query_dataset = RICO_Dataset(query_uis, data_dir, transform= data_transform)
query_loader = torch.utils.data.DataLoader(query_dataset, batch_size= BATCH_SIZE, shuffle=False,
drop_last = False, pin_memory=True, num_workers=16)
gallery_dataset = RICO_Dataset(gallery_uis, data_dir, transform= data_transform)
gallery_loader = torch.utils.data.DataLoader(gallery_dataset, batch_size= BATCH_SIZE, shuffle=False,
drop_last = False, pin_memory=True, num_workers=16)
#Create boundingboxes class instance and intialize with all the bboxes in rico
#boundingBoxes = getBoundingBoxes()
boundingBoxes = getBoundingBoxes_from_info()
#Model
#model = models.create(args.model_name)
model = Autoencoder()
model_path = 'runs_AE_MySemanticUI_h%s%s/%s/ckp_ep20.pth.tar'%(h,w,args.model_name)
#model_path = 'runs_AE_MySemanticUI_h%s%s/%s/ckp_ep20.pth.tar/ckp_ep20.pth.tar'%(h,w,args.model_name)
resume = load_checkpoint(model_path)
model.load_state_dict(resume['state_dict'])
model = model.cuda()
model.eval()
onlyGallery = True
q_feat, q_fnames = extract_features(query_loader, model)
g_feat, g_fnames = extract_features(gallery_loader, model)
#t_feat, t_fnames = extract_features(train_loader, model)
q_feat = np.concatenate(q_feat)
g_feat = np.concatenate(g_feat)
#t_feat = np.concatenate(t_feat)
print('extracted features from query images with shape {}'.format( q_feat.shape))
print('extracted features from gallery images with shape {}'.format(g_feat.shape))
# if not(onlyGallery):
# g_feat = np.vstack((g_feat,t_feat))
# g_fnames = g_fnames + t_fnames
distances = cdist(q_feat, g_feat, metric= 'euclidean')
sort_inds = np.argsort(distances)
overallMeanClassIou, overallMeanWeightedClassIou, classwiseClassIoU = get_overall_Classwise_IOU(boundingBoxes,sort_inds,g_fnames,q_fnames, topk = [1,5,10])
overallMeanAvgPixAcc, overallMeanWeightedPixAcc, classPixAcc = get_overall_pix_acc(boundingBoxes,sort_inds,g_fnames,q_fnames, topk = [1,5,10])
#print(model_name)
#print('GAlleryOnly Flag:', onlyGallery)
print('The overallMeanClassIou = ' + str([ '{:.3f}'.format(x) for x in overallMeanClassIou]) + '\n')
print('The overallMeanWeightedClassIou = ' + str([ '{:.3f}'.format(x) for x in overallMeanWeightedClassIou]) + '\n')
print('The overallMeanAvgPixAcc = ' + str([ '{:.3f}'.format(x) for x in overallMeanAvgPixAcc]) + '\n')
print('The overallMeanWeightedPixAcc = ' + str([ '{:.3f}'.format(x) for x in overallMeanWeightedPixAcc]) + '\n')
#%% Preparing the dataset
def parse_ui_elements(sui):
"""
Parse the json file iteratively using recursion,, un winding all the nested chilfre
returns the dictionay of elements
"""
global counter
counter = 0
elements = defaultdict(dict)
def recurse(sui):
global counter
n_uis = len(sui['children'])
for i in range(n_uis):
[x1, y1, x2, y2] = sui['children'][i]['bounds']
elements[counter]['component_Label'] = sui['children'][i]['componentLabel']
elements[counter]['x'] = x1
elements[counter]['y'] = y1
elements[counter]['w'] = x2-x1
elements[counter]['h'] = y2-y1
elements[counter]['iconClass'] = sui['children'][i].get('iconClass')
elements[counter]['textButtonClass'] = sui['children'][i].get('textButtonClass')
counter +=1
if sui['children'][i].get('children') != None:
recurse(sui['children'][i])
recurse(sui)
return elements, counter
def getBoundingBoxes(data_dir = '/mnt/amber/scratch/Dipu/RICO/semantic_annotations/'):
allBoundingBoxes = BoundingBoxes()
files = glob.glob(data_dir+ "*.json")
for file in files:
imageName = os.path.split(file)[1]
imageName = imageName.replace(".json", "")
# print(imageName)
with open(file, "r") as f:
sui = json.load(f) # sui = semantic ui annotation.
elements, count = parse_ui_elements(sui)
for i in range(count):
box = elements[i]
bb = BoundingBox(
imageName,
box['component_Label'],
box['x'],
box['y'],
box['w'],
box['h'],
iconClass=box['iconClass'],
textButtonClass=box['textButtonClass'])
allBoundingBoxes.addBoundingBox(bb)
# testBoundingBoxes(allBoundingBoxes)
print('Collected {} bounding boxes from {} images'. format(allBoundingBoxes.count(), len(files) ))
return allBoundingBoxes
def getBoundingBoxes_from_info(data_dir = '/home/dipu/dipu_ps/codes/GraphEncoding-GoogleUI/'):
allBoundingBoxes = BoundingBoxes()
info = pickle.load (open(data_dir+ 'behance_box_info.pkl', 'rb'))
info =dict(info)
for key in info.keys():
# imageName = os.path.split(file)[1]
# imageName = imageName.replace(".json", "")
# print(imageName)
imageName = key
count = info[key]['nComponent']
for i in range(count):
xywh = info[key]['bbox'][i]
x = int(xywh[0])
y = int(xywh[1])
w = int(xywh[2])
h = int(xywh[3])
componentLabel = info[key]['componentLabel'][i]
bb = BoundingBox(
imageName,
componentLabel,
x,
y,
w,
h)
allBoundingBoxes.addBoundingBox(bb)
# testBoundingBoxes(allBoundingBoxes)
print('Collected {} bounding boxes from {} images'. format(allBoundingBoxes.count(), len(info.keys()) ))
return allBoundingBoxes
def testBoundingBoxes(boundingBoxes, samples = ['28970', '62918']):
#Visualize if every colored element is plotted or not.
from matplotlib import pyplot as plt
from PIL import Image
import matplotlib.patches as patches
samples = ['28970', '62918']
base_ui_path = '/mnt/amber/scratch/Dipu/RICO/semantic_annotations/'
base_im_path = '/mnt/amber/scratch/Dipu/RICO/combined/'
for sample in samples:
img = base_ui_path + sample + '.png'
img = Image.open(img).convert('RGB')
img2 = base_im_path + sample + '.jpg'
img2 = Image.open(img2).convert('RGB')
fig, ax = plt.subplots(1,2)
plt.setp(ax, xticklabels=[], yticklabels=[])
ax[0].imshow(img2)
ax[1].imshow(img)
bbs = boundingBoxes.getBoundingBoxesByImageName(sample)
for bb in bbs:
bb_cordinates = bb.getBoundingBox()
bb_class = bb.classId
# if bb_cordinates[2] < 0:
rect = patches.Rectangle((bb_cordinates[0], bb_cordinates[1]), bb_cordinates[2], bb_cordinates[3], linewidth=2, edgecolor='r', facecolor= 'none')
ax[1].add_patch(rect)
ax[1].text(bb_cordinates[0], bb_cordinates[1], bb_class, fontsize=8, color= 'r', verticalalignment='top')
plt.show()
def plot_classwiseResults(save_dir, classwiseResult, name):
from matplotlib import pyplot as plt
import collections
D = classwiseResult
for k, v in D.items():
if D[k] == []:
D[k]= [0,0]
else:
D[k] = [np.mean(v), len(v)]
D = sorted(D.items(), key=lambda kv: kv[1][0], reverse=False)
D = collections.OrderedDict(D)
fig, ax = plt.subplots()
for i, (k,v) in enumerate(D.items()):
ax.text( v[0]+0.001, i+0.25 , '{:.2f} ({})'.format(v[0], v[1]), fontsize=10, fontweight='bold', color= 'b', verticalalignment='top')
# print (i ,k ,v)
# fig.title(name)
fig.set_size_inches(7, 5)
ax.barh(range(len(D)), [x[0] for x in D.values()], align='center')
plt.yticks(range(len(D)), list(D.keys()), rotation='horizontal')
plt.subplots_adjust(bottom=0.3)
plt.tight_layout(h_pad=1)
plt.show()
plt.savefig('{}/{}.png'.format(save_dir, name), dpi = 500)
def plot_retrieved_images_and_uis(sort_inds, query_uis, gallery_uis, model_name):
from matplolib import pyplot as plt
base_im_path = '/mnt/scratch/Dipu/RICO/combined/'
base_ui_path = '/mnt/scratch/Dipu/RICO/semantic_annotations/'
for i in range((sort_inds.shape[0])): #range(1):
q_path = base_im_path + query_uis[i] + '.jpg'
q_img = Image.open(q_path).convert('RGB')
q_ui_path = base_ui_path + query_uis[i] + '.png'
q_ui = Image.open(q_ui_path).convert('RGB')
fig, ax = plt.subplots(2,6)
plt.setp(ax, xticklabels=[], yticklabels=[])
fig.suptitle('Query-%s, %s (Gallery_Only-Set)'%(i, model_name), fontsize=20)
#fig = plt.figure(1)
fig.set_size_inches(30, 10)
#f1 = fig.add_subplot(2,6,1)
ax[0,0].imshow(q_ui)
ax[0,0].axis('off')
ax[0,0].set_title('Query: %s '%(i) + query_uis[i] + '.png')
ax[1,0].imshow(q_img)
ax[1,0].axis('off')
ax[1,0].set_title('Query: %s '%(i) + query_uis[i] + '.jpg')
#plt.pause(0.1)
for j in range(5):
path = base_im_path + gallery_uis[sort_inds[i][j]] + '.jpg'
# print(gallery_uis[sort_inds[i][j]] )
im = Image.open(path).convert('RGB')
ui_path = base_ui_path + gallery_uis[sort_inds[i][j]] + '.png'
#print(gallery_uis[sort_inds[i][j]])
ui = Image.open(ui_path).convert('RGB')
ax[0,j+1].imshow(ui)
ax[0,j+1].axis('off')
ax[0,j+1].set_title('Rank: %s '%(j+1) + gallery_uis[sort_inds[i][j]] + '.png')
ax[1,j+1].imshow(im)
ax[1,j+1].axis('off')
ax[1,j+1].set_title('Rank: %s '%(j+1) + gallery_uis[sort_inds[i][j]] + '.jpg')
directory = 'Retrieved_Images_MysemanticUI/{}/Gallery_Only/'.format(model_name)
if not os.path.exists(directory):
os.makedirs(directory)
plt.savefig( directory + str(i) + '.png')
# plt.pause(0.1)
plt.close()
#print('Wait')
print(i)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
# optimization
parser.add_argument('--batch_size', default = 256, type=int, metavar='N',
help='mini-batch size (1 = pure stochastic) Default: 256')
# model
parser.add_argument('--model_name', default = 'upsample_512', type = str,
help = 'which CNN autoencoder: upsample or strided or strided_512 or upsample_512')
parser.add_argument('--gpu_id', type=str, default = '3', help = 'GPU ID')
args = parser.parse_args()
main(args)