2017-07-14 16 views
1

我的模型是U网实现 -只能使用求和模式合并相同输出形状的图层。分层形状

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 

我收到错误如下 -

Traceback (most recent call last): File "/home/zaverichintan/Chintan/PycharmProjects/CNN_wbc_identification/train.py", line 60, in model = mo.get_unet(num_color_component, filter_size); File "/home/zaverichintan/Chintan/PycharmProjects/CNN_wbc_identification/models.py", line 63, in get_unet up7 = merge([UpSampling2D(size=(2, 2))(conv6), conv3], mode='concat', concat_axis=1) File "/home/zaverichintan/anaconda2/lib/python2.7/site-packages/keras/legacy/layers.py", line 456, in merge name=name) File "/home/zaverichintan/anaconda2/lib/python2.7/site-packages/keras/legacy/layers.py", line 107, in init node_indices, tensor_indices) File "/home/zaverichintan/anaconda2/lib/python2.7/site-packages/keras/legacy/layers.py", line 187, in _arguments_validation 'Layer shapes: %s' % (input_shapes)) ValueError: "concat" mode can only merge layers with matching output shapes except for the concat axis. Layer shapes: [(None, 0, 16, 256), (None, 0, 16, 128)]

改变的毗连轴3,那么我得到这个 -

File "/home/zaverichintan/Chintan/PycharmProjects/CNN_wbc_identification/train.py", line 60, in model = mo.get_unet(num_color_component, filter_size); File "/home/zaverichintan/Chintan/PycharmProjects/CNN_wbc_identification/models.py", line 71, in get_unet up8 = keras.layers.merge([UpSampling2D(size=(2, 2))(conv7), conv2], mode='concat', concat_axis=1) File "/home/zaverichintan/anaconda2/lib/python2.7/site-packages/keras/legacy/layers.py", line 456, in merge name=name) File "/home/zaverichintan/anaconda2/lib/python2.7/site-packages/keras/legacy/layers.py", line 107, in init node_indices, tensor_indices) File "/home/zaverichintan/anaconda2/lib/python2.7/site-packages/keras/legacy/layers.py", line 187, in _arguments_validation 'Layer shapes: %s' % (input_shapes)) ValueError: "concat" mode can only merge layers with matching output shapes except for the concat axis. Layer shapes: [(None, 0, 32, 128), (None, 1, 32, 64)]

+0

您可能需要你的'conat_axis'更改为具有不同尺寸的轴,或者改变您的不同轴的尺寸相等。 – KDecker

+0

conat_axis会是? –

+0

对不起,这是一个错字'concat_axis'。 – KDecker

回答

0

这很简单:

ValueError: "concat" mode can only merge layers with matching output shapes except for the concat axis. Layer shapes: [(None, 0, 16, 256), (None, 0, 16, 128)]

您有:

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

他们明确地说,形状应该是除了CONCAT相同

其中形状不同的是第三个维度的尺寸(一个是256,另一个是128)。所以,你应该设置CONCAT轴3不1。如:

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

我希望这有助于:)