{"cells":[{"metadata":{},"cell_type":"markdown","source":"## Tensorflow Hub for Landscapes\n\n_By Nick Brooks, September 2020_"},{"metadata":{"id":"sc1OLbOWhPCO","trusted":true},"cell_type":"code","source":"import IPython.display as display\n\nimport tensorflow as tf\nimport tensorflow_hub as hub\n\nimport matplotlib.pyplot as plt\nimport matplotlib as mpl\nmpl.rcParams['figure.figsize'] = (12,12)\nmpl.rcParams['axes.grid'] = False\n\nimport numpy as np\nimport PIL.Image\nimport time\nimport functools\nimport random\n\nimport time\nimport os\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline\n\nnotebookstart = time.time()","execution_count":null,"outputs":[]},{"metadata":{"id":"wqc0OJHwyFAk","trusted":true},"cell_type":"code","source":"hub_module = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/1')\nstyle_DIR = \"../input/gan-getting-started/monet_jpg/\"\nstyle_images = os.listdir(style_DIR)\n\nlandscape_DIR = \"../input/landscape-pictures/\"\nlandscape_images = os.listdir(landscape_DIR)","execution_count":null,"outputs":[]},{"metadata":{"id":"3TLljcwv5qZs","trusted":true},"cell_type":"code","source":"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\n\ndef imshow(image, ax, title=None):\n    if len(image.shape) > 3:\n        image = tf.squeeze(image, axis=0)\n\n        ax.imshow(image)\n        if title:\n            ax.set_title(title)\n            ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for _ in range(35):\n    landscape_i = landscape_DIR + random.choice(landscape_images)\n    style_i = style_DIR + random.choice(style_images)\n\n    content_image = load_img(landscape_i)\n    style_image = load_img(style_i)\n\n    f, ax = plt.subplots(1,3,figsize=[22,10])\n    imshow(image=style_image, ax=ax[0], title=\"Random Style Image - {}\".format(landscape_i.split(\"/\")[-1]))\n    imshow(image=content_image, ax=ax[1], title=\"Random Content Image - {}\".format(style_i.split(\"/\")[-1]))\n\n    stylized_image = hub_module(tf.constant(content_image), tf.constant(style_image))[0]\n    imshow(image=stylized_image, ax=ax[2], title='Stylized Image')\n    \n    ax[0].axis('off')\n    ax[1].axis('off')\n    ax[2].axis('off')\n    \n    plt.tight_layout(pad=1)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"Notebook Runtime: %0.2f Hours\"%((time.time() - notebookstart)/60/60))","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}