AttributeError:“ tuple”对象没有属性“ layer”

时间:2019-07-01 18:11:57

标签: python keras deep-learning

我正在使用以下代码,但出现以下错误。请帮助解决该问题。

我尝试使用无Lambda层的空间到深度函数,但出现如下错误

'NoneType' object has no attribute '_inbound_nodes'

def my_model(input_shape, output):

  if K.image_dim_ordering() == 'tf':
            input_shape= (input_shape[1], input_shape[2], input_shape[0])

  input = Input(input_shape)

  # First convolution Block

  conv_1 =bn_conv("x", 32, (5,5), 1, padding ="same")(input) 
  conv_2 =bn_conv("conv", 64, (5,5), 1, padding ="same")(conv_1)  
  conv_3 =bn_conv("conv", 128, (5,5), 1, padding ="same")(conv_2)
  concat1_3 = concatenate([conv_1, conv_3])
  conv_4 =bn_conv("x", 256, (5,5), 1, padding ="same")(concat1_3)
  concat1_4 = concatenate([conv_1, conv_4])

  # skip connections of first Blcock

  conv1_1 = Lambda(space_to_depth_x2)(conv_1)
  conv1_2 = Lambda(space_to_depth_x4)(conv_1)
  conv2_1 = Lambda(space_to_depth_x4)(conv_2)
  conv3_1 = Lambda(space_to_depth_x2)(conv_3)
  conv3_2 = Lambda(space_to_depth_x4)(conv_3)
  conv4_1 = Lambda(space_to_depth_x2)(conv_4)
  conv4_2 = Lambda(space_to_depth_x4)(conv_4)

  # First transition Block

  conv_t1 = bn_conv("conv",32, (1,1), 1)(concat1_4)
  max1 = MaxPooling2D(2,2)(conv_t1)

  # Second convolution Block
  conv_5 =bn_conv("x", 32, (3,3), 1, padding ="same")(max1)
  conv5_1 = Lambda(space_to_depth_x2)(conv_5)
  concat4_5 = concatenate([conv4_1, conv_5])
  conv_6 =bn_conv("conv", 64, (5,5), 1, padding ="same")(concat4_5)
  conv6_1 = Lambda(space_to_depth_x2)(conv_6)
  concat3_4_5 = concatenate([conv3_1,conv4_1,conv_5,conv_6])
  conv_7 =bn_conv("x", 128, (3,3), 1, padding ="same")(concat3_4_5)
  conv7_2 = Lambda(space_to_depth_x2)(conv_7)
  concat6_3_5_4_1 = concatenate([conv_7, conv_5, conv_6, conv1_1,  conv3_1, conv4_1])
  conv_8 =bn_conv("conv", 256, (5,5), 1, padding ="same")(concat6_3_5_4_1)
  concat7_5_4_1 = concatenate([conv_8,conv_7,conv_5,conv4_1,conv1_1])

  # Second transition Block

  conv_t2 = bn_conv("conv",32, (1,1), 1)(concat7_5_4_1)
  max2 = MaxPooling2D(2,2)(conv_t2)
  concat6 = concatenate([conv6_1, max2])

  # Second convolution Block
  conv_9 =bn_conv("conv", 32, (5,5), 1, padding ="same")(concat6)
  concat2_7_5 = concatenate([conv_9, conv2_1,conv7_2, conv5_1])
  conv_10 =bn_conv("x", 64, (5,5), 1, padding ="same")(concat2_7_5)
  concat5_9_3_2 = concatenate([conv_10,conv5_1,conv_9,conv3_2,conv2_1])
  conv_11 =bn_conv("conv", 128, (3,3), 1, padding ="same")(concat5_9_3_2)
  concat3_7_10_5_4 = concatenate([conv_11,conv3_2,conv7_2,conv_10,conv5_1,conv4_2])
  conv_12 =bn_conv("x", 256, (5,5), 1, padding ="same")(concat3_7_10_5_4)
  concat7_10_4 = concatenate([conv_12,conv7_2,conv_10,conv4_2 ])

  # Prediction layer
  conv_13 = Conv2D(10,(8,8))(concat7_10_4)

  flat = Flatten() (conv_13)

  act = Activation("softmax")(flat)

  model = Model(inputs=input, outputs=act)

  return model

model = my_model([3, 32, 32], 10)


使用Lambda函数避免'NoneType' object has no attribute '_inbound_nodes'时出现以下错误,因此请提出一些解决方案。

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-17-ca5cd7c7032e> in <module>()
     89   return model
     90 
---> 91 model = my_model([3, 32, 32], 10)
     92 from keras.optimizers import Adam
     93 

6 frames
<ipython-input-17-ca5cd7c7032e> in my_model(input_shape, output)
     36 # skip connections of first Blcock
     37 
---> 38   conv1_1 = Lambda(space_to_depth_x2)(conv_1)
     39   conv1_2 = Lambda(space_to_depth_x4)(conv_1)
     40   conv2_1 = Lambda(space_to_depth_x4)(conv_2)

/usr/local/lib/python3.6/dist-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)

/usr/local/lib/python3.6/dist-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 

/usr/local/lib/python3.6/dist-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)

/usr/local/lib/python3.6/dist-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 

/usr/local/lib/python3.6/dist-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 

/usr/local/lib/python3.6/dist-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'

1 个答案:

答案 0 :(得分:0)

我得到了答案。

我正在使用

from tensorflow.nn import space_to_depth

是在错误上方创建的。