Example #1
0
    def _save_preprocessed_img(
        self, sess, img_preprocessed, img_ph, img_np, img_name
    ):
        """Save preprocessed image when necessary

        Parameters
        ----------
        sess : tf.Session
            Session is required to perform compute
        img_preprocessed: tf.Tensor
            Tensor graph for preprocessing
        img_ph: tf.Tensor
            Tensor input placeholder
        img_np: np.ndarray
            N-dimensional array with data
        img_name: string
            Image file name
        """
        # Create a numpy array of the cropped image
        img_cropped_resized_np = sess.run(
            img_preprocessed,  # graph for preprocessing images
            feed_dict={img_ph: img_np},  # numpy input
        )
        output_path = os.path.join(self.output, '{}.jpg'.format(img_name))
        image_utils.save_np_image(img_cropped_resized_np, output_path)
        self.logger.debug('Image saved at {}'.format(output_path))
def main(unused_argv=None):
    # Load image
    image = np.expand_dims(image_utils.load_np_image(FLAGS.input_image), 0)

    which_styles = ast.literal_eval(FLAGS.which_styles)

    with tf.Graph().as_default(), tf.Session() as sess:
        stylized_images = model.transform(
            tf.concat(0, [image for _ in range(len(which_styles))]),
            normalizer_params={
                'labels': tf.constant(which_styles),
                'num_categories': FLAGS.num_styles,
                'center': True,
                'scale': True
            })
        model_saver = tf.train.Saver(tf.all_variables())
        checkpoint = FLAGS.checkpoint
        if tf.gfile.IsDirectory(checkpoint):
            checkpoint = tf.train.latest_checkpoint(checkpoint)
            tf.logging.info(
                'loading latest checkpoint file: {}'.format(checkpoint))
        model_saver.restore(sess, checkpoint)

        stylized_images = stylized_images.eval()
        for which, stylized_image in zip(which_styles, stylized_images):
            image_utils.save_np_image(
                stylized_image[None, ...],
                '{}/{}_{}.png'.format(FLAGS.output_dir, FLAGS.output_basename,
                                      which))
def _multiple_images(input_image, which_styles, output_dir):
  """Stylizes an image into a set of styles and writes them to disk."""
  with tf.Graph().as_default(), tf.Session() as sess:
    stylized_images = model.transform(
        tf.concat([input_image for _ in range(len(which_styles))], 0),
        normalizer_params={
            'labels': tf.constant(which_styles),
            'num_categories': FLAGS.num_styles,
            'center': True,
            'scale': True})
    _load_checkpoint(sess, FLAGS.checkpoint)

    stylized_images = stylized_images.eval()
    for which, stylized_image in zip(which_styles, stylized_images):
      image_utils.save_np_image(
          stylized_image[None, ...],
          '{}/{}_{}.png'.format(output_dir, FLAGS.output_basename, which))
Example #4
0
def _multiple_images(input_image, which_styles, output_dir):
  """Stylizes an image into a set of styles and writes them to disk."""
  with tf.Graph().as_default(), tf.Session() as sess:
    stylized_images = model.transform(
        tf.concat_v2([input_image for _ in range(len(which_styles))], 0),
        normalizer_params={
            'labels': tf.constant(which_styles),
            'num_categories': FLAGS.num_styles,
            'center': True,
            'scale': True})
    _load_checkpoint(sess, FLAGS.checkpoint)

    stylized_images = stylized_images.eval()
    for which, stylized_image in zip(which_styles, stylized_images):
      image_utils.save_np_image(
          stylized_image[None, ...],
          '{}/{}_{}.png'.format(output_dir, FLAGS.output_basename, which))
def _multiple_styles(input_image, which_styles, output_dir):
  """Stylizes image into a linear combination of styles and writes to disk."""
  with tf.Graph().as_default(), tf.Session() as sess:
    mixture = _style_mixture(which_styles, FLAGS.num_styles)
    stylized_images = model.transform(
        input_image,
        normalizer_fn=ops.weighted_instance_norm,
        normalizer_params={
            'weights': tf.constant(mixture),
            'num_categories': FLAGS.num_styles,
            'center': True,
            'scale': True})
    _load_checkpoint(sess, FLAGS.checkpoint)

    stylized_image = stylized_images.eval()
    image_utils.save_np_image(
        stylized_image,
        os.path.join(output_dir, '%s_%s.png' % (
            FLAGS.output_basename, _describe_style(which_styles))))
