【问题标题】:Keras, get numpy array of corresponding labels from DataFrameIteratorKeras,从 DataFrameIterator 获取相应标签的 numpy 数组
【发布时间】:2020-07-31 17:15:19
【问题描述】:

我对 Keras 有这个问题, 我的 test_set 定义如下:

我的测试集

test_datagen = ImageDataGenerator(rescale = 1./255)
test_set = test_datagen.flow_from_dataframe(dataframe=X_test,
                                            x_col='image_path',
                                            y_col='category_id',
                                            #imagepath_test,
                                            target_size = (64, 64),
                                            batch_size = 32,
                                            class_mode = 'categorical')

为了尽快恢复,我使用 Keras 的 ImageDataGenerator,然后将数据生成器分配给我的 test_set。

我的数据框 X_test 有 2 列,

x_col='image_path' ## The path to my image files
y_col='category_id' ## My categorical features - Labels 

我需要从我的 test_set 中提取 y_col 中的值,因为,

test_set['category_id'] is not in same order neither same shape as X_test['category_id'] 
X_test['category_id'].shape
(315,)

当我查看 test_set 类型时,我得到:

type(test_set)
keras_preprocessing.image.dataframe_iterator.DataFrameIterator

原因:

当我们进行预测时,我们需要在这个“test_set”上进行预测

#_________________________________
# Making Predictions
y_preds = classifier.predict(test_set)

所以, 当我想显示我的分类报告时,我不能使用"test_set",因为格式错误,我也不能使用我的X_test['categorical_id'],因为真实值的顺序与test_set.不同

Below an example  of classification_report with test_set and the result:

print(classification_report(test_set, predicted, 
  target_names=df_data['product_cat1'].unique()))

结果我得到一个错误:

ValueError: Found input variables with inconsistent numbers of samples: [10, 315]

记住,我的'X_test' 形状是:

 X_test['category_id'].shape
    (315,)

无论我尝试将此'test_set' 转换为数组或数据框,都不起作用。

如果我在我的分类报告中使用X_test['category_id'],它可以工作,但分数是假的,

否则,使用 Keras 进行多类分类很有趣但没用,如果我们不能确定每个类的准确率和召回分数以及 f1_score 准确度模型,我的意思是我们只是得到一个全局准确度模型,仅此而已,只对比赛有好处。

欢迎任何想法和解决方法。

【问题讨论】:

    标签: python keras


    【解决方案1】:

    因为它是test_set,并且由于您没有在test_set 上进行训练,所以您不必对其进行洗牌以保留顺序。这样您就可以从X_test['category_id'] 知道标签的顺序(基本事实),并为classification_report 使用相同的顺序

    修复

    test_set = test_datagen.flow_from_dataframe(dataframe=X_test,
                                                x_col='image_path',
                                                y_col='category_id',
                                                shuffle=False,        ### Do not shuffle
                                                target_size = (64, 64),
                                                batch_size = 32,
                                                class_mode = 'categorical')
    
    y_preds = classifier.predict(test_set)
    print(classification_report(test_set, X_test['category_id']))
    

    如果您想对test_set 进行洗牌,那么您可以为其添加一个值并进行预测。然后使用相同的种子并迭代数据生成器并收集标签(基本事实)。设置相同的种子值,您将获得相同的订单。

    【讨论】:

      猜你喜欢
      • 2021-07-12
      • 2019-05-25
      • 2011-08-19
      • 1970-01-01
      • 2016-12-22
      • 2020-03-07
      • 1970-01-01
      • 2017-09-15
      • 2015-07-13
      相关资源
      最近更新 更多