【问题标题】:How to input cifar10 into inceptionv3 in keras如何在keras中将cifar10输入到inceptionv3
【发布时间】:2017-03-01 02:15:10
【问题描述】:

我正在尝试使用 Inception v3 的预训练图像网络权重对 CIFAR10 图像进行分类。我正在使用以下代码。

from keras.applications.inception_v3 import InceptionV3

(xtrain, ytrain), (xtest, ytest) = cifar10.load_data()

input_cifar = Input(shape=(32, 32, 3))

base_model = InceptionV3(weights='imagenet', 
                         include_top=False, 
                         input_tensor=input_cifar)

但它在中间转换层给了我一个错误,比如“负维度”。

当我使用 VGG16 网络时不会发生这种情况。

我正在使用带有 tensorflow 后端和 tf dim ordernig 的 keras。

【问题讨论】:

    标签: python deep-learning keras


    【解决方案1】:

    Inception 网络在 224x224 大小的图像上进行训练,它们的下采样路径下降到 10x10 以下。因此,对于 32,32,3 图像,下采样会导致负维度大小。现在你可以做很多事情了。首先,您可以将 cifar10 数据集中的每个图像的大小调整为 224x224,并将此张量传递给初始模型。您可以删除网络的一些下采样过滤器。然后它仍然可以工作。第三,您可以在不更改分辨率的情况下进行零填充以增加图像大小。

    【讨论】:

    【解决方案2】:

    在这一层的documentation 中,人们可能会发现输入的最小形状是(150, 150, 3)(带有tf 暗排序)。您的输入要小得多。这个最小尺寸来自多个poolingvalid 边界模式,这使得每一层的输出更小——如果它小于某个尺寸——既不执行池化也不执行卷积。

    【讨论】:

    【解决方案3】:

    您需要将原始图像从 CIFAR10 (32, 32, 3) 上采样到至少 75, 75。

    base_model2 = InceptionV3(include_top=False, weights='imagenet', input_shape=(128, 128, 3)) # shape of images after upsampling that inception will accept
    
    for layer in base_model.layers: 
       layer.trainable = False
    
    #input are original (32, 32, 3) images
    inputs = tf.keras.layers.Input(shape=(32, 32, 3))
    #upsampling twice (ie. 32*2*2 = 128) big enough for inception
    upsamp1 = tf.keras.layers.UpSampling2D((2,2))(inputs)
    upsamp2 = tf.keras.layers.UpSampling2D((2,2))(upsamp1)
    pre_trained_model = base_model2(upsamp2)
    dense4 = tf.keras.layers.Dense(128, activation='relu'). 
        (pre_trained_model)
    predictions = tf.keras.layers.Dense(10, activation='softmax')(dense4)
    
    model = Model(inputs = inputs, outputs = predictions)
    model.compile(optimizer=Adam(learning_rate=0.01), loss='categorical_crossentropy', metrics=['accuracy']) 
    model.fit(.......)
    

    '

    【讨论】:

    • 我认为应该在预训练模型之后添加一个Flatten 层(如上所述here),因为OP 使用的导入的CIFAR10 形状为(50000,32,32,3)
    猜你喜欢
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 2018-02-05
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    • 1970-01-01
    相关资源
    最近更新 更多