File size: 1,397 Bytes
6041fbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# -*- 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)