Chen Zheng commited on
Commit
3167f6c
·
1 Parent(s): 9099f0b

ICCV 2023

Browse files

Former-commit-id: 0444f9813957fec259d532097df68a193d3f54c9

basicsr/losses/losses.py CHANGED
@@ -145,114 +145,114 @@ class WeightedTVLoss(L1Loss):
145
  return loss
146
 
147
 
148
- # @LOSS_REGISTRY.register()
149
- # class PerceptualLoss(nn.Module):
150
- # """Perceptual loss with commonly used style loss.
151
- #
152
- # Args:
153
- # layer_weights (dict): The weight for each layer of vgg feature.
154
- # Here is an example: {'conv5_4': 1.}, which means the conv5_4
155
- # feature layer (before relu5_4) will be extracted with weight
156
- # 1.0 in calculating losses.
157
- # vgg_type (str): The type of vgg network used as feature extractor.
158
- # Default: 'vgg19'.
159
- # use_input_norm (bool): If True, normalize the input image in vgg.
160
- # Default: True.
161
- # range_norm (bool): If True, norm images with range [-1, 1] to [0, 1].
162
- # Default: False.
163
- # perceptual_weight (float): If `perceptual_weight > 0`, the perceptual
164
- # loss will be calculated and the loss will multiplied by the
165
- # weight. Default: 1.0.
166
- # style_weight (float): If `style_weight > 0`, the style loss will be
167
- # calculated and the loss will multiplied by the weight.
168
- # Default: 0.
169
- # criterion (str): Criterion used for perceptual loss. Default: 'l1'.
170
- # """
171
- #
172
- # def __init__(self,
173
- # layer_weights,
174
- # vgg_type='vgg19',
175
- # use_input_norm=True,
176
- # range_norm=False,
177
- # perceptual_weight=1.0,
178
- # style_weight=0.,
179
- # criterion='l1'):
180
- # super(PerceptualLoss, self).__init__()
181
- # self.perceptual_weight = perceptual_weight
182
- # self.style_weight = style_weight
183
- # self.layer_weights = layer_weights
184
- # self.vgg = VGGFeatureExtractor(
185
- # layer_name_list=list(layer_weights.keys()),
186
- # vgg_type=vgg_type,
187
- # use_input_norm=use_input_norm,
188
- # range_norm=range_norm)
189
- #
190
- # self.criterion_type = criterion
191
- # if self.criterion_type == 'l1':
192
- # self.criterion = torch.nn.L1Loss()
193
- # elif self.criterion_type == 'l2':
194
- # self.criterion = torch.nn.L2loss()
195
- # elif self.criterion_type == 'fro':
196
- # self.criterion = None
197
- # else:
198
- # raise NotImplementedError(f'{criterion} criterion has not been supported.')
199
- #
200
- # def forward(self, x, gt):
201
- # """Forward function.
202
- #
203
- # Args:
204
- # x (Tensor): Input tensor with shape (n, c, h, w).
205
- # gt (Tensor): Ground-truth tensor with shape (n, c, h, w).
206
- #
207
- # Returns:
208
- # Tensor: Forward results.
209
- # """
210
- # # extract vgg features
211
- # x_features = self.vgg(x)
212
- # gt_features = self.vgg(gt.detach())
213
- #
214
- # # calculate perceptual loss
215
- # if self.perceptual_weight > 0:
216
- # percep_loss = 0
217
- # for k in x_features.keys():
218
- # if self.criterion_type == 'fro':
219
- # percep_loss += torch.norm(x_features[k] - gt_features[k], p='fro') * self.layer_weights[k]
220
- # else:
221
- # percep_loss += self.criterion(x_features[k], gt_features[k]) * self.layer_weights[k]
222
- # percep_loss *= self.perceptual_weight
223
- # else:
224
- # percep_loss = None
225
- #
226
- # # calculate style loss
227
- # if self.style_weight > 0:
228
- # style_loss = 0
229
- # for k in x_features.keys():
230
- # if self.criterion_type == 'fro':
231
- # style_loss += torch.norm(
232
- # self._gram_mat(x_features[k]) - self._gram_mat(gt_features[k]), p='fro') * self.layer_weights[k]
233
- # else:
234
- # style_loss += self.criterion(self._gram_mat(x_features[k]), self._gram_mat(
235
- # gt_features[k])) * self.layer_weights[k]
236
- # style_loss *= self.style_weight
237
- # else:
238
- # style_loss = None
239
- #
240
- # return percep_loss, style_loss
241
- #
242
- # def _gram_mat(self, x):
243
- # """Calculate Gram matrix.
244
- #
245
- # Args:
246
- # x (torch.Tensor): Tensor with shape of (n, c, h, w).
247
- #
248
- # Returns:
249
- # torch.Tensor: Gram matrix.
250
- # """
251
- # n, c, h, w = x.size()
252
- # features = x.view(n, c, w * h)
253
- # features_t = features.transpose(1, 2)
254
- # gram = features.bmm(features_t) / (c * h * w)
255
- # return gram
256
 
