python - 我如何知道图像分类器中图像的最佳重塑尺寸?
问题描述
当我尝试创建只有两个数字(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 的代码将在第一个答案中!
解决方案
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
推荐阅读
- excel - 为什么这个循环在第一次通过后停止?
- javascript - 使用 php 将 html 表单值发送到电子邮件
- python - 将字符串文件转换为所需格式:将 / 替换为 _,逗号前除外
- javascript - 为什么会发生?带有 Date() 减法的 JavaScript 的奇怪行为。CodePen + 视频
- python - 如何使用 1 行代码根据 pandas 数据框中的值删除多行?
- javascript - 富文本编辑器和源代码编辑器,使用 javascript 相互传输内容
- r - 将文本文件中的谷歌评论转换为 rmarkdown 脚注
- swift - 快速解析 get_video_info(YouTube 信息数据)
- c++ - 更改对象并在同一表达式中使用它但子表达式由逗号运算符分隔是未定义的行为吗?
- reactjs - 如何将异步存储值直接分配给状态