{"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":"code","source":"# for loading/processing the images  \nfrom keras.preprocessing.image import load_img \nfrom keras.preprocessing.image import img_to_array \nfrom keras.applications.vgg16 import preprocess_input \n\n# models \nfrom keras.applications.vgg16 import VGG16 \nfrom keras.models import Model\n\n# clustering and dimension reduction\nfrom sklearn.cluster import KMeans\nfrom sklearn.decomposition import PCA\n\n# for everything else\nimport os\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom random import randint\nimport pandas as pd\nimport pickle\nfrom kaggle_datasets import KaggleDatasets\n\n!pip install pydot graphviz","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:17:59.773503Z","iopub.execute_input":"2021-07-29T17:17:59.774096Z","iopub.status.idle":"2021-07-29T17:18:16.4894Z","shell.execute_reply.started":"2021-07-29T17:17:59.773996Z","shell.execute_reply":"2021-07-29T17:18:16.487835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:18:16.491447Z","iopub.execute_input":"2021-07-29T17:18:16.491812Z","iopub.status.idle":"2021-07-29T17:18:17.235834Z","shell.execute_reply.started":"2021-07-29T17:18:16.491777Z","shell.execute_reply":"2021-07-29T17:18:17.234071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = \"../input/gan-getting-started/monet_jpg/\"\n\n# this list holds all the image filename\nflowers = []\n\n# creates a ScandirIterator aliased as files\nwith os.scandir(path) as files:\n  # loops through each file in the directory\n    for file in files:\n        if file.name.endswith('.jpg'):\n          # adds only the image files to the flowers list\n            flowers.append(path + file.name)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:18:17.244258Z","iopub.execute_input":"2021-07-29T17:18:17.24466Z","iopub.status.idle":"2021-07-29T17:18:17.331866Z","shell.execute_reply.started":"2021-07-29T17:18:17.244616Z","shell.execute_reply":"2021-07-29T17:18:17.330178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"flowers[0]","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:18:17.334312Z","iopub.execute_input":"2021-07-29T17:18:17.334717Z","iopub.status.idle":"2021-07-29T17:18:17.343932Z","shell.execute_reply.started":"2021-07-29T17:18:17.33468Z","shell.execute_reply":"2021-07-29T17:18:17.342727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load the image as a 224x224 array\nimg = load_img(flowers[0], target_size=(224,224))\n# convert from 'PIL.Image.Image' to numpy array\nimg = np.array(img)\n\nprint(img.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:18:17.345539Z","iopub.execute_input":"2021-07-29T17:18:17.346031Z","iopub.status.idle":"2021-07-29T17:18:17.38463Z","shell.execute_reply.started":"2021-07-29T17:18:17.345988Z","shell.execute_reply":"2021-07-29T17:18:17.383007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reshaped_img = img.reshape(1,224,224,3)\nprint(reshaped_img.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:18:17.386618Z","iopub.execute_input":"2021-07-29T17:18:17.387138Z","iopub.status.idle":"2021-07-29T17:18:17.395245Z","shell.execute_reply.started":"2021-07-29T17:18:17.387089Z","shell.execute_reply":"2021-07-29T17:18:17.393341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = preprocess_input(reshaped_img)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:18:17.397244Z","iopub.execute_input":"2021-07-29T17:18:17.398007Z","iopub.status.idle":"2021-07-29T17:18:17.41421Z","shell.execute_reply.started":"2021-07-29T17:18:17.397954Z","shell.execute_reply":"2021-07-29T17:18:17.413174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\nimport matplotlib\nimport numpy as np\nimport time\nfrom PIL import Image\n%matplotlib inline","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-29T17:18:17.420359Z","iopub.execute_input":"2021-07-29T17:18:17.421089Z","iopub.status.idle":"2021-07-29T17:18:17.439539Z","shell.execute_reply.started":"2021-07-29T17:18:17.421048Z","shell.execute_reply":"2021-07-29T17:18:17.438263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load model\nmodel = VGG16()\n# remove the output layer\nmodel = Model(inputs=model.inputs, outputs=model.layers[-2].output)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:18:17.442004Z","iopub.execute_input":"2021-07-29T17:18:17.442715Z","iopub.status.idle":"2021-07-29T17:18:23.661292Z","shell.execute_reply.started":"2021-07-29T17:18:17.442672Z","shell.execute_reply":"2021-07-29T17:18:23.660017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = model.predict(x)\nprint(features.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:18:23.663241Z","iopub.execute_input":"2021-07-29T17:18:23.66367Z","iopub.status.idle":"2021-07-29T17:18:24.378931Z","shell.execute_reply.started":"2021-07-29T17:18:23.663624Z","shell.execute_reply":"2021-07-29T17:18:24.377875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load the model first and pass as an argument\nmodel = VGG16()\nmodel = Model(inputs = model.inputs, outputs = model.layers[-2].output)\n\ndef extract_features(file, model):\n    # load the image as a 224x224 array\n    img = load_img(file, target_size=(224,224))\n    # convert from 'PIL.Image.Image' to numpy array\n    img = np.array(img) \n    # reshape the data for the model reshape(num_of_samples, dim 1, dim 2, channels)\n    reshaped_img = img.reshape(1,224,224,3) \n    # prepare image for model\n    imgx = preprocess_input(reshaped_img)\n    # get the feature vector\n    features = model.predict(imgx, use_multiprocessing=True)\n    return features","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:18:24.380422Z","iopub.execute_input":"2021-07-29T17:18:24.380771Z","iopub.status.idle":"2021-07-29T17:18:27.577503Z","shell.execute_reply.started":"2021-07-29T17:18:24.380718Z","shell.execute_reply":"2021-07-29T17:18:27.57632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"flowers[0]","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:18:27.579307Z","iopub.execute_input":"2021-07-29T17:18:27.579784Z","iopub.status.idle":"2021-07-29T17:18:27.589012Z","shell.execute_reply.started":"2021-07-29T17:18:27.579712Z","shell.execute_reply":"2021-07-29T17:18:27.587375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = {}\np = \"flower_features.pkl\"\n\n# lop through each image in the dataset\nfor flower in flowers:\n    # try to extract the features and update the dictionary\n    feat = extract_features(flower,model)\n    data[flower] = feat\n          \n \n# get a list of the filenames\nfilenames = np.array(list(data.keys()))\n\n# get a list of just the features\nfeat = np.array(list(data.values()))\nfeat.shape\n(210, 1, 4096)\n\n# reshape so that there are 210 samples of 4096 vectors\nfeat = feat.reshape(-1,4096)\nfeat.shape\n(210, 4096)\n\nunique_labels = list(set(list(range(30))))","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:18:27.591604Z","iopub.execute_input":"2021-07-29T17:18:27.592421Z","iopub.status.idle":"2021-07-29T17:20:25.180104Z","shell.execute_reply.started":"2021-07-29T17:18:27.592356Z","shell.execute_reply":"2021-07-29T17:20:25.17889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components=100, random_state=22)\npca.fit(feat)\nx = pca.transform(feat)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:20:25.181821Z","iopub.execute_input":"2021-07-29T17:20:25.182329Z","iopub.status.idle":"2021-07-29T17:20:25.855565Z","shell.execute_reply.started":"2021-07-29T17:20:25.18229Z","shell.execute_reply":"2021-07-29T17:20:25.854084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kmeans = KMeans(n_clusters=len(unique_labels),n_jobs=-1, random_state=22)\nkmeans.fit(x)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:20:25.857486Z","iopub.execute_input":"2021-07-29T17:20:25.857955Z","iopub.status.idle":"2021-07-29T17:20:26.574944Z","shell.execute_reply.started":"2021-07-29T17:20:25.857901Z","shell.execute_reply":"2021-07-29T17:20:26.573649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# holds the cluster id and the images { id: [images] }\ngroups = {}\nfor file, cluster in zip(filenames,kmeans.labels_):\n    if cluster not in groups.keys():\n        groups[cluster] = []\n        groups[cluster].append(file)\n    else:\n        groups[cluster].append(file)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:20:26.576705Z","iopub.execute_input":"2021-07-29T17:20:26.577197Z","iopub.status.idle":"2021-07-29T17:20:26.589665Z","shell.execute_reply.started":"2021-07-29T17:20:26.57715Z","shell.execute_reply":"2021-07-29T17:20:26.585306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"groups[0]","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:20:26.59158Z","iopub.execute_input":"2021-07-29T17:20:26.592104Z","iopub.status.idle":"2021-07-29T17:20:26.602255Z","shell.execute_reply.started":"2021-07-29T17:20:26.592051Z","shell.execute_reply":"2021-07-29T17:20:26.600859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\n\nimgs = groups[0]\n\nplt.figure()\n\n#subplot(r,c) provide the no. of rows and columns\nf, axarr = plt.subplots(len(imgs),1, figsize = (len(imgs)*30,30))\n\nfor i, img_path in enumerate(imgs):\n    axarr[i].imshow(cv2.imread(imgs[i]))","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:20:26.604095Z","iopub.execute_input":"2021-07-29T17:20:26.604575Z","iopub.status.idle":"2021-07-29T17:20:28.008616Z","shell.execute_reply.started":"2021-07-29T17:20:26.604534Z","shell.execute_reply":"2021-07-29T17:20:28.007376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# A function to load the input images and set its dimensions to 1024 x 768\ndef load_image(image_path):\n    max_dim=512\n    img = tf.io.read_file(image_path)\n    img = tf.image.decode_image(img, channels=3)# decodes the image into a tensor\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    new_shape = tf.cast(shape * scale, tf.int32)\n\n    img = tf.image.resize(img, new_shape)\n    img = img[tf.newaxis, :]# broadcasting the image array so that it has a batch dimension\n\n    return img","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:20:28.009929Z","iopub.execute_input":"2021-07-29T17:20:28.010262Z","iopub.status.idle":"2021-07-29T17:20:28.019616Z","shell.execute_reply.started":"2021-07-29T17:20:28.010229Z","shell.execute_reply":"2021-07-29T17:20:28.018155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def imshow(image, title=None):\n    if(len(image.shape) > 3):# suppose dim is like 1,2,4,2,2,1... it removes the ones so that only 3 values remain W,H,c\n        image=np.squeeze(image, axis=0)\n    plt.imshow(image)\n    if(title):# if there's a title mention it\n        plt.title(title)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:20:28.022401Z","iopub.execute_input":"2021-07-29T17:20:28.022835Z","iopub.status.idle":"2021-07-29T17:20:28.054387Z","shell.execute_reply.started":"2021-07-29T17:20:28.022797Z","shell.execute_reply":"2021-07-29T17:20:28.053244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's see the images\ncontent_img=load_image('../input/gan-getting-started/photo_jpg/00068bc07f.jpg')\nstyle_img=load_image('../input/gan-getting-started/monet_jpg/2f90c99e10.jpg')\n\nplt.figure(figsize=(12,12))\nplt.subplot(1, 2, 1)\nimshow(content_img, 'Base Target Image')\nplt.subplot(1, 2, 2)\nimshow(style_img, 'Style image')\n","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:20:28.055717Z","iopub.execute_input":"2021-07-29T17:20:28.056061Z","iopub.status.idle":"2021-07-29T17:20:28.683704Z","shell.execute_reply.started":"2021-07-29T17:20:28.05603Z","shell.execute_reply":"2021-07-29T17:20:28.682386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(content_img.shape)\nprint(style_img.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:20:28.685049Z","iopub.execute_input":"2021-07-29T17:20:28.685488Z","iopub.status.idle":"2021-07-29T17:20:28.693823Z","shell.execute_reply.started":"2021-07-29T17:20:28.685438Z","shell.execute_reply":"2021-07-29T17:20:28.69203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the content image representation and load the model\nx=tf.keras.applications.vgg19.preprocess_input(content_img*255)# needs preprocessing for the model to be initialized\nx=tf.image.resize(x, (256,256))# the vgg19 model takes images in 256\nvgg_model=tf.keras.applications.VGG19(include_top=False, weights='imagenet')\nvgg_model.trainable=False\nvgg_model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:20:28.695517Z","iopub.execute_input":"2021-07-29T17:20:28.69601Z","iopub.status.idle":"2021-07-29T17:20:29.951111Z","shell.execute_reply.started":"2021-07-29T17:20:28.695969Z","shell.execute_reply":"2021-07-29T17:20:29.949885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Chooose the content and style layers\ncontent_layers=['block4_conv2']\nstyle_layers = ['block1_conv1',\n                'block2_conv1',\n                'block3_conv1', \n                'block4_conv1', \n                'block5_conv1']","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:20:29.954544Z","iopub.execute_input":"2021-07-29T17:20:29.955195Z","iopub.status.idle":"2021-07-29T17:20:29.962609Z","shell.execute_reply.started":"2021-07-29T17:20:29.95511Z","shell.execute_reply":"2021-07-29T17:20:29.960481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Build the model\ndef my_model(layer_names):\n    # Retrieve the output layers corresponding to the content and style layers\n    vgg_model = tf.keras.applications.VGG19(include_top=False, weights='imagenet')\n    vgg_model.trainable = False\n    outputs = [vgg_model.get_layer(name).output for name in layer_names]\n    model=tf.keras.Model([vgg_model.input], outputs)\n    return model","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:20:29.965031Z","iopub.execute_input":"2021-07-29T17:20:29.965489Z","iopub.status.idle":"2021-07-29T17:20:29.987595Z","shell.execute_reply.started":"2021-07-29T17:20:29.965449Z","shell.execute_reply":"2021-07-29T17:20:29.985917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"style_extractor = my_model(style_layers)\nstyle_outputs = style_extractor(style_img*255)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:20:29.99433Z","iopub.execute_input":"2021-07-29T17:20:29.994724Z","iopub.status.idle":"2021-07-29T17:20:32.424253Z","shell.execute_reply.started":"2021-07-29T17:20:29.994688Z","shell.execute_reply":"2021-07-29T17:20:32.422457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compute the gram matrix\n# Einsum allows defining Tensors by defining their element-wise computation.\n# This computation is defined by equation, a shorthand form based on Einstein summation.\ndef gram_matrix(input_tensor): # input_tensor is of shape ch, n_H, n_W\n    result = tf.linalg.einsum('bijc,bijd->bcd', input_tensor, input_tensor)\n    input_shape = tf.shape(input_tensor)\n    num_locations = tf.cast(input_shape[1]*input_shape[2], tf.float32) # Unrolls n_H and n_W\n    return result/(num_locations)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:20:32.426364Z","iopub.execute_input":"2021-07-29T17:20:32.426782Z","iopub.status.idle":"2021-07-29T17:20:32.433168Z","shell.execute_reply.started":"2021-07-29T17:20:32.426744Z","shell.execute_reply":"2021-07-29T17:20:32.431838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class entire_model(tf.keras.models.Model):\n    def __init__(self, style_layers, content_layers):\n        super(entire_model, self).__init__()\n        self.vgg=my_model(style_layers + content_layers)\n        self.style_layers=style_layers\n        self.content_layers=content_layers\n        self.num_style_layers=len(style_layers)\n        self.vgg.trainable=False\n\n    def call(self, inputs):\n        inputs=inputs*255.0 # Scale back the pixel values\n        preprocessed_input=tf.keras.applications.vgg19.preprocess_input(inputs)\n        outputs=self.vgg(preprocessed_input)# Pass the preprocessed input to my_model\n\n        # Separate the representations of style and content\n        style_outputs, content_outputs=(outputs[:self.num_style_layers], outputs[self.num_style_layers:])\n        # Calculate the gram matrix for each layer in the style output. This will be the final style representation\n        style_outputs=[gram_matrix(layer) for layer in style_outputs]\n\n        # Store the content and style representation in dictionaries in a layer by layer manner\n        content_dict = {content_name:value\n                    for content_name, value\n                    in zip(self.content_layers, content_outputs)}\n\n        style_dict = {style_name:value\n                  for style_name, value\n                  in zip(self.style_layers, style_outputs)}\n\n        return {'content': content_dict, 'style': style_dict}\n        # Returns a dict of dicts with content and style representations, i.e., gram matrix of the style_layers and\n        # the content of the content_layers","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:20:32.435067Z","iopub.execute_input":"2021-07-29T17:20:32.435534Z","iopub.status.idle":"2021-07-29T17:20:32.459439Z","shell.execute_reply.started":"2021-07-29T17:20:32.435486Z","shell.execute_reply":"2021-07-29T17:20:32.458361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Now we extract the style and content features by calling the above class\nextractor=entire_model(style_layers, content_layers)\nstyle_targets = extractor(style_img)['style']\ncontent_targets = extractor(content_img)['content']\n\nresults = extractor(tf.constant(content_img))","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:20:32.460852Z","iopub.execute_input":"2021-07-29T17:20:32.461496Z","iopub.status.idle":"2021-07-29T17:20:38.801676Z","shell.execute_reply.started":"2021-07-29T17:20:32.461444Z","shell.execute_reply":"2021-07-29T17:20:38.800688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"style_weight=40\ncontent_weight=10\n\n# Custom weights for different style layers\nstyle_weights = {'block1_conv1': 0.7,\n                 'block2_conv1': 0.19,\n                 'block3_conv1': 0.24,\n                 'block4_conv1': 0.11,\n                 'block5_conv1': 0.26}\n# style_weights = {'block1_conv1': 0.3,\n#                  'block2_conv1': 0.45,\n#                  'block3_conv1': 0.15,\n#                  'block4_conv1': 0.05,\n#                  'block5_conv1': 0.05}","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:20:38.803145Z","iopub.execute_input":"2021-07-29T17:20:38.80344Z","iopub.status.idle":"2021-07-29T17:20:38.80772Z","shell.execute_reply.started":"2021-07-29T17:20:38.803412Z","shell.execute_reply":"2021-07-29T17:20:38.807047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def total_cost(outputs):\n    style_outputs=outputs['style']\n    content_outputs=outputs['content']\n    style_loss=tf.add_n([style_weights[name]*tf.reduce_mean((style_outputs[name]-style_targets[name])**2)\n                        for name in style_outputs.keys()])\n    style_loss*=style_weight/len(style_layers)# Normalize\n\n    content_loss = tf.add_n([tf.reduce_mean((content_outputs[name]-content_targets[name])**2)\n                             for name in content_outputs.keys()])\n    content_loss*=content_weight/len(content_layers)\n    loss=style_loss+content_loss\n    return loss","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:20:38.808773Z","iopub.execute_input":"2021-07-29T17:20:38.809192Z","iopub.status.idle":"2021-07-29T17:20:38.829232Z","shell.execute_reply.started":"2021-07-29T17:20:38.809162Z","shell.execute_reply":"2021-07-29T17:20:38.828356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define a tf.Variable to contain the image to optimize\ngenerate_image = tf.Variable(content_img)\n# Since this is a float image, define a function to keep the pixel values between 0 and 1\ndef clip_0_1(image):\n    return tf.clip_by_value(image, clip_value_min=0.0, clip_value_max=1.0)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:20:38.830429Z","iopub.execute_input":"2021-07-29T17:20:38.830878Z","iopub.status.idle":"2021-07-29T17:20:38.844264Z","shell.execute_reply.started":"2021-07-29T17:20:38.830846Z","shell.execute_reply":"2021-07-29T17:20:38.84314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = tf.optimizers.Adam(learning_rate=0.02, beta_1=0.99, epsilon=1e-1)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:20:38.845813Z","iopub.execute_input":"2021-07-29T17:20:38.846106Z","iopub.status.idle":"2021-07-29T17:20:38.860857Z","shell.execute_reply.started":"2021-07-29T17:20:38.846077Z","shell.execute_reply":"2021-07-29T17:20:38.859653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"@tf.function()\ndef train_step(image):\n    with tf.GradientTape() as tape:\n        outputs = extractor(image)\n        loss = total_cost(outputs)\n\n    grad = tape.gradient(loss, image)\n    opt.apply_gradients([(grad, image)])\n    image.assign(clip_0_1(image))","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:20:38.86199Z","iopub.execute_input":"2021-07-29T17:20:38.862294Z","iopub.status.idle":"2021-07-29T17:20:38.875485Z","shell.execute_reply.started":"2021-07-29T17:20:38.862264Z","shell.execute_reply":"2021-07-29T17:20:38.874654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_iterations=2500\nfor i in range(num_iterations):\n    train_step(generate_image)\n    if(i%500==0):\n        plt.figure(figsize=(12,12))\n        plt.subplot(1, 3, 1)\n        imshow(content_img, 'Original Image')\n        plt.subplot(1, 3, 2)\n        imshow(style_img, 'Style Image')\n        plt.subplot(1, 3, 3)\n        imshow(np.squeeze(generate_image.read_value(), 0), 'New Image - Step'+str(i))","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:20:38.876586Z","iopub.execute_input":"2021-07-29T17:20:38.877077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_step(generate_image)\nplt.imshow(np.squeeze(generate_image.read_value(), 0))\nplt.axis('off')\nfig1 = plt.gcf()\nfig1.savefig('new_image.png', bbox_inches='tight')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}