在使用tensorflow keras时,我收到错误消息,对于第一个conv2D层,添加的层必须是类的实例

时间:2019-02-22 17:13:43

标签: python-3.x tensorflow keras conv-neural-network

我正在尝试使用cnn模型将癌细胞的图像分类为好,坏或平均。建立模型时出现以下错误。

TypeError                                 Traceback 
(most recent call last)
<ipython-input-12-40a51db8d561> in <module>()
      3 
      4 
----> 5 
 model.add(Conv2D(filters=32,kernel_size=5,strides=1,padding='same',activation='relu',input_shape = (256,256,1)))
      6 
model.add(MaxPool2D(pool_size=5,padding='same'))
      7 

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

TypeError: The added layer must be an instance of class Layer. Found: 
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x00000162545A64A8>

任何人都可以帮助我确定问题所在。 这是代码段:

from tensorflow.keras.layers import Conv2D, MaxPooling2D
model = Sequential()
model.add(Conv2D(filters=32,kernel_size=5,strides=1,padding='same',activation='relu',input_shape = (256,256,1)))

model.add(MaxPool2D(pool_size=5,padding='same')
model.add(Conv2D(filters=50,kernel_size=5,strides=1,padding='same',activation='relu'))
model.add(MaxPool2D(pool_size=5,padding='same'))
model.add(Conv2D(filters=80,kernel_size=5,strides=1,padding='same',activation='relu'))
model.add(MaxPool2D(pool_size=5,padding='same'))

model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512,activation='relu'))
model.add(Dropout(rate=0.5))
model.add(Dense(2,activation='softmax'))
optimizer = Adam(lr=le-3)

model.compile(loss='categorical_crossentropy',
          optimizer=optimizer,
          metrics=['accuracy']
          )

model.fit(x=tr_img_data, y=tr_lbl_data,
      batch_size=6,
      epochs=8)
model.summary()

1 个答案:

答案 0 :(得分:0)

我尝试过,以下对我来说很好:

from tensorflow.python.keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.optimizers import Adam

model = Sequential()

model.add(Conv2D(filters=32,kernel_size=5,strides=1,padding='same',activation='relu', input_shape = (256,256,1)))

model.add(MaxPooling2D(pool_size=5,padding='same'))
model.add(Conv2D(filters=50,kernel_size=5,strides=1,padding='same',activation='relu'))
model.add(MaxPooling2D(pool_size=5,padding='same'))
model.add(Conv2D(filters=80,kernel_size=5,strides=1,padding='same',activation='relu'))
model.add(MaxPooling2D(pool_size=5,padding='same'))

model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512,activation='relu'))
model.add(Dropout(rate=0.5))
model.add(Dense(2,activation='softmax'))
optimizer = Adam(lr=1e-3)

model.compile(loss='categorical_crossentropy',
          optimizer=optimizer,
          metrics=['accuracy']
          )

model.summary()

我唯一更改的是:将MaxPool2D更改为MaxPooling2D,并且在其中一层之后还缺少)。编辑:添加了完整的代码,除.fit之外是为了完整性。