Example #1
0
def get_unet():
    with tf.device('/device:GPU:0'):
        inputs = Input((192, 192, 3))
    
        conv1 = initial_conv_block(inputs, weight_decay=5e-4)
        conv1 = bottleneck_block(conv1, filters=32, cardinality=32, strides=1, weight_decay=5e-4)
        pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
    
        conv2 = bottleneck_block(pool1, filters=32, cardinality=32, strides=1, weight_decay=5e-4)
        conv2 = bottleneck_block(conv2, filters=64, cardinality=32, strides=1, weight_decay=5e-4)
        pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
    
        conv3 = bottleneck_block(pool2, filters=64, cardinality=32, strides=1, weight_decay=5e-4)
        conv3 = bottleneck_block(conv3, filters=128, cardinality=32, strides=1, weight_decay=5e-4)
        pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
    
        conv4 = bottleneck_block(pool3, filters=128, cardinality=32, strides=1, weight_decay=5e-4)
        conv4 = bottleneck_block(conv4, filters=256, cardinality=32, strides=1, weight_decay=5e-4)
        drop4 = Dropout(0.2)(conv4)
        pool4 = MaxPooling2D(pool_size=(2, 2))(drop4)
    
        conv5 = bottleneck_block(pool4, filters=256, cardinality=32, strides=1, weight_decay=5e-4)
        conv5 = bottleneck_block(conv5, filters=256, cardinality=32, strides=1, weight_decay=5e-4)
        drop5 = Dropout(0.2)(conv5)
    with tf.device('/device:GPU:1'):
        up6 = Conv2D(128, (3,3), activation='relu', padding='same', kernel_initializer='he_normal')(UpSampling2D(size = (2,2))(drop5))
        merge6 = keras.layers.Concatenate()([drop4, up6])
        conv6 = bottleneck_block(merge6, filters=128, cardinality=32, strides=1, weight_decay=5e-4)
        conv6 = bottleneck_block(conv6, filters=128, cardinality=32, strides=1, weight_decay=5e-4)
    
        up7 = Conv2D(64, (3,3), activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv6))
        merge7 = keras.layers.Concatenate()([conv3, up7])
        conv7 = bottleneck_block(merge7, filters=64, cardinality=32, strides=1, weight_decay=5e-4)
        conv7 = bottleneck_block(conv7, filters=64, cardinality=32, strides=1, weight_decay=5e-4)
    
        up8 = Conv2D(32, (3,3), activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv7))
        merge8 = keras.layers.Concatenate()([conv2, up8])
        conv8 = bottleneck_block(merge8, filters=32, cardinality=32, strides=1, weight_decay=5e-4)
        conv8 = bottleneck_block(conv8, filters=32, cardinality=32, strides=1, weight_decay=5e-4)
    
        up9 = Conv2D(16, (3,3), activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv8))
        merge9 = keras.layers.Concatenate()([conv1, up9])
        conv9 = Residual(48, 16, merge9)
        conv9 = Residual(16, 4, conv9)
        conv9 = Residual(4, 1, conv9)
        conv10 = Conv2D(1, 1, activation = 'sigmoid')(conv9)
    
        model = Model(inputs, conv10)

        model.compile(loss='binary_crossentropy', optimizer=SGD(lr=0.03, momentum=0.9, nesterov=True), metrics=[dice_coef,jaccard_coef])

    return model
    up9 = Conv2D(16,
                 2,
                 activation='relu',
                 padding='same',
                 kernel_initializer='he_normal',
                 name='UpConv4')(UpSampling2D(size=(2, 2),
                                              name='Up4')(conv8_1))
    merge9 = keras.layers.Concatenate(name='Concat4')([conv1, up9])
    conv9 = Residual17(48, 16, merge9)
    conv10 = Residual18(16, 2, conv9)
    conv10 = Residual19(2, 1, conv10)
    conv11 = Conv2D(1, 1, activation='sigmoid', name='Output')(conv10)

