fantos commited on
Commit
3b6ad82
·
verified ·
1 Parent(s): ac0f0e5

Upload 6 files

Browse files
app (29).py ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import os # 📁 For file operations
8
+
9
+ # 🧠 Neural network layers
10
+ norm_layer = nn.InstanceNorm2d
11
+
12
+ # 🧱 Building block for the generator
13
+ class ResidualBlock(nn.Module):
14
+ def __init__(self, in_features):
15
+ super(ResidualBlock, self).__init__()
16
+
17
+ conv_block = [ nn.ReflectionPad2d(1),
18
+ nn.Conv2d(in_features, in_features, 3),
19
+ norm_layer(in_features),
20
+ nn.ReLU(inplace=True),
21
+ nn.ReflectionPad2d(1),
22
+ nn.Conv2d(in_features, in_features, 3),
23
+ norm_layer(in_features)
24
+ ]
25
+
26
+ self.conv_block = nn.Sequential(*conv_block)
27
+
28
+ def forward(self, x):
29
+ return x + self.conv_block(x)
30
+
31
+ # 🎨 Generator model for creating line drawings
32
+ class Generator(nn.Module):
33
+ def __init__(self, input_nc, output_nc, n_residual_blocks=9, sigmoid=True):
34
+ super(Generator, self).__init__()
35
+
36
+ # 🏁 Initial convolution block
37
+ model0 = [ nn.ReflectionPad2d(3),
38
+ nn.Conv2d(input_nc, 64, 7),
39
+ norm_layer(64),
40
+ nn.ReLU(inplace=True) ]
41
+ self.model0 = nn.Sequential(*model0)
42
+
43
+ # 🔽 Downsampling
44
+ model1 = []
45
+ in_features = 64
46
+ out_features = in_features*2
47
+ for _ in range(2):
48
+ model1 += [ nn.Conv2d(in_features, out_features, 3, stride=2, padding=1),
49
+ norm_layer(out_features),
50
+ nn.ReLU(inplace=True) ]
51
+ in_features = out_features
52
+ out_features = in_features*2
53
+ self.model1 = nn.Sequential(*model1)
54
+
55
+ # 🔁 Residual blocks
56
+ model2 = []
57
+ for _ in range(n_residual_blocks):
58
+ model2 += [ResidualBlock(in_features)]
59
+ self.model2 = nn.Sequential(*model2)
60
+
61
+ # 🔼 Upsampling
62
+ model3 = []
63
+ out_features = in_features//2
64
+ for _ in range(2):
65
+ model3 += [ nn.ConvTranspose2d(in_features, out_features, 3, stride=2, padding=1, output_padding=1),
66
+ norm_layer(out_features),
67
+ nn.ReLU(inplace=True) ]
68
+ in_features = out_features
69
+ out_features = in_features//2
70
+ self.model3 = nn.Sequential(*model3)
71
+
72
+ # 🎭 Output layer
73
+ model4 = [ nn.ReflectionPad2d(3),
74
+ nn.Conv2d(64, output_nc, 7)]
75
+ if sigmoid:
76
+ model4 += [nn.Sigmoid()]
77
+
78
+ self.model4 = nn.Sequential(*model4)
79
+
80
+ def forward(self, x, cond=None):
81
+ out = self.model0(x)
82
+ out = self.model1(out)
83
+ out = self.model2(out)
84
+ out = self.model3(out)
85
+ out = self.model4(out)
86
+
87
+ return out
88
+
89
+ # 🔧 Load the models
90
+ model1 = Generator(3, 1, 3)
91
+ model1.load_state_dict(torch.load('model.pth', map_location=torch.device('cpu'), weights_only=True))
92
+ model1.eval()
93
+
94
+ model2 = Generator(3, 1, 3)
95
+ model2.load_state_dict(torch.load('model2.pth', map_location=torch.device('cpu'), weights_only=True))
96
+ model2.eval()
97
+
98
+ # 🖼️ Function to process the image and create line drawing
99
+ def predict(input_img, ver):
100
+ # Open the image and get its original size
101
+ original_img = Image.open(input_img)
102
+ original_size = original_img.size
103
+
104
+ # Define the transformation pipeline
105
+ transform = transforms.Compose([
106
+ transforms.Resize(256, Image.BICUBIC),
107
+ transforms.ToTensor(),
108
+ transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
109
+ ])
110
+
111
+ # Apply the transformation
112
+ input_tensor = transform(original_img)
113
+ input_tensor = input_tensor.unsqueeze(0)
114
+
115
+ # Process the image through the model
116
+ with torch.no_grad():
117
+ if ver == 'Simple Lines':
118
+ output = model2(input_tensor)
119
+ else:
120
+ output = model1(input_tensor)
121
+
122
+ # Convert the output tensor to an image
123
+ output_img = transforms.ToPILImage()(output.squeeze().cpu().clamp(0, 1))
124
+
125
+ # Resize the output image back to the original size
126
+ output_img = output_img.resize(original_size, Image.BICUBIC)
127
+
128
+ return output_img
129
+
130
+ # 📝 Title for the Gradio interface
131
+ title="🖌️ Image to Line Drawings - Complex and Simple Portraits and Landscapes"
132
+
133
+ # 🖼️ Dynamically generate examples from images in the directory
134
+ examples = []
135
+ image_dir = '.' # Assuming images are in the current directory
136
+ for file in os.listdir(image_dir):
137
+ if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif')):
138
+ examples.append([file, 'Simple Lines'])
139
+ examples.append([file, 'Complex Lines'])
140
+
141
+ # 🚀 Create and launch the Gradio interface
142
+ iface = gr.Interface(
143
+ fn=predict,
144
+ inputs=[
145
+ gr.Image(type='filepath'),
146
+ gr.Radio(['Complex Lines', 'Simple Lines'], label='version', value='Simple Lines')
147
+ ],
148
+ outputs=gr.Image(type="pil"),
149
+ title=title,
150
+ examples=examples
151
+ )
152
+
153
+ iface.launch()
backup1.app.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 == 'Simple Lines':
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="Image to Line Drawings - Complex and Simple Portraits and Landscapes"
111
+ examples=[
112
+ ['01.jpeg', 'Simple Lines'], ['02.jpeg', 'Simple Lines'], ['03.jpeg', 'Simple Lines'],
113
+ ['07.jpeg', 'Complex Lines'], ['08.jpeg', 'Complex Lines'], ['09.jpeg', 'Complex Lines'],
114
+ ['10.jpeg', 'Simple Lines'], ['11.jpeg', 'Simple Lines'], ['12.jpeg', 'Simple Lines'],
115
+ ['01.jpeg', 'Complex Lines'], ['02.jpeg', 'Complex Lines'], ['03.jpeg', 'Complex Lines'],
116
+ ['04.jpeg', 'Simple Lines'], ['05.jpeg', 'Simple Lines'], ['06.jpeg', 'Simple Lines'],
117
+ ['07.jpeg', 'Simple Lines'], ['08.jpeg', 'Simple Lines'], ['09.jpeg', 'Simple Lines'],
118
+ ['04.jpeg', 'Complex Lines'], ['05.jpeg', 'Complex Lines'], ['06.jpeg', 'Complex Lines'],
119
+ ['10.jpeg', 'Complex Lines'], ['11.jpeg', 'Complex Lines'], ['12.jpeg', 'Complex Lines'],
120
+ ['Upload Wild Horses 2.jpeg', 'Complex Lines']
121
+ ]
122
+
123
+ iface = gr.Interface(predict, [gr.inputs.Image(type='filepath'),
124
+ gr.inputs.Radio(['Complex Lines','Simple Lines'], type="value", default='Simple Lines', label='version')],
125
+ gr.outputs.Image(type="pil"), title=title,examples=examples)
126
+
127
+ iface.launch()
image - 2025-02-08T143959.754.webp 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 (11).txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ torch
2
+ torchvision