257
 
258
  @LOSS_REGISTRY.register()
 
145
  return loss
146
 
147
 
148
+ @LOSS_REGISTRY.register()
149
+ class PerceptualLoss(nn.Module):
150
+ """Perceptual loss with commonly used style loss.
151
+
152
+ Args:
153
+ layer_weights (dict): The weight for each layer of vgg feature.
154
+ Here is an example: {'conv5_4': 1.}, which means the conv5_4
155
+ feature layer (before relu5_4) will be extracted with weight
156
+ 1.0 in calculating losses.
157
+ vgg_type (str): The type of vgg network used as feature extractor.
158
+ Default: 'vgg19'.
159
+ use_input_norm (bool): If True, normalize the input image in vgg.
160
+ Default: True.
161
+ range_norm (bool): If True, norm images with range [-1, 1] to [0, 1].
162
+ Default: False.
163
+ perceptual_weight (float): If `perceptual_weight > 0`, the perceptual
164
+ loss will be calculated and the loss will multiplied by the
165
+ weight. Default: 1.0.
166
+ style_weight (float): If `style_weight > 0`, the style loss will be
167
+ calculated and the loss will multiplied by the weight.
168
+ Default: 0.
169
+ criterion (str): Criterion used for perceptual loss. Default: 'l1'.
170
+ """
171
+
172
+ def __init__(self,
173
+ layer_weights,
174
+ vgg_type='vgg19',
175
+ use_input_norm=True,
176
+ range_norm=False,
177
+ perceptual_weight=1.0,
178
+ style_weight=0.,
179
+ criterion='l1'):
180
+ super(PerceptualLoss, self).__init__()
181
+ self.perceptual_weight = perceptual_weight
182
+ self.style_weight = style_weight
183
+ self.layer_weights = layer_weights
184
+ self.vgg = VGGFeatureExtractor(
185
+ layer_name_list=list(layer_weights.keys()),
186
+ vgg_type=vgg_type,
187
+ use_input_norm=use_input_norm,
188
+ range_norm=range_norm)
189
+
190
+ self.criterion_type = criterion
191
+ if self.criterion_type == 'l1':
192
+ self.criterion = torch.nn.L1Loss()
193
+ elif self.criterion_type == 'l2':
194
+ self.criterion = torch.nn.L2loss()
195
+ elif self.criterion_type == 'fro':
196
+ self.criterion = None
197
+ else:
198
+ raise NotImplementedError(f'{criterion} criterion has not been supported.')
199
+
200
+ def forward(self, x, gt):
201
+ """Forward function.
202
+
203
+ Args:
204
+ x (Tensor): Input tensor with shape (n, c, h, w).
205
+ gt (Tensor): Ground-truth tensor with shape (n, c, h, w).
206
+
207
+ Returns:
208
+ Tensor: Forward results.
209
+ """
210
+ # extract vgg features
211
+ x_features = self.vgg(x)
212
+ gt_features = self.vgg(gt.detach())
213
+
214
+ # calculate perceptual loss
215
+ if self.perceptual_weight > 0:
216
+ percep_loss = 0
217
+ for k in x_features.keys():
218
+ if self.criterion_type == 'fro':
219
+ percep_loss += torch.norm(x_features[k] - gt_features[k], p='fro') * self.layer_weights[k]
220
+ else:
221
+ percep_loss += self.criterion(x_features[k], gt_features[k]) * self.layer_weights[k]
222
+ percep_loss *= self.perceptual_weight
223
+ else:
224
+ percep_loss = None
225
+
226
+ # calculate style loss
227
+ if self.style_weight > 0:
228
+ style_loss = 0
229
+ for k in x_features.keys():
230
+ if self.criterion_type == 'fro':
231
+ style_loss += torch.norm(
232
+ self._gram_mat(x_features[k]) - self._gram_mat(gt_features[k]), p='fro') * self.layer_weights[k]
233
+ else:
234
+ style_loss += self.criterion(self._gram_mat(x_features[k]), self._gram_mat(
235
+ gt_features[k])) * self.layer_weights[k]
236
+ style_loss *= self.style_weight
237
+ else:
238
+ style_loss = None
239
+
240
+ return percep_loss, style_loss
241
+
242
+ def _gram_mat(self, x):
243
+ """Calculate Gram matrix.
244
+
245
+ Args:
246
+ x (torch.Tensor): Tensor with shape of (n, c, h, w).
247
+
248
+ Returns:
249
+ torch.Tensor: Gram matrix.
250
+ """
251
+ n, c, h, w = x.size()
252
+ features = x.view(n, c, w * h)
253
+ features_t = features.transpose(1, 2)
254
+ gram = features.bmm(features_t) / (c * h * w)
255
+ return gram
256
 
