33
33
sampler_val = AspectRatioBasedSampler (dataset_val , batch_size = 1 , drop_last = False )
34
34
dataloader_val = DataLoader (dataset_val , num_workers = 1 , collate_fn = collater , batch_sampler = sampler_val )
35
35
36
- model = torch .load ('csv_model_3 .pt' )
36
+ model = torch .load ('csv_model_1 .pt' )
37
37
38
38
use_gpu = True
39
39
44
44
45
45
unnormalize = UnNormalizer ()
46
46
47
-
48
47
def draw_caption (image , box , caption ):
49
48
50
49
b = np .array (box ).astype (int )
@@ -53,29 +52,31 @@ def draw_caption(image, box, caption):
53
52
54
53
for idx , data in enumerate (dataloader_val ):
55
54
56
- scores , classification , transformed_anchors = model (data ['img' ].cuda ().float ())
57
-
58
- idxs = np .where (scores > 0.5 )
59
- img = np .array (255 * unnormalize (data ['img' ][0 , :, :, :])).copy ()
55
+ with torch .no_grad ():
56
+ st = time .time ()
57
+ scores , classification , transformed_anchors = model (data ['img' ].cuda ().float ())
58
+ print ('Elapsed time: {}' .format (time .time ()- st ))
59
+ idxs = np .where (scores > 0.5 )
60
+ img = np .array (255 * unnormalize (data ['img' ][0 , :, :, :])).copy ()
60
61
61
- img [img < 0 ] = 0
62
- img [img > 255 ] = 255
62
+ img [img < 0 ] = 0
63
+ img [img > 255 ] = 255
63
64
64
- img = np .transpose (img , (1 , 2 , 0 ))
65
+ img = np .transpose (img , (1 , 2 , 0 ))
65
66
66
- img = cv2 .cvtColor (img .astype (np .uint8 ), cv2 .COLOR_BGR2RGB )
67
+ img = cv2 .cvtColor (img .astype (np .uint8 ), cv2 .COLOR_BGR2RGB )
67
68
68
- for j in range (idxs [0 ].shape [0 ]):
69
- bbox = transformed_anchors [idxs [0 ][j ], :]
70
- x1 = int (bbox [0 ])
71
- y1 = int (bbox [1 ])
72
- x2 = int (bbox [2 ])
73
- y2 = int (bbox [3 ])
74
- label_name = dataset_val .labels [int (classification [idxs [0 ][j ]])]
75
- draw_caption (img , (x1 , y1 , x2 , y2 ), label_name )
69
+ for j in range (idxs [0 ].shape [0 ]):
70
+ bbox = transformed_anchors [idxs [0 ][j ], :]
71
+ x1 = int (bbox [0 ])
72
+ y1 = int (bbox [1 ])
73
+ x2 = int (bbox [2 ])
74
+ y2 = int (bbox [3 ])
75
+ label_name = dataset_val .labels [int (classification [idxs [0 ][j ]])]
76
+ draw_caption (img , (x1 , y1 , x2 , y2 ), label_name )
76
77
77
- cv2 .rectangle (img , (x1 , y1 ), (x2 , y2 ), color = (0 , 0 , 255 ), thickness = 2 )
78
- print (label_name )
78
+ cv2 .rectangle (img , (x1 , y1 ), (x2 , y2 ), color = (0 , 0 , 255 ), thickness = 2 )
79
+ print (label_name )
79
80
80
- cv2 .imshow ('img' , img )
81
- cv2 .waitKey (0 )
81
+ # cv2.imshow('img', img)
82
+ # cv2.waitKey(0)
0 commit comments