-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_dehazing_testing_script.py
164 lines (141 loc) · 5.67 KB
/
image_dehazing_testing_script.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Define paths for input and output datasets
TEST_DATASET_HAZY_PATH = 'D:/DL_DL/val/hazy'
TEST_DATASET_OUTPUT_PATH = 'D:/DL_DL/val/new'
# Import necessary libraries
import os
import os
import torch
import torch.nn as nn
from torchvision.transforms import transforms
from PIL import Image
from torchvision.transforms.functional import to_pil_image
from torch.utils.data import DataLoader
from torchvision.datasets import ImageFolder
# Get the list of input images
input_images = os.listdir(TEST_DATASET_HAZY_PATH)
num = len(input_images)
# Initialize list for output images
output_images = []
# Preprocess input and output image paths
for i in range(num):
output_images.append(os.path.join(TEST_DATASET_OUTPUT_PATH, input_images[i]))
input_images[i] = os.path.join(TEST_DATASET_HAZY_PATH, input_images[i])
print(i)
# Define the CycleGAN model architecture
class ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride=1):
super(ResidualBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(out_channels)
self.relu = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(out_channels)
def forward(self, x):
identity = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out += identity
out = self.relu(out)
return out
class Generator(nn.Module):
def __init__(self, in_channels, out_channels, num_blocks):
super(Generator, self).__init__()
self.conv1 = nn.Conv2d(in_channels, 64, kernel_size=7, stride=1, padding=3, bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(128)
self.conv3 = nn.Conv2d(128, 256, kernel_size=3, stride=2, padding=1, bias=False)
self.bn3 = nn.BatchNorm2d(256)
self.res_blocks = nn.Sequential(*[ResidualBlock(256, 256) for _ in range(num_blocks)])
self.deconv1 = nn.ConvTranspose2d(256, 128, kernel_size=3, stride=2, padding=1, output_padding=1, bias=False)
self.bn4 = nn.BatchNorm2d(128)
self.deconv2 = nn.ConvTranspose2d(128, 64, kernel_size=3, stride=2, padding=1, output_padding=1, bias=False)
self.bn5 = nn.BatchNorm2d(64)
self.conv4 = nn.Conv2d(64, out_channels, kernel_size=7, stride=1, padding=3, bias=False)
self.tanh = nn.Tanh()
def forward(self, x):
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
out = self.relu(out)
out = self.res_blocks(out)
out = self.deconv1(out)
out = self.bn4(out)
out = self.relu(out)
out = self.deconv2(out)
out = self.bn5(out)
out = self.relu(out)
out = self.conv4(out)
out = self.tanh(out)
return out
class Discriminator(nn.Module):
def __init__(self, in_channels):
super(Discriminator, self).__init__()
self.conv1 = nn.Conv2d(in_channels, 64, kernel_size=4, stride=2, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(64, 128, kernel_size=4, stride=2, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(128)
self.conv3 = nn.Conv2d(128, 256, kernel_size=4, stride=2, padding=1, bias=False)
self.bn3 = nn.BatchNorm2d(256)
self.conv4 = nn.Conv2d(256, 512, kernel_size=4, stride=1, padding=1, bias=False)
self.bn4 = nn.BatchNorm2d(512)
self.conv5 = nn.Conv2d(512, 1, kernel_size=4, stride=1, padding=1, bias=False)
def forward(self, x):
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
out = self.relu(out)
out = self.conv4(out)
out = self.bn4(out)
out = self.relu(out)
out = self.conv5(out)
return out
# Load the trained model
model_path = 'dl_2_trained_model.pth' # Update the path accordingly
print("Model Loaded")
# Initialize the model architecture
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = Generator(3, 3, num_blocks=9).to(device)
# Load the pre-trained weights
checkpoint = torch.load(model_path, map_location=device)
print('Pre-Trained Wweights Loaded')
# Load the state dict
model.load_state_dict(checkpoint['G_AB_state_dict'])
# Set the model to evaluation mode
model.eval()
print("Evaluation Mode ON")
# Set up transforms for preprocessing input images
transform = transforms.Compose([
transforms.Resize((256, 256)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
# Process each image
for i in range(num):
input_image_path = input_images[i]
output_image_path = output_images[i]
print(input_image_path)
# Perform inference
input_image = Image.open(input_image_path).convert('RGB')
input_tensor = transform(input_image).unsqueeze(0).to(device)
with torch.no_grad():
dehazed_image = model(input_tensor).squeeze(0).cpu()
# Save the dehazed image
dehazed_image = to_pil_image(dehazed_image)
dehazed_image.save(output_image_path)
print("Dehazing complete.")