-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
190 lines (152 loc) · 7.61 KB
/
Copy pathutils.py
File metadata and controls
190 lines (152 loc) · 7.61 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import torch
import os
import math
import random
import numpy as np
from PIL import Image
import cv2
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import init
from torchvision import transforms
from warmup_scheduler import GradualWarmupScheduler
from skimage import measure
os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'
def seed_pytorch(seed=42):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
def weights_init_xavier(m):
classname = m.__class__.__name__
if classname.find('Conv2d') != -1 and classname.find('SplAtConv2d') == -1:
init.xavier_normal(m.weight.data)
def weights_init_kaiming(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
elif classname.find('Linear') != -1:
init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
elif classname.find('BatchNorm') != -1:
init.normal_(m.weight.data, 1.0, 0.02)
init.constant_(m.bias.data, 0.0)
class Get_gradient_nopadding(nn.Module):
def __init__(self):
super(Get_gradient_nopadding, self).__init__()
kernel_v = [[0, -1, 0],
[0, 0, 0],
[0, 1, 0]]
kernel_h = [[0, 0, 0],
[-1, 0, 1],
[0, 0, 0]]
kernel_h = torch.FloatTensor(kernel_h).unsqueeze(0).unsqueeze(0)
kernel_v = torch.FloatTensor(kernel_v).unsqueeze(0).unsqueeze(0)
self.weight_h = nn.Parameter(data=kernel_h, requires_grad=False).cuda()
self.weight_v = nn.Parameter(data=kernel_v, requires_grad=False).cuda()
def forward(self, x):
x0 = x[:, 0]
x0_v = F.conv2d(x0.unsqueeze(1), self.weight_v, padding=1)
x0_h = F.conv2d(x0.unsqueeze(1), self.weight_h, padding=1)
x0 = torch.sqrt(torch.pow(x0_v, 2) + torch.pow(x0_h, 2) + 1e-6)
return x0
def random_crop(img, mask, patch_size, pos_prob=None):
h, w = img.shape
if min(h, w) < patch_size:
img = np.pad(img, ((0, max(h, patch_size) - h), (0, max(w, patch_size) - w)),
mode='constant') # Pad the shorter side to match patch_size
mask = np.pad(mask, ((0, max(h, patch_size) - h), (0, max(w, patch_size) - w)),
mode='constant') # Apply the same transformation to the label
h, w = img.shape
while 1:
h_start = random.randint(0, h - patch_size)
h_end = h_start + patch_size
w_start = random.randint(0, w - patch_size)
w_end = w_start + patch_size
img_patch = img[h_start:h_end, w_start:w_end]
mask_patch = mask[h_start:h_end, w_start:w_end]
if pos_prob == None or random.random() > pos_prob:
break
elif mask_patch.sum() > 0:
break
return img_patch, mask_patch
def Normalized(img, img_norm_cfg):
return (img - img_norm_cfg['mean']) / img_norm_cfg['std']
def Denormalization(img, img_norm_cfg):
return img * img_norm_cfg['std'] + img_norm_cfg['mean']
def get_img_norm_cfg(dataset_name, dataset_dir):
# Predefined normalization configurations for known datasets
norm_configs = {
'NUAA-SIRST': dict(mean=101.06385040283203, std=34.619606018066406),
'NUDT-SIRST': dict(mean=107.80905151367188, std=33.02274703979492),
'IRSTD-1K': dict(mean=87.4661865234375, std=39.71953201293945),
'NUDT-SIRST-Sea': dict(mean=43.62403869628906, std=18.91838264465332),
'IRDST-real': {'mean': 101.54053497314453, 'std': 56.49856185913086}
}
# SIRST datasets 2-7 share the same normalization as NUAA-SIRST
sirst_datasets = ['SIRST2', 'SIRST3', 'SIRST4', 'SIRST5', 'SIRST6', 'SIRST7']
for sirst_dataset in sirst_datasets:
norm_configs[sirst_dataset] = norm_configs['NUAA-SIRST']
# Return predefined config if available, otherwise compute from dataset
if dataset_name in norm_configs:
return norm_configs[dataset_name]
else:
# Compute normalization from actual dataset images
with open(dataset_dir + '/' + dataset_name + '/img_idx/train_' + dataset_name + '.txt', 'r') as f:
train_list = f.read().splitlines()
with open(dataset_dir + '/' + dataset_name + '/img_idx/test_' + dataset_name + '.txt', 'r') as f:
test_list = f.read().splitlines()
img_list = train_list + test_list
img_dir = dataset_dir + '/' + dataset_name + '/images/'
mean_list = []
std_list = []
for img_pth in img_list:
try:
img = Image.open((img_dir + img_pth).replace('//', '/') + '.png').convert('I')
except:
try:
img = Image.open((img_dir + img_pth).replace('//', '/') + '.jpg').convert('I')
except:
img = Image.open((img_dir + img_pth).replace('//', '/') + '.bmp').convert('I')
img = np.array(img, dtype=np.float32)
mean_list.append(img.mean())
std_list.append(img.std())
return dict(mean=float(np.array(mean_list).mean()), std=float(np.array(std_list).mean()))
def get_optimizer(net, optimizer_name, scheduler_name, optimizer_settings, scheduler_settings):
if optimizer_name == 'Adam':
optimizer = torch.optim.Adam(net.parameters(), lr=optimizer_settings['lr'])
if optimizer_name == 'Adamweight':
optimizer = torch.optim.Adam(net.parameters(), lr=optimizer_settings['lr'], weight_decay=1e-3)
elif optimizer_name == 'Adagrad':
optimizer = torch.optim.Adagrad(net.parameters(), lr=optimizer_settings['lr'])
elif optimizer_name == 'SGD':
optimizer = torch.optim.SGD(net.parameters(), lr=optimizer_settings['lr'],
momentum=0.9,
weight_decay=scheduler_settings['weight_decay'])
if scheduler_name == 'MultiStepLR':
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=scheduler_settings['step'],
gamma=scheduler_settings['gamma'])
elif scheduler_name == 'CosineAnnealingLR':
warmup_epochs = 10
scheduler_cosine = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=scheduler_settings['epochs'] - warmup_epochs,
eta_min=scheduler_settings['eta_min'])
scheduler = GradualWarmupScheduler(optimizer, multiplier=1, total_epoch=warmup_epochs,
after_scheduler=scheduler_cosine)
elif scheduler_name == 'CosineAnnealingLRw50':
warmup_epochs = 50
scheduler_cosine = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=scheduler_settings['epochs'] - warmup_epochs,
eta_min=scheduler_settings['eta_min'])
scheduler = GradualWarmupScheduler(optimizer, multiplier=1, total_epoch=warmup_epochs,
after_scheduler=scheduler_cosine)
elif scheduler_name == 'CosineAnnealingLRw0':
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=scheduler_settings['epochs'], eta_min=scheduler_settings['eta_min'])
return optimizer, scheduler
def PadImg(img, times=32):
h, w = img.shape
if not h % times == 0:
img = np.pad(img, ((0, (h // times + 1) * times - h), (0, 0)), mode='constant')
if not w % times == 0:
img = np.pad(img, ((0, 0), (0, (w // times + 1) * times - w)), mode='constant')
return img