예제 #1
0
    def call(self, inputs, training=None, mask=None):
        # encoder
        output_01 = self.unit01(inputs)
        pool_1, mask_1 = MaxPoolingWithArgmax2D(self.pool_size)(output_01)
        output_02 = self.unit02(pool_1)
        pool_2, mask_2 = MaxPoolingWithArgmax2D(self.pool_size)(output_02)
        output_03 = self.unit03(pool_2)
        pool_3, mask_3 = MaxPoolingWithArgmax2D(self.pool_size)(output_03)
        output_04 = self.unit04(pool_3)
        pool_4, mask_4 = MaxPoolingWithArgmax2D(self.pool_size)(output_04)
        output_05 = self.unit05(pool_4)
        pool_5, mask_5 = MaxPoolingWithArgmax2D(self.pool_size)(output_05)
        print("Build enceder done..")

        # decoder
        unpool_1 = MaxUnpooling2D(self.pool_size)([pool_5, mask_5])
        output_06 = self.unit06(unpool_1)
        unpool_2 = MaxUnpooling2D(self.pool_size)([output_06, mask_4])
        output_07 = self.unit07(unpool_2)
        unpool_3 = MaxUnpooling2D(self.pool_size)([output_07, mask_3])
        output_08 = self.unit08(unpool_3)
        unpool_4 = MaxUnpooling2D(self.pool_size)([output_08, mask_2])
        output_09 = self.unit09(unpool_4)
        unpool_5 = MaxUnpooling2D(self.pool_size)([output_09, mask_1])
        output_10 = self.unit10(unpool_5)
        output_11 = Reshape(
            (256 * 256, self.n_labels),
            input_shape=(256, 256, self.n_labels),
        )(output_10)

        outputs = Activation(self.output_mode)(output_11)
        print("Build decoder done..")
        return outputs
예제 #2
0
def segnet2(subseq,
            INPUT_CHANNELS,
            filters,
            n_labels,
            kernel=3,
            pool_size=(1, 2),
            output_mode="softmax"):
    # encoder
    # inputs = Input(shape=input_shape)
    inputs = Input((1, subseq, INPUT_CHANNELS))

    conv_1 = Convolution2D(filters, (1, kernel), padding="same")(inputs)
    conv_1 = BatchNormalization()(conv_1)
    conv_1 = Activation("relu")(conv_1)
    conv_2 = Convolution2D(filters, (1, kernel), padding="same")(conv_1)
    conv_2 = BatchNormalization()(conv_2)
    conv_2 = Activation("relu")(conv_2)

    pool_1, mask_1 = MaxPoolingWithArgmax2D(pool_size)(conv_2)

    conv_3 = Convolution2D(2 * filters, (1, kernel), padding="same")(pool_1)
    conv_3 = BatchNormalization()(conv_3)
    conv_3 = Activation("relu")(conv_3)
    conv_4 = Convolution2D(2 * filters, (1, kernel), padding="same")(conv_3)
    conv_4 = BatchNormalization()(conv_4)
    conv_4 = Activation("relu")(conv_4)

    pool_2, mask_2 = MaxPoolingWithArgmax2D(pool_size)(conv_4)

    print("Build enceder done..")

    # decoder

    unpool_4 = MaxUnpooling2D(pool_size)([pool_2, mask_2])

    conv_23 = Convolution2D(2 * filters, (1, kernel), padding="same")(unpool_4)
    conv_23 = BatchNormalization()(conv_23)
    conv_23 = Activation("relu")(conv_23)
    conv_24 = Convolution2D(filters, (1, kernel), padding="same")(conv_23)
    conv_24 = BatchNormalization()(conv_24)
    conv_24 = Activation("relu")(conv_24)

    unpool_5 = MaxUnpooling2D(pool_size)([conv_24, mask_1])

    conv_25 = Convolution2D(filters, (1, kernel), padding="same")(unpool_5)
    conv_25 = BatchNormalization()(conv_25)
    conv_25 = Activation("relu")(conv_25)

    conv_26 = Convolution2D(n_labels, (1, 1), padding="valid")(conv_25)
    conv_26 = BatchNormalization()(conv_26)

    outputs = Activation(output_mode)(conv_26)
    print("Build decoder done..")

    model = Model(inputs=inputs, outputs=outputs, name="SegNet")

    return model
예제 #3
0
    def build_encoder(self):

        if self.encoder_built:
            print("Encoder already built.")
            return

        print("Building encoder")

        self.inputs = Input(shape=self.input_shape)
        conv_1 = Convolution2D(64, self.kernel, padding="same")(self.inputs)
        conv_1 = BatchNormalization()(conv_1)
        self.conv_1 = Activation("relu")(conv_1)

        self.pool_1, self.mask_1 = MaxPoolingWithArgmax2D(self.pool_size)(
            self.conv_1)

        conv_2 = Convolution2D(128, self.kernel, padding="same")(self.pool_1)
        conv_2 = BatchNormalization()(conv_2)
        self.conv_2 = Activation("relu")(conv_2)

        self.pool_2, self.mask_2 = MaxPoolingWithArgmax2D(self.pool_size)(
            self.conv_2)

        conv_3 = Convolution2D(256, self.kernel, padding="same")(self.pool_2)
        conv_3 = BatchNormalization()(conv_3)
        self.conv_3 = Activation("relu")(conv_3)

        self.pool_3, self.mask_3 = MaxPoolingWithArgmax2D(self.pool_size)(
            self.conv_3)

        conv_4 = Convolution2D(512, self.kernel, padding="same")(self.pool_3)
        conv_4 = BatchNormalization()(conv_4)
        self.conv_4 = Activation("relu")(conv_4)
        conv_5 = Convolution2D(512, self.kernel, padding="same")(self.conv_4)
        conv_5 = BatchNormalization()(conv_5)
        self.conv_5 = Activation("relu")(conv_5)

        self.pool_4, self.mask_4 = MaxPoolingWithArgmax2D(self.pool_size)(
            self.conv_5)
        print("Done")

        self.encoder_built = True
예제 #4
0
파일: eLSTM.py 프로젝트: Sandy4321/ReCD
 def initial_block(self,
                   inputT,
                   filters=13,
                   size=(3, 3),
                   strides=(2, 2),
                   reuse=None):
     with tf.variable_scope('initial_block', reuse=reuse):
         conv = tf.keras.layers.Conv2D(filters,
                                       size,
                                       padding='same',
                                       strides=strides)(inputT)
         max_pool, indices = MaxPoolingWithArgmax2D()(inputT)
         merged = tf.keras.layers.concatenate([conv, max_pool], axis=3)
         return merged, indices
예제 #5
0
def segnet(input_height, input_width, pool_size=(2, 2), n_classes=3):

    img_input = tf.keras.layers.Input(shape=(input_height, input_width, 3))

    # -------------------------- VGG Encoder 1 --------------------------
    c1 = Conv2D(64,
                3,
                padding='same',
                activation="selu",
                kernel_initializer='he_normal')(img_input)
    c1 = Conv2D(64,
                3,
                padding='same',
                activation="selu",
                kernel_initializer='he_normal')(c1)
    p1, p1_ind = MaxPoolingWithArgmax2D(pool_size, dtype="float32")(c1)
    # -------------------------- VGG Encoder 2 --------------------------
    c2 = Conv2D(128,
                3,
                padding='same',
                activation="selu",
                kernel_initializer='he_normal')(p1)
    c2 = Conv2D(128,
                3,
                padding='same',
                activation="selu",
                kernel_initializer='he_normal')(c2)
    p2, p2_ind = MaxPoolingWithArgmax2D(pool_size, dtype="float32")(c2)
    # -------------------------- VGG Encoder 3 --------------------------
    c3 = Conv2D(256,
                3,
                padding='same',
                activation="selu",
                kernel_initializer='he_normal')(p2)
    c3 = Conv2D(256,
                3,
                padding='same',
                activation="selu",
                kernel_initializer='he_normal')(c3)
    c3 = Conv2D(256,
                3,
                padding='same',
                activation="selu",
                kernel_initializer='he_normal')(c3)
    p3, p3_ind = MaxPoolingWithArgmax2D(pool_size, dtype="float32")(c3)
    # -------------------------- VGG Encoder 4 --------------------------
    c4 = Conv2D(512,
                3,
                padding='same',
                activation="selu",
                kernel_initializer='he_normal')(p3)
    c4 = Conv2D(512,
                3,
                padding='same',
                activation="selu",
                kernel_initializer='he_normal')(c4)
    c4 = Conv2D(512,
                3,
                padding='same',
                activation="selu",
                kernel_initializer='he_normal')(c4)
    p4, p4_ind = MaxPoolingWithArgmax2D(pool_size, dtype="float32")(c4)

    # ---------------------- Maxpool Index Decoder 1 --------------------
    u3 = MaxUnpooling2D(pool_size, dtype="float32")([p4, p4_ind])
    c6 = Conv2D(512, 3, padding='same', kernel_initializer='he_normal')(u3)
    c6 = BatchNormalization()(c6)
    c6 = Activation("selu")(c6)
    c6 = Conv2D(512, 3, padding='same', kernel_initializer='he_normal')(c6)
    c6 = BatchNormalization()(c6)
    c6 = Activation("selu")(c6)
    c6 = Conv2D(256, 3, padding='same', kernel_initializer='he_normal')(c6)
    c6 = BatchNormalization()(c6)
    c6 = Activation("selu")(c6)
    # ---------------------- Maxpool Index Decoder 2 --------------------
    u4 = MaxUnpooling2D(pool_size, dtype="float32")([c6, p3_ind])
    c7 = Conv2D(256, 3, padding='same', kernel_initializer='he_normal')(u4)
    c7 = BatchNormalization()(c7)
    c7 = Activation("selu")(c7)
    c7 = Conv2D(256, 3, padding='same', kernel_initializer='he_normal')(c7)
    c7 = BatchNormalization()(c7)
    c7 = Activation("selu")(c7)
    c7 = Conv2D(128, 3, padding='same', kernel_initializer='he_normal')(c7)
    c7 = BatchNormalization()(c7)
    c7 = Activation("selu")(c7)
    # ---------------------- Maxpool Index Decoder 3 --------------------
    u5 = MaxUnpooling2D(pool_size, dtype="float32")([c7, p2_ind])
    c8 = Conv2D(128, 3, padding='same', kernel_initializer='he_normal')(u5)
    c8 = BatchNormalization()(c8)
    c8 = Activation("selu")(c8)
    c8 = Conv2D(64, 3, padding='same', kernel_initializer='he_normal')(c8)
    c8 = BatchNormalization()(c8)
    c8 = Activation("selu")(c8)
    # ---------------------- Maxpool Index Decoder 4 --------------------
    u6 = MaxUnpooling2D(pool_size, dtype="float32")([c8, p1_ind])
    c9 = Conv2D(64, 3, padding='same', kernel_initializer='he_normal')(u6)
    c9 = BatchNormalization()(c9)
    c9 = Activation("selu")(c9)
    c9 = Conv2D(64, 3, padding='same', kernel_initializer='he_normal')(c9)
    c9 = BatchNormalization()(c9)
    c9 = Activation("selu")(c9)
    c9 = Conv2D(n_classes, 3, padding='same')(c9)

    output = Activation("softmax", dtype='float32')(c9)

    return tf.keras.Model(inputs=img_input, outputs=output)
예제 #6
0
def segnet(subseq,
           INPUT_CHANNELS,
           filters,
           n_labels,
           kernel=3,
           pool_size=(1, 2),
           output_mode="softmax"):
    # encoder
    # inputs = Input(shape=input_shape)
    inputs = Input((1, subseq, INPUT_CHANNELS))

    conv_1 = Convolution2D(filters, (1, kernel), padding="same")(inputs)
    conv_1 = BatchNormalization()(conv_1)
    conv_1 = Activation("relu")(conv_1)
    conv_2 = Convolution2D(filters, (1, kernel), padding="same")(conv_1)
    conv_2 = BatchNormalization()(conv_2)
    conv_2 = Activation("relu")(conv_2)

    pool_1, mask_1 = MaxPoolingWithArgmax2D(pool_size)(conv_2)

    conv_3 = Convolution2D(2 * filters, (1, kernel), padding="same")(pool_1)
    conv_3 = BatchNormalization()(conv_3)
    conv_3 = Activation("relu")(conv_3)
    conv_4 = Convolution2D(2 * filters, (1, kernel), padding="same")(conv_3)
    conv_4 = BatchNormalization()(conv_4)
    conv_4 = Activation("relu")(conv_4)

    pool_2, mask_2 = MaxPoolingWithArgmax2D(pool_size)(conv_4)

    conv_5 = Convolution2D(4 * filters, (1, kernel), padding="same")(pool_2)
    conv_5 = BatchNormalization()(conv_5)
    conv_5 = Activation("relu")(conv_5)
    conv_6 = Convolution2D(4 * filters, (1, kernel), padding="same")(conv_5)
    conv_6 = BatchNormalization()(conv_6)
    conv_6 = Activation("relu")(conv_6)
    conv_7 = Convolution2D(4 * filters, (1, kernel), padding="same")(conv_6)
    conv_7 = BatchNormalization()(conv_7)
    conv_7 = Activation("relu")(conv_7)

    pool_3, mask_3 = MaxPoolingWithArgmax2D(pool_size)(conv_7)

    conv_8 = Convolution2D(8 * filters, (1, kernel), padding="same")(pool_3)
    conv_8 = BatchNormalization()(conv_8)
    conv_8 = Activation("relu")(conv_8)
    conv_9 = Convolution2D(8 * filters, (1, kernel), padding="same")(conv_8)
    conv_9 = BatchNormalization()(conv_9)
    conv_9 = Activation("relu")(conv_9)
    conv_10 = Convolution2D(8 * filters, (1, kernel), padding="same")(conv_9)
    conv_10 = BatchNormalization()(conv_10)
    conv_10 = Activation("relu")(conv_10)

    pool_4, mask_4 = MaxPoolingWithArgmax2D(pool_size)(conv_10)

    conv_11 = Convolution2D(8 * filters, (1, kernel), padding="same")(pool_4)
    conv_11 = BatchNormalization()(conv_11)
    conv_11 = Activation("relu")(conv_11)
    conv_12 = Convolution2D(8 * filters, (1, kernel), padding="same")(conv_11)
    conv_12 = BatchNormalization()(conv_12)
    conv_12 = Activation("relu")(conv_12)
    conv_13 = Convolution2D(8 * filters, (1, kernel), padding="same")(conv_12)
    conv_13 = BatchNormalization()(conv_13)
    conv_13 = Activation("relu")(conv_13)

    pool_5, mask_5 = MaxPoolingWithArgmax2D(pool_size)(conv_13)
    print("Build enceder done..")

    # decoder

    unpool_1 = MaxUnpooling2D(pool_size)([pool_5, mask_5])

    conv_14 = Convolution2D(8 * filters, (1, kernel), padding="same")(unpool_1)
    conv_14 = BatchNormalization()(conv_14)
    conv_14 = Activation("relu")(conv_14)
    conv_15 = Convolution2D(8 * filters, (1, kernel), padding="same")(conv_14)
    conv_15 = BatchNormalization()(conv_15)
    conv_15 = Activation("relu")(conv_15)
    conv_16 = Convolution2D(8 * filters, (1, kernel), padding="same")(conv_15)
    conv_16 = BatchNormalization()(conv_16)
    conv_16 = Activation("relu")(conv_16)

    unpool_2 = MaxUnpooling2D(pool_size)([conv_16, mask_4])

    conv_17 = Convolution2D(8 * filters, (1, kernel), padding="same")(unpool_2)
    conv_17 = BatchNormalization()(conv_17)
    conv_17 = Activation("relu")(conv_17)
    conv_18 = Convolution2D(8 * filters, (1, kernel), padding="same")(conv_17)
    conv_18 = BatchNormalization()(conv_18)
    conv_18 = Activation("relu")(conv_18)
    conv_19 = Convolution2D(4 * filters, (1, kernel), padding="same")(conv_18)
    conv_19 = BatchNormalization()(conv_19)
    conv_19 = Activation("relu")(conv_19)

    unpool_3 = MaxUnpooling2D(pool_size)([conv_19, mask_3])

    conv_20 = Convolution2D(4 * filters, (1, kernel), padding="same")(unpool_3)
    conv_20 = BatchNormalization()(conv_20)
    conv_20 = Activation("relu")(conv_20)
    conv_21 = Convolution2D(4 * filters, (1, kernel), padding="same")(conv_20)
    conv_21 = BatchNormalization()(conv_21)
    conv_21 = Activation("relu")(conv_21)
    conv_22 = Convolution2D(2 * filters, (1, kernel), padding="same")(conv_21)
    conv_22 = BatchNormalization()(conv_22)
    conv_22 = Activation("relu")(conv_22)

    unpool_4 = MaxUnpooling2D(pool_size)([conv_22, mask_2])

    conv_23 = Convolution2D(2 * filters, (1, kernel), padding="same")(unpool_4)
    conv_23 = BatchNormalization()(conv_23)
    conv_23 = Activation("relu")(conv_23)
    conv_24 = Convolution2D(filters, (1, kernel), padding="same")(conv_23)
    conv_24 = BatchNormalization()(conv_24)
    conv_24 = Activation("relu")(conv_24)

    unpool_5 = MaxUnpooling2D(pool_size)([conv_24, mask_1])

    conv_25 = Convolution2D(filters, (1, kernel), padding="same")(unpool_5)
    conv_25 = BatchNormalization()(conv_25)
    conv_25 = Activation("relu")(conv_25)

    conv_26 = Convolution2D(n_labels, (1, 1), padding="valid")(conv_25)
    conv_26 = BatchNormalization()(conv_26)
    # conv_26 = Reshape(
    #         (n_labels,1,subseq),
    #         input_shape=(INPUT_CHANNELS, 1, subseq))(conv_26)

    outputs = Activation(output_mode)(conv_26)
    print("Build decoder done..")

    model = Model(inputs=inputs, outputs=outputs, name="SegNet")

    return model
예제 #7
0
input_shape = (224, 224, 3)
n_labels = 10
kernel = 3
pool_size = (2, 2)

# encoder
inputs = Input(shape=input_shape)

conv_1 = Conv2D(64, (kernel, kernel), padding="same")(inputs)
conv_1 = BatchNormalization()(conv_1)
conv_1 = Activation("relu")(conv_1)
conv_2 = Conv2D(64, (kernel, kernel), padding="same")(conv_1)
conv_2 = BatchNormalization()(conv_2)
conv_2 = Activation("relu")(conv_2)

pool_1, mask_1 = MaxPoolingWithArgmax2D(pool_size)(conv_2)

conv_3 = Conv2D(128, (kernel, kernel), padding="same")(pool_1)
conv_3 = BatchNormalization()(conv_3)
conv_3 = Activation("relu")(conv_3)
conv_4 = Conv2D(128, (kernel, kernel), padding="same")(conv_3)
conv_4 = BatchNormalization()(conv_4)
conv_4 = Activation("relu")(conv_4)

pool_2, mask_2 = MaxPoolingWithArgmax2D(pool_size)(conv_4)

conv_5 = Conv2D(256, (kernel, kernel), padding="same")(pool_2)
conv_5 = BatchNormalization()(conv_5)
conv_5 = Activation("relu")(conv_5)
conv_6 = Conv2D(256, (kernel, kernel), padding="same")(conv_5)
conv_6 = BatchNormalization()(conv_6)
예제 #8
0
def segunet(
	input_shape,
	n_labels,
	kernel=3,
	pool=2,
	output_mode="softmax",
	gpus=1):

	inputs = Input(shape=input_shape)

	# Encoder
	conv_1 = Convolution2D(64, (kernel, kernel), padding="same")(inputs)
	conv_1 = BatchNormalization()(conv_1)
	conv_1 = Activation("relu")(conv_1)
	conv_2 = Convolution2D(64, (kernel, kernel), padding="same")(conv_1)
	conv_2 = BatchNormalization()(conv_2)
	conv_2 = Activation("relu")(conv_2)

	pool_1, mask_1 = MaxPoolingWithArgmax2D((pool, pool))(conv_2)

	conv_3 = Convolution2D(128, (kernel, kernel), padding="same")(pool_1)
	conv_3 = BatchNormalization()(conv_3)
	conv_3 = Activation("relu")(conv_3)
	conv_4 = Convolution2D(128, (kernel, kernel), padding="same")(conv_3)
	conv_4 = BatchNormalization()(conv_4)
	conv_4 = Activation("relu")(conv_4)

	pool_2, mask_2 = MaxPoolingWithArgmax2D((pool, pool))(conv_4)

	conv_5 = Convolution2D(256, (kernel, kernel), padding="same")(pool_2)
	conv_5 = BatchNormalization()(conv_5)
	conv_5 = Activation("relu")(conv_5)
	conv_6 = Convolution2D(256, (kernel, kernel), padding="same")(conv_5)
	conv_6 = BatchNormalization()(conv_6)
	conv_6 = Activation("relu")(conv_6)
	conv_7 = Convolution2D(256, (kernel, kernel), padding="same")(conv_6)
	conv_7 = BatchNormalization()(conv_7)
	conv_7 = Activation("relu")(conv_7)

	pool_3, mask_3 = MaxPoolingWithArgmax2D((pool, pool))(conv_7)

	conv_8 = Convolution2D(512, (kernel, kernel), padding="same")(pool_3)
	conv_8 = BatchNormalization()(conv_8)
	conv_8 = Activation("relu")(conv_8)
	conv_9 = Convolution2D(512, (kernel, kernel), padding="same")(conv_8)
	conv_9 = BatchNormalization()(conv_9)
	conv_9 = Activation("relu")(conv_9)
	conv_10 = Convolution2D(512, (kernel, kernel), padding="same")(conv_9)
	conv_10 = BatchNormalization()(conv_10)
	conv_10 = Activation("relu")(conv_10)

	pool_4, mask_4 = MaxPoolingWithArgmax2D((pool, pool))(conv_10)

	conv_11 = Convolution2D(512, (kernel, kernel), padding="same")(pool_4)
	conv_11 = BatchNormalization()(conv_11)
	conv_11 = Activation("relu")(conv_11)
	conv_12 = Convolution2D(512, (kernel, kernel), padding="same")(conv_11)
	conv_12 = BatchNormalization()(conv_12)
	conv_12 = Activation("relu")(conv_12)
	conv_13 = Convolution2D(512, (kernel, kernel), padding="same")(conv_12)
	conv_13 = BatchNormalization()(conv_13)
	conv_13 = Activation("relu")(conv_13)

	pool_5, mask_5 = MaxPoolingWithArgmax2D((pool, pool))(conv_13)
	print("Build enceder done..")

	# between encoder and decoder
	conv_14 = Convolution2D(512, (kernel, kernel), padding="same")(pool_5)
	conv_14 = BatchNormalization()(conv_14)
	conv_14 = Activation("relu")(conv_14)
	conv_15 = Convolution2D(512, (kernel, kernel), padding="same")(conv_14)
	conv_15 = BatchNormalization()(conv_15)
	conv_15 = Activation("relu")(conv_15)
	conv_16 = Convolution2D(512, (kernel, kernel), padding="same")(conv_15)
	conv_16 = BatchNormalization()(conv_16)
	conv_16 = Activation("relu")(conv_16)

	# decoder
	unpool_1 = MaxUnpooling2D((pool, pool))([conv_16, mask_5])
	concat_1 = Concatenate()([unpool_1, conv_13])

	conv_17 = Convolution2D(512, (kernel, kernel), padding="same")(concat_1)
	conv_17 = BatchNormalization()(conv_17)
	conv_17 = Activation("relu")(conv_17)
	conv_18 = Convolution2D(512, (kernel, kernel), padding="same")(conv_17)
	conv_18 = BatchNormalization()(conv_18)
	conv_18 = Activation("relu")(conv_18)
	conv_19 = Convolution2D(512, (kernel, kernel), padding="same")(conv_18)
	conv_19 = BatchNormalization()(conv_19)
	conv_19 = Activation("relu")(conv_19)

	unpool_2 = MaxUnpooling2D((pool, pool))([conv_19, mask_4])
	concat_2 = Concatenate()([unpool_2, conv_10])

	conv_20 = Convolution2D(512, (kernel, kernel), padding="same")(concat_2)
	conv_20 = BatchNormalization()(conv_20)
	conv_20 = Activation("relu")(conv_20)
	conv_21 = Convolution2D(512, (kernel, kernel), padding="same")(conv_20)
	conv_21 = BatchNormalization()(conv_21)
	conv_21 = Activation("relu")(conv_21)
	conv_22 = Convolution2D(256, (kernel, kernel), padding="same")(conv_21)
	conv_22 = BatchNormalization()(conv_22)
	conv_22 = Activation("relu")(conv_22)

	unpool_3 = MaxUnpooling2D((pool, pool))([conv_22, mask_3])
	concat_3 = Concatenate()([unpool_3, conv_7])

	conv_23 = Convolution2D(256, (kernel, kernel), padding="same")(concat_3)
	conv_23 = BatchNormalization()(conv_23)
	conv_23 = Activation("relu")(conv_23)
	conv_24 = Convolution2D(256, (kernel, kernel), padding="same")(conv_23)
	conv_24 = BatchNormalization()(conv_24)
	conv_24 = Activation("relu")(conv_24)
	conv_25 = Convolution2D(128, (kernel, kernel), padding="same")(conv_24)
	conv_25 = BatchNormalization()(conv_25)
	conv_25 = Activation("relu")(conv_25)

	unpool_4 = MaxUnpooling2D((pool, pool))([conv_25, mask_2])
	concat_4 = Concatenate()([unpool_4, conv_4])

	conv_26 = Convolution2D(128, (kernel, kernel), padding="same")(concat_4)
	conv_26 = BatchNormalization()(conv_26)
	conv_26 = Activation("relu")(conv_26)
	conv_27 = Convolution2D(64, (kernel, kernel), padding="same")(conv_26)
	conv_27 = BatchNormalization()(conv_27)
	conv_27 = Activation("relu")(conv_27)

	unpool_5 = MaxUnpooling2D((pool, pool))([conv_27, mask_1])
	concat_5 = Concatenate()([unpool_5, conv_2])

	conv_28 = Convolution2D(64, (kernel, kernel), padding="same")(concat_5)
	conv_28 = BatchNormalization()(conv_28)
	conv_28 = Activation("relu")(conv_28)

	conv_29 = Convolution2D(n_labels, (1, 1), padding="valid")(conv_28)
	conv_29 = BatchNormalization()(conv_29)
	conv_29 = Reshape((input_shape[0] * input_shape[1], n_labels), input_shape=(input_shape[0], input_shape[1], n_labels))(conv_29)

	outputs = Activation(output_mode)(conv_29)
	print("Build decoder done..")

	model = Model(inputs=inputs, outputs=outputs, name="SegUNet")

	# make the model parallel
	if gpus > 1:
		model = multi_gpu_model(model, gpus=gpus)

	return model
