【发布时间】:2020-07-03 19:11:06
【问题描述】:
当我尝试创建只有两个数字(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 的代码将在第一个答案中!
【问题讨论】:
标签: python numpy tensorflow keras classification