def fusion2(self,
                conv5_feature,
                conv4_feature,
                upconv3,
                name="fusion2"):  #(1/16, 1/8, 1)
        with tf.variable_scope(name) as scope:
            output_channel = np.shape(upconv3)[3]

            conv5_feature = conv_bn_relu(
                conv5_feature,
                [3, 3, np.shape(conv5_feature)[3], output_channel], 1)
            conv5_feature = UpSampling2D((16, 16))(conv5_feature)

            conv4_feature = conv_bn_relu(
                conv4_feature,
                [3, 3, np.shape(conv4_feature)[3], output_channel], 1)
            conv4_feature = UpSampling2D((8, 8))(conv4_feature)

            upconv3 = conv_bn_relu(
                upconv3, [3, 3, np.shape(upconv3)[3], output_channel], 1)

            output = tf.add(conv5_feature, conv4_feature)
            output = tf.add(output, upconv3)
            output = conv_bn_relu(output,
                                  [3, 3, output_channel, output_channel], 1)
            return output
def get_dae_clf():
    model1 = Sequential()

    model1.add(Lambda(lambda x_: x_ + 0.5, input_shape=(28, 28, 1)))

    # Encoder
    model1.add(Conv2D(3, (3, 3), activation="sigmoid", padding="same", activity_regularizer=regs.l2(1e-9)))
    model1.add(AveragePooling2D((2, 2), padding="same"))
    model1.add(Conv2D(3, (3, 3), activation="sigmoid", padding="same", activity_regularizer=regs.l2(1e-9)))

    # Decoder
    model1.add(Conv2D(3, (3, 3), activation="sigmoid", padding="same", activity_regularizer=regs.l2(1e-9)))
    model1.add(UpSampling2D((2, 2)))
    model1.add(Conv2D(3, (3, 3), activation="sigmoid", padding="same", activity_regularizer=regs.l2(1e-9)))
    model1.add(Conv2D(1, (3, 3), activation='sigmoid', padding='same', activity_regularizer=regs.l2(1e-9)))

    model1.add(Lambda(lambda x_: x_ - 0.5))

    model1.load_weights("./dae/mnist")
    model1.compile(loss='mean_squared_error', metrics=['mean_squared_error'], optimizer='adam')

    model2 = Sequential()

    model2.add(Conv2D(32, (3, 3), input_shape=(28, 28, 1)))
    model2.add(Activation('relu'))
    model2.add(Conv2D(32, (3, 3)))
    model2.add(Activation('relu'))
    model2.add(MaxPooling2D(pool_size=(2, 2)))

    model2.add(Conv2D(64, (3, 3)))
    model2.add(Activation('relu'))
    model2.add(Conv2D(64, (3, 3)))
    model2.add(Activation('relu'))
    model2.add(MaxPooling2D(pool_size=(2, 2)))

    model2.add(Flatten())
    model2.add(Dense(200))
    model2.add(Activation('relu'))
    model2.add(Dropout(0.5))
    model2.add(Dense(200))
    model2.add(Activation('relu'))
    model2.add(Dense(10))

    model2.load_weights("./models/mnist")

    def fn(correct, predicted):
        return tf.nn.softmax_cross_entropy_with_logits(labels=correct, logits=predicted)

    model2.compile(loss=fn, optimizer=SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True), metrics=['accuracy'])

    model = Sequential()
    model.add(model1)
    model.add(model2)
    model.compile(loss=fn, optimizer=SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True), metrics=['accuracy'])

    return model
Пример #3
0
    def Dense_decoder_feature_generation(self, inputs):
        branch1 = self.Conv_Pool_Conv(inputs)
        branch2 = self.Conv_Pool_Conv(inputs)
        branch3 = self.Conv_Pool_Conv(inputs)
        branch4 = self.Conv_Pool_Conv(inputs)

        features = tf.concat([branch1, branch2, branch3, branch4], axis=3)
        D = np.shape(inputs)[3]
        features = conv_bn_relu(features, [3, 3, D, D], stride=1)

        for i in range(4):
            features = self.generation_stage(features)
        features = UpSampling2D((2, 2))(features)
        features = conv_bn_relu(features, [3, 3, D, D], 1)
        return features
