预期为整数参数,浮点数为Pytorch:transform

时间:2019-03-13 17:48:43

标签: python pytorch

我已经检查了所有导入语句,它们工作正常。我正在尝试使用detect_image()方法将图像转换为张量。 image_tensor = img_transforms(img).float()给我以下错误

  

TypeError:预期为整数参数,为浮点数

def detect_image(img):
    # scale and pad image
     # scale and pad image
    ratio = min(img_size/img.size[0], img_size/img.size[1])
    imw = round(img.size[0] * ratio)
    imh = round(img.size[1] * ratio)
    img_transforms = transforms.Compose([ transforms.Resize((imh, imw)),
         transforms.Pad((max(int((imh-imw)/2),0), max(int((imw-imh)/2),0), max(int((imh-imw)/2),0), max(int((imw-imh)/2),0)),
                        (128,128,128)),
         transforms.ToTensor(),
         ])
    # convert image to Tensor
    image_tensor = img_transforms(img).float()
    image_tensor = image_tensor.unsqueeze_(0)
    input_img = Variable(image_tensor.type(Tensor))

    #input_img = ToTensor()(img).unsqueeze(0)
    #input_img = Variable(input_img)
    # run inference on the model and get detections
    with torch.no_grad():
        detections = model(input_img)
        detections = utils.non_max_suppression(detections, 80, conf_thres, nms_thres)
    return detections[0]

我的代码

# load image and get detections
img_path = "images/blueangels.jpg"
prev_time = time.time()
img = Image.open(img_path)
detections = detect_image(img)
inference_time = datetime.timedelta(seconds=time.time() - prev_time)
print ('Inference Time: %s' % (inference_time))

# Get bounding-box colors
cmap = plt.get_cmap('tab20b')
colors = [cmap(i) for i in np.linspace(0, 1, 20)]

img = np.array(img)
plt.figure()
fig, ax = plt.subplots(1, figsize=(12,9))
ax.imshow(img)

pad_x = max(img.shape[0] - img.shape[1], 0) * (img_size / max(img.shape))
pad_y = max(img.shape[1] - img.shape[0], 0) * (img_size / max(img.shape))
unpad_h = img_size - pad_y
unpad_w = img_size - pad_x

if detections is not None:
    unique_labels = detections[:, -1].cpu().unique()
    n_cls_preds = len(unique_labels)
    bbox_colors = random.sample(colors, n_cls_preds)
    # browse detections and draw bounding boxes
    for x1, y1, x2, y2, conf, cls_conf, cls_pred in detections:
        box_h = ((y2 - y1) / unpad_h) * img.shape[0]
        box_w = ((x2 - x1) / unpad_w) * img.shape[1]
        y1 = ((y1 - pad_y // 2) / unpad_h) * img.shape[0]
        x1 = ((x1 - pad_x // 2) / unpad_w) * img.shape[1]
        color = bbox_colors[int(np.where(unique_labels == int(cls_pred))[0])]
        bbox = patches.Rectangle((x1, y1), box_w, box_h, linewidth=2, edgecolor=color, facecolor='none')
        ax.add_patch(bbox)
        plt.text(x1, y1, s=classes[int(cls_pred)], color='white', verticalalignment='top',
                bbox={'color': color, 'pad': 0})
plt.axis('off')
# save image
plt.savefig(img_path.replace(".jpg", "-det.jpg"), bbox_inches='tight', pad_inches=0.0)
plt.show()

错误:

  

TypeErrorTraceback(最近一次通话最近)    在()中         2 prev_time = time.time()         3 img = Image.open(img_path)   ----> 4次检测= detect_image(img)         5 inference_time = datetime.timedelta(seconds = time.time()-prev_time)         6次打印(“推断时间:%s”%(推断时间))

     detect_image(img)中的

       11])        12#将图像转换为张量   ---> 13 image_tensor = img_transforms(img).float()        14 image_tensor = image_tensor.unsqueeze_(0)        15 input_img =变量(image_tensor.type(Tensor))

     

/usr/local/lib/python2.7/dist-packages/torchvision/transforms/transforms.pyc   在通话中(自己,img)        58 def 通话(自我,img):        self.transforms中的t的59:   ---> 60 img = t(img)        61返回img        62

     

/usr/local/lib/python2.7/dist-packages/torchvision/transforms/transforms.pyc   在通话中(自己,img)       193 PIL图像:重新缩放图像。       194“”“   -> 195 return F.resize(img,self.size,self.interpolation)       196       197 def 代表(自己):

     

/usr/local/lib/python2.7/dist-packages/torchvision/transforms/functional.pyc   调整大小(img,大小,插值)       244返回img.resize((ow,oh),插值)       245其他:   -> 246返回img.resize(size [::-1],插值)       247       248

     

/usr/local/lib/python2.7/dist-packages/PIL/Image.pyc调整大小(自己,   大小,重新采样)1643返回   self.convert('RGBa')。resize(size,resample).convert('RGBA')1644   -> 1645 return self._new(self.im.resize(size,resample))1646 1647 def rotation(self,angle,resample = NEAREST,expand = 0,   center = None

     

TypeError:预期为整数参数,为浮点数

0 个答案:

没有答案