简体   繁体   English

比较使用 ImageDataGenerator() 和 cv2.imread() 加载的数据

[英]Comparing Data loaded using ImageDataGenerator() and cv2.imread()

I am new to image classification.我是图像分类的新手。 I have built a model to classify dogs and cats and saved the model as an h5 file.我已经构建了一个 model 来对狗和猫进行分类,并将 model 保存为 h5 文件。 For training, data was loaded using ImageDataGenerator() .对于训练,使用ImageDataGenerator()加载数据。 For testing, I used cv2.imread() to load the data.为了测试,我使用cv2.imread()来加载数据。 For confirmation, I just loaded an image using both these methods and checked the output.为了确认,我刚刚使用这两种方法加载了一个图像并检查了 output。 But the array I got was reversed from what I got from ImageDataGenerator() .但是我得到的数组与我从ImageDataGenerator()得到的数组相反。 I am posting the code and output我发布代码和 output

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])`

The output for the above code is上述代码的 output 是

Found 1 images belonging to 1 classes找到属于 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)`

The code using `cv2.imread() is:使用 `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)`

and output of the above code is和上面代码的 output 是

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)

The pixel values got reversed while using cv2.imread() .Most of the articles and posts I have referred uses cv2.imread() .Is it correct to give this input to the saved model?使用cv2.imread()时像素值反转了。我提到的大多数文章和帖子都使用cv2.imread() 。将此输入提供给保存的 model 是否正确? Is there any other way to load images to serve to model有没有其他方法可以加载图像以提供给 model

cv2.imread arranges the image channels as follows: b, g, r . cv2.imread排列图像通道如下: b, g, r Meanwhile, ImageDataGenerator arranges channels as r, g, b .同时, ImageDataGenerator将通道排列为r, g, b This explains the inverse effect you mentioned.这解释了你提到的inverse效果。

Simply, call: img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) to make opencv images arranged as r, g, b简单地说,调用: img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)使opencv图像排列为r, g, b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM