只能使用和模式合并具有相同输出形状的图层。图层形状

时间:2017-07-14 17:38:56

标签: python keras

我的模型是U-Net实现 -

from keras.layers import Input, merge, Convolution2D, MaxPooling2D, 

UpSampling2D
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint, LearningRateScheduler
from keras import backend as K
from keras.models import Model

def seg_score(y_true, y_pred):
    smooth = 1.0
    y_true_f = K.flatten(y_true)
    y_pred_f = K.flatten(y_pred)
    intersection = K.sum(y_true_f * y_pred_f)
    true_sum = K.sum(y_true_f); pred_sum = K.sum(y_pred_f)
    if(true_sum > pred_sum):
        max_sum = true_sum
    else:
        max_sum = pred_sum
    return (intersection + smooth) / (max_sum + smooth)

def seg_score_loss(y_true, y_pred):
    return -seg_score(y_true, y_pred)

def dice_coef(y_true, y_pred):
    smooth = 1.
    y_true_f = K.flatten(y_true)
    y_pred_f = K.flatten(y_pred)
    intersection = K.sum(y_true_f * y_pred_f)
    return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)

def dice_coef_loss(y_true, y_pred):
    return -dice_coef(y_true, y_pred)


def get_unet(num_color_component, dimension):

    img_rows = dimension; img_cols = dimension;
    inputs = Input((num_color_component, img_rows, img_cols))
    conv1 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(inputs)
    conv1 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

    conv2 = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(pool1)
    conv2 = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(conv2)
    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)

    conv3 = Convolution2D(128, 3, 3, activation='relu', border_mode='same')(pool2)
    conv3 = Convolution2D(128, 3, 3, activation='relu', border_mode='same')(conv3)
    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)

    conv4 = Convolution2D(256, 3, 3, activation='relu', border_mode='same')(pool3)
    conv4 = Convolution2D(256, 3, 3, activation='relu', border_mode='same')(conv4)
    pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)

    conv5 = Convolution2D(512, 3, 3, activation='relu', border_mode='same')(pool4)
    conv5 = Convolution2D(512, 3, 3, activation='relu', border_mode='same')(conv5)

    up6 = merge([UpSampling2D(size=(2, 2))(conv5), conv4], mode='concat', concat_axis=1)

    conv6 = Convolution2D(256, 3, 3, activation='relu', border_mode='same')(up6)
    conv6 = Convolution2D(256, 3, 3, activation='relu', border_mode='same')(conv6)

    up7 = merge([UpSampling2D(size=(2, 2))(conv6), conv3], mode='concat', concat_axis=1)
    conv7 = Convolution2D(128, 3, 3, activation='relu', border_mode='same')(up7)
    conv7 = Convolution2D(128, 3, 3, activation='relu', border_mode='same')(conv7)

    up8 = merge([UpSampling2D(size=(2, 2))(conv7), conv2], mode='concat', concat_axis=1)
    conv8 = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(up8)
    conv8 = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(conv8)

    up9 = merge([UpSampling2D(size=(2, 2))(conv8), conv1], mode='concat', concat_axis=1)
    conv9 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(up9)
    conv9 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(conv9)

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

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

    #model.compile(optimizer=Adam(lr=1e-5), loss=seg_score_loss, metrics=[seg_score])
    model.compile(optimizer=Adam(lr=1e-5), loss=dice_coef_loss, metrics=[dice_coef])

    return model

我收到如下错误 -

  

追踪(最近一次通话):     文件" /home/zaverichintan/Chintan/PycharmProjects/CNN_wbc_identification/train.py" ;,第60行,在       model = mo.get_unet(num_color_component,filter_size);     文件" /home/zaverichintan/Chintan/PycharmProjects/CNN_wbc_identification/models.py" ;,第63行,在get_unet中       up7 = merge([UpSampling2D(size =(2,2))(conv6),conv3],mode =' concat',concat_axis = 1)     文件" /home/zaverichintan/anaconda2/lib/python2.7/site-packages/keras/legacy/layers.py" ;,第456行,合并       名称=名称)     文件" /home/zaverichintan/anaconda2/lib/python2.7/site-packages/keras/legacy/layers.py" ;,第107行, init       node_indices,tensor_indices)     文件" /home/zaverichintan/anaconda2/lib/python2.7/site-packages/keras/legacy/layers.py" ;,第187行,在_arguments_validation中       '图层形状:%s' %(input_shapes))   ValueError:" concat"模式只能合并具有匹配输出形状的图层(concat轴除外)。图层形状:[(无,0,16,256),(无,0,16,128)]

将Concat轴更改为3然后我得到了这个 -

  

文件" /home/zaverichintan/Chintan/PycharmProjects/CNN_wbc_identification/train.py" ;,第60行,在       model = mo.get_unet(num_color_component,filter_size);     文件" /home/zaverichintan/Chintan/PycharmProjects/CNN_wbc_identification/models.py",第71行,在get_unet中       up8 = keras.layers.merge([UpSampling2D(size =(2,2))(conv7),conv2],mode =' concat',concat_axis = 1)     文件" /home/zaverichintan/anaconda2/lib/python2.7/site-packages/keras/legacy/layers.py" ;,第456行,合并       名称=名称)     文件" /home/zaverichintan/anaconda2/lib/python2.7/site-packages/keras/legacy/layers.py" ;,第107行, init       node_indices,tensor_indices)     文件" /home/zaverichintan/anaconda2/lib/python2.7/site-packages/keras/legacy/layers.py" ;,第187行,在_arguments_validation中       '图层形状:%s' %(input_shapes))   ValueError:" concat"模式只能合并具有匹配输出形状的图层(concat轴除外)。图层形状:[(无,0,32,128),(无,1,32,64)]

2 个答案:

答案 0 :(得分:0)

这很简单:

  

ValueError:“concat”模式只能合并具有匹配输出形状的图层,但concat轴除外。图层形状:[(无,0,16,256),(无,0,16,128)]

你有:

up6 = merge([UpSampling2D(size=(2, 2))(conv5), conv4], mode='concat', concat_axis=1)

他们明确地说形状应该是相同的 除了连续轴

形状不同的尺寸是第3维(一个是256,另一个是128)。所以你应该将concat轴设置为3而不是1.如:

up6 = merge([UpSampling2D(size=(2, 2))(conv5), conv4], mode='concat', concat_axis=3)

我希望这会有所帮助:)

答案 1 :(得分:0)

您必须按此处https://keras.io/backend/设置image_data_format": "channels_first"或将输入更改为

 inputs = Input((img_rows, img_cols, num_color_component))

然后concat_axis必须与数据格式对应。

以下是如何在Keras中实施U-net的示例:https://github.com/jocicmarko/ultrasound-nerve-segmentation/blob/master/train.py#L34