<jupyter_start><jupyter_text>InstallsIf necessary, install all of the required modules.<jupyter_code>pip install numpy
pip install Pillow
pip install tensorflow
pip install tensorflow_hub
pip install matplotlib<jupyter_output><empty_output><jupyter_text>ImportsImport all of the required modules.<jupyter_code>import os
import numpy as np
from PIL import Image
import tensorflow as tf
import tensorflow_hub as hub
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>InputsProvide the path for the content and style images.<jupyter_code>content_image_path = "content.jpg"
style_image_path = "style.jpg"<jupyter_output><empty_output><jupyter_text>Getting StartedTo begin we need to load our input images and see how they look. The two functions below will help us do just that!  Load ImageThis function will load the image at the given path and resize it to the given max size (500px by default).<jupyter_code>def load_image(path_to_image, max_size=500):

  def resize_image(image):

    shape = tf.cast(tf.shape(image)[:-1], tf.float32)
    scale = max_size / max(shape)
    new_shape = tf.cast(shape * scale, tf.int32) 

    return tf.image.resize(image, new_shape)[tf.newaxis, :]

  image = tf.io.read_file(path_to_image)
  image = tf.image.decode_image(image, channels=3)
  image = tf.image.convert_image_dtype(image, tf.float32)

  return resize_image(image)<jupyter_output><empty_output><jupyter_text>Show ImageThis function will simply display the given image with an optional title.<jupyter_code>def show_image(image, title=""):

  image = tf.squeeze(image, axis=0)

  plt.title(title)
  plt.imshow(image)<jupyter_output><empty_output><jupyter_text>The Content ImageThe code block below will load and display the content image from the path provided, as well as print the image shape.<jupyter_code>content_image = load_image(content_image_path)
show_image(content_image, "Content Image")
print(content_image.shape)<jupyter_output><empty_output><jupyter_text>The Style ImageThe code block below will load and display the style image from the path provided, as well as print the image shape.<jupyter_code>style_image = load_image(style_image_path)
show_image(style_image, "Style Image")
print(style_image.shape)<jupyter_output><empty_output><jupyter_text>Load the ModelsLoad the pretrained image stylization models from TensorFlow Hub.<jupyter_code>os.environ["TFHUB_MODEL_LOAD_FORMAT"] = "COMPRESSED"
hub_model_one = hub.load("https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/1")
hub_model_two = hub.load("https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2")<jupyter_output><empty_output><jupyter_text>Stylize the ImagesThe code blocks below will fit the content and style images to the two different models and display the results.  View Model One<jupyter_code>stylized_image_one = hub_model_one(tf.constant(content_image), tf.constant(style_image))[0]
show_image(stylized_image_one, "Stylized Image One")<jupyter_output><empty_output><jupyter_text>View Model Two<jupyter_code>stylized_image_two = hub_model_two(tf.constant(content_image), tf.constant(style_image))[0]
show_image(stylized_image_two, "Stylized Image Two")<jupyter_output><empty_output><jupyter_text>SaveIf you're happy with the results you can save one or both of the outputs with the code blocks below.  Save Image One<jupyter_code>Image.fromarray(np.array(stylized_image_one * 255, dtype=np.uint8)[0]).save("stylized_image_one.jpg")<jupyter_output><empty_output><jupyter_text>Save Image Two<jupyter_code>Image.fromarray(np.array(stylized_image_two * 255, dtype=np.uint8)[0]).save("stylized_image_two.jpg")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09346049046321526, 'fraction_numerical': 0.010081743869209809, 'mean_word_length': 7.597189695550352, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '516201', 'license': 'MIT', 'n_tokens_mistral': 1257, 'n_tokens_neox': 1217, 'n_words': 288, 'path': 'dream_net.ipynb', 'repo_name': 'jgphilpott/dream_net', 'size': '3670'}