Keras 检查输入时出错:预期 input_4 具有形状 (299, 299, 3) 但得到形状为 (64, 64, 3) 的数组

Keras Error when checking input: expected input_4 to have shape (299, 299, 3) but got array with shape (64, 64, 3)

我有大量的 pickle 数据,训练、测试、验证类似于以下形状:

(n_samples, 64, 64, 3)


[array([[[26, 16, 24],
         [36, 20, 31],
         [47, 28, 42],
         ...,
         [15,  8, 15],
         [ 8,  5, 10],
         [ 3,  2,  6]],
         ...,
        [[41, 27, 38],
         [54, 37, 51],
         [68, 47, 61],
         ...,
         [22, 14, 21],
         [16,  9, 16],
         [11,  6, 12]]], dtype=uint8),
 array([[[209, 126, 116],
         [212, 125, 117],
         [215, 135, 127],
         ...,

我改成了:

a=[l.tolist() for l in train_images]
   #x = np.expand_dims(a, axis=0)
train_x =np.array(a)


train_x:
array([[[[ 26,  16,  24],
         [ 36,  20,  31],
         [ 47,  28,  42],
         ...,
         [ 15,   8,  15],
         [  8,   5,  10],
         [  3,   2,   6]],

train_x= preprocess_input(train_x)

标签类似于:

from keras.utils.np_utils import to_categorical
train_y = to_categorical(labels, 2)
train_y :
array([[0., 1.],
       [0., 1.],
       [0., 1.],
       ...,
       [0., 1.],
       [1., 0.],
       [0., 1.]], dtype=float32)

我想将此数据拟合到类似于 inception v3 的 keras 模型:

from keras.applications.inception_v3 import InceptionV3
from keras import optimizers

base_model = InceptionV3(weights='imagenet', include_top = True)
model.compile(optimizer = optimizers.SGD(lr=1e-3, momentum=0.9),
                  loss='categorical_crossentropy', metrics=['accuracy'])

model.fit(train_x, train_y , batch_size=128, nb_epoch=1,verbose=0)

但是我得到了这个错误:

Error when checking input:expected input_4 to have the shape (299, 299, 3) but got array with shape (64, 64, 3)

我知道这个错误是关于维度的。我怎样才能将代码修改为运行?也许使用冻结层或微调或更改输入尺寸(我不想丢失功能和重要数据)。请重写正确的代码,如果你知道的话。

base_model = InceptionV3(weights='imagenet', include_top = True) 行中包含 input_tensor=Input(shape=(64, 64, 3)) 如下:

base_model = InceptionV3(weights='imagenet', include_top = True, input_tensor=Input(shape=(64, 64, 3)))

如果您需要使用预训练网络进行迁移学习,但如果原始模型是在与手头任务不同形状的输入上训练的,则需要使用上述方法。

注意:输入形状不能是任何维度,因为我们可能使用的模型结构,如转置卷积、跳过连接等,需要输入具有一定的维度,以便稍后连接或执行元素明智的乘法等。

参考文献:

希望对您有所帮助!