{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Fast Style Transfer","metadata":{"execution":{"iopub.status.busy":"2021-08-13T16:07:23.95685Z","iopub.execute_input":"2021-08-13T16:07:23.957203Z","iopub.status.idle":"2021-08-13T16:07:23.961335Z","shell.execute_reply.started":"2021-08-13T16:07:23.957173Z","shell.execute_reply":"2021-08-13T16:07:23.960433Z"}}},{"cell_type":"markdown","source":"## Summary\nIn this notebook, I am going to use Fast Style Transfer from TFHub to generate monet-styled images. It can generate 7000 images in about 350 seconds, which takes 0.05 second per image.","metadata":{"execution":{"iopub.status.busy":"2021-08-13T16:09:02.705617Z","iopub.execute_input":"2021-08-13T16:09:02.705959Z","iopub.status.idle":"2021-08-13T16:09:02.709656Z","shell.execute_reply.started":"2021-08-13T16:09:02.70593Z","shell.execute_reply":"2021-08-13T16:09:02.70887Z"}}},{"cell_type":"markdown","source":"## Import Packages","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport tensorflow_hub as hub\nimport time\nimport os\nfrom PIL import Image\nimport shutil","metadata":{"execution":{"iopub.status.busy":"2021-08-14T05:59:32.134498Z","iopub.execute_input":"2021-08-14T05:59:32.134895Z","iopub.status.idle":"2021-08-14T05:59:32.140941Z","shell.execute_reply.started":"2021-08-14T05:59:32.134852Z","shell.execute_reply":"2021-08-14T05:59:32.139976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Common Parameters","metadata":{}},{"cell_type":"code","source":"base_path = \"/kaggle/input/gan-getting-started\"\ncontent_path = os.path.join(base_path, \"photo_jpg\")\nstyle_path = os.path.join(base_path, \"monet_jpg\")\noutput_path = \"/kaggle/working/images\"\nimage_size = 256\nnum_outputs = 7000","metadata":{"execution":{"iopub.status.busy":"2021-08-14T06:01:41.53435Z","iopub.execute_input":"2021-08-14T06:01:41.534735Z","iopub.status.idle":"2021-08-14T06:01:41.539801Z","shell.execute_reply.started":"2021-08-14T06:01:41.534703Z","shell.execute_reply":"2021-08-14T06:01:41.538746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Common Functions","metadata":{}},{"cell_type":"code","source":"def sample(photos, count):\n    photos = photos[np.random.choice(len(photos), count)]\n    for photo in photos:\n        img = tf.keras.preprocessing.image.load_img(photo)\n        plt.imshow(img)\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-14T06:01:44.03855Z","iopub.execute_input":"2021-08-14T06:01:44.038896Z","iopub.status.idle":"2021-08-14T06:01:44.044695Z","shell.execute_reply.started":"2021-08-14T06:01:44.038866Z","shell.execute_reply":"2021-08-14T06:01:44.043244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Import Datasets","metadata":{}},{"cell_type":"code","source":"style_images = np.array([os.path.join(style_path, path) for path in os.listdir(style_path)])\nnp.random.shuffle(style_images)\nprint(style_images[:10])","metadata":{"execution":{"iopub.status.busy":"2021-08-14T06:05:32.869671Z","iopub.execute_input":"2021-08-14T06:05:32.870011Z","iopub.status.idle":"2021-08-14T06:05:32.879267Z","shell.execute_reply.started":"2021-08-14T06:05:32.86998Z","shell.execute_reply":"2021-08-14T06:05:32.87811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"content_images = np.array([os.path.join(content_path, path) for path in os.listdir(content_path)])\nnp.random.shuffle(content_images)\nprint(content_images[:10])","metadata":{"execution":{"iopub.status.busy":"2021-08-14T06:05:39.941318Z","iopub.execute_input":"2021-08-14T06:05:39.941728Z","iopub.status.idle":"2021-08-14T06:05:39.969437Z","shell.execute_reply.started":"2021-08-14T06:05:39.941692Z","shell.execute_reply":"2021-08-14T06:05:39.968439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Exploratory Data Analysis","metadata":{}},{"cell_type":"markdown","source":"**Number of style images**","metadata":{"execution":{"iopub.status.busy":"2021-08-14T06:03:46.346988Z","iopub.execute_input":"2021-08-14T06:03:46.347325Z","iopub.status.idle":"2021-08-14T06:03:46.352693Z","shell.execute_reply.started":"2021-08-14T06:03:46.347292Z","shell.execute_reply":"2021-08-14T06:03:46.351289Z"}}},{"cell_type":"code","source":"len(style_images)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T06:05:43.018224Z","iopub.execute_input":"2021-08-14T06:05:43.018565Z","iopub.status.idle":"2021-08-14T06:05:43.02412Z","shell.execute_reply.started":"2021-08-14T06:05:43.018532Z","shell.execute_reply":"2021-08-14T06:05:43.023018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Number of content images**","metadata":{}},{"cell_type":"code","source":"len(content_images)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T06:06:19.57977Z","iopub.execute_input":"2021-08-14T06:06:19.580129Z","iopub.status.idle":"2021-08-14T06:06:19.586577Z","shell.execute_reply.started":"2021-08-14T06:06:19.580097Z","shell.execute_reply":"2021-08-14T06:06:19.585402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Style Images**","metadata":{}},{"cell_type":"code","source":"sample(style_images, 5)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T06:06:27.811241Z","iopub.execute_input":"2021-08-14T06:06:27.811584Z","iopub.status.idle":"2021-08-14T06:06:28.565421Z","shell.execute_reply.started":"2021-08-14T06:06:27.811552Z","shell.execute_reply":"2021-08-14T06:06:28.564379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Content Images**","metadata":{}},{"cell_type":"code","source":"sample(content_images, 5)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T06:06:40.540573Z","iopub.execute_input":"2021-08-14T06:06:40.540899Z","iopub.status.idle":"2021-08-14T06:06:41.663879Z","shell.execute_reply.started":"2021-08-14T06:06:40.540868Z","shell.execute_reply":"2021-08-14T06:06:41.662894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load Pretrained Model","metadata":{}},{"cell_type":"code","source":"hub_module = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')","metadata":{"execution":{"iopub.status.busy":"2021-08-14T06:06:52.061919Z","iopub.execute_input":"2021-08-14T06:06:52.062243Z","iopub.status.idle":"2021-08-14T06:06:54.144531Z","shell.execute_reply.started":"2021-08-14T06:06:52.062212Z","shell.execute_reply":"2021-08-14T06:06:54.143644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Generating outputs","metadata":{}},{"cell_type":"code","source":"%rm -r -f /kaggle/working/images","metadata":{"execution":{"iopub.status.busy":"2021-08-14T06:06:57.00568Z","iopub.execute_input":"2021-08-14T06:06:57.006018Z","iopub.status.idle":"2021-08-14T06:06:57.856678Z","shell.execute_reply.started":"2021-08-14T06:06:57.005987Z","shell.execute_reply":"2021-08-14T06:06:57.855555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%mkdir /kaggle/working/images","metadata":{"execution":{"iopub.status.busy":"2021-08-14T06:06:59.833608Z","iopub.execute_input":"2021-08-14T06:06:59.833955Z","iopub.status.idle":"2021-08-14T06:07:00.544772Z","shell.execute_reply.started":"2021-08-14T06:06:59.833921Z","shell.execute_reply":"2021-08-14T06:07:00.543714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\nfor i in tqdm(range(num_outputs)):\n    content_image_path = content_images[np.random.randint(len(content_images))]\n    style_image_path = style_images[np.random.randint(len(style_images))]\n    content_image = plt.imread(content_image_path)\n    style_image = plt.imread(style_image_path)\n    content_image = content_image.astype(np.float32)[np.newaxis, ...] / 255.\n    style_image = style_image.astype(np.float32)[np.newaxis, ...] / 255.\n    style_image = tf.image.resize(style_image, (image_size, image_size))\n    outputs = hub_module(tf.constant(content_image), tf.constant(style_image))\n    stylized_image = tf.reshape(outputs[0], (image_size, image_size, 3))\n    file_path = os.path.join(output_path, str(i + 1) + \".jpg\")\n    img = Image.fromarray(np.uint8(stylized_image * 255.0))\n    img.save(file_path)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T06:07:04.960017Z","iopub.execute_input":"2021-08-14T06:07:04.960352Z","iopub.status.idle":"2021-08-14T06:12:32.232013Z","shell.execute_reply.started":"2021-08-14T06:07:04.960319Z","shell.execute_reply":"2021-08-14T06:12:32.228019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.make_archive(output_path, 'zip', output_path)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T06:13:11.411885Z","iopub.execute_input":"2021-08-14T06:13:11.412215Z","iopub.status.idle":"2021-08-14T06:13:14.732865Z","shell.execute_reply.started":"2021-08-14T06:13:11.412184Z","shell.execute_reply":"2021-08-14T06:13:14.731833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualize outputs","metadata":{}},{"cell_type":"code","source":"output_images = np.array([os.path.join(output_path, path) for path in os.listdir(output_path)])","metadata":{"execution":{"iopub.status.busy":"2021-08-14T06:13:23.69146Z","iopub.execute_input":"2021-08-14T06:13:23.691822Z","iopub.status.idle":"2021-08-14T06:13:23.717498Z","shell.execute_reply.started":"2021-08-14T06:13:23.691791Z","shell.execute_reply":"2021-08-14T06:13:23.716709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample(output_images, 5)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T06:13:31.491945Z","iopub.execute_input":"2021-08-14T06:13:31.492266Z","iopub.status.idle":"2021-08-14T06:13:32.230241Z","shell.execute_reply.started":"2021-08-14T06:13:31.492236Z","shell.execute_reply":"2021-08-14T06:13:32.229276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## If this notebook is helpful, please give me an upvote, thank you.","metadata":{}}]}