Boundless Colab

View on Run in Google Colab View on GitHub Download notebook See TF Hub models

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


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
2023-11-07 12:20:50.823338: E external/local_xla/xla/stream_executor/cuda/] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2023-11-07 12:20:50.823395: E external/local_xla/xla/stream_executor/cuda/] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2023-11-07 12:20:50.824902: E external/local_xla/xla/stream_executor/cuda/] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered

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
      fd = urlopen(filename)
      fd =, 'rb')

    pil_image =
    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.title("Original", fontsize=24)
  plt.title("Masked", fontsize=24)
  plt.title("Generated", fontsize=24)

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 = ""
# wikimedia = ""
# wikimedia = ""
# wikimedia = ""

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.

Model Selection

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

print("Loading model {} ({})".format(model_name, model_handle))
model = hub.load(model_handle)
Loading model Boundless Quarter (
2023-11-07 12:20:57.117302: E external/local_xla/xla/stream_executor/cuda/] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected

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)