257
 
258
  @LOSS_REGISTRY.register()
options/Train/train_DAT_2_x2.yml CHANGED
@@ -25,7 +25,7 @@ datasets:
25
  use_shuffle: True
26
  num_worker_per_gpu: 12
27
  batch_size_per_gpu: 8
28
- dataset_enlarge_ratio: 100
29
  prefetch_mode: ~
30
 
31
  val:
 
25
  use_shuffle: True
26
  num_worker_per_gpu: 12
27
  batch_size_per_gpu: 8
28
+ dataset_enlarge_ratio: 1
29
  prefetch_mode: ~
30
 
31
  val:
options/Train/train_DAT_2_x3.yml CHANGED
@@ -25,7 +25,7 @@ datasets:
25
  use_shuffle: True
26
  num_worker_per_gpu: 12
27
  batch_size_per_gpu: 8
28
- dataset_enlarge_ratio: 100
29
  prefetch_mode: ~
30
 
31
  val:
 
25
  use_shuffle: True
26
  num_worker_per_gpu: 12
27
  batch_size_per_gpu: 8
28
+ dataset_enlarge_ratio: 1
29
  prefetch_mode: ~
30
 
31
  val:
options/Train/train_DAT_2_x4.yml CHANGED
@@ -25,7 +25,7 @@ datasets:
25
  use_shuffle: True
26
  num_worker_per_gpu: 12
27
  batch_size_per_gpu: 8
28
- dataset_enlarge_ratio: 100
29
  prefetch_mode: ~
30
 
31
  val:
 
25
  use_shuffle: True
26
  num_worker_per_gpu: 12
27
  batch_size_per_gpu: 8
28
+ dataset_enlarge_ratio: 1
29
  prefetch_mode: ~
30
 
31
  val:
options/Train/train_DAT_S_x2.yml CHANGED
@@ -25,7 +25,7 @@ datasets:
25
  use_shuffle: True
26
  num_worker_per_gpu: 12
27
  batch_size_per_gpu: 2
28
- dataset_enlarge_ratio: 100
29
  prefetch_mode: ~
30
 
31
  val:
 
25
  use_shuffle: True
26
  num_worker_per_gpu: 12
27
  batch_size_per_gpu: 2
28
+ dataset_enlarge_ratio: 1
29
  prefetch_mode: ~
30
 
31
  val:
options/Train/train_DAT_S_x3.yml CHANGED
@@ -25,7 +25,7 @@ datasets:
25
  use_shuffle: True
26
  num_worker_per_gpu: 12
27
  batch_size_per_gpu: 8
28
- dataset_enlarge_ratio: 100
29
  prefetch_mode: ~
30
 
31
  val:
 
25
  use_shuffle: True