예제 #9
0
def segnet(input_shape,
           n_labels,
           filters=16,
           kernel=3,
           pool_size=(2, 2),
           output_mode="softmax"):

    inputs = Input(shape=input_shape)
    activation = 'relu'
    # encoder
    print('\nBuilding UNet with pooling indices\n')
    conv_1 = Layer(activation, 4, filters, inputs, kernel)
    conv_2 = Layer(activation, 4, filters, conv_1, kernel)

    pool_1, mask_1 = MaxPoolingWithArgmax2D(pool_size)(conv_2)

    conv_3 = Layer(activation, 8, filters, pool_1, kernel)
    conv_4 = Layer(activation, 8, filters, conv_3, kernel)

    pool_2, mask_2 = MaxPoolingWithArgmax2D(pool_size)(conv_4)

    conv_5 = Layer(activation, 16, filters, pool_2, kernel)
    conv_6 = Layer(activation, 16, filters, conv_5, kernel)
    conv_7 = Layer(activation, 16, filters, conv_6, kernel)

    pool_3, mask_3 = MaxPoolingWithArgmax2D(pool_size)(conv_7)

    conv_8 = Layer(activation, 32, filters, pool_3, kernel)
    conv_9 = Layer(activation, 32, filters, conv_8, kernel)
    conv_10 = Layer(activation, 32, filters, conv_9, kernel)

    pool_4, mask_4 = MaxPoolingWithArgmax2D(pool_size)(conv_10)

    conv_11 = Layer(activation, 32, filters, pool_4, kernel)
    conv_12 = Layer(activation, 32, filters, conv_11, kernel)
    conv_13 = Layer(activation, 32, filters, conv_12, kernel)

    pool_5, mask_5 = MaxPoolingWithArgmax2D(pool_size)(conv_13)
    print("Build encoder done..")

    # decoder
    unpool_1 = MaxUnpooling2D(pool_size)([pool_5, mask_5])

    conv_14 = Layer(activation, 32, filters, unpool_1, kernel)
    conv_15 = Layer(activation, 32, filters, conv_14, kernel)
    conv_16 = Layer(activation, 32, filters, conv_15, kernel)

    unpool_2 = MaxUnpooling2D(pool_size)([conv_16, mask_4])

    conv_17 = Layer(activation, 32, filters, unpool_2, kernel)
    conv_18 = Layer(activation, 32, filters, conv_17, kernel)
    conv_19 = Layer(activation, 16, filters, conv_18, kernel)

    unpool_3 = MaxUnpooling2D(pool_size)([conv_19, mask_3])

    conv_20 = Layer(activation, 16, filters, unpool_3, kernel)
    conv_21 = Layer(activation, 16, filters, conv_20, kernel)
    conv_22 = Layer(activation, 8, filters, conv_21, kernel)

    unpool_4 = MaxUnpooling2D(pool_size)([conv_22, mask_2])

    conv_23 = Layer(activation, 8, filters, unpool_4, kernel)
    conv_24 = Layer(activation, 4, filters, conv_23, kernel)

    unpool_5 = MaxUnpooling2D(pool_size)([conv_24, mask_1])

    conv_25 = Layer(activation, 4, filters, unpool_5, kernel)

    conv_26 = Convolution2D(n_labels, (1, 1), padding="valid")(conv_25)
    conv_26 = BatchNormalization()(conv_26)
    outputs = Activation(output_mode)(conv_26)
    print("Build decoder done..")

    segunet = Model(inputs=inputs, outputs=outputs, name="ContiPathNet")

    return segunet
예제 #10
0
def segnet(input_shape,
           n_labels,
           kernel=3,
           pool_size=(2, 2),
           output_mode="softmax"):
    # encoder
    inputs = Input(shape=input_shape)

    conv_1 = Convolution2D(64, (kernel, kernel), padding="same")(inputs)
    conv_1 = BatchNormalization()(conv_1)
    conv_1 = Activation("relu")(conv_1)
    conv_2 = Convolution2D(64, (kernel, kernel), padding="same")(conv_1)
    conv_2 = BatchNormalization()(conv_2)
    conv_2 = Activation("relu")(conv_2)

    pool_1, mask_1 = MaxPoolingWithArgmax2D(pool_size)(conv_2)

    conv_3 = Convolution2D(128, (kernel, kernel), padding="same")(pool_1)
    conv_3 = BatchNormalization()(conv_3)
    conv_3 = Activation("relu")(conv_3)
    conv_4 = Convolution2D(128, (kernel, kernel), padding="same")(conv_3)
    conv_4 = BatchNormalization()(conv_4)
    conv_4 = Activation("relu")(conv_4)

    pool_2, mask_2 = MaxPoolingWithArgmax2D(pool_size)(conv_4)

    conv_5 = Convolution2D(256, (kernel, kernel), padding="same")(pool_2)
    conv_5 = BatchNormalization()(conv_5)
    conv_5 = Activation("relu")(conv_5)
    conv_6 = Convolution2D(256, (kernel, kernel), padding="same")(conv_5)
    conv_6 = BatchNormalization()(conv_6)
    conv_6 = Activation("relu")(conv_6)
    conv_7 = Convolution2D(256, (kernel, kernel), padding="same")(conv_6)
    conv_7 = BatchNormalization()(conv_7)
    conv_7 = Activation("relu")(conv_7)

    pool_3, mask_3 = MaxPoolingWithArgmax2D(pool_size)(conv_7)

    conv_8 = Convolution2D(512, (kernel, kernel), padding="same")(pool_3)
    conv_8 = BatchNormalization()(conv_8)
    conv_8 = Activation("relu")(conv_8)
    conv_9 = Convolution2D(512, (kernel, kernel), padding="same")(conv_8)
    conv_9 = BatchNormalization()(conv_9)
    conv_9 = Activation("relu")(conv_9)
    conv_10 = Convolution2D(512, (kernel, kernel), padding="same")(conv_9)
    conv_10 = BatchNormalization()(conv_10)
    conv_10 = Activation("relu")(conv_10)

    pool_4, mask_4 = MaxPoolingWithArgmax2D(pool_size)(conv_10)

    conv_11 = Convolution2D(512, (kernel, kernel), padding="same")(pool_4)
    conv_11 = BatchNormalization()(conv_11)
    conv_11 = Activation("relu")(conv_11)
    conv_12 = Convolution2D(512, (kernel, kernel), padding="same")(conv_11)
    conv_12 = BatchNormalization()(conv_12)
    conv_12 = Activation("relu")(conv_12)
    conv_13 = Convolution2D(512, (kernel, kernel), padding="same")(conv_12)
    conv_13 = BatchNormalization()(conv_13)
    conv_13 = Activation("relu")(conv_13)

    pool_5, mask_5 = MaxPoolingWithArgmax2D(pool_size)(conv_13)
    print("Build enceder done..")

    # decoder

    unpool_1 = MaxUnpooling2D(pool_size)([pool_5, mask_5])

    conv_14 = Convolution2D(512, (kernel, kernel), padding="same")(unpool_1)
    conv_14 = BatchNormalization()(conv_14)
    conv_14 = Activation("relu")(conv_14)
    conv_15 = Convolution2D(512, (kernel, kernel), padding="same")(conv_14)
    conv_15 = BatchNormalization()(conv_15)
    conv_15 = Activation("relu")(conv_15)
    conv_16 = Convolution2D(512, (kernel, kernel), padding="same")(conv_15)
    conv_16 = BatchNormalization()(conv_16)
    conv_16 = Activation("relu")(conv_16)

    unpool_2 = MaxUnpooling2D(pool_size)([conv_16, mask_4])

    conv_17 = Convolution2D(512, (kernel, kernel), padding="same")(unpool_2)
    conv_17 = BatchNormalization()(conv_17)
    conv_17 = Activation("relu")(conv_17)
    conv_18 = Convolution2D(512, (kernel, kernel), padding="same")(conv_17)
    conv_18 = BatchNormalization()(conv_18)
    conv_18 = Activation("relu")(conv_18)
    conv_19 = Convolution2D(256, (kernel, kernel), padding="same")(conv_18)
    conv_19 = BatchNormalization()(conv_19)
    conv_19 = Activation("relu")(conv_19)

    unpool_3 = MaxUnpooling2D(pool_size)([conv_19, mask_3])

    conv_20 = Convolution2D(256, (kernel, kernel), padding="same")(unpool_3)
    conv_20 = BatchNormalization()(conv_20)
    conv_20 = Activation("relu")(conv_20)
    conv_21 = Convolution2D(256, (kernel, kernel), padding="same")(conv_20)
    conv_21 = BatchNormalization()(conv_21)
    conv_21 = Activation("relu")(conv_21)
    conv_22 = Convolution2D(128, (kernel, kernel), padding="same")(conv_21)
    conv_22 = BatchNormalization()(conv_22)
    conv_22 = Activation("relu")(conv_22)

    unpool_4 = MaxUnpooling2D(pool_size)([conv_22, mask_2])

    conv_23 = Convolution2D(128, (kernel, kernel), padding="same")(unpool_4)
    conv_23 = BatchNormalization()(conv_23)
    conv_23 = Activation("relu")(conv_23)
    conv_24 = Convolution2D(64, (kernel, kernel), padding="same")(conv_23)
    conv_24 = BatchNormalization()(conv_24)
    conv_24 = Activation("relu")(conv_24)

    unpool_5 = MaxUnpooling2D(pool_size)([conv_24, mask_1])

    conv_25 = Convolution2D(64, (kernel, kernel), padding="same")(unpool_5)
    conv_25 = BatchNormalization()(conv_25)
    conv_25 = Activation("relu")(conv_25)

    conv_26 = Convolution2D(n_labels, (1, 1), padding="valid")(conv_25)
    conv_26 = BatchNormalization()(conv_26)
    conv_26 = Reshape(
        (input_shape[0] * input_shape[1], n_labels),
        input_shape=(input_shape[0], input_shape[1], n_labels))(conv_26)

    outputs = Activation(output_mode)(conv_26)
    print("Build decoder done..")

    model = Model(inputs=inputs, outputs=outputs, name="SegNet")

    return model
예제 #11
0
def model(num_classes=19, input_size=(1024,1024,3), pool=(2,2)):
    '''
    In the original github repo here, https://github.com/toimcio/SegNet-tensorflow/blob/master/SegNet.py
    Here I will mix keras and tf.nn layers to produce the same model. Using parts of the github repo above
    to fill in with tf.nn when necessary. This makes the other convolutions a little easier to read I reckon.
    :param input_size: By default (1024, 1024,3)
    :returns model: A Bayesian Segmentation Network as per the paper https://arxiv.org/abs/1511.02680
    '''
    ###-----INPUTS-----###

    #Setup the inputs layer
    inputs = keras.Input(shape=input_size)
    #Do the Local Response Normalization
    lrn = tf.nn.local_response_normalization(inputs, depth_radius=5, bias=1.0, alpha=0.0001, beta=0.75)

    ###-----ENCODER-----###
    #This is essentially VGG16 with some minor additions

    #First Block of Encoder, 2 Convolutions and then a Max Pooling Layer (which takes the indices)
    conv1 = conv_layer(lrn, 64)
    conv2 = conv_layer(conv1, 64)
    pool1, pool1_indices = MaxPoolingWithArgmax2D(pool)(conv2)

    #Second Block of Encoder, same as the first, but 128 channels
    conv3 = conv_layer(pool1, 128)
    conv4 = conv_layer(conv3, 128)
    pool2, pool2_indices = MaxPoolingWithArgmax2D(pool)(conv4)

    # Third Block of Encoder, 3 convolutions with 256 channels and a max pooling
    conv5 = conv_layer(pool2, 256)
    conv6 = conv_layer(conv5, 256)
    conv7 = conv_layer(conv6, 256)
    pool3, pool3_indices = MaxPoolingWithArgmax2D(pool)(conv7)

    #Fourth Block of Encoder, A Dropout Layer, 3 Convolutions with 512 channels, then a pooling layer
    drop1 = keras.layers.Dropout(0.5)(pool3)
    conv8 = conv_layer(drop1, 512)
    conv9 = conv_layer(conv8, 512)
    conv10 = conv_layer(conv9, 512)
    pool4, pool4_indices = MaxPoolingWithArgmax2D(pool)(conv10)

    #Fifth Block of Encoder, Same as Foruth
    drop2 = keras.layers.Dropout(0.5)(pool4)
    conv11 = conv_layer(drop2, 512)
    conv12 = conv_layer(conv11, 512)
    conv13 = conv_layer(conv12, 512)
    pool5, pool5_indices = MaxPoolingWithArgmax2D(pool)(conv13)

    ###-----DECODER-----###

    #First Block of Decoder, A Dropout, Upsampling, then 3 Convolutions of 512 channels
    drop3 = keras.layers.Dropout(0.5)(pool5)
    up1 = MaxUnpooling2D(pool)([drop3, pool5_indices])
    deconv1 = conv_layer(up1, 512)
    deconv2 = conv_layer(deconv1, 512)
    deconv3 = conv_layer(deconv2, 512)

    #Second Block of Decoder, Same as the first, except last convolution is 256 channels
    drop4 = keras.layers.Dropout(0.5)(deconv3)
    up2 = MaxUnpooling2D(pool)([drop4, pool4_indices])
    deconv4 = conv_layer(up2, 512)
    deconv5 = conv_layer(deconv4, 512)
    deconv6 = conv_layer(deconv5, 256)

    #Third Block of Decoder, Same as the second, except last convolution is 128 channels
    drop5 = keras.layers.Dropout(0.5)(deconv6)
    up3 = MaxUnpooling2D(pool)([drop5, pool3_indices])
    deconv7 = conv_layer(up3, 256)
    deconv8 = conv_layer(deconv7, 256)
    deconv9 = conv_layer(deconv8, 128)

    #Fourth Block of Decoder, Same as before 
    drop6 = keras.layers.Dropout(0.5)(deconv9)
    up4 = MaxUnpooling2D(pool)([drop6, pool2_indices])
    deconv10 = conv_layer(up4, 128)
    deconv11 = conv_layer(deconv10, 64)

    #Fifth Block of Decoder
    up5 = MaxUnpooling2D(pool)([deconv11, pool1_indices])
    deconv12 = conv_layer(up5, 64)
    deconv13 = conv_layer(deconv12, 64)

    #Classifier
    outputs = keras.layers.Conv2D(num_classes, kernel_size=(1,1), strides=(1,1), activation = 'softmax', dtype=tf.float32)(deconv13)

    model = keras.Model(inputs=inputs, outputs=outputs, name="Bayes_Segnet")

    #Now apply the weights from the VGG16 ImageNet Model
    weights = get_vgg16_weights(input_size)
    convolution_indexes = get_conv_layers_for_vgg16(model)

    #Now zip them together, and use set weights to put the weights in
    for weight, index in zip(weights, convolution_indexes):
        model.layers[index].set_weights(weight)

    return model
예제 #12
0
def segnet(pretrained_weights=None, input_size=(512, 512, 1)):
    X = Input(input_size)

    #Stage1
    conv1 = Conv2D(nf1,
                   kernel_size=(3, 3),
                   padding='same',
                   kernel_initializer='he_normal')(X)
    conv1 = BatchNormalization()(conv1)
    conv1 = Activation('relu')(conv1)
    conv1 = Conv2D(nf1,
                   kernel_size=(3, 3),
                   padding='same',
                   kernel_initializer='he_normal')(conv1)
    conv1 = BatchNormalization()(conv1)
    conv1 = Activation('relu')(conv1)
    #pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
    pool1, mask1 = MaxPoolingWithArgmax2D((2, 2))(conv1)

    #Stage2
    conv2 = Conv2D(nf2,
                   kernel_size=(3, 3),
                   padding='same',
                   kernel_initializer='he_normal')(pool1)
    conv2 = BatchNormalization()(conv2)
    conv2 = Activation('relu')(conv2)
    conv2 = Conv2D(nf2,
                   kernel_size=(3, 3),
                   padding='same',
                   kernel_initializer='he_normal')(conv2)
    conv2 = BatchNormalization()(conv2)
    conv2 = Activation('relu')(conv2)
    #pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
    pool2, mask2 = MaxPoolingWithArgmax2D((2, 2))(conv2)

    #Stage3
    conv3 = Conv2D(nf3,
                   kernel_size=(3, 3),
                   padding='same',
                   kernel_initializer='he_normal')(pool2)
    conv3 = BatchNormalization()(conv3)
    conv3 = Activation('relu')(conv3)
    conv3 = Conv2D(nf3,
                   kernel_size=(3, 3),
                   padding='same',
                   kernel_initializer='he_normal')(conv3)
    conv3 = BatchNormalization()(conv3)
    conv3 = Activation('relu')(conv3)
    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
    pool3, mask3 = MaxPoolingWithArgmax2D((2, 2))(conv3)

    #Stage4
    conv4 = Conv2D(nf4,
                   kernel_size=(3, 3),
                   padding='same',
                   kernel_initializer='he_normal')(pool3)
    conv4 = BatchNormalization()(conv4)
    conv4 = Activation('relu')(conv4)
    conv4 = Conv2D(nf4,
                   kernel_size=(3, 3),
                   padding='same',
                   kernel_initializer='he_normal')(conv4)
    conv4 = BatchNormalization()(conv4)
    conv4 = Activation('relu')(conv4)
    conv4 = Dropout(0.5)(conv4)
    #drop4 = Dropout(0.2)(conv4)
    #pool4 = MaxPooling2D(pool_size=(2, 2))(drop4)
    pool4, mask4 = MaxPoolingWithArgmax2D((2, 2))(conv4)

    #Stage5
    conv5 = Conv2D(nf4,
                   kernel_size=(3, 3),
                   padding='same',
                   kernel_initializer='he_normal')(pool4)
    conv5 = BatchNormalization()(conv5)
    conv5 = Activation('relu')(conv5)
    conv5 = Conv2D(nf4,
                   kernel_size=(3, 3),
                   padding='same',
                   kernel_initializer='he_normal')(conv5)
    conv5 = BatchNormalization()(conv5)
    conv5 = Activation('relu')(conv5)
    #conv5 = Dropout(0.5)(conv5)
    #drop5 = Dropout(0.2)(conv5)

    #Stage6
    up6 = Conv2D(nf3,
                 kernel_size=(2, 2),
                 padding='same',
                 kernel_initializer='he_normal')(MaxUnpooling2D(
                     (2, 2))([conv5, mask4]))
    up6 = BatchNormalization()(up6)
    up6 = Activation('relu')(up6)
    #merge6 = concatenate([drop4,up6], axis=3)
    conv6 = Conv2D(nf3,
                   kernel_size=(3, 3),
                   padding='same',
                   kernel_initializer='he_normal')(up6)
    conv6 = BatchNormalization()(conv6)
    conv6 = Activation('relu')(conv6)
    conv6 = Conv2D(nf3,
                   kernel_size=(3, 3),
                   padding='same',
                   kernel_initializer='he_normal')(conv6)
    conv6 = BatchNormalization()(conv6)
    conv6 = Activation('relu')(conv6)

    #Stage7
    up7 = Conv2D(nf2,
                 kernel_size=(2, 2),
                 padding='same',
                 kernel_initializer='he_normal')(MaxUnpooling2D(
                     (2, 2))([conv6, mask3]))
    up7 = BatchNormalization()(up7)
    up7 = Activation('relu')(up7)
    #merge7 = concatenate([conv3,up7], axis = 3)
    conv7 = Conv2D(nf2,
                   kernel_size=(3, 3),
                   padding='same',
                   kernel_initializer='he_normal')(up7)
    conv7 = BatchNormalization()(conv7)
    conv7 = Activation('relu')(conv7)
    conv7 = Conv2D(nf2,
                   kernel_size=(3, 3),
                   padding='same',
                   kernel_initializer='he_normal')(conv7)
    conv7 = BatchNormalization()(conv7)
    conv7 = Activation('relu')(conv7)

    #Stage8
    up8 = Conv2D(nf1,
                 kernel_size=(2, 2),
                 padding='same',
                 kernel_initializer='he_normal')(MaxUnpooling2D(
                     (2, 2))([conv7, mask2]))
    up8 = BatchNormalization()(up8)
    up8 = Activation('relu')(up8)
    #merge8 = concatenate([conv2,up8], axis = 3)
    conv8 = Conv2D(nf1,
                   kernel_size=(3, 3),
                   padding='same',
                   kernel_initializer='he_normal')(up8)
    conv8 = BatchNormalization()(conv8)
    conv8 = Activation('relu')(conv8)
    conv8 = Conv2D(nf1,
                   kernel_size=(3, 3),
                   padding='same',
                   kernel_initializer='he_normal')(conv8)
    conv8 = BatchNormalization()(conv8)
    conv8 = Activation('relu')(conv8)

    #Stage9
    up9 = Conv2D(nf1,
                 kernel_size=(2, 2),
                 padding='same',
                 kernel_initializer='he_normal')(MaxUnpooling2D(
                     (2, 2))([conv8, mask1]))
    up9 = BatchNormalization()(up9)
    up9 = Activation('relu')(up9)
    #merge9 = concatenate([conv1,up9], axis = 3)
    conv9 = Conv2D(nf1,
                   kernel_size=(3, 3),
                   padding='same',
                   kernel_initializer='he_normal')(up9)
    conv9 = BatchNormalization()(conv9)
    conv9 = Activation('relu')(conv9)
    conv9 = Conv2D(nf1,
                   kernel_size=(3, 3),
                   padding='same',
                   kernel_initializer='he_normal')(conv9)
    conv9 = BatchNormalization()(conv9)
    conv9 = Activation('relu')(conv9)

    #Stage10
    conv10 = Conv2D(1, kernel_size=(1, 1), activation='sigmoid')(conv9)

    model = Model(inputs=X, outputs=conv10)
    #model.compile(optimizer = Adam(lr = 1e-5), loss = iou_coef_loss, metrics = [iou_coef])
    #model.compile(optimizer = Adam(lr = 1e-5), loss = dice_coef_loss, metrics = [dice_coef])
    model.compile(optimizer=Adam(lr=1e-5),
                  loss='binary_crossentropy',
                  metrics=['accuracy'])
    #model.summary()

    if (pretrained_weights):
        model.load_weights(pretrained_weights)

    return model
예제 #13
0
def segnet(
        input_shape,
        n_labels,
        kernel=3,
        pool_size=(2, 2),
        output_mode="softmax",
        use_residual=False,
        use_argmax=True):
    # encoder
    inputs = Input(shape=input_shape)

    residual_connections = []
    
    conv_1 = Conv2D(64, kernel, padding="same")(inputs)
    conv_1 = BatchNormalization()(conv_1)
    conv_1 = Activation("relu")(conv_1)
    conv_2 = Conv2D(64, kernel, padding="same")(conv_1)
    conv_2 = BatchNormalization()(conv_2)
    conv_2 = Activation("relu")(conv_2)

    if use_argmax: pool_1, mask_1 = MaxPoolingWithArgmax2D(pool_size)(conv_2)
    else: pool_1 = MaxPooling2D(pool_size)(conv_2)
    if use_residual: residual_connections.append(pool_1)
    
    conv_3 = Conv2D(128, kernel, padding="same")(pool_1)
    conv_3 = BatchNormalization()(conv_3)
    conv_3 = Activation("relu")(conv_3)
    conv_4 = Conv2D(128, kernel, padding="same")(conv_3)
    conv_4 = BatchNormalization()(conv_4)
    conv_4 = Activation("relu")(conv_4)
    
    if use_argmax: pool_2, mask_2 = MaxPoolingWithArgmax2D(pool_size)(conv_4)
    else: pool_2 = MaxPooling2D(pool_size)(conv_4)
    if use_residual: residual_connections.append(pool_2)
    
    conv_5 = Conv2D(256, kernel, padding="same")(pool_2)
    conv_5 = BatchNormalization()(conv_5)
    conv_5 = Activation("relu")(conv_5)
    conv_6 = Conv2D(256, kernel, padding="same")(conv_5)
    conv_6 = BatchNormalization()(conv_6)
    conv_6 = Activation("relu")(conv_6)
    conv_7 = Conv2D(256, kernel, padding="same")(conv_6)
    conv_7 = BatchNormalization()(conv_7)
    conv_7 = Activation("relu")(conv_7)
    
    if use_argmax: pool_3, mask_3 = MaxPoolingWithArgmax2D(pool_size)(conv_7)
    else: pool_3 = MaxPooling2D(pool_size)(conv_7)
    if use_residual: residual_connections.append(pool_3)
    
    conv_8 = Conv2D(512, kernel, padding="same")(pool_3)
    conv_8 = BatchNormalization()(conv_8)
    conv_8 = Activation("relu")(conv_8)
    conv_9 = Conv2D(512, kernel, padding="same")(conv_8)
    conv_9 = BatchNormalization()(conv_9)
    conv_9 = Activation("relu")(conv_9)
    conv_10 = Conv2D(512, kernel, padding="same")(conv_9)
    conv_10 = BatchNormalization()(conv_10)
    conv_10 = Activation("relu")(conv_10)
    
    if use_argmax: pool_4, mask_4 = MaxPoolingWithArgmax2D(pool_size)(conv_10)
    else: pool_4 = MaxPooling2D(pool_size)(conv_10)
    if use_residual: residual_connections.append(pool_4)
    
    conv_11 = Conv2D(512, kernel, padding="same")(pool_4)
    conv_11 = BatchNormalization()(conv_11)
    conv_11 = Activation("relu")(conv_11)
    conv_12 = Conv2D(512, kernel, padding="same")(conv_11)
    conv_12 = BatchNormalization()(conv_12)
    conv_12 = Activation("relu")(conv_12)
    conv_13 = Conv2D(512, kernel, padding="same")(conv_12)
    conv_13 = BatchNormalization()(conv_13)
    conv_13 = Activation("relu")(conv_13)
    
    if use_argmax: pool_5, mask_5 = MaxPoolingWithArgmax2D(pool_size)(conv_13)
    else: pool_5 = MaxPooling2D(pool_size)(conv_13)
    if use_residual: residual_connections.append(pool_5)
    print("Done building encoder..")
    
    # decoder
    
    if use_residual: pool_5 = Add()([pool_5, residual_connections[-1]])
    if use_argmax: unpool_1 = MaxUnpooling2D(pool_size)([pool_5, mask_5])
    else: unpool_1 = UpSampling2D(pool_size)(pool_5)
    
    conv_14 = Conv2D(512, kernel, padding="same")(unpool_1)
    conv_14 = BatchNormalization()(conv_14)
    conv_14 = Activation("relu")(conv_14)
    conv_15 = Conv2D(512, kernel, padding="same")(conv_14)
    conv_15 = BatchNormalization()(conv_15)
    conv_15 = Activation("relu")(conv_15)
    conv_16 = Conv2D(512, kernel, padding="same")(conv_15)
    conv_16 = BatchNormalization()(conv_16)
    conv_16 = Activation("relu")(conv_16)
    
    if use_residual: conv_16 = Add()([conv_16, residual_connections[-2]])
    if use_argmax: unpool_2 = MaxUnpooling2D(pool_size)([conv_16, mask_4])
    else: unpool_2 = UpSampling2D(pool_size)(conv_16)
    
    conv_17 = Conv2D(512, kernel, padding="same")(unpool_2)
    conv_17 = BatchNormalization()(conv_17)
    conv_17 = Activation("relu")(conv_17)
    conv_18 = Conv2D(512, kernel, padding="same")(conv_17)
    conv_18 = BatchNormalization()(conv_18)
    conv_18 = Activation("relu")(conv_18)
    conv_19 = Conv2D(256, kernel, padding="same")(conv_18)
    conv_19 = BatchNormalization()(conv_19)
    conv_19 = Activation("relu")(conv_19)
    
    if use_residual: conv_19 = Add()([conv_19, residual_connections[-3]])
    if use_argmax: unpool_3 = MaxUnpooling2D(pool_size)([conv_19, mask_3])
    else: unpool_3 = UpSampling2D(pool_size)(conv_19)
    
    conv_20 = Conv2D(256, kernel, padding="same")(unpool_3)
    conv_20 = BatchNormalization()(conv_20)
    conv_20 = Activation("relu")(conv_20)
    conv_21 = Conv2D(256, kernel, padding="same")(conv_20)
    conv_21 = BatchNormalization()(conv_21)
    conv_21 = Activation("relu")(conv_21)
    conv_22 = Conv2D(128, kernel, padding="same")(conv_21)
    conv_22 = BatchNormalization()(conv_22)
    conv_22 = Activation("relu")(conv_22)
    
    if use_residual: conv_22 = Add()([conv_22, residual_connections[-4]])
    if use_argmax: unpool_4 = MaxUnpooling2D(pool_size)([conv_22, mask_2])
    else: unpool_4 = UpSampling2D(pool_size)(conv_22)
    
    conv_23 = Conv2D(128, kernel, padding="same")(unpool_4)
    conv_23 = BatchNormalization()(conv_23)
    conv_23 = Activation("relu")(conv_23)
    conv_24 = Conv2D(64, kernel, padding="same")(conv_23)
    conv_24 = BatchNormalization()(conv_24)
    conv_24 = Activation("relu")(conv_24)
    
    if use_residual: conv_24 = Add()([conv_24, residual_connections[-5]])
    if use_argmax: unpool_5 = MaxUnpooling2D(pool_size)([conv_24, mask_1])
    else: unpool_5 = UpSampling2D(pool_size)(conv_24)
    
    conv_25 = Conv2D(64, kernel, padding="same")(unpool_5)
    conv_25 = BatchNormalization()(conv_25)
    conv_25 = Activation("relu")(conv_25)

    conv_26 = Conv2D(n_labels, 1, padding="same")(conv_25)
    conv_26 = BatchNormalization()(conv_26)

    outputs = Activation(output_mode)(conv_26)
    print("Done building decoder..")

    model = Model(inputs=inputs, outputs=outputs, name="SegNet")

    return model
예제 #14
0
파일: eLSTM.py 프로젝트: Sandy4321/ReCD
    def encoder_bottleneck(self,
                           inputT,
                           filters,
                           asymmetric=0,
                           dilated=0,
                           downsample=False,
                           dropout_rate=0.1,
                           name=None,
                           reuse=False):
        with tf.variable_scope(name, reuse=reuse):
            # main branch
            internal = filters // 2

            # 1x1
            input_stride = 2 if downsample else 1  # the 1st 1x1 projection is replaced with a 2x2 convolution when downsampling
            x = tf.keras.layers.Conv2D(internal, (input_stride, input_stride),
                                       strides=(input_stride, input_stride),
                                       use_bias=False)(inputT)
            # Batch normalization + PReLU
            x = tf.keras.layers.BatchNormalization(momentum=0.1)(
                x
            )  # enet_unpooling uses momentum of 0.1, keras default is 0.99
            x = tf.keras.layers.PReLU(shared_axes=[1, 2])(x)

            # conv
            if not asymmetric and not dilated:
                x = tf.keras.layers.Conv2D(internal, (3, 3), padding='same')(x)
            elif asymmetric:
                x = tf.keras.layers.Conv2D(internal, (1, asymmetric),
                                           padding='same',
                                           use_bias=False)(x)
                x = tf.keras.layers.Conv2D(internal, (asymmetric, 1),
                                           padding='same')(x)
            elif dilated:
                x = tf.keras.layers.Conv2D(internal, (3, 3),
                                           dilation_rate=(dilated, dilated),
                                           padding='same')(x)
            else:
                raise (Exception('You shouldn\'t be here'))

            x = tf.keras.layers.BatchNormalization(momentum=0.1)(
                x
            )  # enet_unpooling uses momentum of 0.1, keras default is 0.99
            x = tf.keras.layers.PReLU(shared_axes=[1, 2])(x)

            # 1x1
            x = tf.keras.layers.Conv2D(filters, (1, 1), use_bias=False)(x)

            x = tf.keras.layers.BatchNormalization(momentum=0.1)(
                x
            )  # enet_unpooling uses momentum of 0.1, keras default is 0.99
            x = tf.keras.layers.SpatialDropout2D(dropout_rate)(x)

            # other branch
            other = inputT
            if downsample:
                other, indices = MaxPoolingWithArgmax2D(padding='valid')(other)

                pad_feature_maps = filters - inputT.get_shape().as_list()[3]
                if pad_feature_maps > 0:
                    other = tf.keras.layers.Permute((1, 3, 2))(other)
                    tb_pad = (0, 0)
                    lr_pad = (0, pad_feature_maps)
                    other = tf.keras.layers.ZeroPadding2D(
                        padding=(tb_pad, lr_pad))(other)
                    other = tf.keras.layers.Permute((1, 3, 2))(other)
                else:
                    other = tf.keras.layers.Conv2D(filters, (1, 1),
                                                   use_bias=False)(other)

            x = tf.keras.layers.add([x, other])
            x = tf.keras.layers.PReLU(shared_axes=[1, 2])(x)
            if downsample:
                return x, indices
            else:
                return x
