Esempio n. 1
0
def get_model(input_height, input_width, channels=3, input_count=3):
    # X, Y, Mask
    model_input = tf.keras.Input((input_count, input_height, input_width, channels))
    input_noise, input_real, input_mask = model_input
    encoded = mm.encoder(cat(input_noise, input_mask))
    decoded = mm.decoder(encoded)
    image_result = decoded * (1 - input_mask) + input_real * input_mask
    probabilities = mm.discriminator_red(cat(image_result, input_real))
    image_loss, input_loss = probabilities
    hinge_loss = tf.keras.activations.relu(cat(1-image_loss, 1+input_loss))
Esempio n. 2
0
def inference(from_file_path, args, batch_size=1, device_t='/gpu:0'):
    imgs, style_name = args[0:2]
    # check if the specified model exists
    checkpoint_dir = os.path.join(root_models_dir, style_name,
                                  "checkpoint_long")
    assert os.path.exists(checkpoint_dir), 'Checkpoint not found!'

    ngf = 32  # args.ngf
    OPTIONS = namedtuple('OPTIONS', 'gf_dim  is_training')
    options = OPTIONS._make((ngf, False))

    tfconfig = tf.ConfigProto(
        allow_soft_placement=False)  # THIS IS DIFFERENT FROM IMPL_1
    tfconfig.gpu_options.allow_growth = True
    tf.reset_default_graph()
    with tf.Session(config=tfconfig) as sess:
        # ==================== Define placeholders. ===================== #
        with tf.name_scope('placeholder'):
            input_photo = tf.placeholder(dtype=tf.float32,
                                         shape=[batch_size, None, None, 3],
                                         name='photo')

        # ===================== Wire the graph. ========================= #
        # Encode input images.
        input_photo_features = encoder(image=input_photo,
                                       options=options,
                                       reuse=False)

        # Decode obtained features.
        output_photo = decoder(features=input_photo_features,
                               options=options,
                               reuse=False)

        init_op = tf.global_variables_initializer()
        sess.run(init_op)

        saver = tf.train.Saver()
        if os.path.isdir(checkpoint_dir):
            ckpt = tf.train.get_checkpoint_state(checkpoint_dir)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)
            else:
                raise Exception("No checkpoint found...")
        else:
            saver.restore(sess, checkpoint_dir)

        model_args = (sess, input_photo, output_photo)
        if from_file_path:
            run_from_file_paths(model_args, args)
        else:
            return run_from_layers(model_args, args[0])
def setup(opts):
    sess = tf.Session()
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    with tf.name_scope('placeholder'):
        input_photo = tf.placeholder(dtype=tf.float32,
                                     shape=[1, None, None, 3],
                                     name='photo')
    input_photo_features = encoder(image=input_photo,
                                   options={'gf_dim': 32},
                                   reuse=False)
    output_photo = decoder(features=input_photo_features,
                           options={'gf_dim': 32},
                           reuse=False)
    saver = tf.train.Saver()
    path = opts['styleCheckpoint']
    model_name = [
        p for p in os.listdir(path) if os.path.isdir(os.path.join(path, p))
    ][0]
    checkpoint_dir = os.path.join(path, model_name, 'checkpoint_long')
    ckpt = tf.train.get_checkpoint_state(checkpoint_dir)
    ckpt_name = os.path.basename(ckpt.model_checkpoint_path)
    saver.restore(sess, os.path.join(checkpoint_dir, ckpt_name))
    return dict(sess=sess, input_photo=input_photo, output_photo=output_photo)
Esempio n. 4
0
    def __init__(self, input_height=256, input_width=256, batch_size=1,
                 bar_model_name=None, bar_checkpoint_name=None, mosaic_model_name=None,
                 mosaic_checkpoint_name=None, is_mosaic=False):
        self.bar_model_name = bar_model_name
        self.bar_checkpoint_name = bar_checkpoint_name
        self.mosaic_model_name = mosaic_model_name
        self.mosaic_checkpoint_name = mosaic_checkpoint_name
        self.is_mosaic = is_mosaic

        self.input_height = input_height
        self.input_width = input_width
        self.batch_size = batch_size

        self.check_model_file()
        self.build_model()
        self.discriminator = mm.discriminator_red(tf.keras.Input(batch_size,
                                                                 input_height,
                                                                 input_width,
                                                                 3)
                                                  )
        self.encoder = mm.encoder(tf.keras.Input(batch_size,
                                                 input_height,
                                                 input_width,
                                                 6))
