Spaces:
Runtime error
Runtime error
Commit
Β·
18b70b8
1
Parent(s):
f7fe39d
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
from huggingface_hub import from_pretrained_keras
|
| 9 |
+
|
| 10 |
+
model = from_pretrained_keras("keras-io/CycleGAN")
|
| 11 |
+
|
| 12 |
+
# Define the standard image size.
|
| 13 |
+
orig_img_size = (286, 286)
|
| 14 |
+
# Size of the random crops to be used during training.
|
| 15 |
+
input_img_size = (256, 256, 3)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def normalize_img(img):
|
| 19 |
+
img = tf.cast(img, dtype=tf.float32)
|
| 20 |
+
# Map values in the range [-1, 1]
|
| 21 |
+
return (img / 127.5) - 1.0
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def preprocess_test_image(img):
|
| 25 |
+
# Only resizing and normalization for the test images.
|
| 26 |
+
img = tf.image.resize(img, [input_img_size[0], input_img_size[1]])
|
| 27 |
+
img = normalize_img(img)
|
| 28 |
+
return img
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# img_path = './n02381460_1010.jpg'
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def generate_img(img_path):
|
| 35 |
+
img = tf.io.read_file(img_path)
|
| 36 |
+
img = tf.image.decode_png(img)
|
| 37 |
+
img = tf.expand_dims(img, axis=0)
|
| 38 |
+
img = preprocess_test_image(img)
|
| 39 |
+
prediction = model(img, training=False)[0].numpy()
|
| 40 |
+
prediction = (prediction * 127.5 + 127.5).astype(np.uint8)
|
| 41 |
+
return prediction
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
image = gr.inputs.Image(type="filepath")
|
| 45 |
+
op = gr.outputs.Image(type="numpy")
|
| 46 |
+
|
| 47 |
+
iface = gr.Interface(
|
| 48 |
+
generate_img,
|
| 49 |
+
image,
|
| 50 |
+
op,
|
| 51 |
+
title="CycleGAN",
|
| 52 |
+
description="Keras Implementation of CycleGAN model",
|
| 53 |
+
article='Author: <a href="https://huggingface.co/anuragshas">Anurag Singh</a>. Based on the keras example from <a href="https://keras.io/examples/generative/cyclegan/">A_K_Nain</a>',
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
iface.launch()
|