Ejemplo n.º 1
0
    def _generator(self, z, dims, train_phase, activation=tf.nn.relu, scope_name="generator"):
        N = len(dims)
        image_size = self.resized_image_size // (2 ** (N - 1))
        with tf.variable_scope(scope_name) as scope:
            W_z = utils.weight_variable([self.z_dim, dims[0] * image_size * image_size], name="W_z")
            b_z = utils.bias_variable([dims[0] * image_size * image_size], name="b_z")
            h_z = tf.matmul(z, W_z) + b_z
            h_z = tf.reshape(h_z, [-1, image_size, image_size, dims[0]])
            h_bnz = utils.batch_norm(h_z, dims[0], train_phase, scope="gen_bnz")
            h = activation(h_bnz, name='h_z')
            utils.add_activation_summary(h)

            for index in range(N - 2):
                image_size *= 2
                W = utils.weight_variable([5, 5, dims[index + 1], dims[index]], name="W_%d" % index)
                b = utils.bias_variable([dims[index + 1]], name="b_%d" % index)
                deconv_shape = tf.stack([tf.shape(h)[0], image_size, image_size, dims[index + 1]])
                h_conv_t = utils.conv2d_transpose_strided(h, W, b, output_shape=deconv_shape)
                h_bn = utils.batch_norm(h_conv_t, dims[index + 1], train_phase, scope="gen_bn%d" % index)
                h = activation(h_bn, name='h_%d' % index)
                utils.add_activation_summary(h)

            image_size *= 2
            W_pred = utils.weight_variable([5, 5, dims[-1], dims[-2]], name="W_pred")
            b_pred = utils.bias_variable([dims[-1]], name="b_pred")
            deconv_shape = tf.stack([tf.shape(h)[0], image_size, image_size, dims[-1]])
            h_conv_t = utils.conv2d_transpose_strided(h, W_pred, b_pred, output_shape=deconv_shape)
            pred_image = tf.nn.tanh(h_conv_t, name='pred_image')
            utils.add_activation_summary(pred_image)

        return pred_image
Ejemplo n.º 2
0
def generator_net(x, training, opts, name='Generator'):
    with tf.variable_scope(name, reuse=tf.AUTO_REUSE):
        sz = opts.img_size // 16
        # x is input. 1 x 1 x nz
        # state size. sz x sz x (ngf*8)
        y = deconv(x, opts.ngf*8, sz, 1, 'valid', 'deconv1')
        y = batch_norm(y, training)
        y = relu(y)

        # state size. (sz*2) x (sz*2) x (ngf*4)
        y = deconv(y, opts.ngf*4, 4, 2, 'same', 'deconv2')
        y = batch_norm(y, training)
        y = relu(y)

        # state size. (sz*4) x (sz*4) x (ngf*2)
        y = deconv(y, opts.ngf*2, 4, 2, 'same', 'deconv3')
        y = batch_norm(y, training)
        y = relu(y)

        # state size. (sz*8) x (sz*8) x ngf
        y = deconv(y, opts.ngf, 4, 2, 'same', 'deconv4')
        y = batch_norm(y, training)
        y = relu(y)

        # state size. (sz*16) x (sz*16) x nc
        y = deconv(y, opts.nc, 4, 2, 'same', 'deconv5')
        y = tf.nn.tanh(y)
        tf.logging.info("Generator output: {}".format(y))

        return y
Ejemplo n.º 3
0
    def f(input, params, stats, mode, prefix=''):
        x = F.conv2d(input, params[prefix + 'conv0'], padding=1)
        g0 = group(x, params, stats, prefix + 'group0', mode, 1)
        g1 = group(g0, params, stats, prefix + 'group1', mode, 2)
        g2 = group(g1, params, stats, prefix + 'group2', mode, 2)
        o = F.relu(batch_norm(g2, params, stats, prefix + 'bn', mode))
        o = F.avg_pool2d(o, 8, 1, 0)
        o = o.view(o.size(0), -1)
        o = F.linear(o, params[prefix + 'fc.weight'],
                     params[prefix + 'fc.bias'])
        #x_s = F.conv2d(input, params[prefix+'conv0_s'], padding=1)
        if stu_depth != 0:
            if opt.param_share:
                gs0 = group_student(x, params, stats, prefix + 'group0', mode,
                                    1, n_s)
                gs1 = group_student(gs0, params, stats, prefix + 'group1',
                                    mode, 2, n_s)
                gs2 = group_student(gs1, params, stats, prefix + 'group2',
                                    mode, 2, n_s)
            else:
                gs0 = group_student(x, params, stats, prefix + 'groups0', mode,
                                    1, n_s)
                gs1 = group_student(gs0, params, stats, prefix + 'groups1',
                                    mode, 2, n_s)
                gs2 = group_student(gs1, params, stats, prefix + 'groups2',
                                    mode, 2, n_s)

            os = F.relu(batch_norm(gs2, params, stats, prefix + 'bns', mode))
            os = F.avg_pool2d(os, 8, 1, 0)
            os = os.view(os.size(0), -1)
            os = F.linear(os, params[prefix + 'fcs.weight'],
                          params[prefix + 'fcs.bias'])
            return os, o, [g0, g1, g2, gs0, gs1, gs2]
        else:
            return o, [g0, g1, g2]
Ejemplo n.º 4
0
    def generative_res(self, images, reuse=False):
        img_size = self.run_flags.scale * self.run_flags.input_length
        with tf.variable_scope('generator') as scope:
            if reuse:
                scope.reuse_variables()

            if self.run_flags.run == 'train':
                is_training = True
            else:
                is_training = False

            if self.run_flags.run == 'gan':
                leak = 0.01
            else:
                leak = 0.2

            n = conv2d(images,
                       self.run_flags.input_length,
                       kernel=3,
                       stride=1,
                       name='g_res_conv1')
            n = tf.nn.relu(n)
            tmp = n

            for i in range(16):
                nn = conv2d(n,
                            self.run_flags.input_length,
                            3,
                            1,
                            name='g_resblock/%s/c1' % i)
                nn = batch_norm(nn, name='g_resblock/%s/b1' % i)
                nn = tf.nn.relu(nn)
                nn = conv2d(nn,
                            self.run_flags.input_length,
                            3,
                            1,
                            name='g_resblock/%s/c2' % i)
                nn = batch_norm(nn, name='g_resblock/%s/b2' % i)
                nn = tf.add(nn, n, name='g_resblock/%s/add' % i)
                n = nn

            n = conv2d(n,
                       self.run_flags.input_length,
                       3,
                       1,
                       name='g_res_conv2')
            n = batch_norm(n, name='g_res_bn2')
            n = tf.add(n, tmp, name='g_res_add2')

            n = conv2d(n,
                       2 * self.run_flags.input_length,
                       3,
                       1,
                       name='g_res_conv3')
            n = tf.image.resize_images(n, size=[img_size, img_size])

            n = conv2d(n, 3, 1, 1, name='g_res_conv4')
            n = tf.nn.tanh(n)

            return n
Ejemplo n.º 5
0
    def discriminative(self, images, reuse=False):
        with tf.variable_scope('discriminative') as scope:
            if reuse:
                scope.reuse_variables()

            if self.run_flags.run == 'train':
                is_training = True
            else:
                is_training = False

            conv1 = lrelu(batch_norm(conv2d(images, output_dim=64, kernel=7, stride=1, name='d_conv1'), \
                                     is_training=is_training, name='g_conv1_bn'))  # 128 x 128 x 64

            conv2 = lrelu(batch_norm(conv2d(conv1, output_dim=64, kernel=7, stride=2, name='d_conv2'), \
                                     is_training=is_training, name='g_conv2_bn'))  # 64 x 64 x 64

            conv3 = lrelu(batch_norm(conv2d(conv2, output_dim=32, kernel=3, stride=2, name='d_conv3'), \
                                     is_training=is_training, name='g_conv3_bn'))  # 32 x 32 x 32

            conv4 = lrelu(batch_norm(conv2d(conv3, output_dim=1, kernel=3, stride=2, name='d_conv4'), \
                                     is_training=is_training, name='g_conv4_bn'))  # 16 x 16 x 1

            # conv1 = lrelu(conv2d(images, output_dim=64, kernel=7, stride=1, name='d_conv1'))  # 128 x 128 x 64
            #
            # conv2 = lrelu(conv2d(conv1, output_dim=64, kernel=7, stride=2, name='d_conv2'))  # 64 x 64 x 64
            #
            # conv3 = lrelu(conv2d(conv2, output_dim=32, kernel=3, stride=2, name='d_conv3'))  # 32 x 32 x 32
            #
            # conv4 = lrelu(conv2d(conv3, output_dim=1, kernel=3, stride=2, name='d_conv4'))  # 16 x 16 x 1

            fc = tf.reshape(conv4, [-1, 16 * 16 * 1])

            fc = ful_connect(fc, output_size=1, name='d_fc')

        return fc
Ejemplo n.º 6
0
    def discriminative_gan(self, images, reuse=False):
        '''Discriminate 128 x 128 x 3 images fake or real within the range [fake, real] = [0, 1].'''

        with tf.variable_scope('discriminator') as scope:
            if reuse:
                scope.reuse_variables()

            if self.run_flags.run == 'train':
                is_training = True
            else:
                is_training = False

            conv1 = conv2d(images,
                           output_dim=64,
                           kernel=7,
                           stride=1,
                           name='d_conv1')
            conv1 = batch_norm(conv1,
                               is_training=is_training,
                               name='d_conv1_bn')
            conv1 = lrelu(conv1, 0.01)
            # 128 x 128 x 64

            conv2 = conv2d(conv1,
                           output_dim=64,
                           kernel=7,
                           stride=2,
                           name='d_conv2')
            conv2 = batch_norm(conv2,
                               is_training=is_training,
                               name='d_conv2_bn')
            conv2 = lrelu(conv2, 0.01)
            # 64 x 64 x 64

            conv3 = conv2d(conv2,
                           output_dim=32,
                           kernel=3,
                           stride=2,
                           name='d_conv3')
            conv3 = batch_norm(conv3,
                               is_training=is_training,
                               name='d_conv3_bn')
            conv3 = lrelu(conv3, 0.01)
            # 32 x 32 x 32

            conv4 = conv2d(conv3,
                           output_dim=1,
                           kernel=3,
                           stride=2,
                           name='d_conv4')
            conv4 = batch_norm(conv4,
                               is_training=is_training,
                               name='d_conv4_bn')
            conv4 = lrelu(conv4, 0.01)
            # 16 x 16 x 1

            fc = tf.reshape(conv4, [-1, 16 * 16 * 1])
            fc = ful_connect(fc, output_size=1, name='d_fc')

        return fc
Ejemplo n.º 7
0
        def residual(x, in_channel, out_channel, is_training, norm):
            """residual unit with 2 layers
            convolution:
                width filter: 1
                height filter: 3
            """
            orig_x = x
            with tf.variable_scope('conv1'):
                conv1 = utils.conv(x, [1, 3, in_channel, out_channel],
                                   [out_channel],
                                   padding='SAME')
                if norm:
                    conv1 = utils.batch_norm(conv1, is_training)
                relu1 = utils.activation(conv1)
            with tf.variable_scope('conv2'):
                conv2 = utils.conv(relu1, [1, 3, out_channel, out_channel],
                                   [out_channel],
                                   padding='SAME')
                if norm:
                    conv2 = utils.batch_norm(conv2, is_training)
            with tf.variable_scope('add'):
                if in_channel != out_channel:
                    orig_x = utils.conv(x, [1, 1, in_channel, out_channel],
                                        [out_channel],
                                        padding='SAME')

            return utils.activation(conv2 + orig_x)
Ejemplo n.º 8
0
def discriminator_net(x, training, opts, name='Discriminator'):
    with tf.variable_scope(name, reuse=tf.AUTO_REUSE):
        sz = opts.img_size // 16
        # input is (sz*16) x (sz*16) x nc
        # state size. (sz*8) x (sz*8) x ndf
        y = conv(x, opts.ndf, 4, 2, 'same', 'conv1')
        y = leaky_relu(y, 0.2)

        # state size. (sz*4) x (sz*4) x (ndf*2)
        y = conv(y, opts.ndf * 2, 4, 2, 'same', 'conv2')
        y = batch_norm(y, training)
        y = leaky_relu(y, 0.2)

        # state size. (sz*2) x (sz*2) x (ndf*4)
        y = conv(y, opts.ndf * 4, 4, 2, 'same', 'conv3')
        y = batch_norm(y, training)
        y = leaky_relu(y, 0.2)

        # state size. sz x sz x (ndf*8)
        y = conv(y, opts.ndf * 8, 4, 2, 'same', 'conv4')
        y = batch_norm(y, training)
        y = leaky_relu(y, 0.2)

        # output
        y = conv(y, 1, sz, 1, 'valid', 'conv5')
        logits = tf.reshape(y, (-1, 1))

        return logits
Ejemplo n.º 9
0
 def block(x, params, base, mode, stride):
     y = F.conv2d(x, params[base+'.conv0'], stride=stride, padding=1)
     o1 = F.relu(utils.batch_norm(y, params, base+'.bn0', mode), inplace=True)
     z = F.conv2d(o1, params[base+'.conv1'], stride=1, padding=1)
     o2 = utils.batch_norm(z, params, base+'.bn1', mode)
     if base + '.convdim' in params:
         return F.relu(o2 + F.conv2d(x, params[base+'.convdim'], stride=stride), inplace=True)
     else:
         return F.relu(o2 + x, inplace=True)
Ejemplo n.º 10
0
 def block(x, params, base, mode, stride):
     o1 = F.relu(utils.batch_norm(x, params, base + '.bn0', mode), inplace=True)
     y = F.conv2d(o1, params[base + '.conv0'], stride=stride, padding=1)
     o2 = F.relu(utils.batch_norm(y, params, base + '.bn1', mode), inplace=True)
     z = F.conv2d(o2, params[base + '.conv1'], stride=1, padding=1)
     if base + '.convdim' in params:
         return z + F.conv2d(o1, params[base + '.convdim'], stride=stride)
     else:
         return z + x
Ejemplo n.º 11
0
 def block(x, params, stats, base, mode, stride):
     o1 = F.relu(batch_norm(x, params, stats, base + '.bn0', mode), inplace=True)
     y = F.conv2d(o1, params[base + '.conv0'], stride=stride, padding=1)
     o2 = F.relu(batch_norm(y, params, stats, base + '.bn1', mode), inplace=True)
     z = F.conv2d(o2, params[base + '.conv1'], stride=1, padding=1)
     if base + '.convdim' in params:
         return z + F.conv2d(o1, params[base + '.convdim'], stride=stride)
     else:
         return z + x
