AttributeError:“ Conv2D”对象没有属性“ shape”

时间:2019-08-12 06:33:42

标签: python-3.x tensorflow keras

我是Tensorflow的新手,我想使用tf.concat,所以我使用了这种布局,而不是常规的顺序布局。但是我得到的错误是AttributeError:'tuple'对象没有属性'layer' 错误出现在第二行

inp = Input(shape=(1050,1050,3))
x1= layers.Conv2D(16 ,(3,3), activation='relu')(inp)
x1= layers.Conv2D(32,(3,3), activation='relu')(x1)
x1= layers.MaxPooling2D(2,2)(x1)
x2= layers.Conv2D(32,(3,3), activation='relu')(x1)
x2= layers.Conv2D(64,(3,3), activation='relu')(x2)
x2= layers.MaxPooling2D(3,3)(x2)
x3= layers.Conv2D(64,(3,3), activation='relu')
x3= layers.Conv2D(64,(2,2), activation='relu')(x3)
x3= layers.Conv2D(64,(3,3), activation='relu')(x3)
x3= layers.Dropout(0.2)(x3)
x3= layers.MaxPooling2D(2,2)(x3)
x4= layers.Conv2D(64,(3,3), activation='relu')
x4= layers.MaxPooling2D(2,2)(x4)
x = layers.Dropout(0.2)(x4)
o = layers.Concatenate(axis=3)([x1, x2, x3, x4, x])
y = layers.Flatten()(o)
y = layers.Dense(1024, activation='relu')(y)
y = layers.Dense(5, activation='softmax')(y) 

model = Model(inp, y)
model.summary()
model.compile(loss='sparse_categorical_crossentropy',optimizer=RMSprop(lr=0.001),metrics=['accuracy'])

导入的文件是

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import pandas as pd
import shutil
import csv
import tensorflow as tf
import keras_preprocessing
from keras_preprocessing import image
from keras_preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras import layers
from tensorflow.keras import Model
from keras.layers import Input

错误是

AttributeError                            Traceback (most recent call last)
<ipython-input-8-40840424e579> in <module>
      1 inp = Input(shape=(1050,1050,3))
----> 2 x1= layers.Conv2D(16 ,(3,3), activation='relu')(inp)
      3 x1= layers.Conv2D(32,(3,3), activation='relu')(x1)
      4 x1= layers.MaxPooling2D(2,2)(x1)
      5 x2= layers.Conv2D(32,(3,3), activation='relu')(x1)

/opt/conda/lib/python3.6/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)

/opt/conda/lib/python3.6/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 

/opt/conda/lib/python3.6/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)

/opt/conda/lib/python3.6/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 

/opt/conda/lib/python3.6/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 

/opt/conda/lib/python3.6/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'

请任何人告诉我该怎么做 密码与以前相比变化不大,请再次查看

2 个答案:

答案 0 :(得分:2)

您忘了在第四行中将x2作为输入参数传递给x3和x4。所以不用写

x2= layers.Conv2D(32,(3,3), activation='relu')

您应该拥有

x2= layers.Conv2D(32,(3,3), activation='relu')(x1)

答案 1 :(得分:1)

您需要实例化Input层以将输入提供给第一层:

inp = Input(shape=(1050,1050,3))
x1= layers.Conv2D(16 ,(3,3), activation='relu')(inp)
x1= layers.Conv2D(32,(3,3), activation='relu')(x1)
x1= layers.MaxPooling2D(2,2)(x1)
x2= layers.Conv2D(32,(3,3), activation='relu')(x1)
x2= layers.Conv2D(64,(3,3), activation='relu')(x2)
x2= layers.MaxPooling2D(3,3)(x2)
x3= layers.Conv2D(64,(3,3), activation='relu')(x2)
x3= layers.Conv2D(64,(2,2), activation='relu')(x3)
x3= layers.Conv2D(64,(3,3), activation='relu')(x3)
x3= layers.Dropout(0.2)(x3)
x3= layers.MaxPooling2D(2,2)(x3)
x4= layers.Conv2D(64,(3,3), activation='relu')(x3)
x4= layers.MaxPooling2D(2,2)(x4)
x = layers.Dropout(0.2)(x4)
o = layers.Concatenate(axis=3)([x1, x2, x3, x4, x])
y = layers.Flatten()(o)
y = layers.Dense(1024, activation='relu')(y)
y = layers.Dense(5, activation='softmax')(y) 

model = Model(inp, y)
model.summary()
model.compile(loss='sparse_categorical_crossentropy',optimizer=RMSprop(lr=0.001),metrics=['accuracy'])

如另一个答案中所述,您也没有将正确的输入传递给Conv2D层之一。而且您不能直接在Keras张量上使用tf函数,因为Keras已经具有执行连接的层。