Example #1
0
def LapSRNSingleLevel(net_image, net_feature, reuse=False):
    with tf.variable_scope("Model_level", reuse=reuse):
        tl.layers.set_name_reuse(reuse)

        net_tmp = net_feature
        # recursive block
        for d in range(config.model.resblock_depth):
            net_tmp = PReluLayer(net_tmp, name='prelu_D%s' % (d))
            net_tmp = Conv2dLayer(
                net_tmp,
                shape=[3, 3, 64, 64],
                strides=[1, 1, 1, 1],
                name='conv_D%s' % (d),
                W_init=tf.contrib.layers.xavier_initializer())

        # for r in range(1,config.model.recursive_depth):
        #     for d in range(config.model.resblock_depth):
        #         net_tmp = PReluLayer(net_tmp, name='prelu_R%s_D%s'%(r,d))
        #         net_tmp = Conv2dLayer(net_tmp,shape=[3,3,64,64],strides=[1,1,1,1],
        #                 name='conv_R%s_D%s'%(r,d), W_init=tf.contrib.layers.xavier_initializer())

        net_feature = ElementwiseLayer(layer=[net_feature, net_tmp],
                                       combine_fn=tf.add,
                                       name='add_feature')

        net_feature = PReluLayer(net_feature, name='prelu_feature')
        net_feature = Conv2dLayer(
            net_feature,
            shape=[3, 3, 64, 256],
            strides=[1, 1, 1, 1],
            name='upconv_feature',
            W_init=tf.contrib.layers.xavier_initializer())
        net_feature = SubpixelConv2d(net_feature,
                                     scale=2,
                                     n_out_channel=64,
                                     name='subpixel_feature')

        # add image back
        gradient_level = Conv2dLayer(
            net_feature,
            shape=[3, 3, 64, 3],
            strides=[1, 1, 1, 1],
            act=lrelu,
            name='grad',
            W_init=tf.contrib.layers.xavier_initializer())
        net_image = Conv2dLayer(net_image,
                                shape=[3, 3, 3, 12],
                                strides=[1, 1, 1, 1],
                                name='upconv_image',
                                W_init=tf.contrib.layers.xavier_initializer())
        net_image = SubpixelConv2d(net_image,
                                   scale=2,
                                   n_out_channel=3,
                                   name='subpixel_image')
        net_image = ElementwiseLayer(layer=[gradient_level, net_image],
                                     combine_fn=tf.add,
                                     name='add_image')

    return net_image, net_feature, gradient_level
Example #2
0
    def build_classifier(self, im, inf_norm, reuse=False):
        with tf.variable_scope('C', reuse=reuse) as vs:
            tensorlayer.layers.set_name_reuse(reuse)
            
            x = tf.reshape(im, [-1, 3, 32, 32])
            x = tf.transpose(x, [0, 2, 3, 1])
            
            xmin = tf.clip_by_value(x - inf_norm, 0., 1.)
            xmax = tf.clip_by_value(x + inf_norm, 0., 1.)
            x = tf.random_uniform(tf.shape(x), xmin, xmax, dtype=tf.float32)
            
            # Crop the central [height, width] of the image.
            # x = tf.image.resize_image_with_crop_or_pad(x, 24, 24)
            x = tf.map_fn(lambda frame: tf.image.per_image_standardization(frame), x)
            
            net = InputLayer(x)
            net = Conv2dLayer(net, \
                    act=tf.nn.relu, \
                    shape=[5,5,3,64], \
                    name="conv1")
            net = MaxPool2d(net, \
                    filter_size=(3,3), \
                    strides=(2,2), \
                    name="pool1")
            net = LocalResponseNormLayer(net, \
                    depth_radius=4, \
                    bias=1.0, \
                    alpha = 0.001/9.0, \
                    beta = 0.75, \
                    name="norm1")
            net = Conv2dLayer(net, \
                    act=tf.nn.relu, \
                    shape=[5,5,64,64], \
                    name="conv2")
            net = LocalResponseNormLayer(net, \
                    depth_radius=4, \
                    bias=1.0, \
                    alpha=0.001/9.0, \
                    beta = 0.75, \
                    name="norm2")
            net = MaxPool2d(net, \
                    filter_size=(3,3), \
                    strides=(2,2), \
                    name="pool2")
            net = FlattenLayer(net, name="flatten_1")
            net = DenseLayer(net, n_units=384, name="local3", act=tf.nn.relu)

            net = DenseLayer(net, n_units=192, name="local4", act=tf.nn.relu)
            net = DenseLayer(net, n_units=10, name="softmax_linear", act=tf.identity)

            cla_vars = tf.contrib.framework.get_variables(vs)
            def name_fixer(var):
                return var.op.name.replace("W", "weights") \
                                    .replace("b", "biases") \
                                    .replace("weights_conv2d", "weights") \
                                    .replace("biases_conv2d", "biases")
            cla_vars = {name_fixer(var): var for var in cla_vars}
            return net.outputs, cla_vars
Example #3
0
def cifar10_classifier(im, reuse):
    with tf.variable_scope('C', reuse=reuse) as vs:
        net = InputLayer(im)
        net = Conv2dLayer(net, \
                act=tf.nn.relu, \
                shape=[5,5,3,64], \
                name="conv1")
        net = MaxPool2d(net, \
                filter_size=(3,3), \
                strides=(2,2), \
                name="pool1")
        net = LocalResponseNormLayer(net, \
                depth_radius=4, \
                bias=1.0, \
                alpha = 0.001/9.0, \
                beta = 0.75, \
                name="norm1")
        net = Conv2dLayer(net, \
                act=tf.nn.relu, \
                shape=[5,5,64,64], \
                name="conv2")
        net = LocalResponseNormLayer(net, \
                depth_radius=4, \
                bias=1.0, \
                alpha=0.001/9.0, \
                beta = 0.75, \
                name="norm2")
        net = MaxPool2d(net, \
                filter_size=(3,3), \
                strides=(2,2), \
                name="pool2")
        net = FlattenLayer(net, name="flatten_1")
        net = DenseLayer(net, n_units=384, name="local3", act=tf.nn.relu)
        net = DenseLayer(net, n_units=192, name="local4", act=tf.nn.relu)
        net = DenseLayer(net,
                         n_units=10,
                         name="softmax_linear",
                         act=tf.identity)
        cla_vars = tf.contrib.framework.get_variables(vs)

        def name_fixer(var):
            return var.op.name.replace("W", "weights") \
                                .replace("b", "biases") \
                                .replace("weights_conv2d", "weights") \
                                .replace("biases_conv2d", "biases")

        cla_vars = {name_fixer(var): var for var in cla_vars}
        if not reuse:
            return net.outputs, tf.argmax(net.outputs, axis=1), cla_vars
        return net.outputs, tf.argmax(net.outputs, axis=1)
Example #4
0
    def build_classifier(self, im, inf_norm, reuse=False):
        with tf.variable_scope("C", reuse=reuse) as vs:
            x = tf.reshape(im, [-1, 64, 64, 3])
            xmin = tf.clip_by_value(x - inf_norm, 0., 1.)
            xmax = tf.clip_by_value(x + inf_norm, 0., 1.)
            x = tf.random_uniform(tf.shape(x), xmin, xmax, dtype=tf.float32)

            #x = tf.map_fn(lambda frame: tf.image.per_image_standardization(frame), x)

            net = InputLayer(x)
            n_filters = 3
            for i in range(2):
                net = Conv2dLayer(net, \
                        act=tf.nn.relu, \
                        shape=[5,5,n_filters,64], \
                        name="conv_" + str(i))
                net = MaxPool2d(net, \
                        filter_size=(3,3), \
                        strides=(2,2), \
                        name="mpool_" + str(i))
                net = LocalResponseNormLayer(net, \
                        depth_radius=4, \
                        bias=1.0, \
                        alpha=0.001 / 9.0, \
                        beta=0.75, \
                        name="lrn_" + str(i))
                n_filters = 64
            net = FlattenLayer(net)
            net = DenseLayer(net, n_units=384, act=tf.nn.relu, name="d1")
            net = DenseLayer(net, n_units=192, act=tf.nn.relu, name="d2")
            net = DenseLayer(net, n_units=2, act=tf.identity, name="final")
            cla_vars = tf.contrib.framework.get_variables(vs)
        return net.outputs, cla_vars