Пример #4
0
    def fusion1(self, conv5_feature, upconv4):  #(1/16, 1/8)
        output_channel = np.shape(upconv4)[3]

        conv5_feature = conv_bn_relu(
            conv5_feature,
            [3, 3, np.shape(conv5_feature)[3], output_channel], 1)
        conv5_feature = UpSampling2D((2, 2))(conv5_feature)

        upconv4 = conv_bn_relu(
            upconv4, [3, 3, np.shape(upconv4)[3], output_channel], 1)

        output = tf.add(conv5_feature, upconv4)
        output = conv_bn_relu(output, [3, 3, output_channel, output_channel],
                              1)
        return output
Пример #5
0
    def build_generator(self):

        model = Sequential()

        model.add(
            Dense(128 * 7 * 7, activation="relu", input_dim=self.latent_dim))
        model.add(Reshape((7, 7, 128)))
        model.add(UpSampling2D())
        model.add(Conv2D(128, kernel_size=3, padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Activation("relu"))
        model.add(UpSampling2D())
        model.add(Conv2D(64, kernel_size=3, padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Activation("relu"))
        model.add(Conv2D(self.channels, kernel_size=3, padding="same"))
        model.add(Activation("tanh"))

        # model.summary()

        noise = Input(shape=(self.latent_dim, ))
        img = model(noise)

        return Model(noise, img)
    def fusion1(self, conv5_feature, upconv4, name="fusion1"):  #(1/16, 1/8)
        with tf.variable_scope(name) as scope:
            output_channel = np.shape(upconv4)[3]

            conv5_feature = conv_bn_relu(
                conv5_feature,
                [3, 3, np.shape(conv5_feature)[3], output_channel], 1)
            conv5_feature = UpSampling2D((2, 2))(conv5_feature)

            upconv4 = conv_bn_relu(
                upconv4, [3, 3, np.shape(upconv4)[3], output_channel], 1)

            output = tf.add(conv5_feature, upconv4)
            output = conv_bn_relu(output,
                                  [3, 3, output_channel, output_channel], 1)
            return output
    def Dense_decoder_feature_generation(self, inputs, name):
        with tf.variable_scope(name) as scope:
            branch1 = self.Conv_Pool_Conv(inputs, "b1")
            branch2 = self.Conv_Pool_Conv(inputs, "b2")
            branch3 = self.Conv_Pool_Conv(inputs, "b3")
            branch4 = self.Conv_Pool_Conv(inputs, "b4")

            features = tf.concat([branch1, branch2, branch3, branch4], axis=3)
            D = np.shape(inputs)[3]
            features = conv_bn_relu(features, [3, 3, D, D], stride=1)

            for i in range(4):
                features = self.generation_stage(features, "stage" + str(i))
            features = UpSampling2D((2, 2))(features)
            features = conv_bn_relu(features, [3, 3, D, D], 1)
            return features
Пример #8
0
    def __init__(self):
        self.model_dir = "./dae/"
        self.v_noise = 0.1
        h, w, c = [28, 28, 1]

        model = Sequential()
        model.add(Lambda(lambda x_: x_ + 0.5, input_shape=(28, 28, 1)))

        # Encoder
        model.add(
            Conv2D(3, (3, 3),
                   activation="sigmoid",
                   padding="same",
                   activity_regularizer=regs.l2(1e-9)))
        model.add(AveragePooling2D((2, 2), padding="same"))
        model.add(
            Conv2D(3, (3, 3),
                   activation="sigmoid",
                   padding="same",
                   activity_regularizer=regs.l2(1e-9)))

        # Decoder
        model.add(
            Conv2D(3, (3, 3),
                   activation="sigmoid",
                   padding="same",
                   activity_regularizer=regs.l2(1e-9)))
        model.add(UpSampling2D((2, 2)))
        model.add(
            Conv2D(3, (3, 3),
                   activation="sigmoid",
                   padding="same",
                   activity_regularizer=regs.l2(1e-9)))
        model.add(
            Conv2D(c, (3, 3),
                   activation='sigmoid',
                   padding='same',
                   activity_regularizer=regs.l2(1e-9)))

        model.add(Lambda(lambda x_: x_ - 0.5))

        self.model = model
def get_dae():
    model = Sequential()

    model.add(Lambda(lambda x_: x_ + 0.5, input_shape=(28, 28, 1)))

    # Encoder
    model.add(Conv2D(3, (3, 3), activation="sigmoid", padding="same", activity_regularizer=regs.l2(1e-9)))
    model.add(AveragePooling2D((2, 2), padding="same"))
    model.add(Conv2D(3, (3, 3), activation="sigmoid", padding="same", activity_regularizer=regs.l2(1e-9)))

    # Decoder
    model.add(Conv2D(3, (3, 3), activation="sigmoid", padding="same", activity_regularizer=regs.l2(1e-9)))
    model.add(UpSampling2D((2, 2)))
    model.add(Conv2D(3, (3, 3), activation="sigmoid", padding="same", activity_regularizer=regs.l2(1e-9)))
    model.add(Conv2D(1, (3, 3), activation='sigmoid', padding='same', activity_regularizer=regs.l2(1e-9)))

    model.add(Lambda(lambda x_: x_ - 0.5))

    model.load_weights("./dae/mnist")
    model.compile(loss='mean_squared_error', metrics=['mean_squared_error'], optimizer='adam')

    return model
    def __call__(self, x):
        with tf.variable_scope(self.name, reuse=self.reuse):
            conv1 = conv2d(x, "conv1", 32, 4, 1, "SAME", True, True,
                           self.is_train)  # 512*512*32
            conv1 = conv2d(conv1, "conv2", 32, 4, 2, "SAME", True, True,
                           self.is_train)  #256*256*32

            conv2 = conv2d(conv1, "conv3", 64, 4, 1, "SAME", True, True,
                           self.is_train)  #256*256*64
            conv2 = conv2d(conv2, "conv4", 64, 4, 2, "SAME", True, True,
                           self.is_train)  #128*128*64

            conv3 = conv2d(conv2, "conv5", 128, 4, 1, "SAME", True, True,
                           self.is_train)  #128*128*128
            conv3 = conv2d(conv3, "conv6", 128, 4, 2, "SAME", True, True,
                           self.is_train)  #64*64*128

            conv4 = conv2d(conv3, "conv7", 256, 4, 1, "SAME", True, True,
                           self.is_train)  #64*64*256
            conv4 = conv2d(conv4, "conv8", 256, 4, 2, "SAME", True, True,
                           self.is_train)  #32*32*256

            conv5 = conv2d(conv4, "conv9", 512, 4, 1, "SAME", True, True,
                           self.is_train)  #32*32*512
            conv5 = conv2d(conv5, "conv10", 512, 4, 2, "SAME", True, True,
                           self.is_train)  #16*16*512

            up5 = UpSampling2D((2, 2))(conv5)  #32*32*512
            up5 = conv2d(up5, "conv15", 256, 4, 1, "SAME", True, True,
                         self.is_train)  #32*32*256
            up5 = tf.concat([up5, conv4], axis=3)
            up5 = conv2d(up5, "conv16", 256, 4, 1, "SAME", True, True,
                         self.is_train)  #32*32*256

            up4 = UpSampling2D((2, 2))(up5)  #64*64*256
            up4 = conv2d(up4, "conv17", 128, 4, 1, "SAME", True, True,
                         self.is_train)  #64*64*128
            up4 = tf.concat([up4, conv3], axis=3)
            up4 = conv2d(up4, "conv18", 128, 4, 1, "SAME", True, True,
                         self.is_train)  #64*64*128

            up3 = UpSampling2D((2, 2))(up4)  #128*128*128
            up3 = conv2d(up3, "conv19", 64, 4, 1, "SAME", True, True,
                         self.is_train)  #128*128*64
            up3 = tf.concat([up3, conv2], axis=3)
            up3 = conv2d(up3, "conv20", 64, 4, 1, "SAME", True, True,
                         self.is_train)  #129*128*64

            up2 = UpSampling2D((2, 2))(up3)  #256*256*64
            up2 = conv2d(up2, "conv21", 32, 4, 1, "SAME", True, True,
                         self.is_train)  #256*256*32
            up2 = tf.concat([up2, conv1], axis=3)
            up2 = conv2d(up2, "conv22", 32, 4, 1, "SAME", True, True,
                         self.is_train)  #256*256*32

            up1 = UpSampling2D((2, 2))(up2)  #512*512*32
            up1 = conv2d(up1, "conv23", 32, 4, 1, "SAME", True, True,
                         self.is_train)  #512*512*32
            up1 = conv2d(up1, "conv24", 1, 1, 1, "SAME", False, False,
                         self.is_train)  #512*512*1

            output = tf.nn.tanh(up1)

        if self.reuse is None:
            self.var_list = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                              scope=self.name)
            self.saver = tf.train.Saver(self.var_list)
            self.reuse = True

        return output
Пример #11
0
    def __init__(self, img_size, num_channels, compress_mode=1, resize=None):

        self.compress_mode = compress_mode
        working_img_size = img_size

        encoder_model = Sequential()
        # resize input to a size easy for down-sampled
        if resize:
            encoder_model.add(
                Lambda(lambda image: tf.image.resize_images(
                    image, (resize, resize)),
                       input_shape=(img_size, img_size, num_channels)))
        else:
            encoder_model.add(
                Convolution2D(16,
                              3,
                              strides=1,
                              padding='same',
                              input_shape=(img_size, img_size, num_channels)))

        BatchNormalization(axis=3)
        encoder_model.add(Activation("relu"))
        encoder_model.add(MaxPooling2D(pool_size=2, strides=2, padding='same'))
        working_img_size //= 2

        if compress_mode >= 2:
            encoder_model.add(Convolution2D(16, 3, strides=1, padding='same'))
            BatchNormalization(axis=3)
            encoder_model.add(Activation("relu"))
            encoder_model.add(
                MaxPooling2D(pool_size=2, strides=2, padding='same'))
            working_img_size //= 2

        if compress_mode >= 3:
            encoder_model.add(Convolution2D(16, 3, strides=1, padding='same'))
            BatchNormalization(axis=3)
            encoder_model.add(Activation("relu"))
            encoder_model.add(
                MaxPooling2D(pool_size=2, strides=2, padding='same'))
            working_img_size //= 2

        encoder_model.add(
            Convolution2D(num_channels, 3, strides=1, padding='same'))
        BatchNormalization(axis=3)
        decoder_model = Sequential()
        decoder_model.add(encoder_model)

        if compress_mode >= 3:
            working_img_size *= 2
            decoder_model.add(Convolution2D(16, 3, strides=1, padding='same'))
            BatchNormalization(axis=3)
            decoder_model.add(Activation("relu"))
            #decoder_model.add(Lambda(lambda image: tf.image.resize_images(image, (working_img_size, working_img_size))))
            decoder_model.add(UpSampling2D((2, 2),
                                           data_format='channels_last'))

        if compress_mode >= 2:
            working_img_size *= 2
            decoder_model.add(Convolution2D(16, 3, strides=1, padding='same'))
            BatchNormalization(axis=3)
            decoder_model.add(Activation("relu"))
            #decoder_model.add(Lambda(lambda image: tf.image.resize_images(image, (working_img_size, working_img_size))))
            decoder_model.add(UpSampling2D((2, 2),
                                           data_format='channels_last'))

        working_img_size *= 2
        decoder_model.add(Convolution2D(16, 3, strides=1, padding='same'))
        BatchNormalization(axis=3)
        decoder_model.add(Activation("relu"))
        # decoder_model.add(Lambda(lambda image: tf.image.resize_images(image, (img_size, img_size))))
        decoder_model.add(UpSampling2D((2, 2), data_format='channels_last'))

        if resize:
            decoder_model.add(
                Lambda(lambda image: tf.image.resize_images(
                    image, (img_size, img_size))))

        decoder_model.add(
            Convolution2D(num_channels, 3, strides=1, padding='same'))
        # decoder_model.add(Lambda(lambda image: K.clip(image, -clip_value, clip_value) ))

        print('Encoder model:')
        encoder_model.summary()

        print('Decoder model:')
        decoder_model.summary()

        self.encoder = encoder_model
        self.decoder = decoder_model
Пример #12
0
    def build(self, x, class_num, channel, wd, train=True):
        self.conv1_1 = _conv_layer(x, [3, 3, channel, 64], 'conv1_1')
        self.conv1_2 = _conv_layer(self.conv1_1, [3, 3, 64, 64], 'conv1_2')
        self.pooling1 = _pooling_layer(self.conv1_2, 'pooling1')

        self.conv2_1 = _conv_layer(self.pooling1, [3, 3, 64, 128], 'conv2_1')
        self.conv2_2 = _conv_layer(self.conv2_1, [3, 3, 128, 128], 'conv2_2')
        self.pooling2 = _pooling_layer(self.conv2_2, 'pooling2')

        self.conv3_1 = _conv_layer(self.pooling2, [3, 3, 128, 256], 'conv3_1')
        self.conv3_2 = _conv_layer(self.conv3_1, [3, 3, 256, 256], 'conv3_2')
        self.conv3_3 = _conv_layer(self.conv3_2, [3, 3, 256, 256], 'conv3_3')
        self.pooling3 = _pooling_layer(self.conv3_3, 'pooling3')

        self.conv4_1 = _conv_layer(self.pooling3, [3, 3, 256, 512], 'conv4_1')
        self.conv4_2 = _conv_layer(self.conv4_1, [3, 3, 512, 512], 'conv4_2')
        self.conv4_3 = _conv_layer(self.conv4_2, [3, 3, 512, 512], 'conv4_3')
        self.pooling4 = _pooling_layer(self.conv4_3, 'pooling4')

        self.conv5_1 = _conv_layer(self.pooling4, [3, 3, 512, 512], 'conv5_1')
        self.conv5_2 = _conv_layer(self.conv5_1, [3, 3, 512, 512], 'conv5_2')
        self.conv5_3 = _conv_layer(self.conv5_2, [3, 3, 512, 512], 'conv5_3')
        self.pooling5 = _pooling_layer(self.conv5_3, 'pooling5')

        self.conv6 = _conv_layer(self.pooling5, [7, 7, 512, 4096], 'conv6')
        if train:
            self.conv6 = tf.nn.dropout(self.conv6, 0.5)
        self.conv7 = _conv_layer(self.conv6, [1, 1, 4096, 4096], 'conv7')
        if train:
            self.conv7 = tf.nn.dropout(self.conv7, 0.5)
        self.score_fr = _conv_layer_without_relu(self.conv7, [1, 1, 4096, 64],
                                                 'conv7_1x1conv',
                                                 wd=wd)

        self.pooling4_conv = _conv_layer_without_relu(self.pooling4,
                                                      [1, 1, 512, 64],
                                                      'pool4_1x1conv',
                                                      wd=wd)

        self.pooling3_conv = _conv_layer_without_relu(self.pooling3,
                                                      [1, 1, 256, 64],
                                                      'pool3_1x1conv',
                                                      wd=wd)

        self.upconv5 = UpSampling2D((2, 2))(self.score_fr)  #1/16
        self.upconv4 = UpSampling2D((2, 2))(self.pooling4_conv)  #1/8
        self.upconv3 = UpSampling2D((8, 8))(self.pooling3_conv)  #1
        self.conv5_feature = self.Dense_decoder_feature_generation(
            self.upconv5)

        self.upconv4 = self.fusion1(self.conv5_feature, self.upconv4)  #1/8
        self.conv4_feature = self.Dense_decoder_feature_generation(
            self.upconv4)

        self.upconv3 = self.fusion2(self.conv5_feature, self.conv4_feature,
                                    self.upconv3)
        self.conv3_feature = self.Dense_decoder_feature_generation(
            self.upconv3)

        self.upscore = conv_bn_relu(
            self.conv3_feature,
            [3, 3, np.shape(self.conv3_feature)[3], 64], 1)
        self.upscore = conv_bn_relu(self.upscore, [3, 3, 64, class_num], 1)

        return self.upscore
    def build(self, x, class_num, channel, wd, train=True):

        self.conv1_1 = _conv_layer(x, [3, 3, channel, 64], 'conv1_1')
        self.conv1_2 = _conv_layer(self.conv1_1, [3, 3, 64, 64], 'conv1_2')
        self.pooling1 = _pooling_layer(self.conv1_2, 'pooling1')

        self.conv2_1 = _conv_layer(self.pooling1, [3, 3, 64, 128], 'conv2_1')
        self.conv2_2 = _conv_layer(self.conv2_1, [3, 3, 128, 128], 'conv2_2')
        self.pooling2 = _pooling_layer(self.conv2_2, 'pooling2')

        # self.inception_block2_1 = _inception_layer(self.pooling1, in_c=64, out_1=32, out_21=32, out_22=32, out_31=32, out_32=64, out_33=64)
        # self.inception_block2_2 = _inception_layer(self.inception_block2_1, in_c=128, out_1=32, out_21=32, out_22=32, out_31=32,
        #                                            out_32=64, out_33=64)
        # self.pooling2 = _pooling_layer(self.inception_block2_2, 'pooling2')

        # self.conv3_1 = _conv_layer(self.pooling2, [3, 3, 128, 256], 'conv3_1')
        # self.conv3_2 = _conv_layer(self.conv3_1, [3, 3, 256, 256], 'conv3_2')
        # self.conv3_3 = _conv_layer(self.conv3_2, [3, 3, 256, 256], 'conv3_3')
        # self.pooling3 = _pooling_layer(self.conv3_3, 'pooling3')

        self.inception_block3_1 = _inception_layer(
            'inception3_1',
            self.pooling2,
            in_c=128,
            out_1=64,
            out_21=64,
            out_22=64,
            out_31=64,
            out_32=128,
            out_33=64,
            out_41=64,
            out_42=128,
            out_43=64)  # output channel 256
        self.inception_block3_2 = _inception_layer(
            'inception3_2',
            self.inception_block3_1,
            in_c=256,
            out_1=64,
            out_21=64,
            out_22=64,
            out_31=64,
            out_32=128,
            out_33=64,
            out_41=64,
            out_42=128,
            out_43=64)  # output channel 256
        self.inception_block3_3 = _inception_layer(
            'inception3_3',
            self.inception_block3_2,
            in_c=256,
            out_1=64,
            out_21=64,
            out_22=64,
            out_31=64,
            out_32=128,
            out_33=64,
            out_41=64,
            out_42=128,
            out_43=64)  # output channel 256
        self.pooling3 = _pooling_layer(self.inception_block3_3, 'pooling3')

        self.inception_block4_1 = _inception_layer('inception4_1',
                                                   self.pooling3,
                                                   in_c=256,
                                                   out_1=128,
                                                   out_21=128,
                                                   out_22=128,
                                                   out_31=128,
                                                   out_32=256,
                                                   out_33=128,
                                                   out_41=128,
                                                   out_42=256,
                                                   out_43=128)  # output 512
        self.inception_block4_2 = _inception_layer('inception4_2',
                                                   self.inception_block4_1,
                                                   in_c=512,
                                                   out_1=128,
                                                   out_21=128,
                                                   out_22=128,
                                                   out_31=128,
                                                   out_32=256,
                                                   out_33=128,
                                                   out_41=128,
                                                   out_42=256,
                                                   out_43=128)  # output 512
        self.inception_block4_3 = _inception_layer('inception4_3',
                                                   self.inception_block4_2,
                                                   in_c=512,
                                                   out_1=128,
                                                   out_21=128,
                                                   out_22=128,
                                                   out_31=128,
                                                   out_32=256,
                                                   out_33=128,
                                                   out_41=128,
                                                   out_42=256,
                                                   out_43=128)  # output 512
        self.pooling4 = _pooling_layer(self.inception_block4_3, 'pooling4')

        self.inception_block5_1 = _inception_layer('inception5_1',
                                                   self.pooling4,
                                                   in_c=512,
                                                   out_1=128,
                                                   out_21=128,
                                                   out_22=128,
                                                   out_31=128,
                                                   out_32=256,
                                                   out_33=128,
                                                   out_41=128,
                                                   out_42=256,
                                                   out_43=128)  # output 512
        self.inception_block5_2 = _inception_layer('inception5_2',
                                                   self.inception_block5_1,
                                                   in_c=512,
                                                   out_1=128,
                                                   out_21=128,
                                                   out_22=128,
                                                   out_31=128,
                                                   out_32=256,
                                                   out_33=128,
                                                   out_41=128,
                                                   out_42=256,
                                                   out_43=128)  # output 512
        self.inception_block5_3 = _inception_layer('inception5_3',
                                                   self.inception_block5_2,
                                                   in_c=512,
                                                   out_1=128,
                                                   out_21=128,
                                                   out_22=128,
                                                   out_31=128,
                                                   out_32=256,
                                                   out_33=128,
                                                   out_41=128,
                                                   out_42=256,
                                                   out_43=128)  # output 512
        self.pooling5 = _pooling_layer(self.inception_block5_3, 'pooling5')

        self.conv6 = _conv_layer(self.pooling5, [7, 7, 512, 4096], 'conv6')
        if train:
            self.conv6 = tf.nn.dropout(self.conv6, 0.5)
        self.conv7 = _conv_layer(self.conv6, [1, 1, 4096, 4096], 'conv7')
        if train:
            self.conv7 = tf.nn.dropout(self.conv7, 0.5)
        self.score_fr = _conv_layer_without_relu(self.conv7, [1, 1, 4096, 64],
                                                 'conv7_1x1conv',
                                                 wd=wd)

        self.pooling4_conv = _conv_layer_without_relu(self.pooling4,
                                                      [1, 1, 512, 64],
                                                      'pool4_1x1conv',
                                                      wd=wd)

        self.pooling3_conv = _conv_layer_without_relu(self.pooling3,
                                                      [1, 1, 256, 64],
                                                      'pool3_1x1conv',
                                                      wd=wd)

        self.upconv5 = UpSampling2D((2, 2))(self.score_fr)  #1/16
        self.upconv4 = UpSampling2D((2, 2))(self.pooling4_conv)  #1/8
        self.upconv3 = UpSampling2D((8, 8))(self.pooling3_conv)  #1
        self.conv5_feature = self.Dense_decoder_feature_generation(
            self.upconv5, name="gen1")

        self.upconv4 = self.fusion1(self.conv5_feature, self.upconv4)  #1/8
        self.conv4_feature = self.Dense_decoder_feature_generation(
            self.upconv4, name="gen2")

        self.upconv3 = self.fusion2(self.conv5_feature, self.conv4_feature,
                                    self.upconv3)
        self.conv3_feature = self.Dense_decoder_feature_generation(
            self.upconv3, name="gen3")

        self.upscore = conv_bn_relu(
            self.conv3_feature,
            [3, 3, np.shape(self.conv3_feature)[3], 64],
            1,
            name="final1")
        self.upscore = conv_bn_relu(self.upscore, [3, 3, 64, class_num],
                                    1,
                                    name="final2")

        return self.upscore
    def __init__(self, restore_dae=None, restore_clf=None, session=None, use_softmax=False, activation="relu"):

        print("inside MNISTModelDAE: activation = {}".format(activation))

        self.num_channels = 1
        self.image_size = 28
        self.num_labels = 10

        model1 = Sequential()

        model1.add(Lambda(lambda x_: x_ + 0.5, input_shape=(28, 28, 1)))

        # Encoder
        model1.add(Conv2D(3, (3, 3), activation="sigmoid", padding="same", activity_regularizer=regs.l2(1e-9)))
        model1.add(AveragePooling2D((2, 2), padding="same"))
        model1.add(Conv2D(3, (3, 3), activation="sigmoid", padding="same", activity_regularizer=regs.l2(1e-9)))

        # Decoder
        model1.add(Conv2D(3, (3, 3), activation="sigmoid", padding="same", activity_regularizer=regs.l2(1e-9)))
        model1.add(UpSampling2D((2, 2)))
        model1.add(Conv2D(3, (3, 3), activation="sigmoid", padding="same", activity_regularizer=regs.l2(1e-9)))
        model1.add(Conv2D(1, (3, 3), activation='sigmoid', padding='same', activity_regularizer=regs.l2(1e-9)))

        model1.add(Lambda(lambda x_: x_ - 0.5))

        model1.load_weights(restore_dae)
        model1.compile(loss='mean_squared_error', metrics=['mean_squared_error'], optimizer='adam')


        model2 = Sequential()

        model2.add(Conv2D(32, (3, 3), input_shape=(28, 28, 1)))
        model2.add(Activation(activation))
        model2.add(Conv2D(32, (3, 3)))
        model2.add(Activation(activation))
        model2.add(MaxPooling2D(pool_size=(2, 2)))

        model2.add(Conv2D(64, (3, 3)))
        model2.add(Activation(activation))
        model2.add(Conv2D(64, (3, 3)))
        model2.add(Activation(activation))
        model2.add(MaxPooling2D(pool_size=(2, 2)))

        model2.add(Flatten())
        model2.add(Dense(200))
        model2.add(Activation(activation))
        model2.add(Dense(200))
        model2.add(Activation(activation))
        model2.add(Dense(10))
        # output log probability, used for black-box attack
        if use_softmax:
            model2.add(Activation('softmax'))
        if restore:
            model2.load_weights(restore_clf)

        layer_outputs = []
        for layer in model1.layers:
            if isinstance(layer, Conv2D) or isinstance(layer, Dense):
                layer_outputs.append(K.function([model1.layers[0].input], [layer.output]))
        for layer in model2.layers:
            if isinstance(layer, Conv2D) or isinstance(layer, Dense):
                layer_outputs.append(K.function([model2.layers[0].input], [layer.output]))

        model = Sequential()
        model.add(model1)
        model.add(model2)
        self.model = model
        self.layer_outputs = layer_outputs