2016-11-23 7 views
1

Ich versuche, Dekonvolution in Keras zu implementieren. Mein Modell Definition lautet wie folgt:Wie Dekonvolution in Keras/Theano durchzuführen?

model=Sequential() 


model.add(Convolution2D(32, 3, 3, border_mode='same', 
         input_shape=X_train.shape[1:])) 
model.add(Activation('relu')) 
model.add(Convolution2D(32, 3, 3, border_mode='same')) 
model.add(Activation('relu')) 
model.add(MaxPooling2D(pool_size=(2, 2))) 
model.add(Dropout(0.25)) 

model.add(Convolution2D(64, 3, 3, border_mode='same')) 
model.add(Activation('relu')) 
model.add(Convolution2D(64, 3, 3,border_mode='same')) 
model.add(Activation('relu')) 
model.add(MaxPooling2D(pool_size=(2, 2))) 
model.add(Dropout(0.25)) 

model.add(Flatten()) 
model.add(Dense(512)) 
model.add(Activation('relu')) 
model.add(Dropout(0.5)) 
model.add(Dense(nb_classes)) 
model.add(Activation('softmax')) 

I Entfaltungs oder transponiert Faltung auf den Ausgang von der ersten Faltungsschicht d convolution2d_1 gegeben ausgeführt werden soll.

Lets sagen, dass die Merkmalskarte wir nach der ersten Faltungsschicht X ist, die von (9, 32, 32, 32) ist, wo 9 die Anzahl der Bilder der Dimension ist 32x32 ich durch die Schicht durchlaufen haben. Die Gewichtsmatrix der ersten Schicht erhält man durch get_weights() Funktion von Keras. Die Dimension der Gewichtsmatrix ist (32, 3, 3, 2).

Der Code, den ich für die Durchführung umgesetzt Faltung bin mit ist

conv_out = K.deconv2d(self.x, W, (9,3,32,32), dim_ordering = "th") 
deconv_func = K.function([self.x, K.learning_phase()], conv_out) 
X_deconv = deconv_func([X, 0 ]) 

Aber immer Fehler:

CorrMM shape inconsistency: 
    bottom shape: 9 32 34 34 
    weight shape: 3 32 3 3 
    top shape: 9 32 32 32 (expected 9 3 32 32) 

Kann jemand bitte sagen Sie mir, wo ich falsch gehe?

Antwort

0

Sie können einfach Deconvolution2D Schicht verwenden. Hier

ist, was Sie zu erreichen versuchen:

batch_sz = 1 
output_shape = (batch_sz,) + X_train.shape[1:] 
conv_out = Deconvolution2D(3, 3, 3, output_shape, border_mode='same')(model.layers[0].output) 

deconv_func = K.function([model.input, K.learning_phase()], [conv_out]) 

test_x = np.random.random(output_shape) 
X_deconv = deconv_func([test_x, 0 ]) 

Aber es ist besser, um ein funktionsfähiges Modell zu erstellen, die sowohl für das Training und die Vorhersage helfen ..

batch_sz = 10 
output_shape = (batch_sz,) + X_train.shape[1:] 
conv_out = Deconvolution2D(3, 3, 3, output_shape, border_mode='same')(model.layers[0].output) 

model2 = Model(model.input, [model.output, conv_out]) 
model2.summary() 
model2.compile(loss=['categorical_crossentropy', 'mse'], optimizer='adam') 
model2.fit(X_train, [Y_train, X_train], batch_size=batch_sz) 
0

In Keras, Conv2DTranspose Schicht führen transponierte Faltung in anderen Begriffen Entfaltung. Es unterstützt beide Backend-Bibliotheken, d. H. Theano & Keras.

Keras Documentation sagt:

Conv2DTranspose

Transposed convolution layer (sometimes called Deconvolution).

The need for transposed convolutions generally arises from the desire to use a transformation going in the opposite direction of a normal convolution, i.e., from something that has the shape of the output of some convolution to something that has the shape of its input while maintaining a connectivity pattern that is compatible with said convolution.

Verwandte Themen