Example #6
0
def _multiple_styles(input_image, which_styles, output_dir):
  """Stylizes image into a linear combination of styles and writes to disk."""
  with tf.Graph().as_default(), tf.Session() as sess:
    mixture = _style_mixture(which_styles, FLAGS.num_styles)
    stylized_images = model.transform(
        input_image,
        normalizer_fn=ops.weighted_instance_norm,
        normalizer_params={
            'weights': tf.constant(mixture),
            'num_categories': FLAGS.num_styles,
            'center': True,
            'scale': True})
    _load_checkpoint(sess, FLAGS.checkpoint)

    stylized_image = stylized_images.eval()
    image_utils.save_np_image(
        stylized_image,
        os.path.join(output_dir, '%s_%s.png' % (
            FLAGS.output_basename, _describe_style(which_styles))))
Example #7
0
def main(unused_argv=None):
  tf.logging.set_verbosity(tf.logging.INFO)
  if not tf.gfile.Exists(FLAGS.output_dir):
    tf.gfile.MkDir(FLAGS.output_dir)

  with tf.Graph().as_default(), tf.Session() as sess:
    # Defines place holder for the style image.
    style_img_ph = tf.placeholder(tf.float32, shape=[None, None, 3])
    if FLAGS.style_square_crop:
      style_img_preprocessed = image_utils.center_crop_resize_image(
          style_img_ph, FLAGS.style_image_size)
    else:
      style_img_preprocessed = image_utils.resize_image(style_img_ph,
                                                        FLAGS.style_image_size)

    # Defines place holder for the content image.
    content_img_ph = tf.placeholder(tf.float32, shape=[None, None, 3])
    if FLAGS.content_square_crop:
      content_img_preprocessed = image_utils.center_crop_resize_image(
          content_img_ph, FLAGS.image_size)
    else:
      content_img_preprocessed = image_utils.resize_image(
          content_img_ph, FLAGS.image_size)

    # Defines the model.
    stylized_images, _, _, bottleneck_feat = build_model.build_model(
        content_img_preprocessed,
        style_img_preprocessed,
        trainable=False,
        is_training=False,
        inception_end_point='Mixed_6e',
        style_prediction_bottleneck=100,
        adds_losses=False)

    if tf.gfile.IsDirectory(FLAGS.checkpoint):
      checkpoint = tf.train.latest_checkpoint(FLAGS.checkpoint)
    else:
      checkpoint = FLAGS.checkpoint
      tf.logging.info('loading latest checkpoint file: {}'.format(checkpoint))

    init_fn = slim.assign_from_checkpoint_fn(checkpoint,
                                             slim.get_variables_to_restore())
    sess.run([tf.local_variables_initializer()])
    init_fn(sess)

    # Gets the list of the input style images.
    style_img_list = tf.gfile.Glob(FLAGS.style_images_paths)
    if len(style_img_list) > FLAGS.maximum_styles_to_evaluate:
      np.random.seed(1234)
      style_img_list = np.random.permutation(style_img_list)
      style_img_list = style_img_list[:FLAGS.maximum_styles_to_evaluate]

    # Gets list of input content images.
    content_img_list = tf.gfile.Glob(FLAGS.content_images_paths)

    for content_i, content_img_path in enumerate(content_img_list):
      content_img_np = image_utils.load_np_image_uint8(content_img_path)[:, :, :
                                                                         3]
      content_img_name = os.path.basename(content_img_path)[:-4]

      # Saves preprocessed content image.
      inp_img_croped_resized_np = sess.run(
          content_img_preprocessed, feed_dict={
              content_img_ph: content_img_np
          })
      image_utils.save_np_image(inp_img_croped_resized_np,
                                os.path.join(FLAGS.output_dir,
                                             '%s.jpg' % (content_img_name)))

      # Computes bottleneck features of the style prediction network for the
      # identity transform.
      identity_params = sess.run(
          bottleneck_feat, feed_dict={style_img_ph: content_img_np})

      for style_i, style_img_path in enumerate(style_img_list):
        if style_i > FLAGS.maximum_styles_to_evaluate:
          break
        style_img_name = os.path.basename(style_img_path)[:-4]
        style_image_np = image_utils.load_np_image_uint8(style_img_path)[:, :, :
                                                                         3]

        if style_i % 10 == 0:
          tf.logging.info('Stylizing (%d) %s with (%d) %s' %
                          (content_i, content_img_name, style_i,
                           style_img_name))

        # Saves preprocessed style image.
        style_img_croped_resized_np = sess.run(
            style_img_preprocessed, feed_dict={
                style_img_ph: style_image_np
            })
        image_utils.save_np_image(style_img_croped_resized_np,
                                  os.path.join(FLAGS.output_dir,
                                               '%s.jpg' % (style_img_name)))

        # Computes bottleneck features of the style prediction network for the
        # given style image.
        style_params = sess.run(
            bottleneck_feat, feed_dict={style_img_ph: style_image_np})

        interpolation_weights = ast.literal_eval(FLAGS.interpolation_weights)
        # Interpolates between the parameters of the identity transform and
        # style parameters of the given style image.
        for interp_i, wi in enumerate(interpolation_weights):
          stylized_image_res = sess.run(
              stylized_images,
              feed_dict={
                  bottleneck_feat:
                      identity_params * (1 - wi) + style_params * wi,
                  content_img_ph:
                      content_img_np
              })

          # Saves stylized image.
          image_utils.save_np_image(
              stylized_image_res,
              os.path.join(FLAGS.output_dir, '%s_stylized_%s_%d.jpg' %
                           (content_img_name, style_img_name, interp_i)))
