Can't load ResNet50 to Sequential model...

481 views
Skip to first unread message

Marcelo Mota de Azevedo Junior

unread,
Apr 11, 2018, 1:21:02 PM4/11/18
to Keras-users
Hello everyone!

I am trying to load a ResNet50 with no top and connect it to a dense layer.

I can successfully load the ResNet50. The problem is when I try to add it to a Sequential model. Here is the code, and below the error I get. Already tried to google it but with no luck :/

Note: the problem is when it tries to load the 4th layer os ResNet50, which is a batchnorm (name = 'bn_conv1")

base_model = applications.ResNet50(weights='imagenet', include_top=False, classes=8,
                                input_shape=(224,224,3), pooling='avg')
model = Sequential()
for layer in base_model.layers:
    model.add(layer)


Error:

ValueError: Variable bn_conv1/moving_mean/biased already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:

  File "C:\Users\adminvale\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1269, in __init__
    self._traceback = _extract_stack()
  File "C:\Users\adminvale\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2506, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "C:\Users\adminvale\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 767, in apply_op
    op_def=op_def)


Thanks guys!

Matias Valdenegro

unread,
Apr 11, 2018, 3:17:31 PM4/11/18
to keras...@googlegroups.com
ResNet cannot be implemented as a sequential model, as the skip connections make it a graph model, so your only choice is to use the functional API for this. 

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/6fab89d8-f986-4c73-b921-b5271aa2dd21%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Marcelo Mota de Azevedo Junior

unread,
Apr 11, 2018, 4:00:33 PM4/11/18
to Keras-users
Thanks Matias! But how did this guy do it in this post?

https://www.kaggle.com/dansbecker/transfer-learning/notebook

He is an instructor at kaggle.

Regards

Daπid

unread,
Apr 11, 2018, 5:12:02 PM4/11/18
to Marcelo Mota de Azevedo Junior, Keras-users
He did:

my_new_model.add(ResNet50(include_top=False, pooling='avg', weights=resnet_weights_path))

Which is different than:


for layer in base_model.layers:
    model.add(layer)

In Keras, models can be used as layers, and he is creating a sequential model where the first layer is the whole Resnet module.

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users+unsubscribe@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/0be9bcbb-ce7e-4905-af47-f84ab05db59e%40googlegroups.com.

Marcelo Mota de Azevedo Junior

unread,
Apr 11, 2018, 6:32:37 PM4/11/18
to Keras-users
This for loop was my Second try. First I did his command and got the same error. Then I did the loop to see where It was happening.

Any ideas...? :(

Dodo

unread,
Apr 11, 2018, 9:06:42 PM4/11/18
to Keras-users
Hi,

How about you do something like this?
base_model = applications.ResNet50(weights='imagenet', include_top=False, classes=8,
                                input_shape=(224,224,3), pooling='avg')
top_model = Sequential()
top_model
.add(...(...,input_shape=base_model.output_shape[1:]))
...

intermediate_output
= base_model.layers[-1].output
final_output
= top_model(intermediate_output)

final_model
= Model(inputs=base_model.input,outputs=final_output)
final_model
.compile(...)


This is a work-around only, you may want to find some other correct way to do this. By this way you are going to have a nested network which is very difficult to visualize.

Marcelo Mota de Azevedo Junior

unread,
Apr 11, 2018, 9:23:16 PM4/11/18
to Keras-users
Thanks dodo. But do you guys have any Idea why the error says something about the variable related to a layer name already being used?

Dodo

unread,
Apr 11, 2018, 10:18:23 PM4/11/18
to Keras-users
Totally no idea :(((. May be some tensorflow name_scope & variable_scope thing. But I dont have any clue about that part
Reply all
Reply to author
Forward
0 new messages