所以,我只是训练了模型。
编辑(这是我上课的方式)
classes = os.path.join('emnist-letters-mapping.txt')
with open(classes, 'r') as f:
classes = f.readlines()
classes = list(map(lambda x: x.strip(), classes))
print(classes)
num_classes = len(classes)
.txt文件中的标签。
pred = model.predict(train_features)#[0]#np.array(set_one).reshape((-1,48,48,3)))#
print(pred)
result = [(classes[i], float(pred[i]) * 100.0) for i in range(len(pred))]
result.sort(reverse=True, key=lambda x: x[1])
for i in range(2):
(class_name, prob) = result[i]
print("Top %d ====================" % (i + 1))
print("Class name: %s" % (class_name))
print("Probability: %.2f%%" % (prob))
现在,当我使用预测时,我会得到这个
[[[[0.0000000e+00 0.0000000e+00 0.0000000e+00 1.9776728e-02
4.9749532e-35 3.3859247e-21 5.2264658e-22 0.0000000e+00
0.0000000e+00 2.0263837e-21 1.3618594e-32 0.0000000e+00
4.4031281e-35 0.0000000e+00 0.0000000e+00 0.0000000e+00
6.0703663e-32 0.0000000e+00 6.8396845e-19 0.0000000e+00
4.3259302e-11 0.0000000e+00 1.1961731e-34 0.0000000e+00
2.8515963e-19 9.8022330e-01 0.0000000e+00]]]]
但是当我尝试将其与标签文件相乘时,会出现错误
File "model_predict.py", line 42, in <module>
result = [(classes[i], float(pred[i]) * 100.0) for i in range(len(pred))]
File "model_predict.py", line 42, in <listcomp>
result = [(classes[i], float(pred[i]) * 100.0) for i in range(len(pred))]
TypeError: only size-1 arrays can be converted to Python scalars
答案 0 :(得分:0)
这是因为pred
是一个形状为(1,1,1,27)的numpy数组
为了对其进行迭代并将其项正确用作字典的键,您必须将其展平。
尝试在result = [(classes[i], float(pred[i]) * 100.0) for i in range(len(pred))]
之前添加此行
pred = list(pred.flatten())
因为在循环的第一次迭代时,我将是一个形状为(1,1,27)的数组,该数组无法用于查找字典。