-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_fusionnet.py
184 lines (158 loc) · 7.5 KB
/
model_fusionnet.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
##############################################################################
# This is a copy from https://github.com/liangjiandeng/DLPan-Toolbox/tree/main/01-DL-toolbox(Pytorch)/UDL/pansharpening/models
##############################################################################
import torch
import torch.nn as nn
import numpy as np
import math
import torch.nn.init as int
class loss_with_l2_regularization(nn.Module):
def __init__(self):
super(loss_with_l2_regularization, self).__init__()
def forward(self, criterion, model, weight_decay=1e-5, flag=False):
regularizations = []
for k, v in model.named_parameters():
if 'conv' in k and 'weight' in k:
# print(k)
penality = weight_decay * ((v.data ** 2).sum() / 2)
regularizations.append(penality)
if flag:
print("{} : {}".format(k, penality))
# r = torch.sum(regularizations)
loss = criterion + sum(regularizations)
return loss
# -------------Initialization----------------------------------------
def init_weights(*modules):
for module in modules:
for m in module.modules():
if isinstance(m, nn.Conv2d): ## initialization for Conv2d
# print("initial nn.Conv2d with var_scale_new: ", m)
# try:
# import tensorflow as tf
# tensor = tf.get_variable(shape=m.weight.shape, initializer=tf.variance_scaling_initializer(seed=1))
# m.weight.data = tensor.eval()
# except:
# print("try error, run variance_scaling_initializer")
# variance_scaling_initializer(m.weight)
variance_scaling_initializer(m.weight) # method 1: initialization
# nn.init.kaiming_normal_(m.weight, mode='fan_in', nonlinearity='relu') # method 2: initialization
if m.bias is not None:
nn.init.constant_(m.bias, 0.0)
elif isinstance(m, nn.BatchNorm2d): ## initialization for BN
nn.init.constant_(m.weight, 1.0)
nn.init.constant_(m.bias, 0.0)
elif isinstance(m, nn.Linear): ## initialization for nn.Linear
# variance_scaling_initializer(m.weight)
nn.init.kaiming_normal_(m.weight, mode='fan_in', nonlinearity='relu')
if m.bias is not None:
nn.init.constant_(m.bias, 0.0)
def truncated_normal_(tensor, mean=0.0, std=1.0):
with torch.no_grad():
size = tensor.shape
tmp = tensor.new_empty(size + (4,)).normal_()
valid = (tmp < 2) & (tmp > -2)
ind = valid.max(-1, keepdim=True)[1]
tensor.data.copy_(tmp.gather(-1, ind).squeeze(-1))
tensor.data.mul_(std).add_(mean)
return tensor
def variance_scaling_initializer(tensor):
from scipy.stats import truncnorm
def calculate_fan(shape, factor=2.0, mode='FAN_IN', uniform=False):
# 64 9 3 3 -> 3 3 9 64
# 64 64 3 3 -> 3 3 64 64
if shape:
# fan_in = float(shape[1]) if len(shape) > 1 else float(shape[0])
# fan_out = float(shape[0])
fan_in = float(shape[-2]) if len(shape) > 1 else float(shape[-1])
fan_out = float(shape[-1])
else:
fan_in = 1.0
fan_out = 1.0
for dim in shape[:-2]:
fan_in *= float(dim)
fan_out *= float(dim)
if mode == 'FAN_IN':
# Count only number of input connections.
n = fan_in
elif mode == 'FAN_OUT':
# Count only number of output connections.
n = fan_out
elif mode == 'FAN_AVG':
# Average number of inputs and output connections.
n = (fan_in + fan_out) / 2.0
if uniform:
raise NotImplemented
# # To get stddev = math.sqrt(factor / n) need to adjust for uniform.
# limit = math.sqrt(3.0 * factor / n)
# return random_ops.random_uniform(shape, -limit, limit,
# dtype, seed=seed)
else:
# To get stddev = math.sqrt(factor / n) need to adjust for truncated.
trunc_stddev = math.sqrt(1.3 * factor / n)
return fan_in, fan_out, trunc_stddev
def variance_scaling(x, scale=1.0, mode="fan_in", distribution="truncated_normal", seed=None):
# fan_in, fan_out = torch.nn.init._calculate_fan_in_and_fan_out(x)
x = x.permute(3, 2, 1, 0) # .permute(2, 3, 1, 0)
fan_in, fan_out, trunc_stddev = calculate_fan(x.shape)
# print(trunc_stddev) # debug
# if mode == "fan_in":
# scale /= max(1., fan_in)
# elif mode == "fan_out":
# scale /= max(1., fan_out)
# else:
# scale /= max(1., (fan_in + fan_out) / 2.)
# if distribution == "normal" or distribution == "truncated_normal":
# # constant taken from scipy.stats.truncnorm.std(a=-2, b=2, loc=0., scale=1.)
# stddev = math.sqrt(scale) / .87962566103423978
# print(fan_in,fan_out,scale,stddev)#100,100,0.01,0.1136
truncated_normal_(x, 0.0, trunc_stddev) # 0.001)
x = x.permute(3, 2, 0, 1)
# print(x.min(), x.max())) # debug
return x # /10*1.28
variance_scaling(tensor)
return tensor
# -------------ResNet Block (One)----------------------------------------
class Resblock(nn.Module):
def __init__(self):
super(Resblock, self).__init__()
channel = 32
self.conv20 = nn.Conv2d(in_channels=channel, out_channels=channel, kernel_size=3, stride=1, padding=1,
bias=True)
self.conv21 = nn.Conv2d(in_channels=channel, out_channels=channel, kernel_size=3, stride=1, padding=1,
bias=True)
self.relu = nn.ReLU(inplace=True)
def forward(self, x): # x= hp of ms; y = hp of pan
rs1 = self.relu(self.conv20(x)) # Bsx32x64x64
rs1 = self.conv21(rs1) # Bsx32x64x64
rs = torch.add(x, rs1) # Bsx32x64x64
return rs
class FusionNet(nn.Module):
def __init__(self, spectral_num, channel=32):
super(FusionNet, self).__init__()
# ConvTranspose2d: output = (input - 1)*stride + outpading - 2*padding + kernelsize
self.spectral_num = spectral_num
self.conv1 = nn.Conv2d(in_channels=spectral_num, out_channels=channel, kernel_size=3, stride=1, padding=1,
bias=True)
self.res1 = Resblock()
self.res2 = Resblock()
self.res3 = Resblock()
self.res4 = Resblock()
self.conv3 = nn.Conv2d(in_channels=channel, out_channels=spectral_num, kernel_size=3, stride=1, padding=1,
bias=True)
self.relu = nn.ReLU(inplace=True)
self.backbone = nn.Sequential( # method 2: 4 resnet repeated blocks
self.res1,
self.res2,
self.res3,
self.res4
)
init_weights(self.backbone, self.conv1, self.conv3) # state initialization, important!
self.apply(init_weights)
def forward(self, x, y): # x= lms; y = pan
pan_concat = y.repeat(1, self.spectral_num, 1, 1) # Bsx8x64x64
input = torch.sub(pan_concat, x) # Bsx8x64x64
rs = self.relu(self.conv1(input)) # Bsx32x64x64
rs = self.backbone(rs) # ResNet's backbone!
output = self.conv3(rs) # Bsx8x64x64
output = self.relu(output) # PSDip add this term
return output # lms + outs