Spaces:
Sleeping
Sleeping
| import os | |
| import tensorflow as tf | |
| # Load compressed models from tensorflow_hub | |
| os.environ['TFHUB_MODEL_LOAD_FORMAT'] = 'COMPRESSED' | |
| import IPython.display as display | |
| import matplotlib.pyplot as plt | |
| import matplotlib as mpl | |
| mpl.rcParams['figure.figsize'] = (12, 12) | |
| mpl.rcParams['axes.grid'] = False | |
| import numpy as np | |
| import PIL.Image | |
| import time | |
| import functools | |
| from VGG import vgg_layers, StyleContentModel, style_content_loss | |
| from preprocessing import load_img, clip_0_1, tensor_to_image | |
| #Load and preprocess style and content images | |
| content_path = tf.keras.utils.get_file('YellowLabradorLooking_new.jpg', 'https://storage.googleapis.com/download.tensorflow.org/example_images/YellowLabradorLooking_new.jpg') | |
| style_path = tf.keras.utils.get_file('kandinsky5.jpg','https://storage.googleapis.com/download.tensorflow.org/example_images/Vassily_Kandinsky%2C_1913_-_Composition_7.jpg') | |
| content_image = load_img(content_path) | |
| style_image = load_img(style_path) | |
| #define content and style intermediate layers from VGG network | |
| content_layers = ['block5_conv2'] | |
| style_layers = ['block1_conv1', | |
| 'block2_conv1', | |
| 'block3_conv1', | |
| 'block4_conv1', | |
| 'block5_conv1'] | |
| num_content_layers = len(content_layers) | |
| num_style_layers = len(style_layers) | |
| #Create model | |
| style_extractor = vgg_layers(style_layers) | |
| style_outputs = style_extractor(style_image*255) | |
| #Initialize model to return style and content tensors and run gradient descent | |
| extractor = StyleContentModel(style_layers, content_layers) | |
| # results = extractor(tf.constant(content_image)) | |
| style_targets = extractor(style_image)['style'] | |
| content_targets = extractor(content_image)['content'] | |
| image = tf.Variable(content_image) | |
| #set optimizer | |
| opt = tf.keras.optimizers.Adam(learning_rate=0.02, beta_1=0.99, epsilon=1e-1) | |
| style_weight=1e-2 | |
| content_weight=1e4 | |
| total_variation_weight=30 | |
| epochs = 1 | |
| steps_per_epoch = 100 #number of batches to be selected for one epoch | |
| def train_step(image): | |
| with tf.GradientTape() as tape: | |
| outputs = extractor(image) | |
| loss = style_content_loss(outputs, style_weight, content_weight, style_targets, content_targets, num_style_layers, num_content_layers) | |
| #add in total variation loss on top style and content loss | |
| loss += total_variation_weight*tf.image.total_variation(image) | |
| grad = tape.gradient(loss, image) | |
| opt.apply_gradients([(grad, image)]) | |
| image.assign(clip_0_1(image)) | |
| opt = tf.keras.optimizers.Adam(learning_rate=0.02, beta_1=0.99, epsilon=1e-1) | |
| image = tf.Variable(content_image) | |
| import time | |
| start = time.time() | |
| step = 0 | |
| for n in range(epochs): | |
| for m in range(steps_per_epoch): | |
| step += 1 | |
| train_step(image) | |
| print(".", end='', flush=True) | |
| display.clear_output(wait=True) | |
| display.display(tensor_to_image(image)) | |
| print("Train step: {}".format(step)) | |
| end = time.time() | |
| print("Total time: {:.1f}".format(end-start)) | |
| #finally, save the result | |
| file_name = "image_output.JPEG" | |
| tensor_to_image(image).save(file_name, "JPEG") |