import pandas as pd
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import os
import glob2
tf.__version__
'2.7.0'
os.environ['TFHUB_MODEL_LOAD_FORMAT'] = 'COMPRESSED'
import IPython.display as display
import matplotlib as mpl
import time
import functools
def tensor_to_image(tensor):
    tensor = tensor*255
    tensor = np.array(tensor, dtype=np.uint8)
    if np.ndim(tensor)>3:
        assert tensor.shape[0] == 1
        tensor = tensor[0]
    return PIL.Image.fromarray(tensor)
content_path = 'data/images/style_d2.jpg'
style_path = 'data/images/style.jpg'
def load_img(path_to_img):
    max_dim = 512
    img = tf.io.read_file(path_to_img)
    img = tf.image.decode_image(img, channels=3)
    img = tf.image.convert_image_dtype(img, tf.float32)

    shape = tf.cast(tf.shape(img)[:-1],tf.float32)
    long_dim = max(shape)
    scale = max_dim / long_dim
    new_shape = tf.cast(shape * scale, tf.int32)
    img = tf.image.resize(img, new_shape)
    img = img[tf.newaxis, :]
    return img

  
def imshow(image, title=None):
  if len(image.shape) > 3:
    image = tf.squeeze(image, axis=0)

  plt.imshow(image)
  if title:
    plt.title(title)
content_image = load_img(content_path)
style_image = load_img(style_path)

plt.subplot(1, 2, 1)
imshow(content_image, 'Content Image')

plt.subplot(1, 2, 2)
imshow(style_image, 'Style Image')
%pip install tensorflow_hub
Collecting tensorflow_hubNote: you may need to restart the kernel to use updated packages.
  Downloading tensorflow_hub-0.12.0-py2.py3-none-any.whl (108 kB)
Requirement already satisfied: numpy>=1.12.0 in c:\users\user\anaconda3\lib\site-packages (from tensorflow_hub) (1.20.3)
Requirement already satisfied: protobuf>=3.8.0 in c:\users\user\anaconda3\lib\site-packages (from tensorflow_hub) (3.19.1)
Installing collected packages: tensorflow-hub
Successfully installed tensorflow-hub-0.12.0

import tensorflow_hub as hub
# load hub model
 
stylized_image = model(tf.constant(content_image), tf.constant(style_image))[0]
#tensor_to_image(stylized_image)#.save('data/images/stylized.jpg')
import PIL
from PIL import Image
tensor_to_image(stylized_image).save('data/images/style/stylized.jpg')
content_path = 'data/images/style.jpg'
style_path = 'data/images/vdl.jpg'
content_image = load_img(content_path)
style_image = load_img(style_path)

plt.subplot(1, 2, 1)
imshow(content_image, 'Content Image')

plt.subplot(1, 2, 2)
imshow(style_image, 'Style Image')
stylized_image = model(tf.constant(content_image), tf.constant(style_image))[0]
tensor_to_image(stylized_image).save('data/images/style/ad5.jpg')