import numpy as np\nimport IPython.display as display\n\nimport matplotlib.pyplot as plt\nimport matplotlib as mpl\nmpl.rcParams['figure.figsize'] = (12, 12)\nmpl.rcParams['axes.grid'] = False\n\n\nimport PIL.Image\nimport time\nimport functools<\/code><\/pre>\n\n\n\nCreate a function to <\/p>\n\n\n\n
def tensor_to_image(tensor):\n tensor = tensor*255\n tensor = np.array(tensor, dtype=np.uint8)\n if np.ndim(tensor)>3:\n assert tensor.shape[0] == 1\n tensor = tensor[0]\n return PIL.Image.fromarray(tensor)<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
Import the Madia Gras and Painting Images to Colab<\/p>\n\n\n\n
content_path_1 = 'content.jpg'\ncontent_path_2 = 'content2.jpg'\ncontent_path_3 = 'content3.jpg'\nstyle_path_1 = 'reference.jpg'\nstyle_path_2 = 'reference_style.jpg'\nstyle_path_3 = 'reference_style3.jpg'<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
def load_img(path_to_img):\n max_dim = 512\n img = tf.io.read_file(path_to_img)\n img = tf.image.decode_image(img, channels=3)\n img = tf.image.convert_image_dtype(img, tf.float32)\n\n shape = tf.cast(tf.shape(img)[:-1], tf.float32)\n long_dim = max(shape)\n scale = max_dim \/ long_dim\n\n new_shape = tf.cast(shape * scale, tf.int32)\n\n img = tf.image.resize(img, new_shape)\n img = img[tf.newaxis, :]\n return img<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
def imshow(image, title=None):\n if len(image.shape) > 3:\n image = tf.squeeze(image, axis=0)\n\n plt.imshow(image)\n if title:\n plt.title(title)<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
content_image = load_img(content_path_1)\nstyle_image = load_img(style_path_1)\n\nplt.subplot(1, 2, 1)\nimshow(content_image, 'Content Image')\n\nplt.subplot(1, 2, 2)\nimshow(style_image, 'Style Image')<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
Import TensorFlow Hub to use a pre-trained model<\/strong><\/p>\n\n\n\n<\/p>\n\n\n\n
import tensorflow_hub as hub\nhub_model = hub.load('https:\/\/tfhub.dev\/google\/magenta\/arbitrary-image-stylization-v1-256\/2')\nstylized_image = hub_model(tf.constant(content_image), tf.constant(style_image))[0]\ntensor_to_image(stylized_image)<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
content_image = load_img(content_path_2)\nstyle_image = load_img(style_path_2)\n\nplt.subplot(1, 2, 1)\nimshow(content_image, 'Content Image')\n\nplt.subplot(1, 2, 2)\nimshow(style_image, 'Style Image')<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
import tensorflow_hub as hub\nhub_model = hub.load('https:\/\/tfhub.dev\/google\/magenta\/arbitrary-image-stylization-v1-256\/2')\nstylized_image = hub_model(tf.constant(content_image), tf.constant(style_image))[0]\ntensor_to_image(stylized_image)<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
content_image = load_img(content_path_3)\nstyle_image = load_img(style_path_3)\n\nplt.subplot(1, 2, 1)\nimshow(content_image, 'Content Image')\n\nplt.subplot(1, 2, 2)\nimshow(style_image, 'Style Image')<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
import tensorflow_hub as hub\nhub_model = hub.load('https:\/\/tfhub.dev\/google\/magenta\/arbitrary-image-stylization-v1-256\/2')\nstylized_image = hub_model(tf.constant(content_image), tf.constant(style_image))[0]\ntensor_to_image(stylized_image)<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
Final Images – A Masterpiece<\/h2>\n\n\n\n\n <\/figure>\n\n\n\n <\/figure>\n\n\n\n <\/figure>\nImages generated by Style transfer<\/figcaption><\/figure>\n\n\n\n<\/p>\n\n\n\n
<\/p>\n\n\n\n
<\/p>\n","protected":false},"excerpt":{"rendered":"
Neural style transfer is referred to as an artistic algorithm that takes two images (in this case, a content image and a style reference image) and blends them to produce an image that looks like the content with attributes that take after the styling of the reference. In this tutorial, we would use a pre-trained […]<\/p>\n","protected":false},"author":1,"featured_media":1613,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"tdm_status":"","tdm_grid_status":"","footnotes":""},"categories":[2370,86],"tags":[2371,2372,2374,2373],"yoast_head":"\n
Neural Style Transfer Create Mardi Gras\u00a0Art with Python TF Hub<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n \n \n \n \n \n\t \n\t \n\t \n