Example #1
0
 def test_shape(self):
     input_tensor = tf.constant(1, np.float32,
                                (self.bsize, self.iheight, self.iwidth, 1))
     conv1 = mf.convolution_2d_layer(input_tensor, [4, 4, 1, 1], [2, 2],
                                     "VALID", 0.0, "conv1")
     conv2 = mf.convolution_2d_layer(conv1, [4, 4, 1, 1], [2, 2], "VALID",
                                     0.0, "conv2")
     print(conv1.get_shape())
     print(conv2.get_shape())
     exit(1)
def test_infer_size(label):
    conv1 = mf.convolution_2d_layer(label, [3, 3, 1, 1], [2, 2], 'VALID', 0.0,
                                    'conv1')
    print(conv1)
    conv2 = mf.convolution_2d_layer(conv1, [3, 3, 1, 1], [2, 2], 'VALID', 0.0,
                                    'conv2')
    print(conv2)
    #conv3 = mf.convolution_2d_layer(conv2, [3,3,1,1], [2,2], 'VALID', 0.0, 'conv3')
    #print(conv3)
    #conv4 = mf.convolution_2d_layer(conv3, [3,3,1,1], [2,2], 'VALID', 0.0, 'conv4')
    #print(conv4)
    #conv5 = mf.convolution_2d_layer(conv4, [3,3,1,1], [2,2], 'VALID', 0.0, 'conv5')
    #print(conv5)
    exit(1)
def conv_bottle_net(
            inputs, 
            bottlenet_channel, 
            filters, 
            data_format, 
            is_train, 
            leaky_param, 
            wd):

    bottlenet = mf.convolution_2d_layer(inputs, bottlenet_channel, [1, 1], [1, 1], "SAME",
                                        data_format, True, is_train, leaky_param, wd, 'bottlenet')

    conv = mf.convolution_2d_layer(bottlenet, filters, [3, 3], [1, 1], "SAME",
                                     data_format, True, is_train, leaky_param, wd, 'conv')
    return conv
def dense_transition_layer(input_tensor, is_pool, data_format, is_train,
                           leaky_param, wd, layer_name):
    with tf.variable_scope(layer_name):
        if data_format == "NCHW":
            in_channle = input_tensor.get_shape()[1]
        elif data_format == "NHWC":
            in_channle = input_tensor.get_shape()[3]
        else:
            raise NotImplementedError

        if not is_pool:
            conv = mf.convolution_2d_layer(input_tensor, in_channle, [3, 3], [2, 2], "SAME",
                    data_format, True, is_train, leaky_param, wd, "conv")
        else:
            conv = mf.convolution_2d_layer(input_tensor, in_channle, [1, 1], [1, 1], "SAME",
                    data_format, True, is_train, leaky_param, wd, "conv")
            conv = mf.maxpool_2d_layer(conv, [2, 2], [2, 2], data_format, 'maxpool')
        return conv
Example #5
0
def inference(image, output_shape, keep_prob, is_train):
    wd = 0.0001
    leaky_param = 0.01
    input_shape = image.get_shape().as_list()
    conv1 = mf.convolution_2d_layer(image, [5, 5, 3, 64], [1, 1], 'SAME', wd,
                                    'conv1')
    conv1_pool = mf.maxpool_2d_layer(conv1, [2, 2], [2, 2], 'maxpool1')

    res1 = mf.copy_layer(conv1_pool, mf.res_layer, 3, 'res', [3, 3, 64, 64],
                         [1, 1], "SAME", wd, 'res_1', 2, leaky_param, is_train)
    res1_pool = mf.maxpool_2d_layer(res1, [2, 2], [2, 2], 'maxpool2')
    res1_pad = mf.res_pad(res1_pool, 64, 128, 'res_pad1')

    res2 = mf.copy_layer(res1_pad, mf.res_layer, 4, 'res', [3, 3, 128, 128],
                         [1, 1], "SAME", wd, 'res_2', 2, leaky_param, is_train)
    res2_pool = mf.maxpool_2d_layer(res2, [2, 2], [2, 2], 'maxpool2')
    res2_pad = mf.res_pad(res2_pool, 128, 256, 'res_pad2')

    res3 = mf.copy_layer(res2_pad, mf.res_layer, 6, 'res', [3, 3, 256, 256],
                         [1, 1], "SAME", wd, 'res_3', 2, leaky_param, is_train)
    #res3_pool = mf.maxpool_2d_layer(res3, [2,2], [2,2], 'maxpool3')
    #res3_pad = mf.res_pad(res3_pool, 256, 512, 'res_pad3')

    #res4 = mf.copy_layer(res3_pad, mf.res_layer, 3, 'res', [3,3, 512, 512], [1,1], "SAME", wd, 'res_4', 2, leaky_param, is_train)

    res1_unpool = mf.unpooling_layer(res1, [input_shape[1], input_shape[2]],
                                     'res1_unpool')
    res2_unpool = mf.unpooling_layer(res2, [input_shape[1], input_shape[2]],
                                     'res2_unpool')
    res3_unpool = mf.unpooling_layer(res3, [input_shape[1], input_shape[2]],
                                     'res3_unpool')
    #res4_unpool = mf.unpooling_layer(res4, [input_shape[1], input_shape[2]], 'res4_unpool')
    hypercolumn = tf.concat(3, [res1_unpool, res2_unpool, res3_unpool],
                            name='hypercolumn')

    output_shape = input_shape
    output_shape[3] = 1
    #heatmap = mf.deconvolution_2d_layer(hypercolumn, [1,1,1,hypercolumn.get_shape()[3]], [1,1], output_shape, 'SAME', wd, 'heatmap')
    heatmap = mf.convolution_2d_layer(
        hypercolumn, [1, 1, hypercolumn.get_shape()[3], 1], [1, 1], 'SAME', wd,
        'heatmap')
    return heatmap
