-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
109 lines (83 loc) · 3.45 KB
/
main.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import torch
import torch.nn as nn
import os
import cv2
import numpy as np
class UNet(nn.Module):
def __init__(self):
super(UNet,self).__init__()
self.downsample = nn.Sequential(
nn.Conv2d(3, 64, kernel_size = 3, padding = 1),
nn.BatchNorm2d(64),
nn.LeakyReLU(negative_slope=0.01, inplace = True),
nn.Conv2d(64, 64, kernel_size = 3, padding = 1),
nn.BatchNorm2d(64),
nn.LeakyReLU(negative_slope=0.01, inplace = True),
nn.MaxPool2d(2),
nn.Conv2d(64, 128, kernel_size = 3, padding = 1),
nn.BatchNorm2d(128),
nn.LeakyReLU(negative_slope=0.01, inplace = True),
nn.Conv2d(128, 128, kernel_size = 3, padding = 1),
nn.BatchNorm2d(128),
nn.LeakyReLU(negative_slope=0.01, inplace = True),
nn.MaxPool2d(2),
nn.Conv2d(128, 256, kernel_size = 3, padding = 1),
nn.BatchNorm2d(256),
nn.LeakyReLU(negative_slope=0.01, inplace = True),
nn.Conv2d(256, 256, kernel_size = 3, padding = 1),
nn.BatchNorm2d(256),
nn.LeakyReLU(negative_slope=0.01, inplace = True),
nn.MaxPool2d(2),
)
self.upsample = nn.Sequential(
nn.Upsample(scale_factor = 2, mode = 'bilinear', align_corners = True),
nn.Conv2d(256, 128, kernel_size = 3, padding = 1),
nn.BatchNorm2d(128),
nn.LeakyReLU(negative_slope=0.01, inplace = True),
nn.Upsample(scale_factor = 2, mode = 'bilinear', align_corners = True),
nn.Conv2d(128, 128, kernel_size = 3, padding = 1),
nn.BatchNorm2d(128),
nn.LeakyReLU(negative_slope=0.01, inplace = True),
nn.MaxPool2d(2),
nn.Upsample(scale_factor = 2, mode = 'bilinear', align_corners = True),
nn.Conv2d(128, 64, kernel_size = 3, padding = 1),
nn.BatchNorm2d(64),
nn.LeakyReLU(negative_slope=0.01, inplace= True),
nn.Upsample(scale_factor = 2, mode = 'bilinear', align_corners = True),
nn.Conv2d(64, 64, kernel_size = 3, padding = 1),
nn.BatchNorm2d(64),
nn.LeakyReLU(negative_slope = 0.01, inplace = True),
nn.MaxPool2d(2),
nn.Upsample(scale_factor = 2, mode = 'bilinear', align_corners = True),
nn.Conv2d(64, 3, kernel_size = 3, padding = 1),
nn.ReLU()
)
def forward(self,x):
ds = self.downsample(x)
output = self.upsample(ds)
return output
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = UNet()
model_path = 'models/15_unet_ploss_vgg19.pth'
model.load_state_dict(torch.load(model_path, map_location=device))
model = model.to(device)
model.eval()
input_path = "test/low"
output_path = "test/high"
if not os.path.exists(output_path):
os.makedirs(output_path)
all_files = os.listdir(input_path)
for image_name in all_files:
image_path = os.path.join(input_path, image_name)
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
target_size = (128, 128)
image = cv2.resize(image, target_size, interpolation=cv2.INTER_LINEAR)
image = image.astype(np.float32) / 255.0
image = torch.from_numpy(image).permute(2, 0, 1).unsqueeze(0).to(device)
with torch.no_grad():
predicted = model(image).squeeze(0).cpu().numpy()
predicted = predicted.transpose(1, 2, 0)
predicted = (predicted * 255).astype(np.uint8)
output_image_path = os.path.join(output_path, image_name)
cv2.imwrite(output_image_path, cv2.cvtColor(predicted, cv2.COLOR_RGB2BGR))