2017-10-30 15 views
0

Kerasアプリケーションを使用してネットワークの数を実装しようとしています。ここで私は、コードの一部を添付していますし、このコードはResNet50とVGG16のため正常に動作しますが、それはMobileNetに来るとき、それはエラーを生成します。MobileNet ValueError:ターゲットを確認するときにエラーが発生しました:dense_1が4次元であると予想されましたが、形状(24,2)の配列を持っていました

ValueError: Error when checking target: expected dense_1 to have 4 dimensions, but got array with shape (24, 2)

私は24の3つのチャンネルとバッチサイズで224x224の画像で働いていますし、 2つのクラスでそれらを分類しようとしているので、エラーで言及された番号24はバッチサイズですが、私は数2についてはわかりません。おそらくクラス数です。

Btwは、私がkeras.applications.mobilenetのこのエラーを受け取っている理由を知っている人は誰ですか?

# basic_model = ResNet50() 
# basic_model = VGG16() 
basic_model = MobileNet() 
classes = list(iter(train_generator.class_indices)) 
basic_model.layers.pop() 
for layer in basic_model.layers[:25]: 
    layer.trainable = False 
last = basic_model.layers[-1].output 
temp = Dense(len(classes), activation="softmax")(last) 

fineTuned_model = Model(basic_model.input, temp) 
fineTuned_model.classes = classes 
fineTuned_model.compile(optimizer=Adam(lr=0.0001), loss='categorical_crossentropy', metrics=['accuracy']) 
fineTuned_model.fit_generator(
     train_generator, 
     steps_per_epoch=3764 // batch_size, 
     epochs=100, 
     validation_data=validation_generator, 
     validation_steps=900 // batch_size) 
fineTuned_model.save('mobile_model.h5') 

答えて

0

ソースコードから、Reshape()のレイヤーがポップアップしていることがわかります。コンボリューションの出力(4D)をクラステンソル(2D)に変換するものとまったく同じです。

Source code:

if include_top: 
    if K.image_data_format() == 'channels_first': 
     shape = (int(1024 * alpha), 1, 1) 
    else: 
     shape = (1, 1, int(1024 * alpha)) 

    x = GlobalAveragePooling2D()(x) 
    x = Reshape(shape, name='reshape_1')(x) 
    x = Dropout(dropout, name='dropout')(x) 
    x = Conv2D(classes, (1, 1), 
       padding='same', name='conv_preds')(x) 
    x = Activation('softmax', name='act_softmax')(x) 
    x = Reshape((classes,), name='reshape_2')(x) 

しかし、すべてのkeras畳み込みモデルが異なる方法で使用されることを意味しています。独自のクラス数が必要な場合は、include_top=Falseでこれらのモデルを作成する必要があります。この方法で、モデル(クラスの一部)の最後の部分は、単に存在しないであろうと、あなたは自分自身のレイヤーを追加します。

basic_model = MobileNet(include_top=False) 
for layer in basic_model.layers: 
    layers.trainable=False 

furtherOutputs = YourOwnLayers()(basic_model.outputs) 

おそらくclassesを変え、最後の部分はkerasコードに示すことをコピーしようとしなければなりません自分のクラス数でまたは、完全なモデルの3つのレイヤー(ReshapeActivationConv2D)を自分のものに置き換えてみてください。

+0

ありがとうございます。このリンクにも短い回答があります:https://github.com/fchollet/keras/issues/8301 – Pedram

関連する問題

 関連する問題