Python Keras:检查输入时出错:预期输入_1具有形状(299,299,3),但获得具有形状(229,229,3)的数组
我想在keras训练一个inceptionv3模型 我的数据集被预处理成Python Keras:检查输入时出错:预期输入_1具有形状(299,299,3),但获得具有形状(229,229,3)的数组,python,keras,Python,Keras,我想在keras训练一个inceptionv3模型 我的数据集被预处理成2292293形状 print(data.shape) print(type(data)) print(type(data[0])) 输出 (1458, 229, 229, 3) <class 'numpy.ndarray'> <class 'numpy.ndarray'> 调用model.fit # train the network print("[INFO] training netwo
2292293
形状
print(data.shape)
print(type(data))
print(type(data[0]))
输出
(1458, 229, 229, 3)
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
调用model.fit
# train the network
print("[INFO] training network...")
H = model.fit(train_x, train_y, validation_data=(test_x, test_y),
batch_size=batch_size, epochs=num_of_epochs, verbose=1)
然后我得到了这个错误。我不明白,因为尺寸是正确的
[INFO] training network...
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in
2 print("[INFO] training network...")
3 H = model.fit(train_x, train_y, validation_data=(test_x, test_y),
----> 4 batch_size=batch_size, epochs=num_of_epochs, verbose=1)
5
6 model.save(model_save_path)
~/anaconda3/lib/python3.7/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
950 sample_weight=sample_weight,
951 class_weight=class_weight,
--> 952 batch_size=batch_size)
953 # Prepare validation data.
954 do_validation = False
~/anaconda3/lib/python3.7/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
749 feed_input_shapes,
750 check_batch_axis=False, # Don't enforce the batch size.
--> 751 exception_prefix='input')
752
753 if y is not None:
~/anaconda3/lib/python3.7/site-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
136 ': expected ' + names[i] + ' to have shape ' +
137 str(shape) + ' but got array with shape ' +
--> 138 str(data_shape))
139 return data
140
ValueError: Error when checking input: expected input_1 to have shape (299, 299, 3) but got array with shape (229, 229, 3)
编辑
batch_size = 32
如何调整图像大小
import imutils
import cv2
class AspectAwarePreprocessor:
"""
CONTRUCTOR
witdh : desired width
height : desired height
inter : interpolation method used when resizing the image
"""
def __init__(self,width,height,inter=cv2.INTER_AREA):
self.width = width
self.height = height
self.inter = inter
"""
image : image to be preprocessed
"""
def preprocess(self,image):
# Get wdith and height of image
(h, w) = image.shape[:2]
dW = 0
dH = 0
# if width is the shorter dimension, resize image by width and crop height
if w < h:
image = imutils.resize(image, width=self.width,
inter=self.inter)
dH = int((image.shape[0] - self.height) / 2.0)
# if height is the shorter dimension, resize image by height and crop width
else:
image = imutils.resize(image, height=self.height,
inter=self.inter)
dW = int((image.shape[1] - self.width) / 2.0)
# re-grab the width and height and use the deltas to crop the center of the image:
(h, w) = image.shape[:2]
image = image[dH:h - dH, dW:w - dW]
# our image target image dimensions may be off by ± one pixel; therefore, we make a call to cv2.resize to
# ensure our output image has the desired width and height.
return cv2.resize(image, (self.width, self.height),
interpolation=self.inter)
导入imutils
进口cv2
类AspectawareProcessor:
"""
承包商
witdh:所需宽度
高度:所需高度
inter:调整图像大小时使用的插值方法
"""
定义初始(自身、宽度、高度、内部=cv2.内部面积):
self.width=宽度
自我高度=高度
self.inter=inter
"""
图像:要预处理的图像
"""
def预处理(自身、图像):
#获取图像的宽度和高度
(h,w)=图像形状[:2]
dW=0
dH=0
#如果“宽度”是较短的尺寸,请按宽度和裁剪高度调整图像大小
如果w
您正在为网络提供一个错误形状的数组。您的模型需要一个shape
(299299,3)
数组,但您给它一个shape(229229,3)
(299,299,3)不是(229,229,3)
因此,您需要使用(299,299,3)
形状重塑数据,或者需要更改InceptionV3的预期形状:
model = InceptionV3(include_top=False, input_shape=(229, 229, 3))
如果要指定输入形状而不是默认形状,则必须使用include_top=False
希望我能帮助你 预先训练的模型只能接受特定的形状。你需要调整图片的大小。我建议您使用
PIL
from PIL import Image
import numpy as np
X_train = Image.fromarray(X_train).resize((299, 299))
X_train = np.array(X_train)
这是一个如何进行的示例。您能说明如何定义批次大小吗?看来问题与此有关。@AdamB。我在上面更新了它。批量大小是32。嗨,你能解释一下尺寸是怎么错的吗。因为它们看起来一模一样。还包括_top=False,我只是在学习keras,在这个特定示例中,我没有尝试使用迁移学习。只需在我的数据集中从fresh中训练inceptionv3并进行分类。@Enzio
299
与229
不同。我的意思是他们有点相似,但仍然不一样…这个答案是错误的include_top
是更改输出类的数量,而不是图像形状。正如sxeros所说,仔细看,这不是相同的数字。Include_top=False意味着您将没有最后一个分类器,因此您需要创建自己的分类器。如果您只是想对数据进行培训,最简单的方法是将数据重新调整为(299,299,3)我更新了显示我调整图像大小的部分的代码。我的图像大小调整为(299299,3)
,如上所示。我不明白。如果你的图像真的是(299299,3)
,那么你就没有理由收到这个错误消息。如果是这样的形状,你就没有正确地调整它们的大小。在preprocess
函数的return
语句之前,编写assert np.array(image.shape==(299299,3),“Error,图片形状为{}”。format(np.array(image.shape)
,并确保通过断言测试
from PIL import Image
import numpy as np
X_train = Image.fromarray(X_train).resize((299, 299))
X_train = np.array(X_train)