keras2caffe icon indicating copy to clipboard operation
keras2caffe copied to clipboard

keras SimpleRNN convert to Caffe RNN

Open huangeason26 opened this issue 5 years ago • 1 comments

hello, when i try to convert an SimpleRNN keras model to RNN caffe model , i got an error:

ValueError: could not broadcast input array from shape (256, 256) into shape (256, 1)

here is my keras code:

def model_seq_rec(): rnn_size = 256

input_tensor = Input(shape=(21, 6, 512))

x = Reshape(target_shape=[21, 6*512], input_shape=[21, 6, 512])(input_tensor)
x = BatchNormalization()(x)

x = Dense(rnn_size, activation='relu')(x)
x = BatchNormalization()(x)

rnn_1 = SimpleRNN(rnn_size, return_sequences=True,
                  kernel_initializer='he_normal', name='rnn1', return_state=True, stateful=True)(x)

x = BatchNormalization()(rnn_1)

x = Dense(84, kernel_initializer='he_normal', activation='softmax')(x)

model = Model(inputs=input_tensor, outputs=x)
model.compile(loss='binary_crossentropy', optimizer='adadelta', metrics=['accuracy'])

return model

here is the convertion code:

    elif layer_type == 'SimpleRNN':
        shape = layer.input_shape

        # bottom[0]
        shape0 = (shape[1], shape[2], 1, 1)
        name_bottom0 = name + "_bottom0"
        caffe_net[name_bottom0] = L.Reshape(caffe_net[outputs[bottom]],
                                    reshape_param={'shape': {'dim': list(shape0)}})

        # bottom[1]
        shape1= (shape[1], shape[2])
        name_bottom1 = name + "_bottom1"
        caffe_net[name_bottom1] = L.Reshape(caffe_net[outputs[bottom]],
                                            reshape_param={'shape': {'dim': list(shape1)}})

        
        caffe_net[name] = L.RNN(caffe_net[name_bottom0], caffe_net[name_bottom1], recurrent_param=dict(num_output=config['units'], weight_filler=dict(type='uniform'), bias_filler=dict(type='contant')))

        net_params[name] = blobs

i'll appreciate if you can help to solve this convertion issue

huangeason26 avatar Dec 04 '19 05:12 huangeason26

Sorry I don't have much experience with the recurrent layers. Try to compare params shape for RNN layer in caffe and Keras

uhfband avatar Dec 06 '19 16:12 uhfband