with tf.device('/device:GPU:3'):
    init = initial_conv_block(input, weight_decay=5e-4)

    #x1 = ResidualR(32, 64, init)    #192x192x64
    #x1 = ResidualR(64, 64, x1)
    #x1 = ResidualR(64, 64, x1)    #192x192x64
    x1 = Conv2D(64, (3, 3), padding='same',
                kernel_initializer='he_normal')(init)
    x1 = BatchNormalization()(x1)
    x1 = layers.LeakyReLU()(x1)
    x1concat = keras.layers.Concatenate()([x1, conv9])  #192x192x80
    x1se = squeeze_excite_block(x1concat)
    x1conv1 = SeparableConv2D(80, (1, 1),
                              padding='same',
                              kernel_initializer='he_normal')(x1se)
    x1conv1 = layers.LeakyReLU()(x1conv1)
    x1conv2 = Conv2D(64, (1, 1),
def focusnet():
    
    input = Input((192, 256, 3))
           
    conv1 = initial_conv_block(input) #512
    pool1 = _residual_block(basic_block, filters=32, repetitions=1, is_first_layer=False)(conv1) #256
    
    conv2 = _residual_block(basic_block, filters=64, repetitions=1, is_first_layer=True)(pool1) #256
    pool2 = _residual_block(basic_block, filters=64, repetitions=1, is_first_layer=False)(conv2) #128
    
    conv3 = _residual_block(basic_block, filters=128, repetitions=1, is_first_layer=True)(pool2) #128
    pool3 = _residual_block(basic_block, filters=128, repetitions=1, is_first_layer=False)(conv3) #64

    conv4 = _residual_block(basic_block, filters=256, repetitions=1, is_first_layer=True)(pool3) #64
    drop4 = Dropout(0.2)(conv4)
    
    up5 = Conv2D(256, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(drop4)) #128
    merge5 = keras.layers.Concatenate()([conv3,up5]) 
    conv5 = _residual_block(basic_block, filters=256, repetitions=1, is_first_layer=True)(merge5) #128
    
    up6 = Conv2D(128, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv5)) #256
    merge6 = keras.layers.Concatenate()([conv2,up6])
    conv6 = _residual_block(basic_block, filters=128, repetitions=1, is_first_layer=True)(merge6) #256
    
    up7 = Conv2D(64, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv6)) #512
    merge7 = keras.layers.Concatenate()([conv1,up7])
    conv7 = _residual_block(basic_block, filters=64, repetitions=1, is_first_layer=True)(merge7) #512
    

    conv1r = _conv_bn_relu(filters=64, kernel_size=(7, 7), strides=(1, 1))(input) #512
  
    block1 = _residual_block(basic_block, filters=64, repetitions=1, is_first_layer=True)(conv1r) #512
    se1 = squeeze_excite_block(block1)
    gate1 = Activation('sigmoid')(conv7)
    block1concat = keras.layers.Multiply()([se1, gate1]) #512
    block1se = squeeze_excite_block(block1concat)
    block1b = _residual_block(basic_block, filters=64, repetitions=1, is_first_layer=False)(block1se) #256
    
    block2 = _residual_block(basic_block, filters=128, repetitions=1, is_first_layer=True)(block1b) #256
    se2 = squeeze_excite_block(block2)
    gate2 = Activation('sigmoid')(conv6)
    block2concat = keras.layers.Multiply()([se2, gate2]) #256
    block2se = squeeze_excite_block(block2concat)
    block2b = _residual_block(basic_block, filters=128, repetitions=1, is_first_layer=False)(block2se) #128

    block3 = _residual_block(basic_block, filters=256, repetitions=1, is_first_layer=True)(block2b) #128
    se3 = squeeze_excite_block(block3)
    gate3 = Activation('sigmoid')(conv5)
    block3concat = keras.layers.Multiply()([se3, gate3]) #128
    block3se = squeeze_excite_block(block3concat)
    block3b = _residual_block(basic_block, filters=256, repetitions=1, is_first_layer=False)(block3se) # 64

    block4 = _residual_block(basic_block, filters=512, repetitions=1, is_first_layer=True)(block3b) #64
    block4se = squeeze_excite_block(block4)
    block4b = _residual_block(basic_block, filters=512, repetitions=1, is_first_layer=False)(block4se) #32

    up2_5 = Conv2D(256, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(block4b)) #64
    merge2_5 = keras.layers.Concatenate()([block3b,up2_5])
    conv2_5 = _residual_block(basic_block, filters=256, repetitions=1, is_first_layer=True)(merge2_5) #64
    out1 = Conv2D(32, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (8,8))(conv2_5))
    out1 = Conv2D(1, 1, activation = 'sigmoid', padding = 'same', kernel_initializer = 'he_normal')(out1)

    up2_6 = Conv2D(128, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv2_5)) #128
    merge2_6 = keras.layers.Concatenate()([block2b,up2_6])
    conv2_6 = _residual_block(basic_block, filters=128, repetitions=1, is_first_layer=True)(merge2_6) #128
    out2 = Conv2D(32, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (4,4))(conv2_6))
    out2 = Conv2D(1, 1, activation = 'sigmoid', padding = 'same', kernel_initializer = 'he_normal')(out2)
    
    up2_7 = Conv2D(64, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv2_6)) #256
    merge2_7 = keras.layers.Concatenate()([block1b,up2_7])
    conv2_7 = _residual_block(basic_block, filters=64, repetitions=1, is_first_layer=True)(merge2_7) #256
    out3 = Conv2D(32, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv2_7))
    out3 = Conv2D(1, 1, activation = 'sigmoid', padding = 'same', kernel_initializer = 'he_normal')(out3)
         
    up2_8 = Conv2D(32, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv2_7)) #512
    merge2_8 = keras.layers.Concatenate()([conv1r,up2_8])
    conv2_8 = _residual_block(basic_block, filters=32, repetitions=1, is_first_layer=True)(merge2_8) #512
    conv2_8 = _residual_block(basic_block, filters=16, repetitions=1, is_first_layer=True)(conv2_8)
    conv2_8 = _residual_block(basic_block, filters=4, repetitions=1, is_first_layer=True)(conv2_8)
         
    out4 = Conv2D(1, 1, activation = 'sigmoid', padding = 'same', kernel_initializer = 'he_normal')(conv2_8)

    out_concat = keras.layers.Concatenate()([out1, out2, out3, out4])

    out_concat = Conv2D(32, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(out_concat)

    out = Conv2D(1, 1, activation = 'sigmoid', padding = 'same', kernel_initializer = 'he_normal')(out_concat)
         
    model = Model(inputs=input, outputs=out)

    model.compile(optimizer = SGD(lr=0.0005, momentum=0.9, nesterov=True), loss = losses.focal_tversky, metrics = [losses.tp, losses.tn, losses.dsc, losses.jacard_coef, 'accuracy'])
         
    model.summary()

    return model
Example #4
0
             padding='same',
             kernel_initializer='he_normal')(UpSampling2D(size=(2, 2))(upres2))
