-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathcolorize_app.py
More file actions
33 lines (29 loc) · 1.07 KB
/
colorize_app.py
File metadata and controls
33 lines (29 loc) · 1.07 KB
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
import argparse
import gradio as gr
import torch
import torchvision.transforms as transforms
from torchvision.utils import save_image
from unet import UNet
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
source_process = transforms.Compose(
[transforms.Resize((224, 224)), transforms.ToTensor(),
transforms.Normalize(mean=[0.5], std=[0.5])])
def recognize_digit(image):
image = source_process(image).unsqueeze(0) # add a batch dimension
with torch.no_grad():
prediction = model(image.to(device))[0]
save_image(prediction, "colorized.png", normalize=True)
return "colorized.png"
if __name__=='__main__':
parser = ...
parser.add_argument(...)
args = ...
model = UNet().to(device)
model.load_state_dict(..., map_location=torch.device(device))
model.eval()
gr.Interface(fn=recognize_digit,
inputs=gr.Image(type="pil", image_mode='L'),
outputs="image",
#live=True,
description="Select an image",
).launch(debug=True, share=True);