Example #6
0
    def _add_adapt_pair_loss_single(self, feature, scale, leaky_param,
                                    wd, layer_name):
        with tf.variable_scope(layer_name):
            feat = flip_gradient(feature, scale)
            keep_prob_ph = self.keep_prob_ph
            data_format = "NHWC"

            conv1 = mf.convolution_2d_layer(
                feat, 256, [3, 3], [1, 1],
                "SAME", data_format=data_format, leaky_params=leaky_param,
                wd=wd, layer_name="dann_conv1")

            conv2 = mf.convolution_2d_layer(
                conv1, 512, [3, 3], [1, 1],
                "SAME", data_format=data_format, leaky_params=leaky_param,
                wd=wd, layer_name="dann_conv2")

            conv3 = mf.convolution_2d_layer(
                conv2, 512, [3, 3], [1, 1],
                "SAME", data_format=data_format, leaky_params=leaky_param,
                wd=wd, layer_name="dann_conv3")

            conv3_maxpool = mf.maxpool_2d_layer(
                conv3, [2, 2], [2, 2], data_format=data_format,
                layer_name="dann_maxpool3")

            a_fc1 = mf.fully_connected_layer(
                conv3_maxpool, 2048, leaky_param, wd, "a_fc1")

            a_fc2 = mf.fully_connected_layer(
                a_fc1, 2048, leaky_param, wd, "a_fc2")

            a_fc3 = mf.fully_connected_layer(
                a_fc2, 2, leaky_param, wd, "a_fc3")

        return a_fc3
Example #7
0
    def model(self):
        wd = 0.0004
        leaky_param = 0.01
        with tf.variable_scope("G"):
            rf2 = mf.add_leaky_relu(
                mf.fully_connected_layer(self.ran_code_ph, 8 * 8 * 64, wd,
                                         "fc2"), leaky_param)
            rf3 = tf.reshape(rf2, [self.bsize, 8, 8, 64], name="fc3")
            rdeconv1 = mf.add_leaky_relu(
                mf.deconvolution_2d_layer(rf3, [2, 2, 128, 64], [2, 2],
                                          [self.bsize, 16, 16, 128], "VALID",
                                          wd, "deconv1"), leaky_param)

            rdeconv2 = mf.add_leaky_relu(
                mf.deconvolution_2d_layer(rdeconv1, [2, 2, 256, 128], [2, 2],
                                          [self.bsize, 32, 32, 256], "VALID",
                                          wd, "deconv2"), leaky_param)
            deconv1 = mf.add_leaky_relu(
                mf.deconvolution_2d_layer(rdeconv2, [2, 2, 512, 256], [2, 2],
                                          [self.bsize, 64, 64, 512], "VALID",
                                          wd, "deconv3"), leaky_param)

            deconv2 = mf.add_leaky_relu(
                mf.deconvolution_2d_layer(deconv1, [2, 2, 512, 512], [2, 2],
                                          [self.bsize, 128, 128, 512], "VALID",
                                          wd, "deconv4"), leaky_param)
            conv1 = mf.convolution_2d_layer(deconv2, [1, 1, 512, 1], [1, 1],
                                            "SAME", wd, "conv1")

            self.g_image = tf.sigmoid(conv1, "g_image")

            tf.add_to_collection("image_to_write", self.g_image)
            tf.add_to_collection("image_to_write", self.image_data_ph)

        with tf.variable_scope("D"):
            concat = tf.concat(0, [self.g_image, self.image_data_ph])
            #conv1 = mf.convolution_2d_layer(self.image_data_ph, [5, 5, 2, 64], [2,2], "VALID", wd, "conv1")
            conv1 = mf.add_leaky_relu(
                mf.convolution_2d_layer(concat, [3, 3, 1, 32], [2, 2], "SAME",
                                        wd, "conv1"), leaky_param)
            conv1_maxpool = mf.maxpool_2d_layer(conv1, [2, 2], [2, 2],
                                                "maxpool1")

            conv2 = mf.add_leaky_relu(
                mf.convolution_2d_layer(conv1_maxpool, [3, 3, 32, 64], [2, 2],
                                        "SAME", wd, "conv2"), leaky_param)
            conv2_maxpool = mf.maxpool_2d_layer(conv2, [2, 2], [2, 2],
                                                "maxpool2")

            conv3 = mf.add_leaky_relu(
                mf.convolution_2d_layer(conv2, [3, 3, 64, 128], [2, 2], "SAME",
                                        wd, "conv3"), leaky_param)
            conv3_maxpool = mf.maxpool_2d_layer(conv3, [2, 2], [2, 2],
                                                "maxpool3")

            conv4 = mf.add_leaky_relu(
                mf.convolution_2d_layer(conv3_maxpool, [3, 3, 128, 128],
                                        [2, 2], "SAME", wd, "conv4"),
                leaky_param)
            conv4_maxpool = mf.maxpool_2d_layer(conv4, [2, 2], [2, 2],
                                                "maxpool4")
            self.fc = mf.fully_connected_layer(conv4_maxpool, 1, wd, "fc")
