Python 3.x 属性错误:';Conv2D&x27;对象没有属性';形状';
我是tensorflow的新手,我尝试使用tf.concat,所以我使用了这个布局,而不是常规的顺序布局。但我得到的错误是AttributeError:“tuple”对象没有属性“layer” 第二行中存在错误Python 3.x 属性错误:';Conv2D&x27;对象没有属性';形状';,python-3.x,tensorflow,keras,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= lay
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'
AttributeError回溯(最近一次调用)
在里面
1 inp=输入(形状=(10501050,3))
---->2x1=层。Conv2D(16,(3,3),激活='relu')(inp)
3x1=层。Conv2D(32,(3,3),激活='relu')(x1)
4 x1=层。MaxPoolig2D(2,2)(x1)
5 x2=层。Conv2D(32,(3,3),激活='relu')(x1)
/opt/conda/lib/python3.6/site-packages/tensorflow/python/keras/engine/base\u layer.py in\uuuuu调用(self,input,*args,**kwargs)
661 kwargs.pop(“培训”)
662输入,输出=自。\设置\连接\元数据_(
-->663输入、输出、args、kwargs)
664自我处理活动规则化(输入、输出)
665自设置掩码元数据(输入、输出、上一个掩码)
/opt/conda/lib/python3.6/site-packages/tensorflow/python/keras/engine/base\u layer.py in\u set\u connectivity\u metadata\u(self、input、output、args、kwargs)
1706 kwargs.pop('mask',None)#'mask'不应序列化。
1707自我添加入站节点(
->1708输入张量=输入,输出张量=输出,参数=kwargs)
1709返回输入、输出
1710
/opt/conda/lib/python3.6/site-packages/tensorflow/python/keras/engine/base\u layer.py in\u add\u inbound\u节点(self、输入张量、输出张量、参数)
1793 """
1794 inbound_layers=nest.map_结构(lambda t:t._keras_history.layer,
->1795输入(U张量)
1796 node_index=nest.map_结构(lambda t:t._keras_history.node_index,
1797输入(U张量)
/map_结构中的opt/conda/lib/python3.6/site-packages/tensorflow/python/util/nest.py(func,*structure,**kwargs)
513
514返回包\u序列\u组件(
-->515结构[0],[func(*x)表示条目中的x],
516 expand_composites=expand_composites)
517
/opt/conda/lib/python3.6/site-packages/tensorflow/python/util/nest.py in(.0)
513
514返回包\u序列\u组件(
-->515结构[0],[func(*x)表示条目中的x],
516 expand_composites=expand_composites)
517
/opt/conda/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py in(t)
1792创建节点的调用处的层的“call”方法。
1793 """
->1794 inbound_layers=nest.map_结构(lambda t:t._keras_history.layer,
1795输入(U张量)
1796 node_index=nest.map_结构(lambda t:t._keras_history.node_index,
AttributeError:“元组”对象没有属性“层”
请任何人告诉我该怎么做
代码与以前相比变化不大,请再看一看您忘记在第四行中向x2传递一个输入参数,x3和x4也是如此。因此,不要编写
x2= layers.Conv2D(32,(3,3), activation='relu')
你应该
x2= layers.Conv2D(32,(3,3), activation='relu')(x1)
您需要实例化一个
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已经有一个层来执行连接。如上述问题的答案中所建议的,您需要包装tf.concat()
在Lambda
层内。或者,您可以使用keras.layers.concatenate(…)
进行连接。您还需要有实际的输入层,当前输入是Conv2D。@MatiasValdenegro错误显示在第2行是,所以?问题仍然是一样的,您不能将非输入层作为输入提供给函数API中的另一层。您能解释一下,如果第2行出现新错误,应该怎么办吗ne AttributeError:“tuple”对象没有属性“layer”@Lawlesx请添加整个回溯,孤立的错误消息是没有意义的。@Lawlesx我无法用给定的代码重现错误(我得到了其他错误,但不是你在那一行中提到的错误)。请提供复制错误的自包含示例,我们可以运行。我没有获得自包含示例,但我添加了所需的导入文件