HW4 / app.py
mehdiabruee's picture
Upload app.py
6041fbb
raw
history blame
1.4 kB
# -*- coding: utf-8 -*-
"""app.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/13tu6v1reMxLATyBwle-BgpQrql9p4nqn
"""
from fastai.vision.all import *
from fastai.basics import *
from upit.models.cyclegan import *
from upit.train.cyclegan import *
from upit.data.unpaired import *
import torchvision
import gradio as gr
#dls = get_dls_from_hf("huggan/horse2zebra", load_size=286)
cycle_gan = CycleGAN.from_pretrained('mehdiabruee/HW4_1')
totensor = torchvision.transforms.ToTensor()
normalize_fn = torchvision.transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
topilimage = torchvision.transforms.ToPILImage()
#model = cycle_gan.G_B.cpu().eval()
model = cycle_gan.G_B.cpu().eval()
def predict(input):
im = normalize_fn(totensor(input))
print(im.shape)
preds = model(im.unsqueeze(0))/2 + 0.5
print(preds.shape)
return topilimage(preds.squeeze(0).detach())
gr_interface = gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(256, 256)), outputs="image", title='Horse-to-Zebra CycleGAN with UPIT', description='[This](https://huggingface.co/tmabraham/horse2zebra_cyclegan) CycleGAN model trained on [this dataset](https://huggingface.co/datasets/huggan/horse2zebra), using the [UPIT package](https://github.com/tmabraham/UPIT)', examples=['horse.jpg'])
gr_interface.launch(inline=False,share=False)