-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathvisualize.py
181 lines (144 loc) · 7.65 KB
/
visualize.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
import torch.nn as nn
import torch.optim as optim
import torch.utils.data
import torch.backends.cudnn as cudnn
import torchvision
from torchvision import transforms as transforms
import numpy as np
import visdom
import torch.nn.functional as F
from CocoDetection import CocoDetection
from visualizeImg import *
#from PIL import Image
#import torch
import argparse
import os
from models.RMA_module_with_priori import RMA_module
from models.loss_with_priori import loss_function
from utils import get_target_transform as target_trans
# GPU setting
os.environ.setdefault("CUDA_VISIBLE_DEVICES", "2")
# ==================================================================
# Constants
# ==================================================================
EPOCH = 45 # number of times for each run-through
BATCH_SIZE = 16 # number of images for each epoch
N = 512 # size of input images (512 or 640)
TOPK = 3 # top k highest-ranked labels
GPU_IN_USE = torch.cuda.is_available() # whether using GPU
DIR_TEST_IMAGES = '../dataset/val2017/'
PATH_TEST_ANNFILE = '../dataset/annotations/instances_val2017.json'
PATH_MODEL_PARAMS = './params/params_with_priori.pkl'
NUM_CATEGORIES = 80
OUTPUT_INTERVAL = 100
# ==================================================================
# Parser Initialization
# ==================================================================
parser = argparse.ArgumentParser(description='Pytorch Implementation of ICCV2017_AttentionImageClass')
parser.add_argument('--testBatchSize', default=BATCH_SIZE, type=int, help='testing batch size')
parser.add_argument('--pathModelParams', default=PATH_MODEL_PARAMS, type=str, help='path of model parameters')
parser.add_argument('--loadModel', default=True, type=bool, help='load model parameters')
args = parser.parse_args()
# ==================================================================
# Prepare Dataset(training & test)
# ==================================================================
print('***** Prepare Data ******')
# transforms of test dataset
#normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
# std=[0.229, 0.224, 0.225])
test_transforms = transforms.Compose([
transforms.Resize((N, N)),
transforms.ToTensor(),
])
test_dataset = CocoDetection(root=DIR_TEST_IMAGES, annFile=PATH_TEST_ANNFILE,
transform=test_transforms, target_transform=target_trans)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=args.testBatchSize, shuffle=False, num_workers=2)
print('Data Preparation : Finished')
# ==================================================================
# Prepare Model
# ==================================================================
print('\n***** Prepare Model *****')
vgg16 = torchvision.models.vgg16(pretrained=True)
extract_features = vgg16.features
RMA = RMA_module(lstm_input_size=14, lstm_hidden_size=4096, zk_size=4096)
if args.loadModel:
RMA.load_state_dict(torch.load(args.pathModelParams))
if GPU_IN_USE:
print('CUDA_VISIBLE_DEVICES:', os.environ['CUDA_VISIBLE_DEVICES'])
print('cuda: move all model parameters and buffers to the GPU')
extract_features.cuda()
RMA.cuda()
cudnn.benchmark = True
print('Model Preparation : Finished')
# Test
# ================================================================================
# data: [torch.cuda.FloatTensor of size [batch_size, 3, N, N] N=512/640]
# target: [torch.cuda.FloatTensor of size [batch_size, num_categories]]
# output: [torch.cuda.FloatTensor of size [batch_size, num_categories]]
# prediction: [
# [torch.cuda.FloatTensor of size [batch_size, TOPK] (TOPK)],
# [torch.cuda.LongTensor of size [batch_size, TOPK] (index of TOPK)]
# ]
# ================================================================================
def test():
print('test:')
#RMA.eval() # set the module in evaluation mode
sum_prediction_label = torch.zeros(1, 80) + 1e-6
sum_correct_prediction_label = torch.zeros(1, 80)
sum_ground_truth_label = torch.zeros(1, 80)
for batch_num, (data, target, original_imgs) in enumerate(test_loader):
if target.sum() == 0:
continue
target = target.index_select(0, torch.nonzero(target.sum(dim=1)).view(-1))
data = data.index_select(0, torch.nonzero(target.sum(dim=1)).view(-1))
original_imgs = original_imgs.index_select(0, torch.nonzero(target.sum(dim=1)).view(-1))
#print('original_imgs ', original_imgs.size())
if GPU_IN_USE:
data, target = data.cuda(), target.cuda() # set up GPU Tensor
f_I = extract_features(data)
output, M, scores = RMA(f_I, return_whole_scores=True)
#total_thetas.append(M)
#total_scores.append(scores)
#visualize_attentional_regions(original_imgs, M[1:, :, :, :], scores)
visualize_attentional_regions(original_imgs, M, scores)
prediction = torch.topk(F.softmax(output, dim=1), 10, dim=1)
filter = prediction[0].eq(0.1) + prediction[0].gt(0.1)
prediction_index = torch.mul(prediction[1]+1, filter.type(torch.cuda.LongTensor))
extend_eye_mat = torch.cat((torch.zeros(1, 80), torch.eye(80)), 0)
prediction_label = extend_eye_mat[prediction_index.view(-1)].view(-1, 10, 80).sum(dim=1)
correct_prediction_label = (target.cpu().byte() & prediction_label.byte()).type(torch.FloatTensor)
#count the sum of label vector
sum_prediction_label += prediction_label.sum(dim=0)
sum_correct_prediction_label += correct_prediction_label.sum(dim=0)
sum_ground_truth_label += target.cpu().sum(dim=0)
#for i in range(0, target.size(0)):
# print('-----------------')
# print('ground-truth: ', target[i].nonzero().view(-1))
# print('prediction: ', prediction_index[i] - 1)
# print('-----------------')
if batch_num % OUTPUT_INTERVAL == 0:
print(batch_num)
#print('loss %.3f (batch %d)' % (test_loss / (batch_num+1), batch_num+1))
#evaluation metrics
o_p = torch.div(sum_correct_prediction_label.sum(), sum_prediction_label.sum())
o_r = torch.div(sum_correct_prediction_label.sum(), sum_ground_truth_label.sum())
of1 = torch.div(2 * o_p * o_r, o_p + o_r)
c_p = (torch.div(sum_correct_prediction_label, sum_prediction_label)).sum() / NUM_CATEGORIES
c_r = (torch.div(sum_correct_prediction_label, sum_ground_truth_label)).sum() / NUM_CATEGORIES
cf1 = torch.div(2 * c_p * c_r, c_p + c_r)
print('-------------------------------------------------------------')
print('| CP | CR | CF1 | OP | OR | OF1 |')
print('-------------------------------------------------------------')
print('| %.3f | %.3f | %.3f | %.3f | %.3f | %.3f |' % (c_p, c_r, cf1, o_p, o_r, of1))
print('-------------------------------------------------------------')
# ==================================================================
# Save Parameters of Test
# ==================================================================
#def save():
# torch.save(RMA.state_dict(), args.pathModelParams)
# print('Checkpoint saved to {}'.format(args.pathModelParams))
# ==================================================================
# Main
# ==================================================================
with torch.no_grad():
test()