TensorFlow Lite के साथ सुपर रिज़ॉल्यूशन

TensorFlow.org पर देखें Google Colab में चलाएं GitHub पर स्रोत देखें नोटबुक डाउनलोड करें टीएफ हब मॉडल देखें

अवलोकन

अपने कम रिज़ॉल्यूशन समकक्ष से उच्च रिज़ॉल्यूशन (एचआर) छवि को पुनर्प्राप्त करने का कार्य आमतौर पर सिंगल इमेज सुपर रेज़ोल्यूशन (एसआईएसआर) के रूप में जाना जाता है।

यहां इस्तेमाल किया मॉडल है ESRGAN ( ESRGAN: बढ़ी सुपर संकल्प उत्पादक विरोधात्मक नेटवर्क )। और हम पूर्व-प्रशिक्षित मॉडल पर अनुमान चलाने के लिए TensorFlow Lite का उपयोग करने जा रहे हैं।

TFLite मॉडल इस से बदल जाती है कार्यान्वयन TF हब पर की मेजबानी की। ध्यान दें कि जिस मॉडल को हमने 50x50 कम रिज़ॉल्यूशन वाली छवि को 200x200 उच्च रिज़ॉल्यूशन वाली छवि (स्केल फ़ैक्टर = 4) में परिवर्तित किया है। यदि आप एक अलग इनपुट आकार या स्केल फैक्टर चाहते हैं, तो आपको मूल मॉडल को फिर से बदलने या फिर से प्रशिक्षित करने की आवश्यकता है।

सेट अप

आइए पहले आवश्यक पुस्तकालय स्थापित करें।

pip install matplotlib tensorflow tensorflow-hub

आयात निर्भरताएँ।

import tensorflow as tf
import tensorflow_hub as hub
import matplotlib.pyplot as plt
print(tf.__version__)
2.7.0

ESRGAN मॉडल को डाउनलोड और कन्वर्ट करें

model = hub.load("https://tfhub.dev/captain-pool/esrgan-tf2/1")
concrete_func = model.signatures[tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY]

@tf.function(input_signature=[tf.TensorSpec(shape=[1, 50, 50, 3], dtype=tf.float32)])
def f(input):
  return concrete_func(input);

converter = tf.lite.TFLiteConverter.from_concrete_functions([f.get_concrete_function()], model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_model = converter.convert()

# Save the TF Lite model.
with tf.io.gfile.GFile('ESRGAN.tflite', 'wb') as f:
  f.write(tflite_model)

esrgan_model_path = './ESRGAN.tflite'
WARNING:absl:Found untraced functions such as restored_function_body, restored_function_body, restored_function_body, restored_function_body, restored_function_body while saving (showing 5 of 335). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: /tmp/tmpinlbbz0t/assets
INFO:tensorflow:Assets written to: /tmp/tmpinlbbz0t/assets
2021-11-16 12:15:19.621471: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:363] Ignored output_format.
2021-11-16 12:15:19.621517: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:366] Ignored drop_control_dependency.
WARNING:absl:Buffer deduplication procedure will be skipped when flatbuffer library is not properly loaded

एक परीक्षण छवि (कीट सिर) डाउनलोड करें।

test_img_path = tf.keras.utils.get_file('lr.jpg', 'https://raw.githubusercontent.com/tensorflow/examples/master/lite/examples/super_resolution/android/app/src/main/assets/lr-1.jpg')
Downloading data from https://raw.githubusercontent.com/tensorflow/examples/master/lite/examples/super_resolution/android/app/src/main/assets/lr-1.jpg
16384/6432 [============================================================================] - 0s 0us/step

TensorFlow Lite का उपयोग करके एक सुपर रिज़ॉल्यूशन छवि बनाएं

lr = tf.io.read_file(test_img_path)
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)
interpreter.allocate_tensors()

# 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)
interpreter.invoke()

# 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)

परिणाम की कल्पना करें

lr = tf.cast(tf.squeeze(lr, axis=0), tf.uint8)
plt.figure(figsize = (1, 1))
plt.title('LR')
plt.imshow(lr.numpy());

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

bicubic = tf.image.resize(lr, [200, 200], tf.image.ResizeMethod.BICUBIC)
bicubic = tf.cast(bicubic, tf.uint8)
plt.subplot(1, 2, 2)   
plt.title('Bicubic')
plt.imshow(bicubic.numpy());

पीएनजी

पीएनजी

प्रदर्शन बेंचमार्क

प्रदर्शन बेंचमार्क संख्या उपकरण के साथ उत्पन्न कर रहे हैं यहाँ वर्णित

मॉडल का नाम मॉडल का आकार युक्ति CPU जीपीयू
सुपर रेजोल्यूशन (ESRGAN) 4.8 एमबी पिक्सेल 3 586.8ms* 128.6ms
पिक्सेल 4 385.1ms* 130.3ms

* 4 धागे का इस्तेमाल किया