Example #5
0
def celebA_classifier(ims, reuse):
    with tf.variable_scope("C", reuse=reuse) as vs:
        net = InputLayer(ims)
        n_filters = 3
        for i in range(2):
            net = Conv2dLayer(net, \
                    act=tf.nn.relu, \
                    shape=[5,5,n_filters,64], \
                    name="conv_" + str(i))
            net = MaxPool2d(net, \
                    filter_size=(3,3), \
                    strides=(2,2), \
                    name="mpool_" + str(i))
            net = LocalResponseNormLayer(net, \
                    depth_radius=4, \
                    bias=1.0, \
                    alpha=0.001 / 9.0, \
                    beta=0.75, \
                    name="lrn_" + str(i))
            n_filters = 64
        net = FlattenLayer(net)
        net = DenseLayer(net, n_units=384, act=tf.nn.relu, name="d1")
        net = DenseLayer(net, n_units=192, act=tf.nn.relu, name="d2")
        net = DenseLayer(net, n_units=2, act=tf.identity, name="final")
        cla_vars = tf.contrib.framework.get_variables(vs)
        if not reuse:
            return net.outputs, tf.argmax(net.outputs, axis=1), cla_vars
    return net.outputs, tf.argmax(net.outputs, axis=1)
Example #6
0
def conv2d(layer, out_channels, filter_size=3, stride=1, act=tf.identity, padding='SAME', W_init=w_init, b_init=b_init, name='conv2d'):
    in_channels = layer.outputs.get_shape().as_list()[-1]
    return Conv2dLayer(layer = layer,
                act = act,
                shape = [filter_size, filter_size, in_channels, out_channels],
                strides=[1, stride, stride, 1],
                padding=padding,
                W_init = w_init,
                b_init = b_init,
                name =name)   
Example #7
0
def LapSRN(inputs, is_train=False, reuse=False):
    n_level = int(np.log2(config.model.scale))
    if not n_level >= 1:
        raise ValueError('n_level >=1 expected but not satisfied')

    with tf.variable_scope("LapSRN", reuse=reuse) as vs:
        tl.layers.set_name_reuse(reuse)

        shapes = tf.shape(inputs)
        inputs_level = InputLayer(inputs, name='input_level')

        net_feature = Conv2dLayer(
            inputs_level,
            shape=[3, 3, 3, 64],
            strides=[1, 1, 1, 1],
            W_init=tf.contrib.layers.xavier_initializer(),
            name='init_conv')
        net_image = inputs_level
        net_image1, net_feature1, net_gradient1 = LapSRNSingleLevel(
            net_image, net_feature, reuse=reuse)
        net_image2, net_feature2, net_gradient2 = LapSRNSingleLevel(
            net_image1, net_feature1, reuse=True)

    return net_image2, net_gradient2, net_image1, net_gradient1
