【问题标题】:Comparing Data loaded using ImageDataGenerator() and cv2.imread()比较使用 ImageDataGenerator() 和 cv2.imread() 加载的数据
【发布时间】:2020-07-31 21:17:20
【问题描述】:

我是图像分类的新手。我已经建立了一个模型来对狗和猫进行分类,并将模型保存为 h5 文件。对于训练,使用ImageDataGenerator() 加载数据。为了测试,我使用cv2.imread() 来加载数据。为了确认,我刚刚使用这两种方法加载了一个图像并检查了输出。但是我得到的数组与我从ImageDataGenerator() 得到的数组相反。我正在发布代码和输出

train_image_generator = ImageDataGenerator()
test_data=train_image_generator.flow_from_directory(batch_size=batch_size,directory='/home/josin/my_projects/test1',shuffle=True,target_size=(150, 150),class_mode='binary')
print(test_data[0][0])`

上述代码的输出是

找到属于 1 个类别的 1 张图片

array([[[[203., 164.,  87.]
     [209., 170.,  93.],
     [209., 170.,  93.],
     ...,
     [247., 206., 124.],
     [244., 204., 119.],
     [240., 201., 122.]],
     ...,
     [  2.,   2.,   0.],
     [  2.,   2.,   0.],
     [  2.,   2.,   0.]]]], dtype=float32)`

使用`cv2.imread()的代码是:

img_array = cv2.imread('/home/josin/my_projects/test1/cat/cat.0.jpg')
new_array = cv2.resize(img_array,(150,150))
new_array.reshape(1,150,150,3)`

上面代码的输出是

array([[[[ 87, 164, 203],
     [ 92, 169, 208],
     [ 93, 170, 209],
     ...,
     [124, 206, 247],
     [119, 203, 245],
     [122, 201, 240]],
     ...,
     [  0,   2,   2],
     [  0,   2,   2],
     [  0,   2,   2]]]], dtype=uint8)

使用cv2.imread()时像素值被反转。我提到的大多数文章和帖子都使用cv2.imread()。将这个输入提供给保存的模型是否正确?有没有其他方法可以加载图像以服务于模型

【问题讨论】:

    标签: python image opencv image-processing keras


    【解决方案1】:

    cv2.imread 将图像通道排列如下:b, g, r。同时,ImageDataGenerator 将频道排列为r, g, b。这解释了您提到的inverse 效果。

    只需拨打:img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 即可将opencv 的图片排列为r, g, b

    【讨论】:

      猜你喜欢
      • 2020-07-23
      • 1970-01-01
      • 2018-04-07
      • 1970-01-01
      • 2020-05-08
      • 1970-01-01
      • 1970-01-01
      • 2019-09-17
      • 2021-05-23
      相关资源
      最近更新 更多