4

I'm trying to do transfer learning with keras model, but got stuck on adding new layers to the model. I've tried code below :

prev_model = load_model('final_model.h5') # loading the previously saved model.

new_model = Sequential()
new_model.add(prev_model)
new_model.add(Dense(256,activation='relu'))
new_model.add(Dropout(0.5))
new_model.add(Dense(1,activation='sigmoid'))

but got :

TypeError: The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.layers.core.Flatten object at 0x00000000B74364A8>

It happens whenever i'm using .add() to add layers .

Then I found

number_of_layers_to_freeze = 10
vgg_model = VGG16(include_top=False)
for i in range(number_of_layers_to_freeze):
    vgg_model.layers[i].trainable = False
vgg_output = vgg_model.outputs[0]
output = keras.layers.Dense(10, activation="softmax")(vgg_output)

model = keras.models.Model(inputs=vgg_model.inputs, outputs=output)

at other post. but it leads to

 AttributeError: 'tuple' object has no attribute 'layer'

I'm currently using

keras 2.2.5 
tensorflow-gpu 1.14.0

Is it caused by version conflict ?


full traceback :(AttributeError: 'tuple' object has no attribute 'layer')

    AttributeError                            Traceback (most recent call last)
<ipython-input-15-afcad6e65f32> in <module>
      4 #     vgg_model.layers[i].trainable = False
      5 vgg_output = conv_base.outputs[0]
----> 6 output = tensorflow.keras.layers.Dropout(dropout_rate, name="dropout_out")(vgg_output)
      7 
      8 model1 = models.Model(inputs=conv_base.inputs, outputs=output)

G:\ProgramData\Anaconda3\envs\tensorf\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in __call__(self, inputs, *args, **kwargs)
    661               kwargs.pop('training')
    662             inputs, outputs = self._set_connectivity_metadata_(
--> 663                 inputs, outputs, args, kwargs)
    664           self._handle_activity_regularization(inputs, outputs)
    665           self._set_mask_metadata(inputs, outputs, previous_mask)

G:\ProgramData\Anaconda3\envs\tensorf\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in _set_connectivity_metadata_(self, inputs, outputs, args, kwargs)
   1706     kwargs.pop('mask', None)  # `mask` should not be serialized.
   1707     self._add_inbound_node(
-> 1708         input_tensors=inputs, output_tensors=outputs, arguments=kwargs)
   1709     return inputs, outputs
   1710 

G:\ProgramData\Anaconda3\envs\tensorf\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in _add_inbound_node(self, input_tensors, output_tensors, arguments)
   1793     """
   1794     inbound_layers = nest.map_structure(lambda t: t._keras_history.layer,
-> 1795                                         input_tensors)
   1796     node_indices = nest.map_structure(lambda t: t._keras_history.node_index,
   1797                                       input_tensors)

G:\ProgramData\Anaconda3\envs\tensorf\lib\site-packages\tensorflow\python\util\nest.py in map_structure(func, *structure, **kwargs)
    513 
    514   return pack_sequence_as(
--> 515       structure[0], [func(*x) for x in entries],
    516       expand_composites=expand_composites)
    517 

G:\ProgramData\Anaconda3\envs\tensorf\lib\site-packages\tensorflow\python\util\nest.py in <listcomp>(.0)
    513 
    514   return pack_sequence_as(
--> 515       structure[0], [func(*x) for x in entries],
    516       expand_composites=expand_composites)
    517 

G:\ProgramData\Anaconda3\envs\tensorf\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in <lambda>(t)
   1792             `call` method of the layer at the call that created the node.
   1793     """
-> 1794     inbound_layers = nest.map_structure(lambda t: t._keras_history.layer,
   1795                                         input_tensors)
   1796     node_indices = nest.map_structure(lambda t: t._keras_history.node_index,

AttributeError: 'tuple' object has no attribute 'layer'

fulltraceback :(TypeError: The added layer must be an instance of class Layer.)

TypeError                                 Traceback (most recent call last)
<ipython-input-42-b5858637ba91> in <module>
      2 # model.add(prev_model)
----> 3 model.add(tensorflow.keras.layers.GlobalMaxPooling2D(name="gap"))
      4 model.add(Flatten(name="flatten"))
      5 if dropout_rate > 0:
      6     model.add(layers.Dropout(dropout_rate, name="dropout_out"))

G:\ProgramData\Anaconda3\envs\tensorf\lib\site-packages\keras\engine\sequential.py in add(self, layer)
    131             raise TypeError('The added layer must be '
    132                             'an instance of class Layer. '
--> 133                             'Found: ' + str(layer))
    134         self.built = False
    135         if not self._layers:

TypeError: The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.layers.core.Flatten object at 0x00000000B74364A8>
3
  • 1
    Please post the full trackback here. Commented Nov 13, 2019 at 9:31
  • Traceback will also indicate line numbers of your source code. Could you mark the lines in the posted code to show where the Errors are thrown as well ? Commented Nov 13, 2019 at 9:34
  • 1
    For the layer attribute error, I suppose that vgg_output is not what you suppose it is. You should print it to make sure. For the class Layer error, I remember that Keras didn't like nested models (adding prev_model to your Sequential model) some times ago. Maybe you can investigate this... Commented Nov 13, 2019 at 10:05

1 Answer 1

7
TypeError: The added layer must be an instance of class Layer

You have created model using keras.models.Model while you are adding a layer from tensorflow.keras.layers.
Note that keras and tensorflow.keras is different. Make sure that you stick with one of them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.