Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tensorflow
GitHub Repository: tensorflow/docs-l10n
Path: blob/master/site/en-snapshot/hub/tutorials/boundless.ipynb
25118 views
Kernel: Python 3

Licensed under the Apache License, Version 2.0 (the "License");

#@title Copyright 2020 The TensorFlow Hub Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ==============================================================================

Boundless Colab

Welcome to the Boundless model Colab! This notebook will take you through the steps of running the model on images and visualize the results.

Overview

Boundless is a model for image extrapolation. This model takes an image, internally masks a portion of it (1/2, 1/4, 3/4) and completes the masked part. For more details refer to Boundless: Generative Adversarial Networks for Image Extension or the model documentation on TensorFlow Hub.

Imports and Setup

Lets start with the base imports.

import tensorflow as tf import tensorflow_hub as hub from io import BytesIO from PIL import Image as PilImage import numpy as np from matplotlib import pyplot as plt from six.moves.urllib.request import urlopen

Reading image for input

Lets create a util method to help load the image and format it for the model (257x257x3). This method will also crop the image to a square to avoid distortion and you can use with local images or from the internet.

def read_image(filename): fd = None if(filename.startswith('http')): fd = urlopen(filename) else: fd = tf.io.gfile.GFile(filename, 'rb') pil_image = PilImage.open(fd) width, height = pil_image.size # crop to make the image square pil_image = pil_image.crop((0, 0, height, height)) pil_image = pil_image.resize((257,257),PilImage.LANCZOS) image_unscaled = np.array(pil_image) image_np = np.expand_dims( image_unscaled.astype(np.float32) / 255., axis=0) return image_np

Visualization method

We will also create a visuzalization method to show the original image side by side with the masked version and the "filled" version, both generated by the model.

def visualize_output_comparison(img_original, img_masked, img_filled): plt.figure(figsize=(24,12)) plt.subplot(131) plt.imshow((np.squeeze(img_original))) plt.title("Original", fontsize=24) plt.axis('off') plt.subplot(132) plt.imshow((np.squeeze(img_masked))) plt.title("Masked", fontsize=24) plt.axis('off') plt.subplot(133) plt.imshow((np.squeeze(img_filled))) plt.title("Generated", fontsize=24) plt.axis('off') plt.show()

Loading an Image

We will load a sample image but fell free to upload your own image to the colab and try with it. Remember that the model have some limitations regarding human images.

wikimedia = "https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Nusfjord_road%2C_2010_09.jpg/800px-Nusfjord_road%2C_2010_09.jpg" # wikimedia = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/Beech_forest_M%C3%A1tra_in_winter.jpg/640px-Beech_forest_M%C3%A1tra_in_winter.jpg" # wikimedia = "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b2/Marmolada_Sunset.jpg/640px-Marmolada_Sunset.jpg" # wikimedia = "https://upload.wikimedia.org/wikipedia/commons/thumb/9/9d/Aegina_sunset.jpg/640px-Aegina_sunset.jpg" input_img = read_image(wikimedia)

Selecting a model from TensorFlow Hub

On TensorFlow Hub we have 3 versions of the Boundless model: Half, Quarter and Three Quarters. In the following cell you can chose any of them and try on your image. If you want to try with another one, just chose it and execute the following cells.

#@title Model Selection { display-mode: "form" } model_name = 'Boundless Quarter' # @param ['Boundless Half', 'Boundless Quarter', 'Boundless Three Quarters'] model_handle_map = { 'Boundless Half' : 'https://tfhub.dev/google/boundless/half/1', 'Boundless Quarter' : 'https://tfhub.dev/google/boundless/quarter/1', 'Boundless Three Quarters' : 'https://tfhub.dev/google/boundless/three_quarter/1' } model_handle = model_handle_map[model_name]

Now that we've chosen the model we want, lets load it from TensorFlow Hub.

Note: You can point your browser to the model handle to read the model's documentation.

print("Loading model {} ({})".format(model_name, model_handle)) model = hub.load(model_handle)

Doing Inference

The boundless model have two outputs:

  • The input image with a mask applied

  • The masked image with the extrapolation to complete it

we can use these two images to show a comparisson visualization.

result = model.signatures['default'](tf.constant(input_img)) generated_image = result['default'] masked_image = result['masked_image'] visualize_output_comparison(input_img, masked_image, generated_image)