Spaces:
Runtime error
Runtime error
Create new file
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
import random
|
| 4 |
+
import numpy as np
|
| 5 |
+
from glob import glob
|
| 6 |
+
from PIL import Image, ImageOps
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
import tensorflow as tf
|
| 9 |
+
from tensorflow import keras
|
| 10 |
+
from tensorflow.keras import layers
|
| 11 |
+
|
| 12 |
+
def charbonnier_loss(y_true, y_pred):
|
| 13 |
+
return tf.reduce_mean(tf.sqrt(tf.square(y_true - y_pred) + tf.square(1e-3)))
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def peak_signal_noise_ratio(y_true, y_pred):
|
| 17 |
+
return tf.image.psnr(y_pred, y_true, max_val=255.0)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
model = tf.keras.models.load_model('./MIRNet.h5', custom_objects={'charbonnier_loss':
|
| 21 |
+
charbonnier_loss, "peak_signal_noise_ratio":peak_signal_noise_ratio})
|
| 22 |
+
|
| 23 |
+
def plot_results(images, titles, figure_size=(12, 12)):
|
| 24 |
+
fig = plt.figure(figsize=figure_size)
|
| 25 |
+
for i in range(len(images)):
|
| 26 |
+
fig.add_subplot(1, len(images), i + 1).set_title(titles[i])
|
| 27 |
+
_ = plt.imshow(images[i])
|
| 28 |
+
plt.axis("off")
|
| 29 |
+
plt.show()
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def infer(original_image):
|
| 33 |
+
image = keras.preprocessing.image.img_to_array(original_image)
|
| 34 |
+
image = image.astype("float32") / 255.0
|
| 35 |
+
image = np.expand_dims(image, axis=0)
|
| 36 |
+
output = model.predict(image)
|
| 37 |
+
output_image = output[0] * 255.0
|
| 38 |
+
output_image = output_image.clip(0, 255)
|
| 39 |
+
output_image = output_image.reshape(
|
| 40 |
+
(np.shape(output_image)[0], np.shape(output_image)[1], 3)
|
| 41 |
+
)
|
| 42 |
+
output_image = Image.fromarray(np.uint8(output_image))
|
| 43 |
+
original_image = Image.fromarray(np.uint8(original_image))
|
| 44 |
+
return output_image
|