Ejemplo n.º 12
0
def build_dcgan_discriminator(input_op,
                              is_training=True,
                              scope='UDiscriminator',
                              reuse=False,
                              block_num=4,
                              min_filters=64,
                              kernel_size=3,
                              activation=tf.nn.leaky_relu):
    """build_dcgan_discriminator
    The network structure follows that of DCGAN except that
    1) one more conv before each stride=2 down-sampling conv layer;
    2) change kernel size from 5 to 3 since double 3x3 kernels make similar effect with 5x5
    """
    with tf.variable_scope(scope, reuse=reuse):
        net = input_op
        end_points = dict()
        for idx in range(block_num):
            net = tf.layers.conv2d(net,
                                   min_filters * (idx + 1),
                                   kernel_size=kernel_size,
                                   padding='same',
                                   name='conv_' + str(2 * idx),
                                   use_bias=False,
                                   activation=None,
                                   kernel_initializer=tf.contrib.layers.
                                   xavier_initializer_conv2d())
            net = activation(
                batch_norm(net,
                           is_training=is_training,
                           name='bn_' + str(2 * idx)))
            net = tf.layers.conv2d(net,
                                   min_filters * (idx + 1),
                                   kernel_size=kernel_size,
                                   strides=2,
                                   padding='same',
                                   name='conv_' + str(2 * idx + 1),
                                   activation=None,
                                   use_bias=False,
                                   kernel_initializer=tf.contrib.layers.
                                   xavier_initializer_conv2d())
            net = activation(
                batch_norm(net,
                           is_training=is_training,
                           name='bn_' + str(2 * idx + 1)))
            # end_points should be returned to calculate the
            end_points['pool_' + str(idx)] = net

        batch_size = net.get_shape().as_list()[0]
        net = tf.reshape(net, [batch_size, -1])
        logits = tf.layers.dense(
            net,
            1,
            use_bias=True,
            kernel_initializer=tf.contrib.layers.xavier_initializer())
        return logits, end_points
Ejemplo n.º 13
0
 def block(x, params, base, mode, stride):
     o1 = F.relu(utils.batch_norm(x, params, base + '.bn0', mode), inplace=True)
     y = F.conv2d(o1, params[base + '.conv0'], stride=stride, padding=1)
     o2 = F.relu(utils.batch_norm(y, params, base + '.bn1', mode), inplace=True)
     if dropout > 0:
         o2 = F.dropout(o2, p=dropout, training=mode, inplace=False)
     z = F.conv2d(o2, params[base + '.conv1'], stride=1, padding=1)
     if base + '.convdim' in params:
         return z + F.conv2d(o1, params[base + '.convdim'], stride=stride)
     else:
         return z + x
Ejemplo n.º 14
0
 def discriminator(self, img, cond, reuse):
     dim = len(img.get_shape())
     with tf.variable_scope("disc", reuse=reuse):
         image = tf.concat([img, cond], dim - 1)
         feature = conf.conv_channel_base
         h0 = lrelu(conv2d(image, feature, name="h0"))
         h1 = lrelu(batch_norm(conv2d(h0, feature * 2, name="h1"), "h1"))
         h2 = lrelu(batch_norm(conv2d(h1, feature * 4, name="h2"), "h2"))
         h3 = lrelu(batch_norm(conv2d(h2, feature * 8, name="h3"), "h3"))
         h4 = linear(tf.reshape(h3, [1, -1]), 1, "linear")
     return h4
Ejemplo n.º 15
0
 def block(x, params, base, mode, stride):
     o1 = F.relu(utils.batch_norm(x, params, base + '.bn0', mode),
                 inplace=True)
     y = F.conv2d(o1, params[base + '.conv0'], stride=stride, padding=1)
     o2 = F.relu(utils.batch_norm(y, params, base + '.bn1', mode),
                 inplace=True)
     z = F.conv2d(o2, params[base + '.conv1'], stride=1, padding=1)
     #加入的是bottlneck?1*1的卷积层
     if base + '.convdim' in params:
         return z + F.conv2d(o1, params[base + '.convdim'], stride=stride)
     else:
         return z + x
Ejemplo n.º 16
0
 def deconv_res_block(self, name, x, h, w, cout):
     with tf.variable_scope(name):
         bn1 = utils.batch_norm(name=name + 'bn1')
         x = self.deconv2d(x, [params.bs, h, w, cout],
                           k_h=3,
                           k_w=3,
                           name=name + '.1')
         x = tf.nn.relu(bn1(x))
         bn2 = utils.batch_norm(name=name + 'bn2')
         x = self.conv2d(x, cout, d_h=1, d_w=1, name='conv') + x
         x = tf.nn.relu(bn2(x))
     return x
Ejemplo n.º 17
0
 def build_vgg_upsample_block(input_op, concat, filters, name):
     with tf.variable_scope(name):
         upsampled = tf.layers.conv2d_transpose(input_op, filters,
                                                **upsample_params)
         upsampled = tf.nn.relu(
             batch_norm(upsampled, is_training=is_training, name='upbn_1'))
         net = tf.concat([upsampled, concat], axis=-1)
         net = tf.layers.conv2d(net, filters, **conv_params)
         net = tf.nn.relu(
             batch_norm(net, is_training=is_training, name='upbn_2'))
         conv_params['use_bias'] = True
         return tf.layers.conv2d(net, filters, **conv_params)
