将上采样层添加到模型会产生错误

时间:2018-03-20 13:46:13

标签: python-3.x tensorflow deep-learning keras

我是Keras的新手,我正在使用它来修改一些现有的模型,在背景中使用tensorflow gpu,通过将UpSampling2D图层添加到特定大小。我知道Upsampling2D给出了最接近的整数值的输出,我试图使用这个属性。

我能够获得所需的大小但得到一个AttributeError。

class myUnet(object):

    def __init__(self, img_rows = 200, img_cols = 200):

        self.img_rows = img_rows
        self.img_cols = img_cols

    def get_unet(self):
        inputs = Input((self.img_rows, self.img_cols,1))

        conv1 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(inputs)
        conv1 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv1)
        pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

        conv2 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool1)
        conv2 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv2)
        pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)

        conv3 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool2)
        conv3 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv3)
        pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)

        conv4 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool3)
        conv4 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv4)
        drop4 = Dropout(0.5)(conv4)
        pool4 = MaxPooling2D(pool_size=(2, 2))(drop4)

        conv5 = Conv2D(1024, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool4)
        conv5 = Conv2D(1024, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv5)
        drop5 = Dropout(0.5)(conv5)

        up6 = Conv2D(512, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(drop5))
        crop6=drop4[:,0:24,0:24,:] ### Line Added by Me      
        merge6 = merge([crop6,up6], mode = 'concat', concat_axis = 3)
        conv6 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge6)

        up7 = Conv2D(256, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv6))
        conv6 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv6)
#### Lines Added by Me######
       resize7 = UpSampling2D(size=(conv3.get_shape()[1:3][0].value/up7.get_shape()[1:3][0].value, conv3.get_shape()[1:3][1].value/up7.get_shape()[1:3][1].value))(up7)
       merge7 = merge([conv3,resize7], mode = 'concat', concat_axis = 3)
#####END:Lines Added by Me##### 

       conv7 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge7)
       conv7 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv7)

       up8 = Conv2D(128, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv7))
       merge8 = merge([conv2,up8], mode = 'concat', concat_axis = 3)
       conv8 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge8)
       conv8 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv8)

       up9 = Conv2D(64, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv8))
       merge9 = merge([conv1,up9], mode = 'concat', concat_axis = 3)
       conv9 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge9)
       conv9 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv9)
       conv9 = Conv2D(2, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv9)

       conv10 = Conv2D(1, 1, activation = 'sigmoid')(conv9)

       model = Model(input = inputs, output = conv10)

       model.compile(optimizer = Adam(lr = 1e-6), loss = 'binary_crossentropy', metrics = ['accuracy'])

       return model

但是一旦我添加了这些行,我就会得到一个AttributeError。

 AttributeError: 'Tensor' object has no attribute '_keras_history'

详细的调用图是:

Traceback (most recent call last):
File "unet.py", line 209, in <module>
myunet.train()
File "unet.py", line 166, in train
model = self.get_unet()
File "unet.py", line 154, in get_unet
model = Model(input = inputs, output = conv10)
File "/data/unet/unet_env/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/data/unet/unet_env/lib/python3.6/site-packages/keras/engine/topology.py", line 1732, in __init__
build_map_of_graph(x, finished_nodes, nodes_in_progress)
File "/data/unet/unet_env/lib/python3.6/site-packages/keras/engine/topology.py", line 1722, in build_map_of_graph
layer, node_index, tensor_index)
File "/data/unet/unet_env/lib/python3.6/site-packages/keras/engine/topology.py", line 1722, in build_map_of_graph
layer, node_index, tensor_index)
File "/data/unet/unet_env/lib/python3.6/site-packages/keras/engine/topology.py", line 1722, in build_map_of_graph
layer, node_index, tensor_index)
[Previous line repeated 17 more times]
File "/data/unet/unet_env/lib/python3.6/site-packages/keras/engine/topology.py", line 1693, in build_map_of_graph
layer, node_index, tensor_index = tensor._keras_history
AttributeError: 'Tensor' object has no attribute '_keras_history'

0 个答案:

没有答案