forked from tzzcl/PSOL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_box_imagenet.py
146 lines (135 loc) · 5.61 KB
/
generate_box_imagenet.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
import os
import sys
import cv2
import json
import numpy as np
import torch
import torchvision.transforms as transforms
from torch.backends import cudnn
from torch.autograd import Variable
import torch.nn as nn
import torchvision
import torchvision.models as models
from PIL import Image
from skimage import measure
# from scipy.misc import imresize
from utils.func import *
from utils.vis import *
from utils.IoU import *
import argparse
from loader.ddt_imagenet_dataset import DDTImageNetDataset
parser = argparse.ArgumentParser(description='Parameters for DDT generate box')
parser.add_argument('--input_size',default=448,dest='input_size')
parser.add_argument('data',metavar='DIR',help='path to imagenet dataset')
parser.add_argument('--gpu',help='which gpu to use',default='4,5,6,7',dest='gpu')
parser.add_argument('--output_path',default='ImageNet/Projection/VGG16-448',dest='output_path')
parser.add_argument('--batch_size',default=256,dest='batch_size')
args = parser.parse_args()
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
os.environ['OMP_NUM_THREADS'] = "10"
os.environ['MKL_NUM_THREADS'] = "10"
cudnn.benchmark = True
model_ft = models.vgg16(pretrained=True)
model = model_ft.features
#removed = list(model.children())[:-1]
#model = torch.nn.Sequential(*removed)
model = torch.nn.DataParallel(model).cuda()
model.eval()
projdir = args.output_path
if not os.path.exists(projdir):
os.makedirs(projdir)
transform = transforms.Compose([
transforms.Resize((args.input_size,args.input_size)),
transforms.ToTensor(),
transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225))
])
batch_size = args.batch_size
a = DDTImageNetDataset(root=os.path.join(args.data,'train'),batch_size=args.batch_size, transforms=transform)
# print(classes[0])
for class_ind in range(1000):
#if class_ind == 10:
# import sys
# sys.exit()
now_class_dict = {}
feature_list = []
ddt_bbox = {}
with torch.no_grad():
for (input_img,path) in a[class_ind]:
input_img = to_variable(input_img)
output = model(input_img)
output = to_data(output)
output = torch.squeeze(output).numpy()
if len(output.shape) == 3:
output = np.expand_dims(output,0)
output = np.transpose(output,(0,2,3,1))
n,h,w,c = output.shape
for i in range(n):
now_class_dict[path[i]] = output[i,:,:,:]
output = np.reshape(output,(n*h*w,c))
feature_list.append(output)
X = np.concatenate(feature_list,axis=0)
mean_matrix = np.mean(X, 0)
X = X - mean_matrix
print("Before PCA")
trans_matrix = sk_pca(X, 1)
print("AFTER PCA")
cls = a.label_class_dict[class_ind]
# save json
d = {'mean_matrix': mean_matrix.tolist(), 'trans_matrix': trans_matrix.tolist()}
with open(os.path.join(projdir, '%s_trans.json' % cls), 'w') as f:
json.dump(d, f)
# load json
with open(os.path.join(projdir, '%s_trans.json' % cls), 'r') as f:
t = json.load(f)
mean_matrix = np.array(t['mean_matrix'])
trans_matrix = np.array(t['trans_matrix'])
print('trans_matrix shape is {}'.format(trans_matrix.shape))
cnt = 0
for k,v in now_class_dict.items():
w = 14
h = 14
he = 448
wi = 448
v = np.reshape(v,(h * w,512))
v = v - mean_matrix
heatmap = np.dot(v, trans_matrix.T)
heatmap = np.reshape(heatmap, (h, w))
highlight = np.zeros(heatmap.shape)
highlight[heatmap > 0] = 1
# max component
all_labels = measure.label(highlight)
highlight = np.zeros(highlight.shape)
highlight[all_labels == count_max(all_labels.tolist())] = 1
# visualize heatmap
# show highlight in origin image
highlight = np.round(highlight * 255)
highlight_big = cv2.resize(highlight, (he, wi), interpolation=cv2.INTER_NEAREST)
props = measure.regionprops(highlight_big.astype(int))
if len(props) == 0:
#print(highlight)
bbox = [0, 0, wi, he]
else:
temp = props[0]['bbox']
bbox = [temp[1], temp[0], temp[3], temp[2]]
temp_bbox = [bbox[0], bbox[1], bbox[2] - bbox[0], bbox[3] - bbox[1]]
temp_save_box = [x / 448 for x in temp_bbox]
ddt_bbox[os.path.join(cls, k)] = temp_save_box
highlight_big = np.expand_dims(np.asarray(highlight_big), 2)
highlight_3 = np.concatenate((np.zeros((he, wi, 1)), np.zeros((he, wi, 1))), axis=2)
highlight_3 = np.concatenate((highlight_3, highlight_big), axis=2)
cnt +=1
if cnt < 10:
savepath = 'ImageNet/Visualization/DDT/VGG16-vis-test/%s' % cls
if not os.path.exists(savepath):
os.makedirs(savepath)
from PIL import Image
raw_img = Image.open(k).convert("RGB")
raw_img = raw_img.resize((448,448))
raw_img = np.asarray(raw_img)
raw_img = cv2.cvtColor(raw_img,cv2.COLOR_BGR2RGB)
cv2.rectangle(raw_img, (temp_bbox[0], temp_bbox[1]),
(temp_bbox[2] + temp_bbox[0], temp_bbox[3] + temp_bbox[1]), (255, 0, 0), 4)
save_name = k.split('/')[-1]
cv2.imwrite(os.path.join(savepath, save_name), np.asarray(raw_img))
with open(os.path.join(projdir, '%s_bbox.json' % cls), 'w') as fp:
json.dump(ddt_bbox, fp)