Ejemplo n.º 18
0
 def block(x, params, base, mode, stride, out_dict):
     o1 = F.relu(utils.batch_norm(x, params, base + '.bn0', mode),
                 inplace=True)
     y = F.conv2d(o1, params[base + '.conv0'], stride=stride, padding=1)
     o2 = F.relu(utils.batch_norm(y, params, base + '.bn1', mode),
                 inplace=True)
     z = F.conv2d(o2, params[base + '.conv1'], stride=1, padding=1)
     if base + '.convdim' in params:
         o = z + F.conv2d(o1, params[base + '.convdim'], stride=stride)
     else:
         o = z + x
     out_dict[base + '.relu0'] = o1
     out_dict[base + '.relu1'] = o2
     return o, out_dict
Ejemplo n.º 19
0
    def _generator(self,
                   z,
                   dims,
                   train_phase,
                   activation=tf.nn.relu,
                   scope_name="generator"):
        N = len(dims)
        image_size = self.resized_image_size // (2**(N - 1))
        with tf.variable_scope(scope_name) as scope:
            W_z = utils.weight_variable(
                [self.z_dim, dims[0] * image_size * image_size], name="W_z")
            h_z = tf.matmul(z, W_z)
            h_z = tf.reshape(h_z, [-1, image_size, image_size, dims[0]])
            # h_bnz = tf.contrib.layers.batch_norm(inputs=h_z, decay=0.9, epsilon=1e-5, is_training=train_phase,
            #                                      scope="gen_bnz")
            # h_bnz = utils.batch_norm(h_z, dims[0], train_phase, scope="gen_bnz")
            h_bnz = utils.batch_norm('gen_bnz', h_z, True, 'NHWC', train_phase)
            h = activation(h_bnz, name='h_z')
            utils.add_activation_summary(h)

            for index in range(N - 2):
                image_size *= 2
                W = utils.weight_variable([4, 4, dims[index + 1], dims[index]],
                                          name="W_%d" % index)
                b = tf.zeros([dims[index + 1]])
                deconv_shape = tf.stack(
                    [tf.shape(h)[0], image_size, image_size, dims[index + 1]])
                h_conv_t = utils.conv2d_transpose_strided(
                    h, W, b, output_shape=deconv_shape)
                # h_bn = tf.contrib.layers.batch_norm(inputs=h_conv_t, decay=0.9, epsilon=1e-5, is_training=train_phase,
                #                                     scope="gen_bn%d" % index)
                # h_bn = utils.batch_norm(h_conv_t, dims[index + 1], train_phase, scope="gen_bn%d" % index)
                h_bn = utils.batch_norm("gen_bn%d" % index, h_conv_t, True,
                                        'NHWC', train_phase)
                h = activation(h_bn, name='h_%d' % index)
                utils.add_activation_summary(h)

            image_size *= 2
            W_pred = utils.weight_variable([4, 4, dims[-1], dims[-2]],
                                           name="W_pred")
            b = tf.zeros([dims[-1]])
            deconv_shape = tf.stack(
                [tf.shape(h)[0], image_size, image_size, dims[-1]])
            h_conv_t = utils.conv2d_transpose_strided(
                h, W_pred, b, output_shape=deconv_shape)
            pred_image = tf.nn.tanh(h_conv_t, name='pred_image')
            utils.add_activation_summary(pred_image)

        return pred_image
Ejemplo n.º 20
0
    def _discriminator(self,
                       input_images,
                       dims,
                       train_phase,
                       activation=tf.nn.relu,
                       scope_name="discriminator",
                       scope_reuse=False):
        N = len(dims)
        with tf.variable_scope(scope_name) as scope:
            if scope_reuse:
                scope.reuse_variables()
            h = input_images
            skip_bn = True  # First layer of discriminator skips batch norm
            for index in range(N - 2):
                W = utils.weight_variable([4, 4, dims[index], dims[index + 1]],
                                          name="W_%d" % index)
                b = tf.zeros([dims[index + 1]])
                h_conv = utils.conv2d_strided(h, W, b)
                if skip_bn:
                    h_bn = h_conv
                    skip_bn = False
                else:
                    h_bn = utils.batch_norm(h_conv,
                                            dims[index + 1],
                                            train_phase,
                                            scope="disc_bn%d" % index)
                h = activation(h_bn, name="h_%d" % index)
                utils.add_activation_summary(h)

            W_pred = utils.weight_variable([4, 4, dims[-2], dims[-1]],
                                           name="W_pred")
            b = tf.zeros([dims[-1]])
            h_pred = utils.conv2d_strided(h, W_pred, b)
        return None, h_pred, None  # Return the last convolution output. None values are returned to maintatin disc from other GAN
