Python 预测标签为off-KERAS/Tensorflow
我正在使用KERAS/TF制作一个带有转移学习的图像分类器,包括预先训练的模型权重。图像数据集分为80/10/10。类别是范围从1到80的字符串标签 对于图像的预处理,我使用ImageDataGenerator,评估结果显示,评估集的准确率约为58%。但是,尝试预测评估集上的值的精确度约为0.01。我也在测试集上试过,结果仍然不一致 有人知道预测出了什么问题吗 提前谢谢你 致以最良好的祝愿Python 预测标签为off-KERAS/Tensorflow,python,tensorflow,keras,neural-network,image-classification,Python,Tensorflow,Keras,Neural Network,Image Classification,我正在使用KERAS/TF制作一个带有转移学习的图像分类器,包括预先训练的模型权重。图像数据集分为80/10/10。类别是范围从1到80的字符串标签 对于图像的预处理,我使用ImageDataGenerator,评估结果显示,评估集的准确率约为58%。但是,尝试预测评估集上的值的精确度约为0.01。我也在测试集上试过,结果仍然不一致 有人知道预测出了什么问题吗 提前谢谢你 致以最良好的祝愿 # Predict values using the test generator predict = m
# Predict values using the test generator
predict = model.predict_generator(validation_generator,
steps=(validation_generator.n // 32)+1)
# Choose the highest scoring prediction
validate_df['prediction'] = np.argmax(predict, axis=-1)
# assigning label names to the corresponding indexes
labels = {0 : '1', 1 : '2', 2 : '3', 3 : '4', 4 : '5', 5 : '6', 6 : '7', 7 : '8', 8 : '9', 9 : '10',
10 : '11', 11 : '12', 12 : '13', 13 : '14', 14 : '15', 15 : '16', 16 : '17', 17 : '18', 18 : '19', 19 : '20',
20 : '21', 21 : '22', 22 : '23', 23 : '24', 24 : '25', 25 : '26', 26 : '27', 27 : '28', 28 : '29', 29 : '30',
30 : '31', 31 : '32', 32 : '33', 33 : '34', 34 : '35', 35 : '36', 36 : '37', 37 : '38', 38 : '39', 39 : '40',
40 : '41', 41 : '42', 42 : '43', 43 : '44', 44 : '45', 45 : '46', 46 : '47', 47 : '48', 48 : '49', 49 : '50',
50 : '51', 51 : '52', 52 : '53', 53 : '54', 54 : '55', 55 : '56', 56 : '57', 57 : '58', 58 : '59', 59 : '60',
60 : '61', 61 : '62', 62 : '63', 63 : '64', 64 : '65', 65 : '66', 66 : '67', 67 : '68', 68 : '69', 69 : '70',
70 : '71', 71 : '72', 72 : '73', 73 : '74', 74 : '75', 75 : '76', 76 : '77', 77 : '78', 78 : '79', 79 : '80'}
# Replace with original lables for comparison
validate_df['prediction'] = validate_df['prediction'].replace(labels)
# Print accuracy score and show output
print(accuracy_score(validate_df.label, validate_df.prediction))
test_df.head()
0.013067624959163672
img_name label prediction
0 train_27493.jpg 17 10
1 train_19980.jpg 60 58
2 train_4348.jpg 71 58
3 train_4141.jpg 11 4
4 train_21555.jpg 36 22
社区:
我的代码缺少将值转换回正确标签的部分
选择最高评分预测后,该值必须使用class_指数转换回train_生成器
# Convert the predict category back into train_generator.class_indices
label_map = dict((v,k) for k,v in train_generator.class_indices.items())
validate_df['prediction'] = validate_df['prediction'].replace(label_map)
你使用什么样的架构?您的模型中有任何正则化吗?您是否将火车损失与有效损失进行了对比?您的验证频率是多少?我正在使用InceptionResNetV2体系结构,包括一个topmodel来对在GPU上训练的图像进行分类。Trainloss验证看起来不错,还使用了EarlyStoping来防止过度装配。我不完全熟悉这种体系结构,但如果仅用于分类,则58%的准确率不是一个很好的准确率。如果你以yolo算法为例,它在一个更难的任务中获得了58%的成功。我只是觉得现在你的训练次数太少了。谢谢你的评论。我发现了错误。我忘了将类别重新加载到火车生成器中#将预测类别转换回火车生成器中。class#u索引#label#u map=dict((v,k)表示火车生成器中的k,v。class_index.items())#test#df['prediction']=test#df['prediction']替换(label#map)