merge8 = keras.layers.Add()([res1, up8])
upres1 = seperableConv_bottleneck_block_with_se(merge8,
                                                filters=32,
                                                cardinality=32,
                                                strides=1,
                                                weight_decay=5e-4)
upres1 = seperableConv_bottleneck_block_with_se(upres1,
                                                filters=1,
                                                cardinality=1,
                                                strides=1,
                                                weight_decay=5e-4)
outputConvAutoEncoder = Conv2D(1, (1, 1), activation='sigmoid')(upres1)
#-------------------------------------------------------------------------------------------------------------------------------------
x = initial_conv_block(input_image, weight_decay=5e-4)

x = bottleneck_block_with_se(x,
                             filters=96,
                             cardinality=32,
                             strides=1,
                             weight_decay=5e-4)  # 196x196
x = bottleneck_block_with_se(x,
                             filters=96,
                             cardinality=32,
                             strides=1,
                             weight_decay=5e-4)
x = bottleneck_block_with_se(x,
                             filters=96,
                             cardinality=32,
                             strides=1,
Example #5
0
def get_unet():

    input = Input((256, 256, 3))

    conv1 = initial_conv_block(input)  #512
    pool1 = _residual_block(basic_block,
                            filters=32,
                            repetitions=1,
                            is_first_layer=False)(conv1)  #256

    conv2 = _residual_block(basic_block,
                            filters=64,
                            repetitions=1,
                            is_first_layer=True)(pool1)  #256
    pool2 = _residual_block(basic_block,
                            filters=64,
                            repetitions=1,
                            is_first_layer=False)(conv2)  #128

    conv3 = _residual_block(basic_block,
                            filters=128,
                            repetitions=1,
                            is_first_layer=True)(pool2)  #128
    pool3 = _residual_block(basic_block,
                            filters=128,
                            repetitions=1,
                            is_first_layer=False)(conv3)  #64

    conv4 = _residual_block(basic_block,
                            filters=256,
                            repetitions=1,
                            is_first_layer=True)(pool3)  #64
    drop4 = Dropout(0.2)(conv4)

    up5 = Conv2D(256,
                 2,
                 activation='relu',
                 padding='same',
                 kernel_initializer='he_normal')(
                     UpSampling2D(size=(2, 2))(drop4))  #128
    merge5 = keras.layers.Concatenate()([conv3, up5])
    conv5 = _residual_block(basic_block,
                            filters=256,
                            repetitions=1,
                            is_first_layer=True)(merge5)  #128

    up6 = Conv2D(128,
                 2,
                 activation='relu',
                 padding='same',
                 kernel_initializer='he_normal')(
                     UpSampling2D(size=(2, 2))(conv5))  #256
    merge6 = keras.layers.Concatenate()([conv2, up6])
    conv6 = _residual_block(basic_block,
                            filters=128,
                            repetitions=1,
                            is_first_layer=True)(merge6)  #256

    up7 = Conv2D(64,
                 2,
                 activation='relu',
                 padding='same',
                 kernel_initializer='he_normal')(
                     UpSampling2D(size=(2, 2))(conv6))  #512
    merge7 = keras.layers.Concatenate()([conv1, up7])
    conv7 = _residual_block(basic_block,
                            filters=64,
                            repetitions=1,
                            is_first_layer=True)(merge7)  #512

    conv1r = _conv_bn_relu(filters=64, kernel_size=(7, 7),
                           strides=(1, 1))(input)  #512

    block1 = _residual_block(basic_block,
                             filters=64,
                             repetitions=1,
                             is_first_layer=True)(conv1r)  #512
    se1 = squeeze_excite_block(block1)
    gate1 = Activation('sigmoid')(conv7)
    block1concat = keras.layers.Multiply()([se1, gate1])  #512
    block1se = squeeze_excite_block(block1concat)
    block1b = _residual_block(basic_block,
                              filters=64,
                              repetitions=1,
                              is_first_layer=False)(block1se)  #256

    block2 = _residual_block(basic_block,
                             filters=128,
                             repetitions=1,
                             is_first_layer=True)(block1b)  #256
    se2 = squeeze_excite_block(block2)
    gate2 = Activation('sigmoid')(conv6)
    block2concat = keras.layers.Multiply()([se2, gate2])  #256
    block2se = squeeze_excite_block(block2concat)
    block2b = _residual_block(basic_block,
                              filters=128,
                              repetitions=1,
                              is_first_layer=False)(block2se)  #128

    block3 = _residual_block(basic_block,
                             filters=256,
                             repetitions=1,
                             is_first_layer=True)(block2b)  #128
    se3 = squeeze_excite_block(block3)
    gate3 = Activation('sigmoid')(conv5)
    block3concat = keras.layers.Multiply()([se3, gate3])  #128
    block3se = squeeze_excite_block(block3concat)
    block3b = _residual_block(basic_block,
                              filters=256,
                              repetitions=1,
                              is_first_layer=False)(block3se)  # 64

    block4 = _residual_block(basic_block,
                             filters=512,
                             repetitions=1,
                             is_first_layer=True)(block3b)  #64
    block4se = squeeze_excite_block(block4)
    block4b = _residual_block(basic_block,
                              filters=512,
                              repetitions=1,
                              is_first_layer=False)(block4se)  #32

    up2_5 = Conv2D(256,
                   2,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal')(
                       UpSampling2D(size=(2, 2))(block4b))  #64
    merge2_5 = keras.layers.Concatenate()([block3b, up2_5])
    conv2_5 = _residual_block(basic_block,
                              filters=256,
                              repetitions=1,
                              is_first_layer=True)(merge2_5)  #64

    up2_6 = Conv2D(128,
                   2,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal')(
                       UpSampling2D(size=(2, 2))(conv2_5))  #128
    merge2_6 = keras.layers.Concatenate()([block2b, up2_6])
    conv2_6 = _residual_block(basic_block,
                              filters=128,
                              repetitions=1,
                              is_first_layer=True)(merge2_6)  #128

    up2_7 = Conv2D(64,
                   2,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal')(
                       UpSampling2D(size=(2, 2))(conv2_6))  #256
    merge2_7 = keras.layers.Concatenate()([block1b, up2_7])
    conv2_7 = _residual_block(basic_block,
                              filters=64,
                              repetitions=1,
                              is_first_layer=True)(merge2_7)  #256

    up2_8 = Conv2D(32,
                   2,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal')(
                       UpSampling2D(size=(2, 2))(conv2_7))  #512
    merge2_8 = keras.layers.Concatenate()([conv1r, up2_8])
    conv2_8 = _residual_block(basic_block,
                              filters=32,
                              repetitions=1,
                              is_first_layer=True)(merge2_8)  #512
    conv2_8 = _residual_block(basic_block,
                              filters=16,
                              repetitions=1,
                              is_first_layer=True)(conv2_8)
    conv2_8 = _residual_block(basic_block,
                              filters=4,
                              repetitions=1,
                              is_first_layer=True)(conv2_8)

    out = Conv2D(1,
                 1,
                 activation='sigmoid',
                 padding='same',
                 kernel_initializer='he_normal')(conv2_8)

    model = Model(inputs=input, outputs=out)

    model.compile(optimizer=Adam(lr=1e-4),
                  loss=dice_coef_loss,
                  metrics=[dice_coef, jaccard_coef, 'acc'])

    model.summary()

    return model