Ejemplo n.º 21
0
    def _discriminator(self, input_images, dims, train_phase, activation=tf.nn.relu, scope_name="discriminator",
                       scope_reuse=False):
        N = len(dims)
        with tf.variable_scope(scope_name) as scope:
            if scope_reuse:
                scope.reuse_variables()
            h = input_images
            skip_bn = True  # First layer of discriminator skips batch norm
            for index in range(N - 2):
                W = utils.weight_variable([5, 5, dims[index], dims[index + 1]], name="W_%d" % index)
                b = utils.bias_variable([dims[index + 1]], name="b_%d" % index)
                h_conv = utils.conv2d_strided(h, W, b)
                if skip_bn:
                    h_bn = h_conv
                    skip_bn = False
                else:
                    h_bn = utils.batch_norm(h_conv, dims[index + 1], train_phase, scope="disc_bn%d" % index)
                h = activation(h_bn, name="h_%d" % index)
                utils.add_activation_summary(h)

            shape = h.get_shape().as_list()
            image_size = self.resized_image_size // (2 ** (N - 2))  # dims has input dim and output dim
            h_reshaped = tf.reshape(h, [self.batch_size, image_size * image_size * shape[3]])
            W_pred = utils.weight_variable([image_size * image_size * shape[3], dims[-1]], name="W_pred")
            b_pred = utils.bias_variable([dims[-1]], name="b_pred")
            h_pred = tf.matmul(h_reshaped, W_pred) + b_pred

        return tf.nn.sigmoid(h_pred), h_pred, h
Ejemplo n.º 22
0
    def generative(self, x, reuse=False):
        with tf.variable_scope('generator') as scope:
            if reuse:
                scope.reuse_variables()

            if self.run_flags.run == 'train':
                is_training = True
            else:
                is_training = False

            conv1 = lrelu(batch_norm(conv2d(x, output_dim=32, stride=1, name='g_conv1'), \
                                     is_training=is_training, name='g_conv1_bn')) # 64 x 64 x 32

            conv2 = lrelu(batch_norm(conv2d(conv1, output_dim=128, stride=1, name='g_conv2'), \
                                     is_training=is_training, name='g_conv2_bn')) # 64 x 64 x 128

            conv3 = lrelu(batch_norm(conv2d(conv2, output_dim=128, stride=1, name='g_conv3'), \
                                     is_training=is_training, name='g_conv3_bn')) # 64 x 64 x 128

            conv3_up = tf.image.resize_images(conv3, size=[128, 128])

            conv4 = lrelu(batch_norm(conv2d(conv3_up, output_dim=128, stride=1, name='g_conv4'), \
                                     is_training=is_training, name='g_conv4_bn')) # 128 x 128 x 128

            conv5 = lrelu(batch_norm(conv2d(conv4, output_dim=64, stride=1, name='g_conv5'), \
                                     is_training=is_training, name='g_conv5_bn'))  # 128 x 128 x 64

            conv6 = tf.nn.sigmoid(
                conv2d(conv5, output_dim=3, stride=1,
                       name='g_conv6'))  #128 x 128 x 3

            # conv1 = lrelu(conv2d(x, output_dim=32, stride=1, name='g_conv1')) # 64 x 64 x 32
            #
            # conv2 = lrelu(conv2d(conv1, output_dim=128, stride=1, name='g_conv2')) # 64 x 64 x 128
            #
            # conv3 = lrelu(conv2d(conv2, output_dim=128, stride=1, name='g_conv3')) # 64 x 64 x 128
            #
            # conv3_up = tf.image.resize_images(conv3, size=[128, 128])
            #
            # conv4 = lrelu(conv2d(conv3_up, output_dim=128, stride=1, name='g_conv4')) # 128 x 128 x 128
            #
            # conv5 = lrelu(conv2d(conv4, output_dim=64, stride=1, name='g_conv5'))  # 128 x 128 x 64
            #
            # conv6 = tf.nn.sigmoid(conv2d(conv5, output_dim=3, stride=1, name='g_conv6')) #128 x 128 x 3

        return conv6
Ejemplo n.º 23
0
    def graph(self, input, is_training):
        with tf.name_scope('model'):
            net = ut.conv_layer(input, 64, 7, 2, name='conv1')
            net = ut.bottleneck(net,
                                128,
                                stride=1,
                                training=is_training,
                                name='res1')
            net = ut.max_pool(net, 2, 2, 'max_pool')
            net = ut.bottleneck(net,
                                int(self.nFeats / 2),
                                stride=1,
                                training=is_training,
                                name='res2')
            net = ut.bottleneck(net,
                                self.nFeats,
                                stride=1,
                                training=is_training,
                                name='res3')

            with tf.name_scope('stacks'):
                stack_out = []
                with tf.name_scope('stage_0'):
                    hg = ut.hourglass(net, self.nLow, self.nFeats, 'hourglass')
                    drop = ut.dropout(hg, self.dropout_rate, is_training,
                                      'dropout')
                    ll = ut.conv_layer_bn(drop, self.nFeats, 1, 1, is_training)
                    out = ut.conv_layer(ll, self.num_points, 1, 1, name='out')
                    out_ = ut.conv_layer(out, self.nFeats, 1, 1, name='out_')
                    sum_ = tf.add(net, out_, name='merge')
                    stack_out.append(out)
                for i in range(1, self.nStacks):
                    with tf.name_scope('stage_' + str(i)):
                        hg = ut.hourglass(sum_, self.nLow, self.nFeats,
                                          'hourglass')
                        drop = ut.dropout(hg, self.dropout_rate, is_training,
                                          'dropout')
                        ll = ut.conv_layer_bn(drop, self.nFeats, 1, 1,
                                              is_training)
                        out = ut.conv_layer(ll,
                                            self.num_points,
                                            1,
                                            1,
                                            name='out')
                        out_ = ut.conv_layer(ll,
                                             self.nFeats,
                                             1,
                                             1,
                                             name='out_')
                        sum_ = tf.add(sum_, out_, name='merge')
                        stack_out.append(out)
            with tf.name_scope('upsampling'):
                net = ut.batch_norm(sum_, is_training)
                net = ut.conv_layer_bn(net, self.nFeats, 3, 1, is_training)
                up1 = ut.deconv_layer(net, self.num_points, 1, 2, name='up_1')
                net = ut.conv_layer_bn(up1, self.nFeats, 3, 1, is_training)
                up2 = ut.deconv_layer(net, self.num_points, 1, 2, name='up_2')
            return tf.stack(stack_out, axis=1, name='stack_out'), up1, up2
