首页 > 解决方案 > 我如何知道图像分类器中图像的最佳重塑尺寸?

问题描述

当我尝试创建只有两个数字(7 和 10)的手写数字的图像数据集时,我尝试加载自定义图像(原始颜色:黑白,尺寸:251 x 54 请参见下面的示例)我得到了这个我的 load_img 函数错误如下:

from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.models import load_model


# load and prepare the image
def load_image(filename):
    # load the image
    img = load_img(filename, color_mode="grayscale",interpolation='nearest')
    # convert to array
    img = img_to_array(img)
    # reshape into a single sample with 1 channel
    img = img.reshape(2, 200, 50, 1)
    # prepare pixel data
    img = img.astype('float32')
    img = img / 255.0
    return img




# load an image and predict the class
def run_example():
    # load the image
    img = load_image('C:/Users/ADEM/Desktop/msi_youssef/PFE/dataset/10/kz.png')
    # load model
    model = load_model('C:/Users/ADEM/Desktop/msi_youssef/PFE/other_shit/first_try.h5')
    # predict the class
    digit = model.predict_classes(img)
    print(digit[0])


# entry point, run the example
run_example()  

这是我得到的错误:

ValueError                                Traceback (most recent call last)
<ipython-input-2-5427252e970b> in <module>
     32 
     33 # entry point, run the example
---> 34 run_example()

<ipython-input-2-5427252e970b> in run_example()
     23 def run_example():
     24     # load the image
---> 25     img = load_image('C:/Users/ADEM/Desktop/msi_youssef/PFE/dataset/10/kz.png')
     26     # load model
     27     model = load_model('C:/Users/ADEM/Desktop/msi_youssef/PFE/other_shit/final_model.h5')

<ipython-input-2-5427252e970b> in load_image(filename)
     11     img = img_to_array(img)
     12     # reshape into a single sample with 1 channel
---> 13     img = img.reshape(2, 200, 50, 1)
     14     # prepare pixel data
     15     img = img.astype('float32')

ValueError: cannot reshape array of size 13554 into shape (2,200,50,1) 

请注意,在 final_model.h5 中,我将 img 平均大小设置为 200 , 50
final_model.h5 的代码将在第一个答案中!

标签: pythonnumpytensorflowkerasclassification

解决方案


2D 卷积层需要输入为 -

if using channels_last: (batch_size, imageside1, imageside2, channels)
if using channels_first: (batch_size, channels, imageside1, imageside2)

在您的情况下,它将是 batch_size= 不指定, imageside1= 200, imageside1= 50, channels= 1(灰度图像)

因此load_image,通过以下更改修改您的功能

# load and prepare the image
def load_image(filename):
    # load the image with target size
    img = load_img(filename, color_mode="grayscale",interpolation='nearest',target_size=(200,50))
    # convert to array
    img = img_to_array(img)
    # reshape into a single sample with 1 channel
    # img = img.reshape(2, 200, 50, 1) --> This is not required now and why batch size argument as 2?
    # prepare pixel data
    img = img.astype('float32')
    img = img / 255.0
    return img

推荐阅读