Example #8
0
def main(unused_argv=None):
    print('timer start')
    start = time.time()

    words = [
        '布', '植物', 'ガラス', '革', '金属', '紙', 'プラスチック', '石', '水', '木', '樹脂',
        'アクリル', 'アルミニウム', '牛皮', 'レンガ', '絹'
    ]

    config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))
    sess = tf.Session(config=config)

    tf.logging.set_verbosity(tf.logging.INFO)
    if not tf.gfile.Exists(FLAGS.output_dir):
        tf.gfile.MkDir(FLAGS.output_dir)

    with tf.Graph().as_default(), sess:
        # Defines place holder for the style image.
        style_img_ph = tf.placeholder(tf.float32, shape=[None, None, 3])
        if FLAGS.style_square_crop:
            style_img_preprocessed = image_utils.center_crop_resize_image(
                style_img_ph, FLAGS.style_image_size)
        else:
            style_img_preprocessed = image_utils.resize_image(
                style_img_ph, FLAGS.style_image_size)

        # Defines place holder for the content image.
        content_img_ph = tf.placeholder(tf.float32, shape=[None, None, 3])
        if FLAGS.content_square_crop:
            content_img_preprocessed = image_utils.center_crop_resize_image(
                content_img_ph, FLAGS.image_size)
        else:
            content_img_preprocessed = image_utils.resize_image(
                content_img_ph, FLAGS.image_size)

        # Defines the model.
        stylized_images, _, _, bottleneck_feat = build_model.build_model(
            content_img_preprocessed,
            style_img_preprocessed,
            trainable=False,
            is_training=False,
            inception_end_point='Mixed_6e',
            style_prediction_bottleneck=100,
            adds_losses=False)

        if tf.gfile.IsDirectory(FLAGS.checkpoint):
            checkpoint = tf.train.latest_checkpoint(FLAGS.checkpoint)
        else:
            checkpoint = FLAGS.checkpoint
            tf.logging.info(
                'loading latest checkpoint file: {}'.format(checkpoint))

        init_fn = slim.assign_from_checkpoint_fn(
            checkpoint, slim.get_variables_to_restore())
        sess.run([tf.local_variables_initializer()])
        init_fn(sess)

        # Gets the list of the input style images.
        style_img_list = tf.gfile.Glob(FLAGS.style_images_paths)
        if len(style_img_list) > FLAGS.maximum_styles_to_evaluate:
            np.random.seed(1234)
            style_img_list = np.random.permutation(style_img_list)
            style_img_list = style_img_list[:FLAGS.maximum_styles_to_evaluate]

        # Gets list of input content images.
        content_img_list = tf.gfile.Glob(FLAGS.content_images_paths)

        j = -1
        for content_i, content_img_path in enumerate(content_img_list):
            j += 1
            content_img_np = image_utils.load_np_image_uint8(
                content_img_path)[:, :, :3]
            content_img_name = os.path.basename(content_img_path)[:-4]

            # Saves preprocessed content image.
            inp_img_croped_resized_np = sess.run(
                content_img_preprocessed,
                feed_dict={content_img_ph: content_img_np})
            image_utils.save_np_image(
                inp_img_croped_resized_np,
                os.path.join(FLAGS.output_dir, '%s.jpg' % (content_img_name)))

            if FLAGS.color_preserve is True:
                print('color preserve mode!')
                # convert content iamge to ycc from bgr
                height, width, channels = inp_img_croped_resized_np[
                    0].shape[:3]
                # print(inp_img_croped_resized_np)
                wgap = 4 - (
                    width % 4
                )  # fuking translater made some gaps because of decoder
                hgap = 4 - (height % 4)
                inp_img_croped_resized_np = inp_img_croped_resized_np * 255
                content_img_np_ycc = cv2.cvtColor(inp_img_croped_resized_np[0],
                                                  cv2.COLOR_RGB2YCR_CB)
                # print(content_img_np_ycc)
                zeros = np.zeros((height, width), content_img_np_ycc.dtype)
                zeros = zeros + 128  # YCC's zero is center of 255
                tmp = cv2.cvtColor(content_img_np_ycc, cv2.COLOR_YCR_CB2BGR)
                cv2.imwrite("gray.jpg", tmp)
                # print(zeros)
                Ycontent, Crcontent, Cbcontent = cv2.split(content_img_np_ycc)
                # print(Ycontent.shape, Crcontent.shape, Cbcontent.shape, zeros.shape)
                # print(Crcontent)
                # print(content_img_np_ycc)
                # content_img_np_ycc_y = cv2.merge((Y, zeros, zeros))
                # content_img_np_gry = cv2.cvtColor(content_img_np_ycc_y, cv2.COLOR_YCR_CB2RGB)
                # print(content_img_np_gry)
                # cv2.imwrite("gray.jpg", content_img_np_gry)
                # print(np.shape(content_img_np))
                # content_img_np = content_img_np_gry

            # Computes bottleneck features of the style prediction network for the
            # identity transform.

            identity_params = sess.run(
                bottleneck_feat, feed_dict={style_img_ph: content_img_np})

            i = 0
            for word in words:
                # word = words[i]
                print(word)
                i += 1
                # if style_i > FLAGS.maximum_styles_to_evaluate:
                # break
                # style_img_name = os.path.basename(style_img_path)[:-4]
                # style_image_np = image_utils.load_np_image_uint8(style_img_path)[:, :, :
                # 3]

                # if style_i % 10 == 0:
                # tf.logging.info('Stylizing (%d) %s with (%d) %s' %
                # (content_i, content_img_name, style_i,
                # style_img_name))

                # Saves preprocessed style image.
                # style_img_croped_resized_np = sess.run(
                # style_img_preprocessed, feed_dict={
                # style_img_ph: style_image_np
                # })
                # image_utils.save_np_image(style_img_croped_resized_np,
                # os.path.join(FLAGS.output_dir,
                # '%s.jpg' % (style_img_name)))

                # Computes bottleneck features of the style prediction network for the
                # given style image.
                # style_params_ori = sess.run(
                # bottleneck_feat, feed_dict={style_img_ph: style_image_np})

                # print(np.shape(style_params))
                picklename = 'params/{}_{}.pickle'.format(word, j)
                f = open(picklename, 'r')
                style_params = pickle.load(f)
                # print(style_params)

                # print('diff of original para and made para:')
                # print(style_params_ori - style_params)

                interpolation_weights = ast.literal_eval(
                    FLAGS.interpolation_weights)
                # Interpolates between the parameters of the identity transform and
                # style parameters of the given style image.
                for interp_i, wi in enumerate(interpolation_weights):
                    stylized_image_res = sess.run(
                        stylized_images,
                        feed_dict={
                            bottleneck_feat:
                            identity_params * (1 - wi) + style_params * wi,
                            content_img_ph:
                            content_img_np
                        })

                    if FLAGS.color_preserve is True:
                        # print(stylized_image_res[0].shape)
                        stylized_image_res_ycc = cv2.cvtColor(
                            stylized_image_res[0], cv2.COLOR_RGB2YCR_CB)
                        Ystylized, Crstylized, Cbstylized = cv2.split(
                            stylized_image_res_ycc)
                        if wgap == 4:  # if original image is just fit
                            Ystylized_crop = Ystylized * 255
                        else:
                            Ystylized_crop = Ystylized[:, :-1 * wgap] * 255
                        if hgap == 4:
                            Ystylized_crop = Ystylized_crop
                        else:
                            Ystylized_crop = Ystylized_crop[:-1 * hgap, :]
                        print(Ystylized_crop.shape, Cbcontent.shape)
                        # print(wgap)
                        swapped_ycc = cv2.merge(
                            (Ystylized_crop, Crcontent, Cbcontent))
                        # print(swapped_ycc)
                        stylized_image_res = cv2.cvtColor(
                            swapped_ycc, cv2.COLOR_YCR_CB2BGR)
                        # print(stylized_image_res)
                        cv2.imwrite(
                            os.path.join(
                                FLAGS.output_dir, '%s_stylized_%s_%d.jpg' %
                                (content_img_name, word, interp_i)),
                            stylized_image_res)

                    # Saves stylized image.
                    else:
                        image_utils.save_np_image(
                            stylized_image_res,
                            os.path.join(
                                FLAGS.output_dir, '%s_stylized_%s_%d.jpg' %
                                (content_img_name, word, interp_i)))

    elapsed_time = time.time() - start
    print("timer stop")
    print(elapsed_time)