26
  num_worker_per_gpu: 12
27
  batch_size_per_gpu: 8
28
+ dataset_enlarge_ratio: 1
29
  prefetch_mode: ~
30
 
31
  val:
options/Train/train_DAT_S_x4.yml CHANGED
@@ -25,7 +25,7 @@ datasets:
25
  use_shuffle: True
26
  num_worker_per_gpu: 12
27
  batch_size_per_gpu: 8
28
- dataset_enlarge_ratio: 100
29
  prefetch_mode: ~
30
 
31
  val:
 
25
  use_shuffle: True
26
  num_worker_per_gpu: 12
27
  batch_size_per_gpu: 8
28
+ dataset_enlarge_ratio: 1
29
  prefetch_mode: ~
30
 
31
  val:
options/Train/train_DAT_light_x2.yml CHANGED
@@ -25,7 +25,7 @@ datasets:
25
  use_shuffle: True
26
  num_worker_per_gpu: 12
27
  batch_size_per_gpu: 8
28
- dataset_enlarge_ratio: 100
29
  prefetch_mode: ~
30
 
31
  val:
 
25
  use_shuffle: True
26
  num_worker_per_gpu: 12
27
  batch_size_per_gpu: 8
28
+ dataset_enlarge_ratio: 1
29
  prefetch_mode: ~
30
 
31
  val:
options/Train/train_DAT_light_x3.yml CHANGED
@@ -25,7 +25,7 @@ datasets:
25
  use_shuffle: True
26
  num_worker_per_gpu: 12
27
  batch_size_per_gpu: 8
28
- dataset_enlarge_ratio: 100
29
  prefetch_mode: ~
30
 
31
  val:
 
25
  use_shuffle: True
26
  num_worker_per_gpu: 12
27
  batch_size_per_gpu: 8
28
+ dataset_enlarge_ratio: 1
29
  prefetch_mode: ~
30
 
31
  val:
options/Train/train_DAT_light_x4.yml CHANGED
@@ -25,7 +25,7 @@ datasets:
25
  use_shuffle: True
26
  num_worker_per_gpu: 12
27
  batch_size_per_gpu: 8
28
- dataset_enlarge_ratio: 100
29
  prefetch_mode: ~
30
 
31
  val:
 
25
  use_shuffle: True
26
  num_worker_per_gpu: 12
27
  batch_size_per_gpu: 8
28
+ dataset_enlarge_ratio: 1
29
  prefetch_mode: ~
30
 
31
  val:
options/Train/train_DAT_x2.yml CHANGED
@@ -25,7 +25,7 @@ datasets:
25
  use_shuffle: True
26
  num_worker_per_gpu: 12
27
  batch_size_per_gpu: 8
28
- dataset_enlarge_ratio: 100
29
  prefetch_mode: ~
30
 
31
  val:
 
25
  use_shuffle: True
26
  num_worker_per_gpu: 12
27
  batch_size_per_gpu: 8
28
+ dataset_enlarge_ratio: 1
29
  prefetch_mode: ~
30
 
31
  val:
options/Train/train_DAT_x3.yml CHANGED
@@ -25,7 +25,7 @@ datasets:
25
  use_shuffle: True
26
  num_worker_per_gpu: 12
27
  batch_size_per_gpu: 8
28
- dataset_enlarge_ratio: 100
29
  prefetch_mode: ~
30
 
31
  val:
 
25
  use_shuffle: True
26
  num_worker_per_gpu: 12
27
  batch_size_per_gpu: 8
28
+ dataset_enlarge_ratio: 1
29
  prefetch_mode: ~
30
 
31
  val:
options/Train/train_DAT_x4.yml CHANGED
@@ -25,7 +25,7 @@ datasets:
25
  use_shuffle: True
26
  num_worker_per_gpu: 12
27
  batch_size_per_gpu: 8
28
- dataset_enlarge_ratio: 100
29
  prefetch_mode: ~
30
 
31
  val:
 
25
  use_shuffle: True
26
  num_worker_per_gpu: 12
27
  batch_size_per_gpu: 8
28
+ dataset_enlarge_ratio: 1
29
  prefetch_mode: ~
30
 
31
  val: