Super resolution with TensorFlow Lite

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


The task of recovering a high resolution (HR) image from its low resolution counterpart is commonly referred to as Single Image Super Resolution (SISR).

The model used here is ESRGAN (ESRGAN: Enhanced Super-Resolution Generative Adversarial Networks). And we are going to use TensorFlow Lite to run inference on the pretrained model.

The TFLite model is converted from this implementation hosted on TF Hub. Note that the model we converted upsamples a 50x50 low resolution image to a 200x200 high resolution image (scale factor=4). If you want a different input size or scale factor, you need to re-convert or re-train the original model.


Let's install required libraries first.

pip install matplotlib tensorflow tensorflow-hub

Import dependencies.

import tensorflow as tf
import tensorflow_hub as hub
import matplotlib.pyplot as plt
2021-07-23 11:17:08.751392: I tensorflow/stream_executor/platform/default/] Successfully opened dynamic library

Download and convert the ESRGAN model

model = hub.load("")
concrete_func = model.signatures[tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY]
concrete_func.inputs[0].set_shape([1, 50, 50, 3])
converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func])
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_model = converter.convert()

# Save the TF Lite model.
with'ESRGAN.tflite', 'wb') as f:

esrgan_model_path = './ESRGAN.tflite'
2021-07-23 11:17:15.750580: I tensorflow/stream_executor/platform/default/] Successfully opened dynamic library
2021-07-23 11:17:15.754548: E tensorflow/stream_executor/cuda/] failed call to cuInit: CUDA_ERROR_SYSTEM_DRIVER_MISMATCH: system has unsupported display driver / cuda driver combination
2021-07-23 11:17:15.754584: I tensorflow/stream_executor/cuda/] retrieving CUDA diagnostic information for host: kokoro-gcp-ubuntu-prod-1315497834
2021-07-23 11:17:15.754592: I tensorflow/stream_executor/cuda/] hostname: kokoro-gcp-ubuntu-prod-1315497834
2021-07-23 11:17:15.754687: I tensorflow/stream_executor/cuda/] libcuda reported version is: 470.57.2
2021-07-23 11:17:15.754712: I tensorflow/stream_executor/cuda/] kernel reported version is: 465.27.0
2021-07-23 11:17:15.754718: E tensorflow/stream_executor/cuda/] kernel version 465.27.0 does not match DSO version 470.57.2 -- cannot find working devices in this configuration
2021-07-23 11:17:15.755072: I tensorflow/core/platform/] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 AVX512F FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-07-23 11:17:19.601044: I tensorflow/core/grappler/] Number of eligible GPUs (core count >= 8, compute capability >= 0.0): 0
2021-07-23 11:17:19.601273: I tensorflow/core/grappler/clusters/] Starting new session
2021-07-23 11:17:19.602137: I tensorflow/core/platform/profile_utils/] CPU Frequency: 2000175000 Hz
2021-07-23 11:17:19.690642: I tensorflow/core/grappler/optimizers/] Optimization results for grappler item: graph_to_optimize
  function_optimizer: Graph size after: 1953 nodes (1608), 3017 edges (2672), time = 49.882ms.
  function_optimizer: function_optimizer did nothing. time = 1.073ms.

2021-07-23 11:17:21.581037: W tensorflow/compiler/mlir/lite/python/] Ignored output_format.
2021-07-23 11:17:21.581097: W tensorflow/compiler/mlir/lite/python/] Ignored drop_control_dependency.
2021-07-23 11:17:21.668047: I tensorflow/compiler/mlir/tensorflow/utils/] disabling MLIR crash reproducer, set env var `MLIR_CRASH_REPRODUCER_DIRECTORY` to enable.
2021-07-23 11:17:21.929411: I tensorflow/lite/tools/optimize/] Skipping quantization of tensor model/rrdb_net/conv2d_8/Conv2D;StatefulPartitionedCall/model/rrdb_net/conv2d_8/Conv2D because it has fewer than 1024 elements (864).
2021-07-23 11:17:21.929552: I tensorflow/lite/tools/optimize/] Skipping quantization of tensor model/rrdb_net/conv2d_176/Conv2D;StatefulPartitionedCall/model/rrdb_net/conv2d_176/Conv2D because it has fewer than 1024 elements (864).

Download a test image (insect head).

test_img_path = tf.keras.utils.get_file('lr.jpg', '')
Downloading data from
8192/6432 [======================================] - 0s 0us/step

Generate a super resolution image using TensorFlow Lite

lr =
lr = tf.image.decode_jpeg(lr)
lr = tf.expand_dims(lr, axis=0)
lr = tf.cast(lr, tf.float32)

# Load TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path=esrgan_model_path)

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Run the model
interpreter.set_tensor(input_details[0]['index'], lr)

# Extract the output and postprocess it
output_data = interpreter.get_tensor(output_details[0]['index'])
sr = tf.squeeze(output_data, axis=0)
sr = tf.clip_by_value(sr, 0, 255)
sr = tf.round(sr)
sr = tf.cast(sr, tf.uint8)

Visualize the result

lr = tf.cast(tf.squeeze(lr, axis=0), tf.uint8)
plt.figure(figsize = (1, 1))

plt.figure(figsize=(10, 4))
plt.subplot(1, 2, 1)        
plt.title(f'ESRGAN (x4)')

bicubic = tf.image.resize(lr, [200, 200], tf.image.ResizeMethod.BICUBIC)
bicubic = tf.cast(bicubic, tf.uint8)
plt.subplot(1, 2, 2)   



Performance Benchmarks

Performance benchmark numbers are generated with the tool described here.

Model Name Model Size Device CPU GPU
super resolution (ESRGAN) 4.8 Mb Pixel 3 586.8ms* 128.6ms
Pixel 4 385.1ms* 130.3ms

*4 threads used