我有一个预测犬种的代码,在CNN模型上训练后,我从下面的函数得到分类指数。我想显示从函数获得的类idx文件夹中的随机图像。
class_name = [item for item in loaders['train'].dataset.classes]
def predict_dog_breed(img,model,class_names):
image = Image.open(img).convert('RGB')
transform = transforms.Compose([
transforms. RandomResizedCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485,0.456,0.406],
std=[0.229, 0.224, 0.225])])
image = transform(image)
test_image = image.unsqueeze(0)
net.eval()
output = net(test_image)
idx = torch.argmax(output)
a = random.choice(os.listdir("./dogImages/train/{}/".format (class_name[idx])))
imshow(a)
return class_name[idx]当我试图显示随机图像时,我得到以下错误:
TypeError跟踪(最近一次调用)在img_file in os.listdir(‘./os.listdir’):2图像=os.path.join(‘./dog_or_human’,img_file) ->3 dog_or_human(图像) 在dog_or_human(img) 5 plt.show() 6 if dog_detector(img) == True:-->7 predict_dog = predict_dog_breed(img,net,class_name) 8打印(“检测到狗!品种是{}".format(predict_dog)) 9 elif face_detector> 0: 在predict_dog_breed(img,model )18a=predict_dog_breed 19打印(A) ~/Library/Python/3.7/lib/python/site-packages/matplotlib/pyplot.py in imshow(X、cmap、范数、方面、插值、alpha、vmin、vmax、原点、范围、形状、滤波范数、滤波、imlim、重采样、url、data、**kwargs) 2697 filternorm=filternorm、filterrad=filterrad、imlim=imlim、2698 resample=resample、url=url、**(“数据”:数据}如果数据不是-> 2699无其他{})、**kwargs) 2700 sci( __ret ) 2701返回__ret。 ~/Library/Python/3.7/lib/python/site-packages/matplotlib/init.py在内部(ax,data,*args,**kwargs) 1808“Matplotlib列表!”% (label_namer,func.name),1809 RuntimeWarning,stacklevel=2) -> 1810返回函数(ax,*args,**kwargs) 1811 1812 inner.doc = _add_data_doc(inner.doc, ~/Library/Python/3.7/lib/python/site-packages/matplotlib/axes/_axes.py in imshow(self、X、cmap、norm、方面、插值、alpha、vmin、vmax、起源、范围、形状、滤波范数、过滤器、imlim、重采样、url、**kwargs) 5492 resample=resample、**kwargs) 5493 -> 5494 im.set_data(X) 5495 im.set_alpha(alpha) 5496 (如果im.get_clip_path()为空的话: ~/Library/Python/3.7/lib/python/site-packages/matplotlib/image.py in set_data(self,A) 632 if (self._A.dtype != np.uint8和633 not np.can_cast(self._A.dtype,float,"same_kind")):-> 634 raise (“图像数据不能转换为浮动”) TypeError:图像数据不能转换为浮动
在这方面的任何帮助都将不胜感激!
发布于 2019-07-10 18:55:05
因此,我试图在代码这里中复制错误,并成功地做到了这一点。由于代码中的以下几行,您得到了错误:
a = random.choice(os.listdir("./dogImages/train/{}/".format(class_name[idx])))
imshow(a)random.choice(os.listdir("./dogImages/train/{}/".format(class_name[idx])))基本上返回一个图像文件名,它是一个字符串。您不是读取图像,只是将文件名传递给imshow函数,这是不正确的。请查看下面的数字以求澄清。
有错误的代码:

代码没有错误:

因此,将predict_do_breed函数更改为:
def predict_dog_breed(img,model,class_name):
image = Image.open(img).convert('RGB')
transform = transforms.Compose([transforms.RandomResizedCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])])
image = transform(image)
test_image = image.unsqueeze(0)
net.eval()
output = net(test_image)
idx = torch.argmax(output)
a = random.choice(os.listdir("./dogImages/train/{}/".format(class_name[idx])))
print(a)
img = cv2.imread("./dogImages/train/{}/".format(class_name[idx])+a)
imshow(img)
return class_name[idx]在上面的代码中,使用cv2.imread函数读取由random.choice(os.listdir("./dogImages/train/{}/".format(class_name[idx])))输出的图像文件名。
https://stackoverflow.com/questions/56862204
复制相似问题