我怎么知道图像分类器中图像的最佳重塑尺寸?
how can i know the best reshape size of an image in an image classifier?
当我尝试创建仅包含两个数字(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的代码会在第一个答案里!
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
# dimensions of our images.
img_width, img_height = 200, 55
train_data_dir = 'C:/Users/ADEM/Desktop/msi_youssef/PFE/test/numbers/data/train'
validation_data_dir = 'C:/Users/ADEM/Desktop/msi_youssef/PFE/test/numbers/data/valid'
nb_train_samples = 140
nb_validation_samples = 30
epochs = 10 # how much time you want to train your model on the data
batch_size = 16
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy'])
# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.1,
zoom_range=0.05,
horizontal_flip=False)
# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
model.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=nb_validation_samples // batch_size)
model.save('first_try.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
当我尝试创建仅包含两个数字(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的代码会在第一个答案里!
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
# dimensions of our images.
img_width, img_height = 200, 55
train_data_dir = 'C:/Users/ADEM/Desktop/msi_youssef/PFE/test/numbers/data/train'
validation_data_dir = 'C:/Users/ADEM/Desktop/msi_youssef/PFE/test/numbers/data/valid'
nb_train_samples = 140
nb_validation_samples = 30
epochs = 10 # how much time you want to train your model on the data
batch_size = 16
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy'])
# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.1,
zoom_range=0.05,
horizontal_flip=False)
# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
model.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=nb_validation_samples // batch_size)
model.save('first_try.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