Keras:检查输入时出错:预期输入_1的形状为(299,299,3),但数组的形状为(229,229,3)

恩齐奥

我正在尝试在keras中训练inceptionv3模型。

我的数据集已预处理为229, 229, 3形状。

print(data.shape)
print(type(data))
print(type(data[0]))

输出

(1458, 229, 229, 3)

<class 'numpy.ndarray'>

<class 'numpy.ndarray'>

我这样初始化我的模型

import os, sys
from keras.optimizers import SGD
from keras.applications import InceptionV3

model = InceptionV3()

# copile model
opt = SGD(lr=0.05)
model.compile(loss="categorical_crossentropy", optimizer=opt,
              metrics=["accuracy"])

调用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)
锡伯克杆菌

您为网络提供了形状错误的阵列。
您的模型期望有一个形状数组,(299, 299, 3)但是您给它一个形状数组(229, 229, 3)

(2 9 9,2 9 9,3)不是(2 2 9,2 2 9,9)

因此,您需要使用形状来重塑数据(299, 299, 3),或者需要更改InceptionV3的预期形状:

model = InceptionV3(include_top=False, input_shape=(229, 229, 3))

include_top=False如果要指定输入形状而不是默认形状,则必须使用

https://keras.io/applications/#inceptionv3

希望我能帮助你!

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档