python - Keras 检查输入 : expected input_4 to have shape (299, 299, 3) 时出错,但得到形状为 (64, 64, 3) 的数组

标签 python tensorflow keras computer-vision keras-layer

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

(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)

我想将此数据适合 keras 模型,例如 inception v3:

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)

但是我收到了这个错误:

检查输入时出错:预期 input_4 具有形状 (299, 299, 3),但得到的数组具有形状 (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)))

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

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

引用文献:

希望这有帮助!

关于python - Keras 检查输入 : expected input_4 to have shape (299, 299, 3) 时出错,但得到形状为 (64, 64, 3) 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59598927/

相关文章:

python - InvalidArgumentError : Specified a list with shape [60, 9] 来自形状为 [56,9] 的张量

python - Matplotlib 缩放时绘图分辨率不佳

python - 如何修复函数 'cv::matchTemplate'

linux - 尝试安装 Tensorflow 时在 Ubuntu 上出现 "No space left on device"

python - 只需要注意即可,仅保留视频分类的编码部分

python - Django,从 POST 获取值

c++ - Eigen sum() float

Python keras : to_categorical for multilabel values gives ValueError: invalid literal for int() with base 10

ios - 在 coremltools 转换中指定 swift 版本

machine-learning - Keras 中 fit_generator 的下一个纪元的起点