def __init__(self, scope: str = "Generator", ngf: int = 64, reg: float = 0.0005, norm: str = "instance", more: bool = True): super(Generator, self).__init__(name=scope) self.c7s1_32 = ops.c7s1_k(scope="c7s1_32", k=ngf, reg=reg, norm=norm) self.d64 = ops.dk(scope="d64", k=2 * ngf, reg=reg, norm=norm) self.d128 = ops.dk(scope="d128", k=4 * ngf, reg=reg, norm=norm) if more: self.res_output = ops.n_res_blocks(scope="8_res_blocks", n=8, k=4 * ngf, reg=reg, norm=norm) else: self.res_output = ops.n_res_blocks(scope="6_res_blocks", n=6, k=4 * ngf, reg=reg, norm=norm) self.u64 = ops.uk(scope="u64", k=2 * ngf, reg=reg, norm=norm) self.u32 = ops.uk(scope="u32", k=ngf, reg=reg, norm=norm) self.outconv = ops.c7s1_k(scope="output", k=3, reg=reg, norm=norm)
def deconder(self, res_output): # fractional-strided convolution u64 = ops.uk(res_output, 2 * self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='u64') # (?, w/2, h/2, 64) u32 = ops.uk(u64, self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='u32', output_size=self.image_size) # (?, w, h, 32) # conv layer # Note: the paper said that ReLU and _norm were used # but actually tanh was used and no _norm here output = ops.c7s1_k(u32, 3, norm=None, activation='tanh', reuse=self.reuse, name='output') # (?, w, h, 3) return output
def __call__(self, input): """ Args: input: batch_size x width x height x 3 Returns: output: same size as input """ with tf.variable_scope(self.name): c7s1_32 = ops.c7s1_k(input, self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='c7s1_32') d64 = ops.dk(c7s1_32, 2 * self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='d64') d128 = ops.dk(d64, 4 * self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='d128') if self.image_size <= 128: res_output = ops.n_res_blocks(d128, reuse=self.reuse, n=6) else: res_output = ops.n_res_blocks(d128, reuse=self.reuse, n=9) # fractional-strided convolution u64 = ops.uk(res_output, 2 * self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='u64') u32 = ops.uk(u64, self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='u32', output_size=self.image_size) output = ops.c7s1_k(u32, 3, norm=None, activation='tanh', reuse=self.reuse, name='output') self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) return output
def __call__(self, input): """ Args: input: batch_size x width x height x 3 Returns: output: same size as input """ with tf.variable_scope(self.name): g = tf.get_default_graph() with g.gradient_override_map({"Identity": "ReverseGrad"}): idinput = tf.identity(input) res_input = ops.n_res_blocks(idinput, reuse=self.reuse, n=3) #(?, w/4, h/4, 48) # fractional-strided convolution u32 = ops.uk(res_input, 2 * self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='u32') # (?, w/2, h/2, 32) u16 = ops.uk(u32, self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='u16', output_size=self.image_size) #(?, w, h, 16) # conv layer # Note: the paper said that ReLU and _norm were used # but actually tanh was used and no _norm here output = ops.c7s1_k(u16, 3, norm=None, activation='tanh', reuse=self.reuse, name='output') # (?, w, h, 3) ## fractional-strided convolution #u64 = ops.uk(idinput, 2*self.ngf, is_training=self.is_training, norm=self.norm, #reuse=self.reuse, name='u64') # (?, w/2, h/2, 64) #u32 = ops.uk(u64, self.ngf, is_training=self.is_training, norm=self.norm, #reuse=self.reuse, name='u32', output_size=self.image_size) # (?, w, h, 32) ## conv layer ## Note: the paper said that ReLU and _norm were used ## but actually tanh was used and no _norm here #output = ops.c7s1_k(u32, 3, norm=None, #activation='tanh', reuse=self.reuse, name='output') # (?, w, h, 3) # set reuse=True for next call self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) return output
def __call__(self, input): """ Args: input: batch_size x 1 x 1 x 10 Returns: output: 8 x 8 x nfe """ with tf.variable_scope(self.name): # fractional-strided convolution fsconv1 = ops.uk(input, 64, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='fsconv1', output_size=2) # (?, 2, 2, 64) fsconv2 = ops.uk(fsconv1, 32, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='fsconv2', output_size=4) #(?, 4, 4, 32) fsconv3 = ops.uk(fsconv2, 16, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='fsconv3', output_size=8) #(?, 8, 8, 16) # use 3 residual blocks res_output = ops.n_res_blocks(fsconv3, reuse=self.reuse, n=3) # set reuse=True for next call self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) return res_output
def __call__(self, input): """ Args: input: batch_size x width x height x 3 Returns: output: same size as input """ with tf.variable_scope(self.name): # conv layers # Filters dim: (7, 7, 3); Output dim: (w, h, 64) c7s1_32 = ops.c7s1_k(input, self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='c7s1_32') # Filters dim: (3, 3, 64); Output dim: (w/2, h/2, 128) d64 = ops.dk(c7s1_32, 2 * self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='d64') # Filters dim: (3, 3, 128); Output dim: (w/4, h/4, 256) d128 = ops.dk(d64, 4 * self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='d128') # Filters dim: (3, 3, 256); Output dim: (w/4, h/4, 256) res_output = ops.n_res_blocks(d128, reuse=self.reuse, n=9) # fractional-strided convolution # Filters dim: (3, 3, 256); Output dim: (w/2, h/2, 128) u64 = ops.uk(res_output, 2 * self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='u64') # Filters dim: (3, 3, 128); Output dim: (w, h, 64) u32 = ops.uk(u64, self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='u32', output_length=self.image_length, output_height=self.image_height) # conv layer # Note: the paper said that ReLU and _norm were used # but actually tanh was used and no _norm here # Filters dim: (3, 3, 64); Output dim: (w/2, h/2, 3) output = ops.c7s1_k(u32, 3, norm=None, activation='tanh', reuse=self.reuse, name='output') # set reuse=True for next call self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) return output
def __call__(self, input): """ Args: input: batch_size x width x height x 3 Returns: output: same size as input """ with tf.variable_scope(self.name): # conv layers c7s1_32 = ops.c7s1_k(input, self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='c7s1_32') # (?, w, h, 32) d64 = ops.dk(c7s1_32, 2*self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='d64') # (?, w/2, h/2, 64) d128 = ops.dk(d64, 4*self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='d128') # (?, w/4, h/4, 128) #if self.image_size <= 128: if max(self.image_size) <= 128: # use 6 residual blocks for 128x128 images res_output = ops.n_res_blocks(d128, reuse=self.reuse, n=6) # (?, w/4, h/4, 128) else: # 9 blocks for higher resolution res_output = ops.n_res_blocks(d128, reuse=self.reuse, n=9) # (?, w/4, h/4, 128) # fractional-strided convolution u64 = ops.uk(res_output, 2*self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='u64') # (?, w/2, h/2, 64) u32 = ops.uk(u64, self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='u32', output_size=self.image_size) # (?, w, h, 32) # conv layer # Note: the paper said that ReLU and _norm were used # but actually tanh was used and no _norm here output = ops.c7s1_k(u32, 3, norm=None, activation='tanh', reuse=self.reuse, name='output') # (?, w, h, 3) # set reuse=True for next call self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) return output
def __call__(self, input): """ Args: input: batch_size x width x height x 3 Returns: output: same size as input """ with tf.variable_scope(self.name): # conv layers c7s1_32 = ops.c7s1_k(input, self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='c7s1_32') # (?, w, h, 32) d64 = ops.dk(c7s1_32, 2*self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='d64') # (?, w/2, h/2, 64) d128 = ops.dk(d64, 4*self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='d128') # (?, w/4, h/4, 128) if self.image_size <= 128: # use 6 residual blocks for 128x128 images res_output = ops.n_res_blocks(d128, reuse=self.reuse, n=6) # (?, w/4, h/4, 128) else: # 9 blocks for higher resolution res_output = ops.n_res_blocks(d128, reuse=self.reuse, n=9) # (?, w/4, h/4, 128) # fractional-strided convolution u64 = ops.uk(res_output, 2*self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='u64') # (?, w/2, h/2, 64) u32 = ops.uk(u64, self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='u32', output_size=self.image_size) # (?, w, h, 32) # conv layer # Note: the paper said that ReLU and _norm were used # but actually tanh was used and no _norm here output = ops.c7s1_k(u32, 3, norm=None, activation='tanh', reuse=self.reuse, name='output') # (?, w, h, 3) # set reuse=True for next call self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) return output
def test_uk(self): input = tf.placeholder(tf.float32, shape=[64, 256, 256, 32]) result = ops.uk(input, 32, name='Uk64') print(result)
def __call__(self, input): """ Args: input: representation 8 x 8 x nf Returns: output: batch_size x width x height x 3 """ with tf.variable_scope(self.name): if self.reverse: g = tf.get_default_graph() with g.gradient_override_map({"Identity": "ReverseGrad"}): # gradient reversal layer idinput = tf.identity(input) #resnet blocks right after input res_input = ops.n_res_blocks(idinput, reuse=self.reuse, n=3) # (?, w/4, h/4, nf) # fractional-strided convolutions fsconv1 = ops.uk(res_input, self.conv_size[0], is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='fsconv1') # (?, w/2, h/2, 8) fsconv2 = ops.uk(fsconv1, self.conv_size[1], is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='fsconv2', output_size=self.image_size) #(?, w, h, 4) # conv layer # Note: the paper said that ReLU and _norm were used # but actually tanh was used and no _norm here output = ops.c7s1_k(fsconv2, 3, norm=None, activation='tanh', reuse=self.reuse, name='output') # (?, w, h, 3) else: res_input = ops.n_res_blocks(input, reuse=self.reuse, n=3) # (?, w/4, h/4, nf) #resnet blocks right after input # fractional-strided convolution fsconv1 = ops.uk(res_input, self.conv_size[0], is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='fsconv1') # (?, w/2, h/2, 8) fsconv2 = ops.uk(fsconv1, self.conv_size[1], is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='fsconv2', output_size=self.image_size) #(?, w, h, 4) # conv layer # Note: the paper said that ReLU and _norm were used # but actually tanh was used and no _norm here output = ops.c7s1_k(fsconv2, 3, norm=None, activation='tanh', reuse=self.reuse, name='output') # (?, w, h, 3) # set reuse=True for next call self.reuse = True self.variables = tf.get_collection( tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) return output
def __call__(self, input): """ 输入: input: batch_size x width x height x 3 返回: output: 输入输出分辨率一致 """ with tf.variable_scope(self.name): # conv layers c7s1_32 = ops.c7s1_k(input, self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='c7s1_32') # (?, w, h, 32) d64 = ops.dk(c7s1_32, 2 * self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='d64') # (?, w/2, h/2, 64) d128 = ops.dk(d64, 4 * self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='d128') # (?, w/4, h/4, 128) if self.image_size <= 128: # use 6 residual blocks for 128x128 images res_output = ops.n_res_blocks(d128, reuse=self.reuse, n=6) # (?, w/4, h/4, 128) else: # 9 blocks for higher resolution res_output = ops.n_res_blocks(d128, reuse=self.reuse, n=9) # (?, w/4, h/4, 128) # fractional-strided convolution u64 = ops.uk(res_output, 2 * self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='u64') # (?, w/2, h/2, 64) u32 = ops.uk(u64, self.ngf, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='u32', output_size=self.image_size) # (?, w, h, 32) output = ops.c7s1_k(u32, 3, norm=None, activation='tanh', reuse=self.reuse, name='output') # (?, w, h, 3) # set reuse=True for next call self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) return output # change for residual cycle gan
def resnet_generator(self, input, in_channels=3, out_channels=3, ngf=64, norm_type='instance', init_type='normal', init_gain=1.0, dropout=False, is_training=True, n_blocks=6): """ Resnet-based generator that contains Resnet blocks in between some downsampling and upsampling layers. Args: input: in_channels: out_channels: ngf: norm_type: init_type: init_gain: dropout: is_training: n_blocks: Returns: c7s1_3: Final output of the Resnet-based generator """ # 7x7 convolution-instance norm-relu layer with 64 filters and stride 1 c7s1_64 = ops.conv(input, in_channels=in_channels, out_channels=ngf, filter_size=7, stride=1, padding_type='VALID', weight_init_type=init_type, weight_init_gain=init_gain, norm_type=norm_type, is_training=is_training, scope='c7s1-64', reuse=self.reuse) # 3x3 convolution-instance norm-relu layer with 128 filters and stride 2 d128 = ops.conv(c7s1_64, in_channels=ngf, out_channels=2 * ngf, filter_size=3, stride=2, weight_init_type=init_type, weight_init_gain=init_gain, norm_type=norm_type, is_training=is_training, scope='d128', reuse=self.reuse) # 3x3 convolution-instance norm-relu layer with 256 filters and stride 2 d256 = ops.conv(d128, in_channels=2 * ngf, out_channels=4 * ngf, filter_size=3, stride=2, weight_init_type=init_type, weight_init_gain=init_gain, norm_type=norm_type, is_training=is_training, scope='d256', reuse=self.reuse) r256 = d256 # Resnet blocks with 256 filters for idx in range(n_blocks): # residual block that contains two 3x3 convolution layers with 256 filters and stride 1 r256 = ops.resnet_block(r256, in_channels=4 * ngf, out_channels=4 * ngf, filter_size=3, stride=1, norm_type=norm_type, is_training=is_training, dropout=dropout, scope='r256-' + str(idx), reuse=self.reuse) # 3x3 fractional strided convolution-instance norm-relu layer with 128 filters and stride 1/2 u128 = ops.uk(r256, in_channels=4 * ngf, out_channels=2 * ngf, out_shape=2 * r256.get_shape().as_list()[1], filter_size=3, stride=2, weight_init_type=init_type, weight_init_gain=init_gain, norm_type=norm_type, is_training=is_training, scope='u128', reuse=self.reuse) # 3x3 fractional strided convolution-instance norm-relu layer with 64 filters and stride 1/2 u64 = ops.uk(u128, in_channels=2 * ngf, out_channels=ngf, out_shape=2 * u128.get_shape().as_list()[1], filter_size=3, stride=2, weight_init_type=init_type, weight_init_gain=init_gain, norm_type=norm_type, is_training=is_training, scope='u64', reuse=self.reuse) # 7x7 convolution-instance norm-relu layer with 3 filters and stride 1 c7s1_3 = ops.conv(u64, in_channels=ngf, out_channels=out_channels, filter_size=7, stride=1, padding_type='VALID', weight_init_type=init_type, weight_init_gain=init_gain, norm_type=None, activation_type='tanh', is_training=is_training, scope='c7s1-3', reuse=self.reuse) return c7s1_3
def __call__(self, input): """ Args: input: batch_size x 128 x 128 x 3 Returns: output: same size as input """ with tf.variable_scope(self.name): # conv layers c7s1_32 = ops.c7s1_k(input, 32, is_training=self.is_training, reuse=self.reuse, name='c7s1_32') # (?, 128, 128, 32) d64 = ops.dk(c7s1_32, 64, is_training=self.is_training, reuse=self.reuse, name='d64') # (?, 64, 64, 64) d128 = ops.dk(d64, 128, is_training=self.is_training, reuse=self.reuse, name='d128') # (?, 32, 32, 128) # 6 residual blocks R128_1 = ops.Rk(d128, 128, reuse=self.reuse, name='R128_1') # (?, 32, 32, 128) R128_2 = ops.Rk(R128_1, 128, reuse=self.reuse, name='R128_2') # (?, 32, 32, 128) R128_3 = ops.Rk(R128_2, 128, reuse=self.reuse, name='R128_3') # (?, 32, 32, 128) R128_4 = ops.Rk(R128_3, 128, reuse=self.reuse, name='R128_4') # (?, 32, 32, 128) R128_5 = ops.Rk(R128_4, 128, reuse=self.reuse, name='R128_5') # (?, 32, 32, 128) R128_6 = ops.Rk(R128_5, 128, reuse=self.reuse, name='R128_6') # (?, 32, 32, 128) # fractional-strided convolution u64 = ops.uk(R128_6, 64, is_training=self.is_training, reuse=self.reuse, name='u64') # (?, 64, 64, 64) u32 = ops.uk(u64, 32, is_training=self.is_training, reuse=self.reuse, name='u32') # (?, 128, 128, 32) # conv layer # Note: the paper said that ReLU and batch_norm were used # but actually tanh was used and no batch_norm here output = ops.c7s1_k(u32, 3, batch_norm=False, activation='tanh', reuse=self.reuse, name='output') # (?, 128, 128, 3) # set reuse=True for next call self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) return output