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 __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 16 Returns: output: batch_size x width x height x 32 """ with tf.variable_scope(self.name): g = tf.get_default_graph() with g.gradient_override_map({"Identity": "ReverseGrad"}): idinput = tf.identity(input) #idinput = tf.stop_gradient(input) res_input = ops.n_res_blocks(idinput, reuse=self.reuse, n=3) # conv layer - output depth is 32 (as in shared representation) output = ops.c7s1_k(res_input, 2 * self.ngf, is_training=self.is_training, norm=self.norm, activation='tanh', reuse=self.reuse, name='c7s1_32') # (?, w, h, 32) # set reuse=True for next call self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) return output
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): # 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) output = d128 """ Drop the transformation residual blocks in the middle --> from encoding to decoding directly 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) """ # 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, output_media_features=False, use_media_features_from_former_network=None, use_media_features=False, enconder_name='twin_enconder_0', deconder_name='twin_deconder_0'): """ Args: input: batch_size x width x height x 3 Returns: output: same size as input """ #pdb.set_trace() with tf.variable_scope(self.name + '/' + enconder_name): # conv layers if not use_media_features: 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) #### FCN_mask = ops.FCN_MASK(d128, reuse=self.reuse, n=1) # (?, w/4, h/4, 128) self.FCN_mask = FCN_mask #### 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) res_output = tf.multiply(FCN_mask, res_output) + res_output else: res_output = use_media_features_from_former_network with tf.variable_scope(self.name + '/' + deconder_name): output = self.deconder(res_output) self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) return (output, res_output) if output_media_features else 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 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 __call__(self, input): """ Args: input: batch_size x width x height x 3 Returns: output: split representation 8 x 8 x nfs+nfe """ with tf.variable_scope(self.name): # conv layers conv1 = ops.c7s1_k(input, self.conv_size[0], is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='conv1') # (?,w, h, 4) conv2 = ops.dk(conv1, self.conv_size[1], is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='conv2') # (?,w/2, h/2, 8) # Fixed architecture, 2 chunks of ngf for shared, 1 for exclusive conv3 = ops.dk(conv2, self.nfs + self.nfe, is_training=self.is_training, norm=self.norm, reuse=self.reuse, name='conv3') # (?,w/4, h/4, 16) # use 3 residual blocks res_output = ops.n_res_blocks(conv3, reuse=self.reuse, n=3) # (?,w/4, h/4, 16) shared = res_output[:, :, :, 0:self.nfs] #print("size shared ",shared) exclusive = res_output[:, :, :, self.nfs:] # set reuse=True for next call self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) return shared, exclusive
def test_c7s1_k(self): input = tf.placeholder(tf.float32, shape=[64, 256, 256, 3]) result = ops.c7s1_k(input, 32) 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 __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