Ejemplo n.º 24
0
def build_lenet(input_op, is_training=True):
    params = {
        'kernel_size': 5,
        'padding': 'same',
        'use_bias': False,
        'activation': None,
        'strides': 2,
        'kernel_initializer': tf.contrib.layers.xavier_initializer_conv2d()
    }
    conv_1 = coord_conv(input_op, 32, name='conv_1', **params)
    conv_1 = tf.nn.leaky_relu(
        batch_norm(conv_1, is_training=is_training, name='bn_1'))
    conv_2 = tf.layers.conv2d(conv_1, 64, name='conv_2', **params)
    conv_2 = tf.nn.leaky_relu(
        batch_norm(conv_2, is_training=is_training, name='bn_2'))
    conv_3 = tf.layers.conv2d(conv_2, 128, name='conv_3', **params)
    conv_3 = tf.nn.leaky_relu(
        batch_norm(conv_3, is_training=is_training, name='bn_3'))

    params = {
        'kernel_size': 5,
        'strides': 2,
        'padding': 'same',
        'activation': None,
        'use_bias': False,
        'kernel_initializer': tf.contrib.layers.xavier_initializer_conv2d()
    }
    deconv_1 = tf.layers.conv2d_transpose(conv_3,
                                          64,
                                          name='deconv_1',
                                          **params)
    deconv_1 = tf.nn.leaky_relu(batch_norm(deconv_1, is_training=is_training))
    deconv_2 = tf.layers.conv2d_transpose(tf.concat([deconv_1, conv_2],
                                                    axis=-1),
                                          32,
                                          name='deconv_2',
                                          **params)
    deconv_2 = tf.nn.leaky_relu(
        batch_norm(deconv_2, is_training=is_training, name='bn_4'))
    params['activation'] = None
    params['use_bias'] = True
    return tf.layers.conv2d_transpose(tf.concat([deconv_2, conv_1], axis=-1),
                                      1,
                                      name='deconv_3',
                                      **params)
Ejemplo n.º 25
0
    def __init__(self, sess, image_size=64, is_crop=False, batch_size=64, sample_size=64, lowres=8, z_dim=100,
                 gf_dim=64, df_dim=64, gfc_dim=1024, dfc_dim=1024, c_dim=3, checkpoint_dir=None, lam=0.1):
        """

        :param sess: TensorFlow session
        :param batch_size: the size of batch. Should be specified before training
        :param lowres: (optional) Low resolution image/mask shrink factor.
        :param gf_dim: (optional)Dimension of generator filters in first conv layer
        :param df_dim: (optional)Dimension of discriminator filters in first conv layer
        :param gfc_dim: (optional)Dimension of gen units for fully connected layer
        :param dfc_dim: (optional)Dimension of discrim units for fully connected layer
        :param c_dim: (optional) Dimension of image color. For grayscale input, set to 1.
        """
        self.sess = sess
        self.is_crop = is_crop

        self.batch_size = batch_size
        self.image_size = image_size
        self.sample_size = sample_size
        self.image_shape = [image_size, image_size, c_dim]

        self.lowres = lowres
        self.lowres_size = image_size // lowres
        self.lowres_shape = [self.lowres_size, self.lowres_size, c_dim]

        self.z_dim = z_dim

        self.gf_dim = gf_dim
        self.df_dim = df_dim

        self.gfc_dim = gfc_dim
        self.dfc = dfc_dim

        self.lam = lam
        self.c_dim = c_dim

        self.d_bns = [batch_norm(name='d_bn{}'.format(format(i, )) for i in range(4))]

        log_size = int(math.log(image_size) / math.log(2))

        self.g_bns = [batch_norm(name='g_bn{}'.format(format(i, )) for i in range(log_size))]

        self.checkpoint_dir = checkpoint_dir
        self.build_model()
        self.model_name = 'DCGAN.model'
Ejemplo n.º 26
0
 def f(input, params, stats, mode, prefix=''):
     x = F.conv2d(input, params[prefix+'conv0'], padding=1)
     g0 = group(x, params, stats, prefix+'group0', mode, 1)
     g1 = group(g0, params, stats, prefix+'group1', mode, 2)
     g2 = group(g1, params, stats, prefix+'group2', mode, 2)
     o = F.relu(batch_norm(g2, params, stats, prefix+'bn', mode))
     o = F.avg_pool2d(o, 8, 1, 0)
     o = o.view(o.size(0), -1)
     o = F.linear(o, params[prefix+'fc.weight'], params[prefix+'fc.bias'])
     return o, [g0, g1, g2]
