1

私は、1000の代わりに8つのクラスを持つ分類問題のための事前秤量(heuritech/convnets-keras)を持つAlexNetを使用しています。Model(input=..,output=..)でネットワークを初期化し、最後にDense(1000)とActivation(softmax)の2つのレイヤーを作成し、Dense(8)とActivation(softmax)の2つのレイヤーを追加します。 しかし、その後、実行した後に私が手Kerraで事前に訓練されたAlexNet分類を変更する

Error when checking model target: expected softmax to have shape (None, 1000) but got array with shape (32, 8) 

エラーが32は私が推測する発電機からのバッチサイズですが、ソフトマックスはまだ前の層からの1000の寸法を期待し、なぜ私は理解していません。

誰かが私を助けることができますか?私はそれがモデルの出力パラメータを行う何かを持っていると思うが、これは試してみて、そしてグーグル・グーグル・グーグルで回った後の半野生の推測です。 ありがとう!

コード:

import ... 

pp = os.path.dirname(os.path.abspath(__file__)) 

##### Define Model ##### 
inputs = Input(shape=(3,227,227)) 
conv_1 = Convolution2D(96, 11, 11,subsample=(4,4),activation='relu', name='conv_1')(inputs) 
... 
... 
... 
dense_1 = MaxPooling2D((3, 3), strides=(2,2),name="convpool_5")(conv_5) 
dense_1 = Flatten(name="flatten")(dense_1) 
dense_1 = Dense(4096, activation='relu',name='dense_1')(dense_1) 
dense_2 = Dropout(0.5)(dense_1) 
dense_2 = Dense(4096, activation='relu',name='dense_2')(dense_2) 
dense_3 = Dropout(0.5)(dense_2) 
dense_3 = Dense(1000,name='dense_3')(dense_3) 
prediction = Activation("softmax",name="softmax")(dense_3) 

model = Model(input=inputs, output=prediction) 

for layer in model.layers[:27]: 
    print layer.name 
    layer.trainable = False 

model.load_weights(pp+"/weights/alexnet_weights.h5") 
print model.output_shape 

print model.layers[-1] 
model.layers.pop() 
print model.output_shape 
model.layers.pop() 
print model.layers[-1] 
print model.output_shape 
model.layers.append(Dense(8, activation='softmax',name='dense_4')) 

print model.layers[-1] 
##### Get Data ##### 
train_datagen = ImageDataGenerator(
     rescale=1./255, 
     shear_range=0.2, 
     zoom_range=0.2) 

test_datagen = ImageDataGenerator(rescale=1./255) 

train_generator = train_datagen.flow_from_directory(
     pp+'/dataset/training', 
     target_size=(227,227), 
     class_mode='categorical') 

validation_generator = test_datagen.flow_from_directory(
     pp+'/dataset/test', 
     target_size=(227,227), 
     class_mode='categorical') 

##### Compile and Fit #### 
sgd = SGD(lr=1e-4, decay=1e-6, momentum=0.9, nesterov=True) 
model.compile(optimizer=sgd, loss='mse') 

model.fit_generator(
     train_generator, 
     samples_per_epoch=500, 
     nb_epoch=5, 
     validation_data=validation_generator, 
     nb_val_samples=150) 

model.save_weights('first_try.h5') 

答えて

3

[OK]を、それも/ポップで新しいレイヤーを入れた後、何も変更するようだので、私はちょうど、ネットワークの定義を変更することはできませんようです。私はまだ機能APIによって定義され、ロードされたネットワークを変更する方法を知りたいのですが

1) Load the default AlexNet 

2) Load the pre-trained weights 

3) Pop the 2 top layers 

4) Add two new top layers 

5) Save the weights 

6) Change Network definition to use the two new layers 

7) Load the new AlexNet with the saved weights 

8) Profit! 

: は、だから私はこれをしませんでした。

関連する問題