如何将cv2.imread与keras image.img_load输出相匹配

was*_*asd 12 opencv numpy image-processing python-3.x keras

我正在学习深度学习.训练了图像分类算法.然而,问题是训练我使用的图像:

test_image = image.load_img('some.png', target_size = (64, 64))
test_image = image.img_to_array(test_image)
Run Code Online (Sandbox Code Playgroud)

在实际应用中,我使用:

test_image = cv2.imread('trick.png')
test_image = cv2.resize(test_image, (64, 64))
Run Code Online (Sandbox Code Playgroud)

但我发现那些给出了不同的ndarray(不同的数据):

load_image的最后一个条目:

  [ 64.  71.  66.]
  [ 64.  71.  66.]
  [ 62.  69.  67.]]]
Run Code Online (Sandbox Code Playgroud)

来自cv2.imread的最后条目:

  [ 15  23  27]
  [ 16  24  28]
  [ 14  24  28]]]
Run Code Online (Sandbox Code Playgroud)

,所以系统无法正常工作.有没有办法将结果相互匹配?

ray*_*ica 11

OpenCV以BGR格式读取图像,而在keras中,它以RGB表示.要使OpenCV版本符合我们期望的顺序(RGB),只需反转通道:

test_image = cv2.imread('trick.png')
test_image = cv2.resize(test_image, (64, 64))
test_image = test_image[...,::-1] # Added
Run Code Online (Sandbox Code Playgroud)

最后一行将通道反转为RGB顺序.然后,您可以将其提供给您的keras模型.

我想补充的另一点是,cv2.imread通常会uint8精确地读取图像.检查keras加载图像的输出,您可以看到数据是浮点精度,因此您可能还希望转换为浮点表示,例如float32:

import numpy as np
# ...
# ...
test_image = test_image[...,::-1].astype(np.float32)
Run Code Online (Sandbox Code Playgroud)

最后一点,根据您训练模型的方式,通常习惯将图像像素值标准化为一个[0,1]范围.如果您使用keras模型执行此操作,请确保在通过OpenCV读入的图像中将值除以255:

import numpy as np
# ...
# ...
test_image = (test_image[...,::-1].astype(np.float32)) / 255.0
Run Code Online (Sandbox Code Playgroud)

  • @JeruLuke:D我以前从未听过那种表达.我听说最接近的是使用鸟类,但芒果也有效:) (2认同)

Sou*_*rav 7

最近,我遇到了同样的问题。我尝试使用 OpenCV 转换颜色通道并调整图像大小。但是,PIL 和 OpenCV 具有非常不同的图像大小调整方式。这是此问题的确切解决方案。

这是获取图像文件路径,转换为目标大小并为Keras模型做准备的功能-

import cv2
import keras
import numpy as np
from keras.preprocessing import image
from PIL import Image

def prepare_image (file):
    im_resized = image.load_img(file, target_size = (224,224))
    img_array = image.img_to_array(im_resized)
    image_array_expanded = np.expand_dims(img_array, axis = 0)
    return keras.applications.mobilenet.preprocess_input(image_array_expanded)

# execute the function
PIL_image = prepare_image ("lena.png")
Run Code Online (Sandbox Code Playgroud)

如果你有一个 OpenCV 图像,那么函数将是这样的 -

def prepare_image2 (img):
    # convert the color from BGR to RGB then convert to PIL array
    cvt_image =  cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    im_pil = Image.fromarray(cvt_image)

    # resize the array (image) then PIL image
    im_resized = im_pil.resize((224, 224))
    img_array = image.img_to_array(im_resized)
    image_array_expanded = np.expand_dims(img_array, axis = 0)
    return keras.applications.mobilenet.preprocess_input(image_array_expanded)

# execute the function
img = cv2.imread("lena.png")
cv2_image = prepare_image2 (img)

# finally check if it is working  
np.array_equal(PIL_image, cv2_image)
>> True
Run Code Online (Sandbox Code Playgroud)


小智 5

除了使用BGR格式的CV2和使用RGB格式的Keras(使用PIL作为后端)之外,使用相同参数的CV2和PIL的调整大小方法也存在显着差异。

可以在Internet上找到多个参考,但是总体思路是,两种调整大小算法中使用的像素坐标系存在细微差异,并且在插值算法中作为中间步骤的不同浮法转换方法也存在潜在问题。最终结果是一个视觉上相似的图像,但在不同版本之间略有偏移/干扰。

对抗攻击的完美示例,尽管输入差异很小,但仍可能导致准确性产生巨大差异。