Example #8
0
    def model_infer(self, data_ph, model_params):
        input_ph = data_ph.get_input()
        gl_ph = data_ph.get_gl()
        train_test_ph = data_ph.get_train_test()

        input_ph = tf.cond(train_test_ph, lambda: self.add_data_arg(
            input_ph), lambda: input_ph)

        data_format = "NHWC"
        bn = False
        keep_prob_ph = data_ph.get_keep_prob()
        self.keep_prob_ph = keep_prob_ph

        b, _, _, _ = input_ph.get_shape()
        self.b = b

        leaky_param = model_params["leaky_param"]
        wd = model_params["weight_decay"]

        hyper_list = list()

        print(input_ph)
        conv11 = mf.convolution_2d_layer(
            input_ph, 64, [3, 3], [1, 1], "SAME", data_format=data_format,
            leaky_params=leaky_param, wd=wd, layer_name="conv11")
        print(conv11)

        conv1_maxpool = mf.maxpool_2d_layer(conv11, [2, 2],
                                            [2, 2], data_format, "maxpool1")
        print(conv1_maxpool)

        conv21 = mf.convolution_2d_layer(
            conv1_maxpool, 128, [3, 3], [1, 1], "SAME", data_format=data_format,
            leaky_params=leaky_param, wd=wd, layer_name="conv21")
        print(conv21)

        conv2_maxpool = mf.maxpool_2d_layer(conv21, [2, 2],
                                            [2, 2], data_format, "maxpool2")
        print(conv2_maxpool)

        conv31 = mf.convolution_2d_layer(
            conv2_maxpool, 256, [3, 3], [1, 1], "SAME", data_format=data_format,
            leaky_params=leaky_param, wd=wd, layer_name="conv31")

        print(conv31)

        if model_params['adapt']:
            if self.model_params["adapt_loss_type"] == "MULTI":
                self._add_adapt_multi_loss(conv31,
                                           gl_ph, leaky_param, wd)
            elif self.model_params["adapt_loss_type"] == "PAIR":
                self._add_adapt_pair_loss(conv31,
                                          gl_ph, leaky_param, wd)
            else:
                raise NotImplementedError

        conv3_maxpool = mf.maxpool_2d_layer(
                conv31, [2, 2], [2, 2], data_format, "maxpool3")

        conv41 = mf.convolution_2d_layer(
            conv3_maxpool, 256, [3, 3], [1, 1], "SAME", data_format=data_format,
            leaky_params=leaky_param, wd=wd, layer_name="conv4")

        fc1 = mf.fully_connected_layer(
            conv41, 2048, leaky_param, wd, "fc1")

        fc1_drop = tf.nn.dropout(fc1, keep_prob_ph, name="dropout1")

        fc2 = mf.fully_connected_layer(
            fc1_drop, 1024, leaky_param, wd, "fc2")

        fc2_drop = tf.nn.dropout(fc2, keep_prob_ph, name="dropout2")

        fc3 = mf.fully_connected_layer(fc2_drop, 10, 0.0, wd, "fc3")

        self.fc = fc3
