-
Notifications
You must be signed in to change notification settings - Fork 42
/
detect.py
executable file
·138 lines (119 loc) · 4.73 KB
/
detect.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
import cv2
from model.fcos import FCOSDetector
import torch
from torchvision import transforms
import numpy as np
from dataset.VOC_dataset import VOCDataset
import time
import matplotlib.patches as patches
import matplotlib.pyplot as plt
from matplotlib.ticker import NullLocator
def preprocess_img(image,input_ksize):
'''
resize image and bboxes
Returns
image_paded: input_ksize
bboxes: [None,4]
'''
min_side, max_side = input_ksize
h, w, _ = image.shape
smallest_side = min(w,h)
largest_side=max(w,h)
scale=min_side/smallest_side
if largest_side*scale>max_side:
scale=max_side/largest_side
nw, nh = int(scale * w), int(scale * h)
image_resized = cv2.resize(image, (nw, nh))
pad_w=32-nw%32
pad_h=32-nh%32
image_paded = np.zeros(shape=[nh+pad_h, nw+pad_w, 3],dtype=np.uint8)
image_paded[:nh, :nw, :] = image_resized
return image_paded
def convertSyncBNtoBN(module):
module_output = module
if isinstance(module, torch.nn.modules.batchnorm._BatchNorm):
module_output = torch.nn.BatchNorm2d(module.num_features,
module.eps, module.momentum,
module.affine,
module.track_running_stats)
if module.affine:
module_output.weight.data = module.weight.data.clone().detach()
module_output.bias.data = module.bias.data.clone().detach()
module_output.running_mean = module.running_mean
module_output.running_var = module.running_var
for name, child in module.named_children():
module_output.add_module(name,convertSyncBNtoBN(child))
del module
return module_output
if __name__=="__main__":
cmap = plt.get_cmap('tab20b')
colors = [cmap(i) for i in np.linspace(0, 1, 20)]
class Config():
#backbone
pretrained=False
freeze_stage_1=True
freeze_bn=True
#fpn
fpn_out_channels=256
use_p5=True
#head
class_num=20
use_GN_head=True
prior=0.01
add_centerness=True
cnt_on_reg=False
#training
strides=[8,16,32,64,128]
limit_range=[[-1,64],[64,128],[128,256],[256,512],[512,999999]]
#inference
score_threshold=0.3
nms_iou_threshold=0.4
max_detection_boxes_num=300
model=FCOSDetector(mode="inference",config=Config)
# model=torch.nn.SyncBatchNorm.convert_sync_batchnorm(model)
# print("INFO===>success convert BN to SyncBN")
model = torch.nn.DataParallel(model)
model.load_state_dict(torch.load("./checkpoint/voc_78.7.pth",map_location=torch.device('cpu')))
# model=convertSyncBNtoBN(model)
# print("INFO===>success convert SyncBN to BN")
model=model.eval()
print("===>success loading model")
import os
root="./test_images/"
names=os.listdir(root)
for name in names:
img_bgr=cv2.imread(root+name)
img_pad=preprocess_img(img_bgr,[800,1333])
img=cv2.cvtColor(img_pad.copy(),cv2.COLOR_BGR2RGB)
img1=transforms.ToTensor()(img)
img1= transforms.Normalize([0.485,0.456,0.406], [0.229,0.224,0.225],inplace=True)(img1)
img1=img1
start_t=time.time()
with torch.no_grad():
out=model(img1.unsqueeze_(dim=0))
end_t=time.time()
cost_t=1000*(end_t-start_t)
print("===>success processing img, cost time %.2f ms"%cost_t)
# print(out)
scores,classes,boxes=out
boxes=boxes[0].cpu().numpy().tolist()
classes=classes[0].cpu().numpy().tolist()
scores=scores[0].cpu().numpy().tolist()
plt.figure()
fig, ax = plt.subplots(1)
ax.imshow(img)
for i,box in enumerate(boxes):
pt1=(int(box[0]),int(box[1]))
pt2=(int(box[2]),int(box[3]))
img_pad=cv2.rectangle(img_pad,pt1,pt2,(0,255,0))
b_color = colors[int(classes[i]) - 1]
bbox = patches.Rectangle((box[0],box[1]),width=box[2]-box[0],height=box[3]-box[1],linewidth=1,facecolor='none',edgecolor=b_color)
ax.add_patch(bbox)
plt.text(box[0], box[1], s="%s %.3f"%(VOCDataset.CLASSES_NAME[int(classes[i])],scores[i]), color='white',
verticalalignment='top',
bbox={'color': b_color, 'pad': 0})
plt.axis('off')
plt.gca().xaxis.set_major_locator(NullLocator())
plt.gca().yaxis.set_major_locator(NullLocator())
plt.savefig('out_images/{}'.format(name), bbox_inches='tight', pad_inches=0.0)
plt.close()