Example #8
0
    def conv_layers(net_in):
        with tf.name_scope('preprocess'):
            # Notice that we include a preprocessing layer that takes the RGB image
            # with pixels values in the range of 0-255 and subtracts the mean image
            # values (calculated over the entire ImageNet training set).
            mean = tf.constant([123.68, 116.779, 103.939],
                               dtype=tf.float32,
                               shape=[1, 1, 1, 3],
                               name='img_mean')
            net_in.outputs = net_in.outputs - mean

        # conv1
        net = Conv2dLayer(
            net_in,
            act=tf.nn.relu,
            shape=[3, 3, 3, 64],  # 64 features for each 3x3 patch
            strides=[1, 1, 1, 1],
            padding='SAME',
            name='conv1_1')
        net = Conv2dLayer(
            net,
            act=tf.nn.relu,
            shape=[3, 3, 64, 64],  # 64 features for each 3x3 patch
            strides=[1, 1, 1, 1],
            padding='SAME',
            name='conv1_2')
        net = PoolLayer(net,
                        ksize=[1, 2, 2, 1],
                        strides=[1, 2, 2, 1],
                        padding='SAME',
                        pool=tf.nn.max_pool,
                        name='pool1')

        # conv2
        net = Conv2dLayer(
            net,
            act=tf.nn.relu,
            shape=[3, 3, 64, 128],  # 128 features for each 3x3 patch
            strides=[1, 1, 1, 1],
            padding='SAME',
            name='conv2_1')
        net = Conv2dLayer(
            net,
            act=tf.nn.relu,
            shape=[3, 3, 128, 128],  # 128 features for each 3x3 patch
            strides=[1, 1, 1, 1],
            padding='SAME',
            name='conv2_2')
        net = PoolLayer(net,
                        ksize=[1, 2, 2, 1],
                        strides=[1, 2, 2, 1],
                        padding='SAME',
                        pool=tf.nn.max_pool,
                        name='pool2')

        # conv3
        net = Conv2dLayer(
            net,
            act=tf.nn.relu,
            shape=[3, 3, 128, 256],  # 256 features for each 3x3 patch
            strides=[1, 1, 1, 1],
            padding='SAME',
            name='conv3_1')
        net = Conv2dLayer(
            net,
            act=tf.nn.relu,
            shape=[3, 3, 256, 256],  # 256 features for each 3x3 patch
            strides=[1, 1, 1, 1],
            padding='SAME',
            name='conv3_2')
        net = Conv2dLayer(
            net,
            act=tf.nn.relu,
            shape=[3, 3, 256, 256],  # 256 features for each 3x3 patch
            strides=[1, 1, 1, 1],
            padding='SAME',
            name='conv3_3')
        net = PoolLayer(net,
                        ksize=[1, 2, 2, 1],
                        strides=[1, 2, 2, 1],
                        padding='SAME',
                        pool=tf.nn.max_pool,
                        name='pool3')

        # conv4
        net = Conv2dLayer(
            net,
            act=tf.nn.relu,
            shape=[3, 3, 256, 512],  # 512 features for each 3x3 patch
            strides=[1, 1, 1, 1],
            padding='SAME',
            name='conv4_1')
        net = Conv2dLayer(
            net,
            act=tf.nn.relu,
            shape=[3, 3, 512, 512],  # 512 features for each 3x3 patch
            strides=[1, 1, 1, 1],
            padding='SAME',
            name='conv4_2')
        net = Conv2dLayer(
            net,
            act=tf.nn.relu,
            shape=[3, 3, 512, 512],  # 512 features for each 3x3 patch
            strides=[1, 1, 1, 1],
            padding='SAME',
            name='conv4_3')
        net = PoolLayer(net,
                        ksize=[1, 2, 2, 1],
                        strides=[1, 2, 2, 1],
                        padding='SAME',
                        pool=tf.nn.max_pool,
                        name='pool4')

        # conv5
        net = Conv2dLayer(
            net,
            act=tf.nn.relu,
            shape=[3, 3, 512, 512],  # 512 features for each 3x3 patch
            strides=[1, 1, 1, 1],
            padding='SAME',
            name='conv5_1')
        net = Conv2dLayer(
            net,
            act=tf.nn.relu,
            shape=[3, 3, 512, 512],  # 512 features for each 3x3 patch
            strides=[1, 1, 1, 1],
            padding='SAME',
            name='conv5_2')
        net = Conv2dLayer(
            net,
            act=tf.nn.relu,
            shape=[3, 3, 512, 512],  # 512 features for each 3x3 patch
            strides=[1, 1, 1, 1],
            padding='SAME',
            name='conv5_3')
        net = PoolLayer(net,
                        ksize=[1, 2, 2, 1],
                        strides=[1, 2, 2, 1],
                        padding='SAME',
                        pool=tf.nn.max_pool,
                        name='pool5')
        return net
