【问题标题】:Prediction labels are off - KERAS / Tensorflow预测标签已关闭 - KERAS / Tensorflow
【发布时间】:2020-11-23 16:29:52
【问题描述】:

我正在使用 KERAS/TF 制作一个带有迁移学习的图像分类器,包括预训练的模型权重。图像数据集按 80/10/10 拆分。类别是范围从 1 到 80 的字符串标签。

对于图像的预处理,我使用 ImageDataGenerator 并且评估在评估集上显示出良好的准确度,约为 58%。但是,尝试预测评估集上的值给出了大约 0.01 的准确度。我也在测试集上试过了,结果还是不行。

有人知道预测出了什么问题吗?

提前谢谢你!

最好的问候,

# 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

【问题讨论】:

  • 您使用哪种架构?你的模型中有任何正则化吗?您是否针对有效损失绘制了火车损失?你的验证频率是多少?
  • 我正在使用 InceptionResNetV2 架构,包括一个顶级模型来对在 GPU 上训练的图像进行分类。 Trainloss 验证看起来不错,还使用earlystopping 来防止过拟合
  • 我对这种架构并不完全熟悉,但如果只是为了分类准确度为 58 %,准确度不是很好。如果您以 yolo 算法为例,它在更艰巨的任务上达到 58%。我只是觉得目前你的训练时间太少了。
  • 感谢您的评论。我发现了错误。我忘记将类别重新加载回火车生成器 # 将预测类别转换回 train_generator.class_indeces #label_map = dict((v,k) for k,v in train_generator.class_indices.items()) #test_df['prediction' ] = test_df['prediction'].replace(label_map)

标签: python tensorflow keras neural-network image-classification


【解决方案1】:

对于社区:

我的代码缺少将值转换回正确标签的部分。

选择得分最高的预测后,该值必须使用 class_indices 转换回 train_generator

# 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)

【讨论】:

    猜你喜欢
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 2017-08-23
    • 2018-01-23
    • 2020-05-11
    • 2020-03-07
    • 1970-01-01
    • 2019-11-06
    相关资源
    最近更新 更多