![]() | ![]() | ![]() | ![]() | ![]() |
Genel Bakış
Düşük çözünürlüklü muadilinden yüksek çözünürlüklü (HR) bir görüntüyü kurtarma görevi, genellikle Tek Görüntü Süper Çözünürlük (SISR) olarak adlandırılır.
Burada kullanılan model ESRGAN'dır ( ESRGAN: Enhanced Super-Resolution Generative Adversarial Networks ). Ve önceden eğitilmiş model üzerinde çıkarım yapmak için TensorFlow Lite'ı kullanacağız.
TFLite modeli, TF Hub'da barındırılan bu uygulamadan dönüştürülür. Dönüştürdüğümüz modelin 50x50 düşük çözünürlüklü bir görüntüyü 200x200 yüksek çözünürlüklü bir görüntüye örneklediğini unutmayın (ölçek faktörü = 4). Farklı bir girdi boyutu veya ölçek faktörü istiyorsanız, orijinal modeli yeniden dönüştürmeniz veya yeniden eğitmeniz gerekir.
Kurulum
Önce gerekli kitaplıkları kuralım.
pip install -q matplotlib tensorflow tensorflow-hub
Bağımlılıkları içe aktarın.
import tensorflow as tf
import tensorflow_hub as hub
import matplotlib.pyplot as plt
print(tf.__version__)
2.4.1
ESRGAN modelini indirin ve dönüştürün
model = hub.load("https://tfhub.dev/captain-pool/esrgan-tf2/1")
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 tf.io.gfile.GFile('ESRGAN.tflite', 'wb') as f:
f.write(tflite_model)
esrgan_model_path = './ESRGAN.tflite'
Bir test görüntüsü (böcek kafası) indirin.
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 8192/6432 [======================================] - 0s 0us/step
TensorFlow Lite kullanarak süper çözünürlüklü bir görüntü oluşturun
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)
Sonucu görselleştirin
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());
<matplotlib.image.AxesImage at 0x7f99dad41588>
Performans Karşılaştırmaları
Performans karşılaştırma numaraları, burada açıklanan araçla oluşturulur.
Model adı | Model Boyutu | cihaz | İşlemci | GPU |
---|---|---|---|---|
süper çözünürlük (ESRGAN) | 4.8 Mb | Piksel 3 | 586,8 ms * | 128,6 ms |
Piksel 4 | 385,1 ms * | 130,3 ms |
* 4 iplik kullanıldı