Ejemplo n.º 27
0
 def harmonic_block(x, params, base, mode, stride=1, padding=1):
     y = F.conv2d(x,
                  params['dct0'],
                  stride=stride,
                  padding=padding,
                  groups=x.size(1))
     if base + '.bn.running_mean' in params:
         y = utils.batch_norm(y, params, base + '.bn', mode, affine=False)
     z = F.conv2d(y, params[base + '.conv'], padding=0)
     return z
Ejemplo n.º 28
0
 def f(input, params, mode):
     x = F.conv2d(input, params['conv0'], padding=1)
     g0 = group(x, params, 'group0', mode, 1)
     g1 = group(g0, params, 'group1', mode, 2)
     g2 = group(g1, params, 'group2', mode, 2)
     o = F.relu(utils.batch_norm(g2, params, 'bn', mode))
     o = F.avg_pool2d(o, 8, 1, 0)
     o = o.view(o.size(0), -1)
     o = F.linear(o, params['fc.weight'], params['fc.bias'])
     return o
Ejemplo n.º 29
0
 def f(input, params, stats, mode):
     x = F.conv2d(input, params['conv0'], padding=1)
     g0 = group(x, params, stats, 'group0', mode, 1)
     g1 = group(g0, params, stats, 'group1', mode, 2)
     g2 = group(g1, params, stats, 'group2', mode, 2)
     o = F.relu(batch_norm(g2, params, stats, 'bn', mode, 1.))
     o = F.conv2d(o, params['conv1'])
     o = F.avg_pool2d(o, 8, 1, 0)
     o = o.view(o.size(0), -1)
     return o
Ejemplo n.º 30
0
def VGG19(x, n_classes, is_pretrain=True):
    with tf.name_scope('VGG16'):
        x = utils.conv('conv1_1', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        x = utils.conv('conv1_2', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        with tf.name_scope('pool1'):
            x = utils.pool('pool1', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True)

        x = utils.conv('conv2_1', x, 128, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        x = utils.conv('conv2_2', x, 128, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        with tf.name_scope('pool2'):
            x = utils.pool('pool2', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True)

        x = utils.conv('conv3_1', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        x = utils.conv('conv3_2', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        x = utils.conv('conv3_3', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        x = utils.conv('conv3_4', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        with tf.name_scope('pool3'):
            x = utils.pool('pool3', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True)

        x = utils.conv('conv4_1', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        x = utils.conv('conv4_2', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        x = utils.conv('conv4_3', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        x = utils.conv('conv4_4', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        with tf.name_scope('pool4'):
            x = utils.pool('pool4', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True)

        x = utils.conv('conv5_1', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        x = utils.conv('conv5_2', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        x = utils.conv('conv5_3', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        x = utils.conv('conv5_4', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
        with tf.name_scope('pool5'):
            x = utils.pool('pool5', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True)

        x = utils.FC_layer('fc6', x, out_nodes=4096)
        with tf.name_scope('batch_norm1'):
            x = utils.batch_norm(x)
        x = utils.FC_layer('fc7', x, out_nodes=4096)
        with tf.name_scope('batch_norm2'):
            x = utils.batch_norm(x)
        x = utils.FC_layer('fc8', x, out_nodes=n_classes)

        return x
def net(x, is_training=False, verbose=False):
    x = x.as_in_context(w1.context)
    h1_conv = nd.Convolution(data=x,
                             weight=w1,
                             bias=b1,
                             kernel=w1.shape[2:],
                             num_filter=c1)
    h1_bn = utils.batch_norm(h1_conv, gamma1, beta1, is_training, moving_mean1,
                             moving_variance1)
    h1_activation = nd.relu(h1_conv)
    h1 = nd.Pooling(data=h1_activation,
                    pool_type='max',
                    kernel=(2, 2),
                    stride=(2, 2))

    h2_conv = nd.Convolution(data=h1,
                             weight=w2,
                             bias=b2,
                             kernel=w2.shape[2:],
                             num_filter=c2)
    h2_bn = utils.batch_norm(h2_conv, gamma2, beta2, is_training, moving_mean2,
                             moving_variance2)
    h2_activation = nd.relu(h2_conv)
    h2 = nd.Pooling(data=h2_activation,
                    pool_type='max',
                    kernel=(2, 2),
                    stride=(2, 2))
    h2 = nd.flatten(h2)

    h3_linear = nd.dot(h2, w3) + b3
    h3 = nd.relu(h3_linear)

    h4_linear = nd.dot(h3, w4) + b4

    if verbose:
        print('h1 conv block: ', h1.shape)
        print('h2 conv block: ', h2.shape)
        print('h3 conv block: ', h3.shape)
        print('h4 conv block: ', h4_linear.shape)
        print('output: ', h4_linear)

    return h4_linear.as_in_context(ctx)