Esempio n. 5
0
    def build_model(self):
        # ------- variables
        tf.compat.v1.disable_eager_execution()
        self.X = tf.compat.v1.placeholder(
            tf.float32,
            [self.batch_size, self.input_height, self.input_width, 3])
        self.Y = tf.compat.v1.placeholder(
            tf.float32,
            [self.batch_size, self.input_height, self.input_width, 3])

        self.MASK = tf.compat.v1.placeholder(
            tf.float32,
            [self.batch_size, self.input_height, self.input_width, 3])
        IT = tf.compat.v1.placeholder(tf.float32)

        # ------- structure

        input = tf.concat([self.X, self.MASK], 3)

        vec_en = mm.encoder(input, reuse=False, name='G_en')

        vec_con = mm.contextual_block(vec_en,
                                      vec_en,
                                      self.MASK,
                                      3,
                                      50.0,
                                      'CB1',
                                      stride=1)

        I_co = mm.decoder(vec_en,
                          self.input_height,
                          self.input_height,
                          reuse=False,
                          name='G_de')
        I_ge = mm.decoder(vec_con,
                          self.input_height,
                          self.input_height,
                          reuse=True,
                          name='G_de')

        self.image_result = I_ge * (1 - self.MASK) + self.Y * self.MASK

        D_real_red = mm.discriminator_red(self.Y, reuse=False, name='disc_red')
        D_fake_red = mm.discriminator_red(self.image_result,
                                          reuse=True,
                                          name='disc_red')

        # ------- Loss

        Loss_D_red = tf.reduce_mean(
            input_tensor=tf.nn.relu(1 + D_fake_red)) + tf.reduce_mean(
                input_tensor=tf.nn.relu(1 - D_real_red))

        Loss_D = Loss_D_red

        Loss_gan_red = -tf.reduce_mean(input_tensor=D_fake_red)

        Loss_gan = Loss_gan_red

        Loss_s_re = tf.reduce_mean(input_tensor=tf.abs(I_ge - self.Y))
        Loss_hat = tf.reduce_mean(input_tensor=tf.abs(I_co - self.Y))

        A = tf.image.rgb_to_yuv((self.image_result + 1) / 2.0)
        A_Y = tf.cast(A[:, :, :, 0:1] * 255.0, dtype=tf.int32)

        B = tf.image.rgb_to_yuv((self.Y + 1) / 2.0)
        B_Y = tf.cast(B[:, :, :, 0:1] * 255.0, dtype=tf.int32)

        ssim = tf.reduce_mean(input_tensor=tf.image.ssim(A_Y, B_Y, 255.0))

        alpha = IT / 1000000

        Loss_G = 0.1 * Loss_gan + 10 * Loss_s_re + 5 * (1 - alpha) * Loss_hat

        # --------------------- variable & optimizer

        var_D = [
            v for v in tf.compat.v1.global_variables()
            if v.name.startswith('disc_red')
        ]
        var_G = [
            v for v in tf.compat.v1.global_variables()
            if v.name.startswith('G_en') or v.name.startswith('G_de')
            or v.name.startswith('CB1')
        ]

        update_ops = tf.compat.v1.get_collection(
            tf.compat.v1.GraphKeys.UPDATE_OPS)

        with tf.control_dependencies(update_ops):
            optimize_D = tf.compat.v1.train.AdamOptimizer(learning_rate=0.0004,
                                                          beta1=0.5,
                                                          beta2=0.9).minimize(
                                                              Loss_D,
                                                              var_list=var_D)
            optimize_G = tf.compat.v1.train.AdamOptimizer(learning_rate=0.0001,
                                                          beta1=0.5,
                                                          beta2=0.9).minimize(
                                                              Loss_G,
                                                              var_list=var_G)

        config = tf.compat.v1.ConfigProto()
        # config.gpu_options.per_process_gpu_memory_fraction = 0.4
        # config.gpu_options.allow_growth = False

        self.sess = tf.compat.v1.Session(config=config)

        init = tf.compat.v1.global_variables_initializer()
        self.sess.run(init)
        saver = tf.compat.v1.train.Saver()

        if self.is_mosaic:
            Restore = tf.compat.v1.train.import_meta_graph(
                self.mosaic_model_name)
            Restore.restore(
                self.sess,
                tf.train.latest_checkpoint(self.mosaic_checkpoint_name))
        else:
            Restore = tf.compat.v1.train.import_meta_graph(self.bar_model_name)
            Restore.restore(
                self.sess,
                tf.train.latest_checkpoint(self.bar_checkpoint_name))
Esempio n. 6
0
saving_iter = 10000
Max_iter = 1000000

# ------- variables

X = tf.placeholder(tf.float32, [BATCH_SIZE, HEIGHT, WIDTH, 3])
Y = tf.placeholder(tf.float32, [BATCH_SIZE, HEIGHT, WIDTH, 3])

MASK = tf.placeholder(tf.float32, [BATCH_SIZE, HEIGHT, WIDTH, 3])
IT = tf.placeholder(tf.float32)

# ------- structure

input = tf.concat([X, MASK], 3)

vec_en = mm.encoder(input, reuse=False)

vec_con = mm.contextual_block(vec_en, vec_en, MASK)

I_co = mm.decoder(vec_en, reuse=False)
I_ge = mm.decoder(vec_con, reuse=True)

image_result = I_ge * (1 - MASK) + Y * MASK

D_real_red = mm.discriminator_red(Y, reuse=False)
D_fake_red = mm.discriminator_red(image_result, reuse=True)

# ------- Loss

Loss_D_red = tf.reduce_mean(tf.nn.relu(1 + D_fake_red)) + tf.reduce_mean(
    tf.nn.relu(1 - D_real_red))
Esempio n. 7
0
saving_iter = 10000
Max_iter = 1000000

# ------- variables

X = tf.placeholder(tf.float32, [batch_size, Height, Width, 3])
Y = tf.placeholder(tf.float32, [batch_size, Height, Width, 3])

MASK = tf.placeholder(tf.float32, [batch_size, Height, Width, 3])
IT = tf.placeholder(tf.float32)

# ------- structure

input = tf.concat([X, MASK], 3)

vec_en = mm.encoder(input, reuse=False, name='G_en')

vec_con = mm.contextual_block(vec_en, vec_en, MASK, 3, 50.0, 'CB1', stride=1)

I_co = mm.decoder(vec_en, Height, reuse=False, name='G_de')
I_ge = mm.decoder(vec_con, Height, reuse=True, name='G_de')

image_result = I_ge * (1 - MASK) + Y * MASK

D_real_red = mm.discriminator_red(Y, reuse=False, name='disc_red')
D_fake_red = mm.discriminator_red(image_result, reuse=True, name='disc_red')

# ------- Loss

Loss_D_red = tf.reduce_mean(tf.nn.relu(1 + D_fake_red)) + tf.reduce_mean(
    tf.nn.relu(1 - D_real_red))