训练完模型后,我尝试绘制softmax输出的图,但是这会导致标题中提到的运行时错误。
以下是以下代码段:
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
import helper
# Test out your network!
dataiter = iter(testloader)
images, labels = dataiter.next()
img = images[1]
# TODO: Calculate the class probabilities (softmax) for img
ps = torch.exp(model(img))
# Plot the image and probabilities
helper.view_classify(img, ps, version='Fashion')
答案 0 :(得分:0)
问题出在这部分(我想)。
13 s
问题:正在加载的图像尺寸为28x28,但是,模型输入中的第一个索引通常是批处理大小。由于只有1张图片,因此您必须将第一个尺寸设置为尺寸1。为此,请执行fitEllipse
或5 ms
。另外,第一层的权重似乎是784 x128。即,图像应转换为矢量并馈入模型。为此,我们做#!/usr/bin/python3
# 2019/02/13
# https://stackoverflow.com/a/54604608/54661984
import cv2
import numpy as np
fpath = "sem.png"
img = cv2.imread(fpath)
## Convert into grayscale and threshed it
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
th, threshed = cv2.threshold(gray, 120, 255, cv2.THRESH_BINARY)
## Morph to denoise
threshed = cv2.dilate(threshed, None)
threshed = cv2.erode(threshed, None)
## Find the external contours
cnts = cv2.findContours(threshed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[-2]
cv2.drawContours(img, cnts, -1, (255, 0, 0), 2, cv2.LINE_AA)
## Fit ellipses
for cnt in cnts:
if cnt.size < 10 or cv2.contourArea(cnt) < 100:
continue
rbox = cv2.fitEllipse(cnt)
cv2.ellipse(img, rbox, (255, 100, 255), 2, cv2.LINE_AA)
## This it
cv2.imwrite("dst.jpg", img)
。
因此,您总共需要做
img = images[1]
# TODO: Calculate the class probabilities (softmax) for img
ps = torch.exp(model(img))
或者您可以只使用一个命令而不是两个命令(无需挤压)
img = img.view( (-1,) + img.shape)