예제 #15
0
def segnet(multi_task=False,
           input_shape=(128, 128, 5),
           dist_cl=5,
           n_labels=2,
           kernel=3,
           pool_size=(2, 2),
           output_mode='softmax'):
    # encoder
    inputs = Input(shape=input_shape)

    conv_1 = Convolution2D(64, (kernel, kernel), padding="same")(inputs)
    conv_1 = BatchNormalization()(conv_1)
    conv_1 = Activation("relu")(conv_1)
    conv_2 = Convolution2D(64, (kernel, kernel), padding="same")(conv_1)
    conv_2 = BatchNormalization()(conv_2)
    conv_2 = Activation("relu")(conv_2)

    pool_1, mask_1 = MaxPoolingWithArgmax2D(pool_size)(conv_2)

    conv_3 = Convolution2D(128, (kernel, kernel), padding="same")(pool_1)
    conv_3 = BatchNormalization()(conv_3)
    conv_3 = Activation("relu")(conv_3)
    conv_4 = Convolution2D(128, (kernel, kernel), padding="same")(conv_3)
    conv_4 = BatchNormalization()(conv_4)
    conv_4 = Activation("relu")(conv_4)

    pool_2, mask_2 = MaxPoolingWithArgmax2D(pool_size)(conv_4)

    conv_5 = Convolution2D(256, (kernel, kernel), padding="same")(pool_2)
    conv_5 = BatchNormalization()(conv_5)
    conv_5 = Activation("relu")(conv_5)
    conv_6 = Convolution2D(256, (kernel, kernel), padding="same")(conv_5)
    conv_6 = BatchNormalization()(conv_6)
    conv_6 = Activation("relu")(conv_6)
    conv_7 = Convolution2D(256, (kernel, kernel), padding="same")(conv_6)
    conv_7 = BatchNormalization()(conv_7)
    conv_7 = Activation("relu")(conv_7)

    pool_3, mask_3 = MaxPoolingWithArgmax2D(pool_size)(conv_7)

    conv_8 = Convolution2D(512, (kernel, kernel), padding="same")(pool_3)
    conv_8 = BatchNormalization()(conv_8)
    conv_8 = Activation("relu")(conv_8)
    conv_9 = Convolution2D(512, (kernel, kernel), padding="same")(conv_8)
    conv_9 = BatchNormalization()(conv_9)
    conv_9 = Activation("relu")(conv_9)
    conv_10 = Convolution2D(512, (kernel, kernel), padding="same")(conv_9)
    conv_10 = BatchNormalization()(conv_10)
    conv_10 = Activation("relu")(conv_10)

    pool_4, mask_4 = MaxPoolingWithArgmax2D(pool_size)(conv_10)

    conv_11 = Convolution2D(512, (kernel, kernel), padding="same")(pool_4)
    conv_11 = BatchNormalization()(conv_11)
    conv_11 = Activation("relu")(conv_11)
    conv_12 = Convolution2D(512, (kernel, kernel), padding="same")(conv_11)
    conv_12 = BatchNormalization()(conv_12)
    conv_12 = Activation("relu")(conv_12)
    conv_13 = Convolution2D(512, (kernel, kernel), padding="same")(conv_12)
    conv_13 = BatchNormalization()(conv_13)
    conv_13 = Activation("relu")(conv_13)

    pool_5, mask_5 = MaxPoolingWithArgmax2D(pool_size)(conv_13)

    #classificaiton
    flatten = Flatten()(pool_5)
    fc = Dense(4096, activation='relu', name='fc1')(flatten)
    fc = Dense(128, activation='relu', name='fc2')(fc)

    print("Build enceder done..")

    # decoder

    unpool_1 = MaxUnpooling2D(pool_size)([pool_5, mask_5])

    conv_14 = Convolution2D(512, (kernel, kernel), padding="same")(unpool_1)
    conv_14 = BatchNormalization()(conv_14)
    conv_14 = Activation("relu")(conv_14)
    conv_15 = Convolution2D(512, (kernel, kernel), padding="same")(conv_14)
    conv_15 = BatchNormalization()(conv_15)
    conv_15 = Activation("relu")(conv_15)
    conv_16 = Convolution2D(512, (kernel, kernel), padding="same")(conv_15)
    conv_16 = BatchNormalization()(conv_16)
    conv_16 = Activation("relu")(conv_16)

    unpool_2 = MaxUnpooling2D(pool_size)([conv_16, mask_4])

    conv_17 = Convolution2D(512, (kernel, kernel), padding="same")(unpool_2)
    conv_17 = BatchNormalization()(conv_17)
    conv_17 = Activation("relu")(conv_17)
    conv_18 = Convolution2D(512, (kernel, kernel), padding="same")(conv_17)
    conv_18 = BatchNormalization()(conv_18)
    conv_18 = Activation("relu")(conv_18)
    conv_19 = Convolution2D(256, (kernel, kernel), padding="same")(conv_18)
    conv_19 = BatchNormalization()(conv_19)
    conv_19 = Activation("relu")(conv_19)

    unpool_3 = MaxUnpooling2D(pool_size)([conv_19, mask_3])

    conv_20 = Convolution2D(256, (kernel, kernel), padding="same")(unpool_3)
    conv_20 = BatchNormalization()(conv_20)
    conv_20 = Activation("relu")(conv_20)
    conv_21 = Convolution2D(256, (kernel, kernel), padding="same")(conv_20)
    conv_21 = BatchNormalization()(conv_21)
    conv_21 = Activation("relu")(conv_21)
    conv_22 = Convolution2D(128, (kernel, kernel), padding="same")(conv_21)
    conv_22 = BatchNormalization()(conv_22)
    conv_22 = Activation("relu")(conv_22)

    unpool_4 = MaxUnpooling2D(pool_size)([conv_22, mask_2])

    conv_23 = Convolution2D(128, (kernel, kernel), padding="same")(unpool_4)
    conv_23 = BatchNormalization()(conv_23)
    conv_23 = Activation("relu")(conv_23)
    conv_24 = Convolution2D(64, (kernel, kernel), padding="same")(conv_23)
    conv_24 = BatchNormalization()(conv_24)
    conv_24 = Activation("relu")(conv_24)

    unpool_5 = MaxUnpooling2D(pool_size)([conv_24, mask_1])

    conv_25 = Convolution2D(64, (kernel, kernel), padding="same")(unpool_5)
    conv_25 = BatchNormalization()(conv_25)
    conv_25 = Activation("relu")(conv_25)

    #Branch 1 : 5band distance
    if (multi_task):
        conv_25_2 = Convolution2D(dist_cl, (1, 1),
                                  padding='same',
                                  name='distance1')(conv_25)
        dist_map = Softmax(axis=-1, name='distance')(conv_25_2)

        conv_26 = Activation("relu")(conv_25_2)
        concat = concatenate([conv_25, conv_26], axis=3)
        binary_mask = Convolution2D(n_labels, (1, 1),
                                    padding="valid",
                                    activation=output_mode,
                                    name='binary')(concat)

        #         flatten2 =  Flatten()(conv_25)
        #         fc2 = Dense(128, activation='relu')(flatten2)
        #         fc = concatenate([fc1,fc2], axis=1)
        classification = Dense(n_labels,
                               activation='sigmoid',
                               name='classification')(fc)
        print("Build decoder done..")

        model = Model(inputs=inputs,
                      outputs=[binary_mask, dist_map, classification],
                      name="SegNet")

    else:

        conv_26 = Convolution2D(n_labels, (1, 1), padding="valid")(conv_25)
        conv_26 = BatchNormalization()(conv_26)
        #     conv_26 = Reshape(
        #             (input_shape[0]*input_shape[1], n_labels),
        #    outputs = Activation(output_mode, name = 'binary')(conv_26)
        print("Build decoder done..")
        model = Model(inputs=inputs, outputs=[conv_26], name="SegNet")

    model.summary()

    return model