def build_partial_resnet_block(inputres, mask_in, dim, name="resnet"): """build a single block of resnet. :param inputres: inputres :param dim: dim :param name: name :param padding: for tensorflow version use REFLECT; for pytorch version use CONSTANT :return: a single block of resnet. """ padding = "CONSTANT" with tf.variable_scope(name): inputres_ = tf.pad(inputres, [[0, 0], [1, 1], [1, 1], [0, 0]], padding) mask_in_ = tf.pad(mask_in, [[0, 0], [1, 1], [1, 1], [0, 0]], padding) out_res, mask = layers.general_partial_conv2d(inputres_, mask_in_, tf.constant(False, dtype=bool), dim, 3, 3, 1, 1, 0.02, name="c1") out_res = tf.pad(out_res, [[0, 0], [1, 1], [1, 1], [0, 0]], padding) mask = tf.pad(mask, [[0, 0], [1, 1], [1, 1], [0, 0]], padding) out_res_in = tf.multiply(out_res, mask) out_res, mask = layers.general_partial_conv2d(out_res_in, mask, tf.constant(False, dtype=bool), dim, 3, 3, 1, 1, 0.02, name="c2", do_relu=False) out_res_in = tf.multiply(out_res, mask) mask_end = tf.cast(tf.greater(mask + mask_in, 0.), tf.float32) return tf.nn.relu(out_res_in + inputres), mask_end
def discriminator_bis(inputdisc, mask, transition_rate, donorm, name="discriminator"): """ Instead of normal convolutions, We make use of [Uhrig et al: Sparsity Invariant CNNs]. However we omit the normalizing factor they use by replacing it by 1. This removes the effect of the bias coming from masked regions in the input. """ with tf.variable_scope(name): mask = tf.cast(tf.greater_equal(mask, transition_rate), tf.float32) inputdisc = tf.multiply(inputdisc, mask) f = 4 padw = 2 pad_input = tf.pad(inputdisc, [[0, 0], [padw, padw], [padw, padw], [0, 0]], "CONSTANT") pad_mask = tf.pad(mask, [[0, 0], [padw, padw], [padw, padw], [0, 0]], "CONSTANT") o_c1, mask_1 = layers.general_partial_conv2d(pad_input, pad_mask, donorm, ndf, f, f, 2, 2, 0.02, "VALID", "c1", relufactor=0.2) o_c1 = tf.multiply(o_c1, mask_1) pad_o_c1 = tf.pad(o_c1, [[0, 0], [padw, padw], [padw, padw], [0, 0]], "CONSTANT") pad_mask = tf.pad(mask_1, [[0, 0], [padw, padw], [padw, padw], [0, 0]], "CONSTANT") o_c2, mask_2 = layers.general_partial_conv2d(pad_o_c1, pad_mask, donorm, ndf * 2, f, f, 2, 2, 0.02, "VALID", "c2", relufactor=0.2) o_c2 = tf.multiply(o_c2, mask_2) pad_o_c2 = tf.pad(o_c2, [[0, 0], [padw, padw], [padw, padw], [0, 0]], "CONSTANT") pad_mask = tf.pad(mask_2, [[0, 0], [padw, padw], [padw, padw], [0, 0]], "CONSTANT") o_c3, mask_3 = layers.general_partial_conv2d(pad_o_c2, pad_mask, donorm, ndf * 4, f, f, 2, 2, 0.02, "VALID", "c3", relufactor=0.2) o_c3 = tf.multiply(o_c3, mask_3) pad_o_c3 = tf.pad(o_c3, [[0, 0], [padw, padw], [padw, padw], [0, 0]], "CONSTANT") pad_mask = tf.pad(mask_3, [[0, 0], [padw, padw], [padw, padw], [0, 0]], "CONSTANT") o_c4, mask_4 = layers.general_partial_conv2d(pad_o_c3, pad_mask, donorm, ndf * 8, f, f, 1, 1, 0.02, "VALID", "c4", relufactor=0.2) o_c4 = tf.multiply(o_c4, mask_4) pad_o_c4 = tf.pad(o_c4, [[0, 0], [padw, padw], [padw, padw], [0, 0]], "CONSTANT") pad_mask = tf.pad(mask_4, [[0, 0], [padw, padw], [padw, padw], [0, 0]], "CONSTANT") o_c5, mask_5 = layers.general_partial_conv2d(pad_o_c4, pad_mask, tf.constant(False, dtype=bool), 1, f, f, 1, 1, 0.02, "VALID", "c5", do_relu=False) return o_c5
def build_generator_resnet_9blocks_bis(inputgen, mask, transition_rate, name="generator", skip=False): """ Instead of normal convolutions, We make use of [Uhrig et al: Sparsity Invariant CNNs]. However we omit the normalizing factor they use by replaqcing it by 1. This removes the effect of the bias coming from masked regions in the input. """ with tf.variable_scope(name): f = 7 ks = 3 padding = "CONSTANT" inputgen = tf.pad(inputgen, [[0, 0], [ks, ks], [ks, ks], [0, 0]], padding) mask = tf.pad(mask, [[0, 0], [ks, ks], [ks, ks], [0, 0]], padding) o_c1, mask = layers.general_partial_conv2d(inputgen, mask, tf.constant(False, dtype=bool), ngf, f, f, 1, 1, 0.02, name="c1") o_c1_in = tf.multiply(o_c1, mask) o_c2, mask = layers.general_partial_conv2d(o_c1_in, mask, tf.constant(False, dtype=bool), ngf * 2, ks, ks, 2, 2, 0.02, padding='same', name="c2") o_c2_in = tf.multiply(o_c2, mask) o_c3, mask = layers.general_partial_conv2d(o_c2_in, mask, tf.constant(False, dtype=bool), ngf * 4, ks, ks, 2, 2, 0.02, padding='same', name="c3") o_c3_in = tf.multiply(o_c3, mask) o_r1, mask_r1 = build_partial_resnet_block(o_c3_in, mask, ngf * 4, "r1") o_r2, mask_r2 = build_partial_resnet_block(o_r1, mask_r1, ngf * 4, "r2") o_r3, mask_r3 = build_partial_resnet_block(o_r2, mask_r2, ngf * 4, "r3") o_r4, mask_r4 = build_partial_resnet_block(o_r3, mask_r3, ngf * 4, "r4") o_r5, mask_r5 = build_partial_resnet_block(o_r4, mask_r4, ngf * 4, "r5") o_r6, mask_r6 = build_partial_resnet_block(o_r5, mask_r5, ngf * 4, "r6") o_r7, mask_r7 = build_partial_resnet_block(o_r6, mask_r6, ngf * 4, "r7") o_r8, mask_r8 = build_partial_resnet_block(o_r7, mask_r7, ngf * 4, "r8") o_r9, mask_r9 = build_partial_resnet_block(o_r8, mask_r8, ngf * 4, "r9") o_c4 = layers.general_deconv2d(o_r9, [BATCH_SIZE, 128, 128, ngf * 2], ngf * 2, ks, ks, 2, 2, 0.02, "SAME", "c4") o_c5 = layers.general_deconv2d(o_c4, [BATCH_SIZE, 256, 256, ngf], ngf, ks, ks, 2, 2, 0.02, "SAME", "c5") o_c6 = layers.general_conv2d(o_c5, IMG_CHANNELS, f, f, 1, 1, 0.02, "SAME", "c6", do_norm=False, do_relu=False) if skip is True: out_gen = tf.nn.tanh(inputgen + o_c6, "t1") else: out_gen = tf.nn.tanh(o_c6, "t1") return out_gen
def discriminator_bis(inputdisc, mask, transition_rate, donorm, name="discriminator"): with tf.variable_scope(name): mask = tf.cast(tf.greater_equal(mask, transition_rate), tf.float32) inputdisc = tf.multiply(inputdisc, mask) f = 4 padw = 2 pad_input = tf.pad(inputdisc, [[0, 0], [padw, padw], [padw, padw], [0, 0]], "CONSTANT") pad_mask = tf.pad(mask, [[0, 0], [padw, padw], [padw, padw], [0, 0]], "CONSTANT") o_c1, mask_1 = layers.general_partial_conv2d(pad_input, pad_mask, donorm, ndf, f, f, 2, 2, 0.02, "VALID", "c1", relufactor=0.2) # o_c1 = tf.multiply(o_c1, mask_1) pad_o_c1 = tf.pad(o_c1, [[0, 0], [padw, padw], [padw, padw], [0, 0]], "CONSTANT") pad_mask = tf.pad(mask_1, [[0, 0], [padw, padw], [padw, padw], [0, 0]], "CONSTANT") o_c2, mask_2 = layers.general_partial_conv2d(pad_o_c1, pad_mask, donorm, ndf * 2, f, f, 2, 2, 0.02, "VALID", "c2", relufactor=0.2) # o_c2 = tf.multiply(o_c2, mask_2) pad_o_c2 = tf.pad(o_c2, [[0, 0], [padw, padw], [padw, padw], [0, 0]], "CONSTANT") pad_mask = tf.pad(mask_2, [[0, 0], [padw, padw], [padw, padw], [0, 0]], "CONSTANT") o_c3, mask_3 = layers.general_partial_conv2d(pad_o_c2, pad_mask, donorm, ndf * 4, f, f, 2, 2, 0.02, "VALID", "c3", relufactor=0.2) # o_c3 = tf.multiply(o_c3, mask_3) pad_o_c3 = tf.pad(o_c3, [[0, 0], [padw, padw], [padw, padw], [0, 0]], "CONSTANT") pad_mask = tf.pad(mask_3, [[0, 0], [padw, padw], [padw, padw], [0, 0]], "CONSTANT") o_c4, mask_4 = layers.general_partial_conv2d(pad_o_c3, pad_mask, donorm, ndf * 8, f, f, 1, 1, 0.02, "VALID", "c4", relufactor=0.2) # o_c4 = tf.multiply(o_c4, mask_4) pad_o_c4 = tf.pad(o_c4, [[0, 0], [padw, padw], [padw, padw], [0, 0]], "CONSTANT") pad_mask = tf.pad(mask_4, [[0, 0], [padw, padw], [padw, padw], [0, 0]], "CONSTANT") o_c5, mask_5 = layers.general_partial_conv2d(pad_o_c4, pad_mask, tf.constant(False, dtype=bool), 1, f, f, 1, 1, 0.02, "VALID", "c5", do_relu=False) return o_c5
def build_generator_resnet_9blocks_bis(inputgen, mask, transition_rate, name="generator", skip=False): with tf.variable_scope(name): f = 7 ks = 3 padding = "CONSTANT" inputgen = tf.pad(inputgen, [[0, 0], [ks, ks], [ks, ks], [0, 0]], padding) mask = tf.pad(mask, [[0, 0], [ks, ks], [ks, ks], [0, 0]], padding) o_c1, mask = layers.general_partial_conv2d(inputgen, mask, tf.constant(False, dtype=bool), ngf, f, f, 1, 1, 0.02, name="c1") # o_c1_in = tf.multiply(o_c1, mask) o_c2, mask = layers.general_partial_conv2d(o_c1, mask, tf.constant(False, dtype=bool), ngf * 2, ks, ks, 2, 2, 0.02, padding='same', name="c2") # o_c2_in = tf.multiply(o_c2, mask) o_c3, mask = layers.general_partial_conv2d(o_c2, mask, tf.constant(False, dtype=bool), ngf * 4, ks, ks, 2, 2, 0.02, padding='same', name="c3") # o_c3_in = tf.multiply(o_c3, mask) #o_r1, mask_r1 = build_partial_resnet_block(o_c3, mask, ngf * 4, "r1") #o_r2, mask_r2 = build_partial_resnet_block(o_r1, mask_r1, ngf * 4, "r2") #o_r3, mask_r3 = build_partial_resnet_block(o_r2, mask_r2, ngf * 4, "r3") #o_r4, mask_r4 = build_partial_resnet_block(o_r3, mask_r3, ngf * 4, "r4") #o_r5, mask_r5 = build_partial_resnet_block(o_r4, mask_r4, ngf * 4, "r5") #o_r6, mask_r6 = build_partial_resnet_block(o_r5, mask_r5, ngf * 4, "r6") #o_r7, mask_r7 = build_partial_resnet_block(o_r6, mask_r6, ngf * 4, "r7") #o_r8, mask_r8 = build_partial_resnet_block(o_r7, mask_r7, ngf * 4, "r8") #o_r9, mask_r9 = build_partial_resnet_block(o_r8, mask_r8, ngf * 4, "r9") o_r1 = build_resnet_block(o_c3, ngf * 4, "r1", padding) o_r2 = build_resnet_block(o_r1, ngf * 4, "r2", padding) o_r3 = build_resnet_block(o_r2, ngf * 4, "r3", padding) o_r4 = build_resnet_block(o_r3, ngf * 4, "r4", padding) o_r5 = build_resnet_block(o_r4, ngf * 4, "r5", padding) o_r6 = build_resnet_block(o_r5, ngf * 4, "r6", padding) o_r7 = build_resnet_block(o_r6, ngf * 4, "r7", padding) o_r8 = build_resnet_block(o_r7, ngf * 4, "r8", padding) o_r9 = build_resnet_block(o_r8, ngf * 4, "r9", padding) o_c4 = layers.general_deconv2d(o_r9, [BATCH_SIZE, 128, 128, ngf * 2], ngf * 2, ks, ks, 2, 2, 0.02, "SAME", "c4") o_c5 = layers.general_deconv2d(o_c4, [BATCH_SIZE, 256, 256, ngf], ngf, ks, ks, 2, 2, 0.02, "SAME", "c5") o_c6 = layers.general_conv2d(o_c5, IMG_CHANNELS, f, f, 1, 1, 0.02, "SAME", "c6", do_norm=False, do_relu=False) if skip is True: out_gen = tf.nn.tanh(inputgen + o_c6, "t1") else: out_gen = tf.nn.tanh(o_c6, "t1") return out_gen