def inference(feature, output_shape, keep_prob, is_train):
    b, h, w, c = feature.get_shape().as_list()
    wd = 0.0004
    leaky_param = 0.01
    f_select = mf.convolution_2d_layer(feature, [1, 1, c, 1024], [1, 1],
                                       "SAME", wd, 'f_select')

    deconv11 = mf.deconvolution_2d_layer(f_select, [3, 3, 256, 1024], [1, 1],
                                         [b, 56, 56, 256], 'SAME', wd,
                                         'deconv11')
    deconv11_relu = mf.add_leaky_relu(deconv11, leaky_param)

    deconv12 = mf.deconvolution_2d_layer(deconv11_relu, [3, 3, 256, 256],
                                         [1, 1], [b, 56, 56, 256], 'SAME', wd,
                                         'deconv12')
    deconv12_relu = mf.add_leaky_relu(deconv12, leaky_param)

    deconv21 = mf.deconvolution_2d_layer(deconv12_relu, [3, 3, 128, 256],
                                         [2, 2], [b, 113, 113, 128], 'VALID',
                                         wd, 'deconv21')
    deconv21_relu = mf.add_leaky_relu(deconv21, leaky_param)
    deconv22 = mf.deconvolution_2d_layer(deconv21_relu, [3, 3, 128, 128],
                                         [1, 1], [b, 113, 113, 128], 'SAME',
                                         wd, 'deconv22')
    deconv22_relu = mf.add_leaky_relu(deconv22, leaky_param)

    deconv31 = mf.deconvolution_2d_layer(deconv22_relu, [3, 3, 64, 128],
                                         [2, 2], [b, 227, 227, 64], 'VALID',
                                         wd, 'deconv31')
    deconv31_relu = mf.add_leaky_relu(deconv31, leaky_param)

    #deconv32 = mf.deconvolution_2d_layer(deconv31_relu, [3, 3, 1, 64], [1,1], [b, 227, 227, 1], 'SAME', wd, 'deconv32')
    #deconv32_relu = mf.add_leaky_relu(deconv32, leaky_param = 0.0)

    conv1 = mf.convolution_2d_layer(deconv31_relu, [3, 3, 64, 3], [1, 1],
                                    "SAME", wd, 'conv3x3')
    conv1_relu = mf.add_leaky_relu(conv1, leaky_param)

    conv2 = mf.convolution_2d_layer(conv1_relu, [1, 1, 3, 1], [1, 1], "SAME",
                                    wd, 'conv1x1')
    #conv2_sig = tf.sigmoid(conv2, name = 'conv2_sig')
    conv2_relu = mf.add_leaky_relu(conv2, leaky_param=0.0)

    #feature_pad = tf.pad(feature, [[0,0],[9,9],[9,9],[0, 0]])
    #deconv11 = mf.deconvolution_2d_layer(feature_pad, [3, 3, 128, c], [1,1], [b, 74, 74, 128], 'SAME', wd, 'deconv11')
    #deconv11_relu = mf.add_leaky_relu(deconv11, leaky_param)

    #deconv12 = mf.deconvolution_2d_layer(deconv11_relu, [3, 3, 128, 128], [1,1], [b, 74, 74, 128], 'SAME', wd, 'deconv12')
    #deconv12_relu = mf.add_leaky_relu(deconv12, leaky_param)

    #deconv21 = mf.deconvolution_2d_layer(deconv12_relu, [3, 3, 64, 128], [2,2], [b, 149, 149, 64], 'VALID', wd, 'deconv21')
    #deconv21_relu = mf.add_leaky_relu(deconv21, leaky_param)
    #deconv22 = mf.deconvolution_2d_layer(deconv21_relu, [3, 3, 64, 64], [1,1], [b, 149, 149, 64], 'SAME', wd, 'deconv22')
    #deconv22_relu = mf.add_leaky_relu(deconv22, leaky_param)
    #
    #deconv31 = mf.deconvolution_2d_layer(deconv22_relu, [3, 3, 32, 64], [2,2], [b, 299, 299, 32], 'VALID', wd, 'deconv31')
    #deconv31_relu = mf.add_leaky_relu(deconv31, leaky_param)
    #deconv32 = mf.deconvolution_2d_layer(deconv31_relu, [3, 3, 1, 32], [1,1], [b, 299, 299, 1], 'SAME', wd, 'deconv32')

    #fc1 = mf.fully_connected_layer(deconv32_relu, 1000, wd, "fc1")
    fc1 = mf.fully_connected_layer(conv2, 1000, wd, "fc1")
    fc1_relu = mf.add_leaky_relu(fc1, leaky_param)
    fc2 = mf.fully_connected_layer(fc1_relu, 1, wd, "fc2")
    fc2_relu = mf.add_leaky_relu(fc2, leaky_param)

    return conv2_relu, fc2_relu