我正在尝试使用keras模型进行迁移学习, 但是在为模型添加新图层时陷入困境。 我已经尝试过以下代码:
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'))
但是得到了:
TypeError: The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.layers.core.Flatten object at 0x00000000B74364A8>
只要我使用.add()
添加图层,就会发生这种情况。
然后我找到了
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)
在其他职位。 但它导致
AttributeError: 'tuple' object has no attribute 'layer'
我当前正在使用
keras 2.2.5
tensorflow-gpu 1.14.0
是由于版本冲突引起的吗?
完整回溯:(AttributeError:“ tuple”对象没有属性“ 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:添加的层必须是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>
答案 0 :(得分:1)
TypeError: The added layer must be an instance of class Layer
从 tensorflow.keras.layers 添加图层时,您已经使用 keras.models.Model 创建了模型。
请注意, keras 和 tensorflow.keras 是不同的。确保坚持使用其中之一。