Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keras SimpleRNN convert to Caffe RNN #20

Open
huangeason26 opened this issue Dec 4, 2019 · 1 comment
Open

keras SimpleRNN convert to Caffe RNN #20

huangeason26 opened this issue Dec 4, 2019 · 1 comment

Comments

@huangeason26
Copy link

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

@uhfband
Copy link
Owner

uhfband commented Dec 6, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants