CHEONMA010 commited on
Commit
30d84d7
1 Parent(s): b56e28a

Upload 4 files

Browse files
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ import torch
4
+ import numpy as np
5
+ import sys
6
+ import os
7
+ import RRDBNet_arch as arch
8
+ # Add the `scripts/` folder to the system path
9
+ sys.path.append(os.path.join(os.path.dirname(__file__), 'scripts'))
10
+
11
+
12
+
13
+
14
+ # Path to the pretrained model
15
+ model_path = os.path.join(os.path.dirname(__file__), 'models', 'RRDB_ESRGAN_x4.pth')
16
+
17
+ # Load ESRGAN model
18
+ device = torch.device('cpu') #'cuda' if using GPU
19
+ model = arch.RRDBNet(3, 3, 64, 23, gc=32)
20
+ model.load_state_dict(torch.load(model_path, map_location=device), strict=True)
21
+ model.eval()
22
+ model = model.to(device)
23
+
24
+
25
+ def upscale_image(image):
26
+ img = np.array(image) / 255.0
27
+ img = torch.from_numpy(np.transpose(img[:, :, [2, 1, 0]], (2, 0, 1))).float()
28
+ img_LR = img.unsqueeze(0).to(device)
29
+
30
+ with torch.no_grad():
31
+ output = model(img_LR).data.squeeze().float().cpu().clamp_(0, 1).numpy()
32
+ output = np.transpose(output[[2, 1, 0], :, :], (1, 2, 0))
33
+ output = (output * 255.0).round().astype(np.uint8)
34
+ return Image.fromarray(output)
35
+
36
+ # Gradio interface
37
+ def gradio_interface(image):
38
+ try:
39
+
40
+ if image is None:
41
+ raise ValueError("No image uploaded. Please upload an image to upscale.")
42
+
43
+
44
+ upscaled_image = upscale_image(image)
45
+ original_size = image.size
46
+ upscaled_size = upscaled_image.size
47
+
48
+ return image, upscaled_image, f"Original Size: {original_size[0]}x{original_size[1]}", f"Upscaled Size: {upscaled_size[0]}x{upscaled_size[1]}"
49
+
50
+ except Exception as e:
51
+
52
+ return None, None, "Error", str(e)
53
+
54
+
55
+ gr_interface = gr.Interface(
56
+ fn=gradio_interface,
57
+ inputs=gr.Image(type="pil"),
58
+ outputs=[gr.Image(), gr.Image(), gr.Text(), gr.Text()],
59
+ title="ESRGAN Image Upscaler",
60
+ description="Upload an image to upscale it using ESRGAN."
61
+ )
62
+
63
+ if __name__ == '__main__':
64
+
65
+ gr_interface.launch()
scripts/RRDBNet_arch.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright [2021] Xintao Wang
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+
17
+ import functools
18
+ import torch
19
+ import torch.nn as nn
20
+ import torch.nn.functional as F
21
+
22
+
23
+ def make_layer(block, n_layers):
24
+ layers = []
25
+ for _ in range(n_layers):
26
+ layers.append(block())
27
+ return nn.Sequential(*layers)
28
+
29
+
30
+ class ResidualDenseBlock_5C(nn.Module):
31
+ def __init__(self, nf=64, gc=32, bias=True):
32
+ super(ResidualDenseBlock_5C, self).__init__()
33
+ # gc: growth channel, i.e. intermediate channels
34
+ self.conv1 = nn.Conv2d(nf, gc, 3, 1, 1, bias=bias)
35
+ self.conv2 = nn.Conv2d(nf + gc, gc, 3, 1, 1, bias=bias)
36
+ self.conv3 = nn.Conv2d(nf + 2 * gc, gc, 3, 1, 1, bias=bias)
37
+ self.conv4 = nn.Conv2d(nf + 3 * gc, gc, 3, 1, 1, bias=bias)
38
+ self.conv5 = nn.Conv2d(nf + 4 * gc, nf, 3, 1, 1, bias=bias)
39
+ self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True)
40
+
41
+ # initialization
42
+ # mutil.initialize_weights([self.conv1, self.conv2, self.conv3, self.conv4, self.conv5], 0.1)
43
+
44
+ def forward(self, x):
45
+ x1 = self.lrelu(self.conv1(x))
46
+ x2 = self.lrelu(self.conv2(torch.cat((x, x1), 1)))
47
+ x3 = self.lrelu(self.conv3(torch.cat((x, x1, x2), 1)))
48
+ x4 = self.lrelu(self.conv4(torch.cat((x, x1, x2, x3), 1)))
49
+ x5 = self.conv5(torch.cat((x, x1, x2, x3, x4), 1))
50
+ return x5 * 0.2 + x
51
+
52
+
53
+ class RRDB(nn.Module):
54
+ '''Residual in Residual Dense Block'''
55
+
56
+ def __init__(self, nf, gc=32):
57
+ super(RRDB, self).__init__()
58
+ self.RDB1 = ResidualDenseBlock_5C(nf, gc)
59
+ self.RDB2 = ResidualDenseBlock_5C(nf, gc)
60
+ self.RDB3 = ResidualDenseBlock_5C(nf, gc)
61
+
62
+ def forward(self, x):
63
+ out = self.RDB1(x)
64
+ out = self.RDB2(out)
65
+ out = self.RDB3(out)
66
+ return out * 0.2 + x
67
+
68
+
69
+ class RRDBNet(nn.Module):
70
+ def __init__(self, in_nc, out_nc, nf, nb, gc=32):
71
+ super(RRDBNet, self).__init__()
72
+ RRDB_block_f = functools.partial(RRDB, nf=nf, gc=gc)
73
+
74
+ self.conv_first = nn.Conv2d(in_nc, nf, 3, 1, 1, bias=True)
75
+ self.RRDB_trunk = make_layer(RRDB_block_f, nb)
76
+ self.trunk_conv = nn.Conv2d(nf, nf, 3, 1, 1, bias=True)
77
+ #### upsampling
78
+ self.upconv1 = nn.Conv2d(nf, nf, 3, 1, 1, bias=True)
79
+ self.upconv2 = nn.Conv2d(nf, nf, 3, 1, 1, bias=True)
80
+ self.HRconv = nn.Conv2d(nf, nf, 3, 1, 1, bias=True)
81
+ self.conv_last = nn.Conv2d(nf, out_nc, 3, 1, 1, bias=True)
82
+
83
+ self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True)
84
+
85
+ def forward(self, x):
86
+ fea = self.conv_first(x)
87
+ trunk = self.trunk_conv(self.RRDB_trunk(fea))
88
+ fea = fea + trunk
89
+
90
+ fea = self.lrelu(self.upconv1(F.interpolate(fea, scale_factor=2, mode='nearest')))
91
+ fea = self.lrelu(self.upconv2(F.interpolate(fea, scale_factor=2, mode='nearest')))
92
+ out = self.conv_last(self.lrelu(self.HRconv(fea)))
93
+
94
+ return out
scripts/__pycache__/RRDBNet_arch.cpython-311.pyc ADDED
Binary file (6.62 kB). View file
 
scripts/models/RRDB_ESRGAN_x4.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:65fece06e1ccb48853242aa972bdf00ad07a7dd8938d2dcbdf4221b59f6372ce
3
+ size 66929193