forked from HelloJianHan/msrlOB
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun.py
More file actions
434 lines (382 loc) · 16.2 KB
/
run.py
File metadata and controls
434 lines (382 loc) · 16.2 KB
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import torch
import torchvision
from models.Q_net import Q_zoom, Q_refine
from data import load_images_names_in_data_set, get_bb_of_gt_from_pascal_xml_annotation
import torchvision.transforms as T
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import argparse
import os
from PIL import Image, ImageDraw
from utils import cal_iou, reward_func
import time
# hyper-parameters
BATCH_SIZE = 100
LR = 1e-6
GAMMA = 0.9
MEMORY_CAPACITY = 1000
Q_NETWORK_ITERATION = 100
epochs = 50
NUM_ACTIONS = 6
his_actions = 4
subscale = 1/2
NUM_STATES = 7*7*512+his_actions*NUM_ACTIONS
path_voc = "/home/hanj/dataset/VOCdevkit/VOC2007/"
path_voc = "/kaggle/input/pascal-voc-2007/VOCtrainval_06-Nov-2007/VOCdevkit/VOC2007/"
path_voc_test = "/kaggle/input/pascal-voc-2007/VOCtest_06-Nov-2007/VOCdevkit/VOC2007/"
class DQN():
"""docstring for DQN"""
def __init__(self, device):
super().__init__()
self.device = device
self.eval_net, self.target_net = Q_zoom(), Q_zoom()
self.eval_net.to(self.device)
self.target_net.to(self.device)
self.learn_step_counter = 0
self.memory_counter = 0
self.memory = np.zeros((MEMORY_CAPACITY, NUM_STATES * 2 + 2))
# why the NUM_STATE*2 +2
# When we store the memory, we put the state, action, reward and next_state in the memory
# here reward and action is a number, state is a ndarray
self.optimizer = torch.optim.Adam(self.eval_net.parameters(), lr=LR)
self.loss_func = nn.MSELoss()
def choose_action(self, state, EPISILO):
state = torch.unsqueeze(torch.FloatTensor(state), 0).to(
self.device) # get a 1D array
if np.random.randn() <= EPISILO: # random policy
action = np.random.randint(0, NUM_ACTIONS)
else: # greedy policy
action_value = self.eval_net.forward(state)
action = torch.max(action_value, 1)[1].cpu().item()
return action
def store_transition(self, state, action, reward, next_state):
transition = np.hstack((state, [action, reward], next_state))
index = self.memory_counter % MEMORY_CAPACITY
self.memory[index, :] = transition
self.memory_counter += 1
def learn(self):
# update the parameters
if self.learn_step_counter % Q_NETWORK_ITERATION == 0:
self.target_net.load_state_dict(self.eval_net.state_dict())
self.learn_step_counter += 1
# sample batch from memory
sample_index = np.random.choice(MEMORY_CAPACITY, BATCH_SIZE)
batch_memory = self.memory[sample_index, :]
batch_state = torch.FloatTensor(
batch_memory[:, :NUM_STATES]).to(self.device)
batch_action = torch.LongTensor(
batch_memory[:, NUM_STATES:NUM_STATES+1].astype(int)).to(self.device)
batch_reward = torch.FloatTensor(
batch_memory[:, NUM_STATES+1:NUM_STATES+2]).to(self.device)
batch_next_state = torch.FloatTensor(
batch_memory[:, -NUM_STATES:]).to(self.device)
# q_eval
q_eval = self.eval_net(batch_state).gather(1, batch_action)
q_next = self.target_net(batch_next_state).detach()
q_target_unterminated = batch_reward + GAMMA * \
q_next.max(1)[0].view(BATCH_SIZE, 1)
q_target = torch.where(
batch_action != 5, q_target_unterminated, batch_reward)
loss = self.loss_func(q_eval, q_target)
print("step loss is {:.3f}".format(loss.cpu().detach().item()))
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
def init_process(image, transform=None):
# image.show()
# time.sleep(5)
if transform:
image = transform(image)
return image.unsqueeze(0)
def inter_process(image, bbx, transform=None):
(left, upper, right, lower) = (bbx[0], bbx[2], bbx[1], bbx[3])
image_crop = image.crop((left, upper, right, lower))
# image_crop.show()
# time.sleep(5)
if transform:
image_crop = transform(image_crop)
return image_crop.unsqueeze(0)
# def update_bbx(bbx, action):
# new_bbx = np.zeros(4)
# if action == 0: # top left
# new_bbx[0] = bbx[0] # x1
# new_bbx[1] = bbx[0] + (bbx[1]-bbx[0]) * subscale # x2
# new_bbx[2] = bbx[2] # y1
# new_bbx[3] = bbx[2] + (bbx[3]-bbx[2]) * subscale # y2
# elif action == 1: # top right
# new_bbx[0] = bbx[1] - (bbx[1]-bbx[0]) * subscale # x1
# new_bbx[1] = bbx[1] # x2
# new_bbx[2] = bbx[2] # y1
# new_bbx[3] = bbx[2] + (bbx[3]-bbx[2]) * subscale # y2
# elif action == 2: # lower left
# new_bbx[0] = bbx[0] # x1
# new_bbx[1] = bbx[0] + (bbx[1]-bbx[0]) * subscale # x2
# new_bbx[2] = bbx[3] - (bbx[3]-bbx[2]) * subscale # y1
# new_bbx[3] = bbx[3] # y2
# elif action == 3: # lower right
# new_bbx[0] = bbx[1] - (bbx[1]-bbx[0]) * subscale # x1
# new_bbx[1] = bbx[1] # x2
# new_bbx[2] = bbx[3] - (bbx[3]-bbx[2]) * subscale # y1
# new_bbx[3] = bbx[3] # y2
# elif action == 4: # center
# new_bbx[0] = (bbx[0]+bbx[1])/2-(bbx[1]-bbx[0]) * subscale/2 # x1
# new_bbx[1] = (bbx[0]+bbx[1])/2+(bbx[1]-bbx[0]) * subscale/2 # x2
# new_bbx[2] = (bbx[2]+bbx[3])/2-(bbx[3]-bbx[2]) * subscale/2 # y1
# new_bbx[3] = (bbx[2]+bbx[3])/2+(bbx[3]-bbx[2]) * subscale/2 # y2
# elif action == 5:
# new_bbx = bbx
# return new_bbx
def update_bbx(bbx, action):
new_bbx = np.zeros(4)
if action == 0: # left
new_bbx[0] = bbx[0] # x1
new_bbx[1] = bbx[0] + (bbx[1]-bbx[0]) * subscale # x2
new_bbx[2] = bbx[2] # y1
new_bbx[3] = bbx[3] #+ (bbx[3]-bbx[2]) * subscale # y2
elif action == 1: # right
new_bbx[0] = bbx[1] - (bbx[1]-bbx[0]) * subscale # x1
new_bbx[1] = bbx[1] # x2
new_bbx[2] = bbx[2] # y1
new_bbx[3] = bbx[3] # + (bbx[3]-bbx[2]) * subscale # y2
elif action == 2: # lower
new_bbx[0] = bbx[0] # x1
new_bbx[1] = bbx[1] #+ (bbx[1]-bbx[0]) * subscale # x2
new_bbx[2] = bbx[3] - (bbx[3]-bbx[2]) * subscale # y1
new_bbx[3] = bbx[3] # y2
elif action == 3: # top
new_bbx[0] = bbx[0] #- (bbx[1]-bbx[0]) * subscale # x1
new_bbx[1] = bbx[1] # x2
new_bbx[2] = bbx[2] # y1
new_bbx[3] = bbx[2] + (bbx[3]-bbx[2]) * subscale # y2
elif action == 4: # center
new_bbx[0] = (bbx[0]+bbx[1])/2-(bbx[1]-bbx[0]) * subscale # x1
new_bbx[1] = (bbx[0]+bbx[1])/2+(bbx[1]-bbx[0]) * subscale # x2
new_bbx[2] = (bbx[2]+bbx[3])/2-(bbx[3]-bbx[2]) * subscale # y1
new_bbx[3] = (bbx[2]+bbx[3])/2+(bbx[3]-bbx[2]) * subscale # y2
elif action == 5:
new_bbx = bbx
return new_bbx
def main(args):
# best reward is set to -inf
best_reward = float("-inf")
# Class category of PASCAL that the RL agent will be searching
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
image_names = np.array(load_images_names_in_data_set(
'aeroplane_trainval', path_voc))
feature_exactrator = torchvision.models.vgg16(
pretrained=True).features.to(device)
single_plane_image_names = []
single_plane_image_gts = []
dqn = DQN(device)
EPISILO = args.EPISILO
subscale = args.Subscale
for image_name in image_names:
annotation = get_bb_of_gt_from_pascal_xml_annotation(
image_name, path_voc)
if (len(annotation) > 1):
continue
single_plane_image_names.append(image_name)
single_plane_image_gts.append(annotation[0][1:]) # [[x1,x2,y1,y2] ...]
trans = T.Compose([
T.Resize((224, 224)),
T.ToTensor(),
])
for i in range(epochs):
ep_reward = 0
for index, image_name in enumerate(single_plane_image_names):
image_path = os.path.join(
path_voc + "JPEGImages", image_name + ".jpg")
image_original = Image.open(image_path)
width, height = image_original.size
# image_original = image_original.resize((224,224))
bbx_gt = single_plane_image_gts[index]
# draw = ImageDraw.Draw(image_original)
# draw.rectangle([bbx_gt[0],bbx_gt[2],bbx_gt[1],bbx_gt[3]],outline='red')
# image_original.show()
# return
image = init_process(image_original, trans).to(device)
# print(image.shape)
bbx = [0, width, 0, height]
history_action = np.zeros(his_actions*NUM_ACTIONS)
with torch.no_grad():
vector = feature_exactrator(
image).cpu().detach().numpy().reshape(7*7*512)
state = np.concatenate([history_action, vector])
step = 0
while (step < 10):
iou = cal_iou(bbx, bbx_gt)
if iou > 0.5:
action = 5
else:
action = dqn.choose_action(state, EPISILO)
# print(action)
# execute action and step to new bbx
new_bbx = update_bbx(bbx, action)
reward = reward_func(bbx, new_bbx, bbx_gt, action)
# get new state
action_vec = np.zeros(NUM_ACTIONS)
action_vec[action] = 1.0
history_action = np.concatenate(
[history_action[NUM_ACTIONS:], action_vec])
with torch.no_grad():
vector = feature_exactrator(inter_process(image_original, new_bbx, trans).to(
device)).cpu().detach().numpy().reshape(7*7*512)
next_state = np.concatenate([history_action, vector])
# store transition
dqn.store_transition(state, action, reward, next_state)
ep_reward += reward
if dqn.memory_counter >= MEMORY_CAPACITY:
print("episode: {},".format(i), end=' ')
dqn.learn()
# termation
if action == 5:
break
state = next_state
bbx = new_bbx
step += 1
if (EPISILO > 0.1):
EPISILO -= 0.1
print("episode: {} , this epoch reward is {}".format(
i, round(ep_reward, 3))) # 0.001 precision
if ep_reward > best_reward:
best_reward = ep_reward
torch.save(dqn.eval_net.state_dict(), 'eval_net.pth')
print("model saved")
# save model's weights
# torch.save(dqn.eval_net.state_dict(), 'eval_net.pth')
# load model's weights and perform inference
dqn.eval_net.load_state_dict(torch.load('eval_net.pth'))
dqn.eval_net.eval()
dqn.eval_net.to(device)
print("model loaded")
# calculate the average IOU over test set
image_names = np.array(load_images_names_in_data_set(
'aeroplane_test', path_voc_test))
single_plane_image_names = []
single_plane_image_gts = []
for image_name in image_names:
annotation = get_bb_of_gt_from_pascal_xml_annotation(
image_name, path_voc_test)
if (len(annotation) > 1):
continue
single_plane_image_names.append(image_name)
single_plane_image_gts.append(annotation[0][1:]) # [[x1,x2,y1,y2] ...
# save single_plane_image_names and single_plane_image_gts for future use
np.save('single_plane_image_names.npy', single_plane_image_names)
np.save('single_plane_image_gts.npy', single_plane_image_gts)
print("single_plane_image_names and single_plane_image_gts saved")
# print out the average IOU
total_iou = 0
for index, image_name in enumerate(single_plane_image_names):
image_path = os.path.join(
path_voc_test + "JPEGImages", image_name + ".jpg")
image_original = Image.open(image_path)
width, height = image_original.size
bbx_gt = single_plane_image_gts[index]
image = init_process(image_original, trans).to(device)
bbx = [0, width, 0, height]
history_action = np.zeros(his_actions*NUM_ACTIONS)
with torch.no_grad():
vector = feature_exactrator(
image).cpu().detach().numpy().reshape(7*7*512)
state = np.concatenate([history_action, vector])
step = 0
while (step < 10):
iou = cal_iou(bbx, bbx_gt)
if iou > 0.5:
action = 5
else:
action = dqn.choose_action(state, EPISILO)
new_bbx = update_bbx(bbx, action)
if action == 5:
break
state = np.concatenate([history_action, vector])
bbx = new_bbx
step += 1
total_iou += cal_iou(bbx, bbx_gt)
# write out bbx, bbx_gt to file
with open('bbx.txt', 'a') as f:
f.write("bbx: {}, bbx_gt: {}\n".format(bbx, bbx_gt))
print("average IOU is {:.3f}".format(
total_iou/len(single_plane_image_names)))
def demo_single_image(args, image_name):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
feature_extractor = torchvision.models.vgg16(
pretrained=True).features.to(device)
dqn = DQN(device)
dqn.eval_net.load_state_dict(torch.load(
'eval_net.pth', map_location=torch.device('cpu')))
dqn.eval_net.eval()
dqn.eval_net.to(device)
trans = T.Compose([
T.Resize((224, 224)),
T.ToTensor(),
])
image_path = os.path.join(
path_voc_test + "JPEGImages", image_name + ".jpg")
image_original = Image.open(image_path)
width, height = image_original.size
bbx_gt = get_bb_of_gt_from_pascal_xml_annotation(image_name, path_voc_test)[
0][1:]
image = init_process(image_original, trans).to(device)
bbx = [0, width, 0, height]
history_action = np.zeros(his_actions*NUM_ACTIONS)
with torch.no_grad():
vector = feature_extractor(
image).cpu().detach().numpy().reshape(7*7*512)
state = np.concatenate([history_action, vector])
step = 0
while step < 10:
iou = cal_iou(bbx, bbx_gt)
print('iou co sai khong:', iou)
if iou > 0.5:
action = 5
else:
action = dqn.choose_action(state, args.EPISILO)
print(action)
new_bbx = update_bbx(bbx, action)
reward = reward_func(bbx, new_bbx, bbx_gt, action)
action_vec = np.zeros(NUM_ACTIONS)
action_vec[action] = 1.0
history_action = np.concatenate(
[history_action[NUM_ACTIONS:], action_vec])
with torch.no_grad():
vector = feature_extractor(inter_process(image_original, new_bbx, trans).to(
device)).cpu().detach().numpy().reshape(7*7*512)
print(vector)
num_zeros = np.count_nonzero(vector == 0)
print(vector.shape)
print(num_zeros)
next_state = np.concatenate([history_action, vector])
dqn.store_transition(state, action, reward, next_state)
if action == 5:
break
state = next_state
bbx = new_bbx
step += 1
# Visualize the bounding box at each step
draw = ImageDraw.Draw(image_original)
draw.rectangle([bbx[0], bbx[2], bbx[1], bbx[3]], outline='red')
draw.rectangle([bbx_gt[0], bbx_gt[2], bbx_gt[1], bbx_gt[3]], outline='blue')
image_original.show()
# save image
image_original.save('output/{}_step{}.jpg'.format(image_name, step))
time.sleep(0.5)
print("Final bounding box:", bbx)
print("Ground truth bounding box:", bbx_gt)
print("Final IOU:", cal_iou(bbx, bbx_gt))
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Hierarchical Object Detection with Deep Reinforcement Learning')
parser.add_argument('--gpu-devices', default='1', type=str,
help='gpu device ids for CUDA_VISIBLE_DEVICES')
parser.add_argument('--use_gpu', default=True, action='store_true')
parser.add_argument('--EPISILO', type=int, default=1.0)
parser.add_argument('--Subscale', type=float, default=3/4)
parser.add_argument('--image_name', type=str, default='001373',
help='name of the image for demonstration')
#main(parser.parse_args())
args = parser.parse_args()
demo_single_image(args, args.image_name)