Example #9
0
 def _build_arch(self, net_in):
     network = Conv2dLayer(net_in,
                           act=tf.nn.relu,
                           shape=[3, 3, 3, 64],
                           strides=[1, 1, 1, 1],
                           padding='SAME',
                           name=self.name + '_conv1_1')
     network = Conv2dLayer(network,
                           act=tf.nn.relu,
                           shape=[3, 3, 64, 64],
                           strides=[1, 1, 1, 1],
                           padding='SAME',
                           name=self.name + '_conv1_2')
     network = PoolLayer(network,
                         ksize=[1, 2, 2, 1],
                         strides=[1, 2, 2, 1],
                         padding='SAME',
                         pool=tf.nn.max_pool,
                         name=self.name + '_pool1')
     network = Conv2dLayer(network,
                           act=tf.nn.relu,
                           shape=[3, 3, 64, 128],
                           strides=[1, 1, 1, 1],
                           padding='SAME',
                           name=self.name + '_conv2_1')
     network = Conv2dLayer(network,
                           act=tf.nn.relu,
                           shape=[3, 3, 128, 128],
                           strides=[1, 1, 1, 1],
                           padding='SAME',
                           name=self.name + '_conv2_2')
     network = PoolLayer(network,
                         ksize=[1, 2, 2, 1],
                         strides=[1, 2, 2, 1],
                         padding='SAME',
                         pool=tf.nn.max_pool,
                         name=self.name + '_pool2')
     network = Conv2dLayer(network,
                           act=tf.nn.relu,
                           shape=[3, 3, 128, 256],
                           strides=[1, 1, 1, 1],
                           padding='SAME',
                           name=self.name + '_conv3_1')
     network = Conv2dLayer(network,
                           act=tf.nn.relu,
                           shape=[3, 3, 256, 256],
                           strides=[1, 1, 1, 1],
                           padding='SAME',
                           name=self.name + '_conv3_2')
     network = Conv2dLayer(network,
                           act=tf.nn.relu,
                           shape=[3, 3, 256, 256],
                           strides=[1, 1, 1, 1],
                           padding='SAME',
                           name=self.name + '_conv3_3')
     network = Conv2dLayer(network,
                           act=tf.nn.relu,
                           shape=[3, 3, 256, 256],
                           strides=[1, 1, 1, 1],
                           padding='SAME',
                           name=self.name + '_conv3_4')
     network = PoolLayer(network,
                         ksize=[1, 2, 2, 1],
                         strides=[1, 2, 2, 1],
                         padding='SAME',
                         pool=tf.nn.max_pool,
                         name=self.name + '_pool3')
     network = Conv2dLayer(network,
                           act=tf.nn.relu,
                           shape=[3, 3, 256, 512],
                           strides=[1, 1, 1, 1],
                           padding='SAME',
                           name=self.name + '_conv4_1')
     network = Conv2dLayer(network,
                           act=tf.nn.relu,
                           shape=[3, 3, 512, 512],
                           strides=[1, 1, 1, 1],
                           padding='SAME',
                           name=self.name + '_conv4_2')
     network = Conv2dLayer(network,
                           act=tf.nn.relu,
                           shape=[3, 3, 512, 512],
                           strides=[1, 1, 1, 1],
                           padding='SAME',
                           name=self.name + '_conv4_3')
     network = Conv2dLayer(network,
                           act=tf.nn.relu,
                           shape=[3, 3, 512, 512],
                           strides=[1, 1, 1, 1],
                           padding='SAME',
                           name=self.name + '_conv4_4')
     network = PoolLayer(network,
                         ksize=[1, 2, 2, 1],
                         strides=[1, 2, 2, 1],
                         padding='SAME',
                         pool=tf.nn.max_pool,
                         name=self.name + '_pool4')
     network = Conv2dLayer(network,
                           act=tf.nn.relu,
                           shape=[3, 3, 512, 512],
                           strides=[1, 1, 1, 1],
                           padding='SAME',
                           name=self.name + '_conv5_1')
     network = Conv2dLayer(network,
                           act=tf.nn.relu,
                           shape=[3, 3, 512, 512],
                           strides=[1, 1, 1, 1],
                           padding='SAME',
                           name=self.name + '_conv5_2')
     network = Conv2dLayer(network,
                           act=tf.nn.relu,
                           shape=[3, 3, 512, 512],
                           strides=[1, 1, 1, 1],
                           padding='SAME',
                           name=self.name + '_conv5_3')
     network = Conv2dLayer(network,
                           act=tf.nn.relu,
                           shape=[3, 3, 512, 512],
                           strides=[1, 1, 1, 1],
                           padding='SAME',
                           name=self.name + '_conv5_4')
     network = PoolLayer(network,
                         ksize=[1, 2, 2, 1],
                         strides=[1, 2, 2, 1],
                         padding='SAME',
                         pool=tf.nn.max_pool,
                         name=self.name + '_pool5')
     network = FlattenLayer(network, name=self.name + '_flatten')
     network = DenseLayer(network,
                          n_units=4096,
                          act=tf.nn.relu,
                          name=self.name + '_fc6')
     network = DenseLayer(network,
                          n_units=4096,
                          act=tf.nn.relu,
                          name=self.name + '_fc7')
     network = DenseLayer(network,
                          n_units=1000,
                          act=tf.identity,
                          name=self.name + '_fc8')
     return network