-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.py
executable file
·248 lines (189 loc) · 7.61 KB
/
edit.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
'''
edit.py
'''
#FROM Python LIBRARY
import time
import math
import numpy as np
import psutil
import random
from collections import OrderedDict
#FROM PyTorch
import torch
import torchvision
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from torchvision import datasets
from torchvision import transforms
from torchvision.utils import save_image
#from iqa-pytorch
from IQA_pytorch import MS_SSIM, SSIM, GMSD, LPIPSvgg
#from this project
import backbone.vision as vision
import model
import backbone.utils as utils
import backbone.structure as structure
import backbone.module.module as module
import backbone.predefined as predefined
from backbone.utils import loadModels, saveModels, backproagateAndWeightUpdate
from backbone.config import Config
from backbone.structure import Epoch
from dataLoader import DataLoader
from warmup_scheduler import GradualWarmupScheduler
################ V E R S I O N ################
# VERSION START (DO NOT EDIT THIS COMMENT, for tools/codeArchiver.py)
version = '1-ICASSP2025'
subversion = '1-mfg_crt'
# VERSION END (DO NOT EDIT THIS COMMENT, for tools/codeArchiver.py)
###############################################
#################################################
############### EDIT THIS AREA ################
#################################################
#################################################################################
# MODEL #
#################################################################################
class ModelList(structure.ModelListBase):
def __init__(self):
super(ModelList, self).__init__()
##############################################################
# self.(모델이름) :: model :: 필 수
# self.(모델이름)_optimizer :: optimizer :: 없어도됨
# self.(모델이름)_scheduler :: Learning Rate Scheduler :: 없어도됨
#-------------------------------------------------------------
# self.(모델이름)_pretrained :: pretrained 파일 경로 :: ** /model/ 폴더 밑에 저장된 모델이 없을 시 OR optimizer 가 없을 시 ---> pretrained 경로에서 로드
#
# trainStep() 에서 사용 방법
# modelList.(모델 인스턴스 이름)_optimizer
##############################################################
self.NET = predefined.MFG_CRT(
in_chans=3,
aux_in_chans=1,
out_chans=1,
embed_dim=48,
depths=(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
num_heads=(6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6),
split_size_0 = 4,
split_size_1 = 16,
mlp_ratio=2.,
qkv_bias=True,
qk_scale=None,
norm_layer=nn.LayerNorm,
upscale=2,
img_range=1.,
upsampler='',
resi_connection='1conv')
self.NET_optimizer = torch.optim.RAdam(self.NET.parameters(), lr=1.e-4)
self.NET_pretrained = "./MFG-CRT_SRx16.pth"
self.initApexAMP()
self.initDataparallel()
#################################################################################
# STEPS #
#################################################################################
def trainStep(epoch, modelList, dataDict):
lr_images = dataDict['LR_X16']
eo_images = dataDict['EO']
gt_images = dataDict['GT']
#define loss function
l1_criterion = nn.L1Loss()
#train mode
modelList.NET.train()
#SR
sr_images = modelList.NET(eo_images, lr_images)
#calculate loss and backpropagation
loss = l1_criterion(sr_images, gt_images)
backproagateAndWeightUpdate(
modelList,
loss,
modelNames=["NET"]
)
#return values
lossDict = {
'TRAIN_LOSS': loss
}
resultImagesDict = {
"SR": sr_images
}
return lossDict, resultImagesDict
def validationStep(epoch, modelList, dataDict):
lr_images = dataDict['LR_X16']
eo_images = dataDict['EO']
gt_images = dataDict['GT']
#define loss function
l1_criterion = nn.L1Loss()
#eval mode
modelList.NET.eval()
with torch.no_grad():
###### SR
sr_images = modelList.NET(eo_images, lr_images)
#calculate loss
loss = l1_criterion(sr_images, gt_images)
#return values
lossDict = {
'VAL_LOSS': loss
}
resultImagesDict = {
"SR": sr_images
}
return lossDict, resultImagesDict
def inferenceStep(epoch, modelList, dataDict):
lr_images = dataDict['LR_X16']
eo_images = dataDict['EO_X16']
#eval mode
modelList.NET.eval()
with torch.no_grad():
###### SR
sr_images = modelList.NET(eo_images, lr_images)
#return values
resultImagesDict = {
"SR": sr_images
}
return {}, resultImagesDict
#################################################################################
# EPOCH #
#################################################################################
modelList = ModelList()
trainEpoch = Epoch(
dataLoader = DataLoader('train_gtisr'),
modelList = modelList,
step = trainStep,
researchVersion = version,
researchSubVersion = subversion,
writer = utils.initTensorboardWriter(version, subversion),
scoreMetricDict = {
},
resultSaveData = [] ,
resultSaveFileName = 'train',
isNoResultArchiving = Config.param.save.remainOnlyLastSavedResult,
earlyStopIteration = Config.param.train.step.earlyStopStep,
name = 'TRAIN'
)
validationEpoch = Epoch(
dataLoader = DataLoader('validation_gtisr'),
modelList = modelList,
step = validationStep,
researchVersion = version,
researchSubVersion = subversion,
writer = utils.initTensorboardWriter(version, subversion),
scoreMetricDict = {
},
resultSaveData = [] ,
resultSaveFileName = 'validation',
isNoResultArchiving = Config.param.save.remainOnlyLastSavedResult,
earlyStopIteration = Config.param.train.step.earlyStopStep,
name = 'VALIDATION'
)
inferenceEpoch = Epoch(
dataLoader = DataLoader('inference_gtisr'),
modelList = modelList,
step = inferenceStep,
researchVersion = version,
researchSubVersion = subversion,
writer = utils.initTensorboardWriter(version, subversion),
scoreMetricDict = {},
resultSaveData = [] ,
resultSaveFileName = 'inference',
isNoResultArchiving = Config.param.save.remainOnlyLastSavedResult,
earlyStopIteration = Config.param.train.step.earlyStopStep,
name = 'INFERENCE'
)