Commit
·
83574ec
0
Parent(s):
Duplicate from carolineec/informativedrawings
Browse filesCo-authored-by: Caroline Chan <[email protected]>
- .gitattributes +29 -0
- README.md +13 -0
- app.py +120 -0
- bridge.png +0 -0
- cat.png +0 -0
- lizard.png +0 -0
- model.pth +3 -0
- model2.pth +3 -0
- requirements.txt +2 -0
.gitattributes
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bin.* filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
model.pth filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
model2.pth filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Informativedrawings
|
| 3 |
+
emoji: 📉
|
| 4 |
+
colorFrom: gray
|
| 5 |
+
colorTo: blue
|
| 6 |
+
sdk: gradio
|
| 7 |
+
app_file: app.py
|
| 8 |
+
pinned: false
|
| 9 |
+
license: mit
|
| 10 |
+
duplicated_from: carolineec/informativedrawings
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
|
app.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import torchvision.transforms as transforms
|
| 7 |
+
|
| 8 |
+
norm_layer = nn.InstanceNorm2d
|
| 9 |
+
|
| 10 |
+
class ResidualBlock(nn.Module):
|
| 11 |
+
def __init__(self, in_features):
|
| 12 |
+
super(ResidualBlock, self).__init__()
|
| 13 |
+
|
| 14 |
+
conv_block = [ nn.ReflectionPad2d(1),
|
| 15 |
+
nn.Conv2d(in_features, in_features, 3),
|
| 16 |
+
norm_layer(in_features),
|
| 17 |
+
nn.ReLU(inplace=True),
|
| 18 |
+
nn.ReflectionPad2d(1),
|
| 19 |
+
nn.Conv2d(in_features, in_features, 3),
|
| 20 |
+
norm_layer(in_features)
|
| 21 |
+
]
|
| 22 |
+
|
| 23 |
+
self.conv_block = nn.Sequential(*conv_block)
|
| 24 |
+
|
| 25 |
+
def forward(self, x):
|
| 26 |
+
return x + self.conv_block(x)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class Generator(nn.Module):
|
| 30 |
+
def __init__(self, input_nc, output_nc, n_residual_blocks=9, sigmoid=True):
|
| 31 |
+
super(Generator, self).__init__()
|
| 32 |
+
|
| 33 |
+
# Initial convolution block
|
| 34 |
+
model0 = [ nn.ReflectionPad2d(3),
|
| 35 |
+
nn.Conv2d(input_nc, 64, 7),
|
| 36 |
+
norm_layer(64),
|
| 37 |
+
nn.ReLU(inplace=True) ]
|
| 38 |
+
self.model0 = nn.Sequential(*model0)
|
| 39 |
+
|
| 40 |
+
# Downsampling
|
| 41 |
+
model1 = []
|
| 42 |
+
in_features = 64
|
| 43 |
+
out_features = in_features*2
|
| 44 |
+
for _ in range(2):
|
| 45 |
+
model1 += [ nn.Conv2d(in_features, out_features, 3, stride=2, padding=1),
|
| 46 |
+
norm_layer(out_features),
|
| 47 |
+
nn.ReLU(inplace=True) ]
|
| 48 |
+
in_features = out_features
|
| 49 |
+
out_features = in_features*2
|
| 50 |
+
self.model1 = nn.Sequential(*model1)
|
| 51 |
+
|
| 52 |
+
model2 = []
|
| 53 |
+
# Residual blocks
|
| 54 |
+
for _ in range(n_residual_blocks):
|
| 55 |
+
model2 += [ResidualBlock(in_features)]
|
| 56 |
+
self.model2 = nn.Sequential(*model2)
|
| 57 |
+
|
| 58 |
+
# Upsampling
|
| 59 |
+
model3 = []
|
| 60 |
+
out_features = in_features//2
|
| 61 |
+
for _ in range(2):
|
| 62 |
+
model3 += [ nn.ConvTranspose2d(in_features, out_features, 3, stride=2, padding=1, output_padding=1),
|
| 63 |
+
norm_layer(out_features),
|
| 64 |
+
nn.ReLU(inplace=True) ]
|
| 65 |
+
in_features = out_features
|
| 66 |
+
out_features = in_features//2
|
| 67 |
+
self.model3 = nn.Sequential(*model3)
|
| 68 |
+
|
| 69 |
+
# Output layer
|
| 70 |
+
model4 = [ nn.ReflectionPad2d(3),
|
| 71 |
+
nn.Conv2d(64, output_nc, 7)]
|
| 72 |
+
if sigmoid:
|
| 73 |
+
model4 += [nn.Sigmoid()]
|
| 74 |
+
|
| 75 |
+
self.model4 = nn.Sequential(*model4)
|
| 76 |
+
|
| 77 |
+
def forward(self, x, cond=None):
|
| 78 |
+
out = self.model0(x)
|
| 79 |
+
out = self.model1(out)
|
| 80 |
+
out = self.model2(out)
|
| 81 |
+
out = self.model3(out)
|
| 82 |
+
out = self.model4(out)
|
| 83 |
+
|
| 84 |
+
return out
|
| 85 |
+
|
| 86 |
+
model1 = Generator(3, 1, 3)
|
| 87 |
+
model1.load_state_dict(torch.load('model.pth', map_location=torch.device('cpu')))
|
| 88 |
+
model1.eval()
|
| 89 |
+
|
| 90 |
+
model2 = Generator(3, 1, 3)
|
| 91 |
+
model2.load_state_dict(torch.load('model2.pth', map_location=torch.device('cpu')))
|
| 92 |
+
model2.eval()
|
| 93 |
+
|
| 94 |
+
def predict(input_img, ver):
|
| 95 |
+
input_img = Image.open(input_img)
|
| 96 |
+
transform = transforms.Compose([transforms.Resize(256, Image.BICUBIC), transforms.ToTensor()])
|
| 97 |
+
input_img = transform(input_img)
|
| 98 |
+
input_img = torch.unsqueeze(input_img, 0)
|
| 99 |
+
|
| 100 |
+
drawing = 0
|
| 101 |
+
with torch.no_grad():
|
| 102 |
+
if ver == 'style 2':
|
| 103 |
+
drawing = model2(input_img)[0].detach()
|
| 104 |
+
else:
|
| 105 |
+
drawing = model1(input_img)[0].detach()
|
| 106 |
+
|
| 107 |
+
drawing = transforms.ToPILImage()(drawing)
|
| 108 |
+
return drawing
|
| 109 |
+
|
| 110 |
+
title="informative-drawings"
|
| 111 |
+
description="Gradio Demo for line drawing generation. "
|
| 112 |
+
# article = "<p style='text-align: center'><a href='TODO' target='_blank'>Project Page</a> | <a href='codelink' target='_blank'>Github</a></p>"
|
| 113 |
+
examples=[['cat.png', 'style 1'], ['bridge.png', 'style 1'], ['lizard.png', 'style 2'],]
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
iface = gr.Interface(predict, [gr.inputs.Image(type='filepath'),
|
| 117 |
+
gr.inputs.Radio(['style 1','style 2'], type="value", default='style 1', label='version')],
|
| 118 |
+
gr.outputs.Image(type="pil"), title=title,description=description,examples=examples)
|
| 119 |
+
|
| 120 |
+
iface.launch()
|
bridge.png
ADDED
|
cat.png
ADDED
|
lizard.png
ADDED
|
model.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c686ced2a666b4850b4bb6ccf0748031c3eda9f822de73a34b8979970d90f0c6
|
| 3 |
+
size 17173511
|
model2.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:30a534781061f34e83bb9406b4335da4ff2616c95d22a585c1245aa8363e74e0
|
| 3 |
+
size 17173511
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|