-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
573 lines (551 loc) · 20 KB
/
utils.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
### python lib
import os, sys, random, math, cv2, pickle, subprocess
import numpy as np
# from PIL import Image
from PIL import Image, ImageOps
### torch lib
import torch
from torch.utils.data.sampler import Sampler
from torch.utils.data import DataLoader
### custom lib
import math
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import pdb
FLO_TAG = 202021.25
EPS = 1e-12
UNKNOWN_FLOW_THRESH = 1e7
SMALLFLOW = 0.0
LARGEFLOW = 1e8
######################################################################################
## Training utility
######################################################################################
def repackage_hidden(h):
"""Wraps hidden states in new Variables, to detach them from their history."""
if isinstance(h, torch.Tensor):
return h.detach()
else:
return tuple(repackage_hidden(v) for v in h)
def normalize_ImageNet_stats(batch):
mean = torch.zeros_like(batch)
std = torch.zeros_like(batch)
mean[:, 0, :, :] = 0.485
mean[:, 1, :, :] = 0.456
mean[:, 2, :, :] = 0.406
std[:, 0, :, :] = 0.229
std[:, 1, :, :] = 0.224
std[:, 2, :, :] = 0.225
batch_out = (batch - mean) / std
return batch_out
###################### get image path list ######################
IMG_EXTENSIONS = ['.jpg', '.JPG', '.jpeg', '.JPEG', '.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP']
def is_image_file(filename):
return any(filename.endswith(extension) for extension in IMG_EXTENSIONS)
def _get_paths_from_images(path):
"""get image path list from image folder"""
assert os.path.isdir(path), '{:s} is not a valid directory'.format(path)
images = []
for dirpath, _, fnames in sorted(os.walk(path)):
for fname in sorted(fnames):
if is_image_file(fname):
img_path = os.path.join(dirpath, fname)
images.append(img_path)
assert images, '{:s} has no valid image file'.format(path)
return images
def img2tensor(img):
img_t = np.expand_dims(img.transpose(2, 0, 1), axis=0)
img_t = torch.from_numpy(img_t.astype(np.float32))
return img_t
def tensor2img(tensor, out_type=np.uint8, min_max=(0, 1)):
'''
Converts a torch Tensor into an image Numpy array
Input: 4D(B,(3/1),H,W), 3D(C,H,W), or 2D(H,W), any range, RGB channel order
Output: 3D(H,W,C) or 2D(H,W), [0,255], np.uint8 (default)
'''
tensor = tensor.squeeze().float().cpu().clamp_(*min_max) # clamp
tensor = (tensor - min_max[0]) / (min_max[1] - min_max[0]) # to range [0,1]
n_dim = tensor.dim()
if n_dim == 4:
n_img = len(tensor)
img_np = make_grid(tensor, nrow=int(math.sqrt(n_img)), normalize=False).numpy()
img_np = np.transpose(img_np[[2, 1, 0], :, :], (1, 2, 0)) # HWC, BGR
elif n_dim == 3:
img_np = tensor.numpy()
img_np = np.transpose(img_np[[2, 1, 0], :, :], (1, 2, 0)) # HWC, BGR
elif n_dim == 2:
img_np = tensor.numpy()
else:
raise TypeError(
'Only support 4D, 3D and 2D tensor. But received with dimension: {:d}'.format(n_dim))
if out_type == np.uint8:
img_np = (img_np * 255.0).round()
# Important. Unlike matlab, numpy.unit8() WILL NOT round by default.
return img_np.astype(out_type)
def save_img(img, img_path, mode='RGB'):
cv2.imwrite(img_path, img)
def save_model(model, optimizer, opts):
# save opts
opts_filename = os.path.join(opts.model_dir, "opts.pth")
print("Save %s" %opts_filename)
with open(opts_filename, 'wb') as f:
pickle.dump(opts, f)
# serialize model and optimizer to dict
state_dict = {
'model': model.state_dict(),
'optimizer' : optimizer.state_dict(),
}
model_filename = os.path.join(opts.model_dir, "model_epoch_%d.pth" %model.epoch)
print("Save %s" %model_filename)
torch.save(state_dict, model_filename)
def load_model(model, optimizer, opts, epoch):
# load model
model_filename = os.path.join(opts.model_dir, "model_epoch_%d.pth" %epoch)
print("Load %s" %model_filename)
state_dict = torch.load(model_filename)
model.load_state_dict(state_dict['model'])
optimizer.load_state_dict(state_dict['optimizer'])
### move optimizer state to GPU
for state in optimizer.state.values():
for k, v in state.items():
if torch.is_tensor(v):
state[k] = v.cuda()
model.epoch = epoch ## reset model epoch
return model, optimizer
class SubsetSequentialSampler(Sampler):
def __init__(self, indices):
self.indices = indices
def __iter__(self):
return (self.indices[i] for i in range(len(self.indices)))
def __len__(self):
return len(self.indices)
def create_data_loader(data_set, opts, mode):
### generate random index
if mode == 'train':
total_samples = opts.train_epoch_size * opts.batch_size
else:
total_samples = opts.valid_epoch_size * opts.batch_size
num_epochs = int(math.ceil(float(total_samples) / len(data_set)))
indices = np.random.permutation(len(data_set))
indices = np.tile(indices, num_epochs)
indices = indices[:total_samples]
### generate data sampler and loader
sampler = SubsetSequentialSampler(indices)
print('data_set',len(data_set),'num_epochs',num_epochs)
data_loader = DataLoader(dataset=data_set, num_workers=8, batch_size=opts.batch_size, pin_memory=True) ##opts.threads:8
return data_loader
def learning_rate_decay(opts, epoch):
### 1 ~ offset : lr_init
### offset ~ offset + step : lr_init * drop^1
### offset + step ~ offset + step * 2 : lr_init * drop^2
### ...
if opts.lr_drop == 0: # constant learning rate
decay = 0
else:
assert(opts.lr_step > 0)
decay = math.floor( float(epoch) / opts.lr_step )
decay = max(decay, 0) ## decay = 1 for the first lr_offset iterations
lr = opts.lr_init * math.pow(opts.lr_drop, decay)
lr = max(lr, opts.lr_init * opts.lr_min)
return lr
def count_network_parameters(model):
parameters = filter(lambda p: p.requires_grad, model.parameters())
N = sum([np.prod(p.size()) for p in parameters])
return N
######################################################################################
## Image utility
######################################################################################
def rotate_image(img, degree, interp=cv2.INTER_LINEAR):
height, width = img.shape[:2]
image_center = (width/2, height/2)
rotation_mat = cv2.getRotationMatrix2D(image_center, degree, 1.)
abs_cos = abs(rotation_mat[0,0])
abs_sin = abs(rotation_mat[0,1])
bound_w = int(height * abs_sin + width * abs_cos)
bound_h = int(height * abs_cos + width * abs_sin)
rotation_mat[0, 2] += bound_w/2 - image_center[0]
rotation_mat[1, 2] += bound_h/2 - image_center[1]
img_out = cv2.warpAffine(img, rotation_mat, (bound_w, bound_h), flags=interp+cv2.WARP_FILL_OUTLIERS)
return img_out
def numpy_to_PIL(img_np):
## input image is numpy array in [0, 1]
## convert to PIL image in [0, 255]
img_PIL = np.uint8(img_np * 255)
img_PIL = Image.fromarray(img_PIL)
return img_PIL
def PIL_to_numpy(img_PIL):
img_np = np.asarray(img_PIL)
img_np = np.float32(img_np) / 255.0
return img_np
def read_img(filename, grayscale=0):
## read image and convert to RGB in [0, 1]
if grayscale:
img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
if img is None:
raise Exception("Image %s does not exist" %filename)
img = np.expand_dims(img, axis=2)
else:
img = cv2.imread(filename)
if img is None:
raise Exception("Image %s does not exist" %filename)
img = img[:, :, ::-1] ## BGR to RGB
img = np.float32(img) / 255.0
return img
# ######################################################################################
## Flow utility
######################################################################################
def read_flo(filename):
with open(filename, 'rb') as f:
tag = np.fromfile(f, np.float32, count=1)
if tag != FLO_TAG:
sys.exit('Wrong tag. Invalid .flo file %s' %filename)
else:
w = int(np.fromfile(f, np.int32, count=1))
h = int(np.fromfile(f, np.int32, count=1))
data = np.fromfile(f, np.float32, count=2*w*h)
# Reshape data into 3D array (columns, rows, bands)
flow = np.resize(data, (h, w, 2))
return flow
def save_flo(flow, filename):
with open(filename, 'wb') as f:
tag = np.array([FLO_TAG], dtype=np.float32)
(height, width) = flow.shape[0:2]
w = np.array([width], dtype=np.int32)
h = np.array([height], dtype=np.int32)
tag.tofile(f)
w.tofile(f)
h.tofile(f)
flow.tofile(f)
def resize_flow(flow, W_out=0, H_out=0, scale=0):
if W_out == 0 and H_out == 0 and scale == 0:
raise Exception("(W_out, H_out) or scale should be non-zero")
H_in = flow.shape[0]
W_in = flow.shape[1]
if scale == 0:
y_scale = float(H_out) / H_in
x_scale = float(W_out) / W_in
else:
y_scale = scale
x_scale = scale
flow_out = cv2.resize(flow, None, fx=x_scale, fy=y_scale, interpolation=cv2.INTER_LINEAR)
flow_out[:, :, 0] = flow_out[:, :, 0] * x_scale
flow_out[:, :, 1] = flow_out[:, :, 1] * y_scale
return flow_out
def rotate_flow(flow, degree, interp=cv2.INTER_LINEAR):
## angle in radian
angle = math.radians(degree)
H = flow.shape[0]
W = flow.shape[1]
#rotation_matrix = cv2.getRotationMatrix2D((W/2, H/2), math.degrees(angle), 1)
#flow_out = cv2.warpAffine(flow, rotation_matrix, (W, H))
flow_out = rotate_image(flow, degree, interp)
fu = flow_out[:, :, 0] * math.cos(-angle) - flow_out[:, :, 1] * math.sin(-angle)
fv = flow_out[:, :, 0] * math.sin(-angle) + flow_out[:, :, 1] * math.cos(-angle)
flow_out[:, :, 0] = fu
flow_out[:, :, 1] = fv
return flow_out
def hflip_flow(flow):
flow_out = cv2.flip(flow, flipCode=0)
flow_out[:, :, 0] = flow_out[:, :, 0] * (-1)
return flow_out
def vflip_flow(flow):
flow_out = cv2.flip(flow, flipCode=1)
flow_out[:, :, 1] = flow_out[:, :, 1] * (-1)
return flow_out
def flow_to_rgb(flow):
"""
Convert flow into middlebury color code image
:param flow: optical flow map
:return: optical flow image in middlebury color
"""
u = flow[:, :, 0]
v = flow[:, :, 1]
maxu = -999.
maxv = -999.
minu = 999.
minv = 999.
idxUnknow = (abs(u) > UNKNOWN_FLOW_THRESH) | (abs(v) > UNKNOWN_FLOW_THRESH)
u[idxUnknow] = 0
v[idxUnknow] = 0
maxu = max(maxu, np.max(u))
minu = min(minu, np.min(u))
maxv = max(maxv, np.max(v))
minv = min(minv, np.min(v))
rad = np.sqrt(u ** 2 + v ** 2)
maxrad = max(-1, np.max(rad))
u = u/(maxrad + np.finfo(float).eps)
v = v/(maxrad + np.finfo(float).eps)
img = compute_color(u, v)
idx = np.repeat(idxUnknow[:, :, np.newaxis], 3, axis=2)
img[idx] = 0
return np.float32(img) / 255.0
def compute_color(u, v):
"""
compute optical flow color map
:param u: optical flow horizontal map
:param v: optical flow vertical map
:return: optical flow in color code
"""
[h, w] = u.shape
img = np.zeros([h, w, 3])
nanIdx = np.isnan(u) | np.isnan(v)
u[nanIdx] = 0
v[nanIdx] = 0
colorwheel = make_color_wheel()
ncols = np.size(colorwheel, 0)
rad = np.sqrt(u**2+v**2)
a = np.arctan2(-v, -u) / np.pi
fk = (a+1) / 2 * (ncols - 1) + 1
k0 = np.floor(fk).astype(int)
k1 = k0 + 1
k1[k1 == ncols+1] = 1
f = fk - k0
for i in range(0, np.size(colorwheel,1)):
tmp = colorwheel[:, i]
col0 = tmp[k0-1] / 255
col1 = tmp[k1-1] / 255
col = (1-f) * col0 + f * col1
idx = rad <= 1
col[idx] = 1-rad[idx]*(1-col[idx])
notidx = np.logical_not(idx)
col[notidx] *= 0.75
img[:, :, i] = np.uint8(np.floor(255 * col*(1-nanIdx)))
return img
def make_color_wheel():
"""
Generate color wheel according Middlebury color code
:return: Color wheel
"""
RY = 15
YG = 6
GC = 4
CB = 11
BM = 13
MR = 6
ncols = RY + YG + GC + CB + BM + MR
colorwheel = np.zeros([ncols, 3])
col = 0
# RY
colorwheel[0:RY, 0] = 255
colorwheel[0:RY, 1] = np.transpose(np.floor(255*np.arange(0, RY) / RY))
col += RY
# YG
colorwheel[col:col+YG, 0] = 255 - np.transpose(np.floor(255*np.arange(0, YG) / YG))
colorwheel[col:col+YG, 1] = 255
col += YG
# GC
colorwheel[col:col+GC, 1] = 255
colorwheel[col:col+GC, 2] = np.transpose(np.floor(255*np.arange(0, GC) / GC))
col += GC
# CB
colorwheel[col:col+CB, 1] = 255 - np.transpose(np.floor(255*np.arange(0, CB) / CB))
colorwheel[col:col+CB, 2] = 255
col += CB
# BM
colorwheel[col:col+BM, 2] = 255
colorwheel[col:col+BM, 0] = np.transpose(np.floor(255*np.arange(0, BM) / BM))
col += + BM
# MR
colorwheel[col:col+MR, 2] = 255 - np.transpose(np.floor(255 * np.arange(0, MR) / MR))
colorwheel[col:col+MR, 0] = 255
return colorwheel
def compute_flow_magnitude(flow):
flow_mag = flow[:, :, 0] ** 2 + flow[:, :, 1] ** 2
return flow_mag
def compute_flow_gradients(flow):
H = flow.shape[0]
W = flow.shape[1]
flow_x_du = np.zeros((H, W))
flow_x_dv = np.zeros((H, W))
flow_y_du = np.zeros((H, W))
flow_y_dv = np.zeros((H, W))
flow_x = flow[:, :, 0]
flow_y = flow[:, :, 1]
flow_x_du[:, :-1] = flow_x[:, :-1] - flow_x[:, 1:]
flow_x_dv[:-1, :] = flow_x[:-1, :] - flow_x[1:, :]
flow_y_du[:, :-1] = flow_y[:, :-1] - flow_y[:, 1:]
flow_y_dv[:-1, :] = flow_y[:-1, :] - flow_y[1:, :]
return flow_x_du, flow_x_dv, flow_y_du, flow_y_dv
def detect_occlusion(fw_flow, bw_flow):
## fw-flow: img1 => img2
## bw-flow: img2 => img1
with torch.no_grad():
## convert to tensor
fw_flow_t = img2tensor(fw_flow).cuda()
bw_flow_t = img2tensor(bw_flow).cuda()
## warp fw-flow to img2
flow_warping = Resample2d().cuda()
fw_flow_w = flow_warping(fw_flow_t, bw_flow_t)
## convert to numpy array
fw_flow_w = tensor2img(fw_flow_w)
## occlusion
fb_flow_sum = fw_flow_w + bw_flow
fb_flow_mag = compute_flow_magnitude(fb_flow_sum)
fw_flow_w_mag = compute_flow_magnitude(fw_flow_w)
bw_flow_mag = compute_flow_magnitude(bw_flow)
mask1 = fb_flow_mag > 0.01 * (fw_flow_w_mag + bw_flow_mag) + 0.5
## motion boundary
fx_du, fx_dv, fy_du, fy_dv = compute_flow_gradients(bw_flow)
fx_mag = fx_du ** 2 + fx_dv ** 2
fy_mag = fy_du ** 2 + fy_dv ** 2
mask2 = (fx_mag + fy_mag) > 0.01 * bw_flow_mag + 0.002
## combine mask
mask = np.logical_or(mask1, mask2)
occlusion = np.zeros((fw_flow.shape[0], fw_flow.shape[1]))
occlusion[mask == 1] = 1
return occlusion
######################################################################################
## Other utility
######################################################################################
def save_vector_to_txt(matrix, filename):
with open(filename, 'w') as f:
print("Save %s" %filename)
for i in range(matrix.size):
line = "%f" %matrix[i]
f.write("%s\n"%line)
def run_cmd(cmd):
print(cmd)
subprocess.call(cmd, shell=True)
def make_video(input_dir, img_fmt, video_filename, fps=24):
cmd = "ffmpeg -y -loglevel error -framerate %s -i %s/%s -vcodec libx264 -pix_fmt yuv420p -vf \"scale=trunc(iw/2)*2:trunc(ih/2)*2\" %s" \
%(fps, input_dir, img_fmt, video_filename)
run_cmd(cmd)
'''
def psnr(img1, img2):
# img1 and img2 have range [0, 255]
img1 = img1.astype(np.float64)
img2 = img2.astype(np.float64)
mse = np.mean((img1 - img2)**2)
if mse == 0:
return float('inf')
return 20 * math.log10(255.0 / math.sqrt(mse))
'''
def psnr(img1, img2):
# img1 and img2 have range [0, 255]
assert img1.dtype == img2.dtype == np.uint8, 'np.uint8 is supposed.'
img1 = img1.astype(np.float64)
img2 = img2.astype(np.float64)
test_Y = True
if test_Y:
print('Testing Y channel.')
else:
print('Testing RGB channels.')
if test_Y and img1.shape[2] == 3: # evaluate on Y channel in YCbCr color space
img1_in = rgb2ycbcr(img1)
img2_in = rgb2ycbcr(img2)
h,w = img1_in.shape[:2]
img2_in = img2_in[0:h,0:w,:]
else:
img1_in = img1
img2_in = img2
mse = np.mean((img1_in - img2_in)**2)
if mse == 0:
return float('inf')
return 20 * math.log10(255.0 / math.sqrt(mse))
def bgr2ycbcr(img, only_y=True):
'''same as matlab rgb2ycbcr
only_y: only return Y channel
Input:
uint8, [0, 255]
float, [0, 1]
'''
in_img_type = img.dtype
img.astype(np.float32)
if in_img_type != np.uint8:
img *= 255.
# convert
if only_y:
rlt = np.dot(img, [24.966, 128.553, 65.481]) / 255.0 + 16.0
else:
rlt = np.matmul(img, [[24.966, 112.0, -18.214], [128.553, -74.203, -93.786],
[65.481, -37.797, 112.0]]) / 255.0 + [16, 128, 128]
if in_img_type == np.uint8:
rlt = rlt.round()
else:
rlt /= 255.
return rlt.astype(in_img_type)
def rgb2ycbcr(img, only_y=True):
"""same as matlab rgb2ycbcr
only_y: only return Y channel
Input:
uint8, [0, 255]
float, [0, 1]
"""
in_img_type = img.dtype
img.astype(np.float32)
if in_img_type != np.uint8:
img *= 255.
# convert
if only_y:
rlt = np.dot(img, [65.481, 128.553, 24.966]) / 255.0 + 16.0
rlt = np.expand_dims(rlt, axis=2)
else:
rlt = np.matmul(img, [[65.481, -37.797, 112.0], [128.553, -74.203, -93.786],
[24.966, 112.0, -18.214]]) / 255.0 + [16, 128, 128]
if in_img_type == np.uint8:
rlt = rlt.round()
else:
rlt /= 255.
return rlt.astype(in_img_type)
def get_patch(img_in, img_tar, patch_size, scale, multi_scale=False):
ih, iw = img_in.shape[:2]
#if ih != 85 or iw != 149:
# print("there is a error !!!")
if int(scale)==2 or int(scale)==4:
ih = int(math.ceil(float(ih) / 4) * 4)
iw = int(math.ceil(float(iw) / 4) * 4)
if int(scale)==3:
ih = int(math.ceil(float(ih) / 3) * 3)
iw = int(math.ceil(float(iw) / 3) * 3)
p = scale if multi_scale else 1
tp = p * patch_size
ip = tp // scale
# if ip != 32 or tp != 96:
ix = random.randrange(0, iw - ip-1)
iy = random.randrange(0, ih - ip-1)
tx, ty = scale * ix, scale * iy
img_in = img_in[iy:iy + ip, ix:ix + ip, :]
img_tar = img_tar[ty:ty + tp, tx:tx + tp, :]
return img_in, img_tar
def _get_patch(self, lr, hr):
patch_size = self.args.patch_size
scale = self.scale[self.idx_scale]
multi_scale = len(self.scale) > 1
if self.train:
lr, hr = get_patch(
lr, hr, patch_size, scale, multi_scale=multi_scale
)
else:
ih, iw = lr.shape[0:2]
hr = hr[0:ih * scale, 0:iw * scale]
return lr, hr
def index_generation(crt_i, max_n, N, padding='reflection'):
"""Generate an index list for reading N frames from a sequence of images
Args:
crt_i (int): current center index
max_n (int): max number of the sequence of images (calculated from 1)
N (int): reading N frames
padding (str): padding mode, one of replicate | reflection | new_info | circle
Example: crt_i = 0, N = 5
reflection: [2, 1, 0, 1, 2]
Returns:
return_l (list [int]): a list of indexes
"""
max_n = max_n - 1
n_pad = N // 2
return_l = []
for i in range(crt_i - n_pad, crt_i + n_pad + 1):
if i < 0:
if padding == 'reflection':
add_idx = -i
else:
raise ValueError('Wrong padding mode')
elif i > max_n:
if padding == 'reflection':
add_idx = max_n * 2 - i
else:
raise ValueError('Wrong padding mode')
else:
add_idx = i
return_l.append(add_idx)
return return_l