Open EMUNES opened 4 years ago
Another similiar problem occurs when I directly use images in size 512 for inference. codes from baseline that I use:
detect_thresh = 0.3
nms_thresh = 0.5
size = 1024
nr = []
pos = []
for image_id, (test_image, emp_label) in enumerate(learn.data.test_ds):
#mean, std = learn.data.stats
pos.append([0, 0, 0, 0])
nr.append(0)
image_ori = test_image
# ori_shape = image_ori.shape[:2]
# image = cv2.resize(image_ori, (size, size))
# image = pil2tensor(image / 255., np.float32).cuda()
#image = transforms.Normalize(mean, std)(image)
image = test_image.resize(1024).data.cuda()
# class_pred_batch, bbox_pred_batch, _ = learn.model(image[None, :, :, :])
class_pred_batch, bbox_pred_batch, _ = learn.model(image[None, :])
for clas_pred, bbox_pred in zip(class_pred_batch, bbox_pred_batch):
bbox_pred, scores, preds = process_output(clas_pred, bbox_pred, anchors, detect_thresh)
if bbox_pred is not None:
to_keep = nms(bbox_pred, scores, nms_thresh)
bbox_pred, preds, scores = bbox_pred[to_keep].cpu(), preds[to_keep].cpu(), scores[to_keep].cpu()
t_sz = torch.Tensor(ori_shape)[None].float()
bbox_pred = rescale_box(bbox_pred, t_sz)
temp_pos = [0, 0, 0, 0]
for id, box in enumerate(bbox_pred[:2]):
x = to_np(box[0] + box[2] / 2)
y = to_np(box[1] + box[3] / 2)
if id == 0:
temp_pos[ :2] = x,y
nr[image_id] = 1
else:
temp_pos[2: ] = x, y
nr[image_id] = 2
output:
RuntimeError Traceback (most recent call last)
<ipython-input-65-883a98e5b6df> in <module>
27
28 for clas_pred, bbox_pred in zip(class_pred_batch, bbox_pred_batch):
---> 29 bbox_pred, scores, preds = process_output(clas_pred, bbox_pred, anchors, detect_thresh)
30
31 if bbox_pred is not None:
D:\ProgramFile\anaconda\envs\ai\lib\site-packages\object_detection_fastai\helper\object_detection_helper.py in process_output(clas_pred, bbox_pred, anchors, detect_thresh)
227
228 def process_output(clas_pred, bbox_pred, anchors, detect_thresh=0.25):
--> 229 bbox_pred = activ_to_bbox(bbox_pred, anchors.to(clas_pred.device))
230 clas_pred = torch.sigmoid(clas_pred)
231
D:\ProgramFile\anaconda\envs\ai\lib\site-packages\object_detection_fastai\helper\object_detection_helper.py in activ_to_bbox(acts, anchors, flatten)
74 if flatten:
75 acts.mul_(acts.new_tensor([[0.1, 0.1, 0.2, 0.2]]))
---> 76 centers = anchors[...,2:] * acts[...,:2] + anchors[...,:2]
77 sizes = anchors[...,2:] * torch.exp(acts[...,2:])
78 return torch.cat([centers, sizes], -1)
RuntimeError: The size of tensor a (24480) must match the size of tensor b (24192) at non-singleton dimension 0
Two numbers don't match, same as above - 24480 (the number of anchors) and 24192 (what is this about). I guess this has something to do with the resizing as both those problems occur after resizing but I really can't come up with anything to fix this. Also I'm using GPU for training.
This is a great project which makes object detection in fastai much easier. Everything works pretty well until I use transfer learning for object detection. My code to build the learner is pretty much the same with the cocotiny_retina_net example in the project code: I set
size, bs = 512, 16
for the learner to train 8 rounds, and after that I uselearn.data=data
in which the new data hassize, bs = 1024, 4
in it. Those are all the difference for transfer learning but when I train the model with image size of 1024 I always get:I work on win10-cuda1.02-pytorch1.5.0-torchvision0.6 and everything works right besides this. Isn't it the same size in each batch when I transfer
size, bs=512, 16
tosize, bs=1024, 4
? How could this Index error occur?