Spaces:
Sleeping
Sleeping
Create arch_utils.py
Browse files- RealESRGAN/arch_utils.py +38 -0
RealESRGAN/arch_utils.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from torch import nn as nn
|
| 3 |
+
from torch.nn import init as init
|
| 4 |
+
from torch.nn.modules.batchnorm import _BatchNorm
|
| 5 |
+
|
| 6 |
+
@torch.no_grad()
|
| 7 |
+
def default_init_weights(module_list, scale=1, bias_fill=0, **kwargs):
|
| 8 |
+
if not isinstance(module_list, list):
|
| 9 |
+
module_list = [module_list]
|
| 10 |
+
for module in module_list:
|
| 11 |
+
for m in module.modules():
|
| 12 |
+
if isinstance(m, nn.Conv2d):
|
| 13 |
+
init.kaiming_normal_(m.weight, **kwargs)
|
| 14 |
+
m.weight.data *= scale
|
| 15 |
+
if m.bias is not None:
|
| 16 |
+
m.bias.data.fill_(bias_fill)
|
| 17 |
+
elif isinstance(m, nn.Linear):
|
| 18 |
+
init.kaiming_normal_(m.weight, **kwargs)
|
| 19 |
+
m.weight.data *= scale
|
| 20 |
+
if m.bias is not None:
|
| 21 |
+
m.bias.data.fill_(bias_fill)
|
| 22 |
+
elif isinstance(m, _BatchNorm):
|
| 23 |
+
init.constant_(m.weight, 1)
|
| 24 |
+
if m.bias is not None:
|
| 25 |
+
m.bias.data.fill_(bias_fill)
|
| 26 |
+
|
| 27 |
+
def make_layer(basic_block, num_basic_block, **kwarg):
|
| 28 |
+
layers = []
|
| 29 |
+
for _ in range(num_basic_block):
|
| 30 |
+
layers.append(basic_block(**kwarg))
|
| 31 |
+
return nn.Sequential(*layers)
|
| 32 |
+
|
| 33 |
+
def pixel_unshuffle(x, scale):
|
| 34 |
+
b, c, hh, hw = x.size()
|
| 35 |
+
out_channel = c * (scale**2)
|
| 36 |
+
h, w = hh // scale, hw // scale
|
| 37 |
+
x_view = x.view(b, c, h, scale, w, scale)
|
| 38 |
+
return x_view.permute(0, 1, 3, 5, 2, 4).reshape(b, out_channel, h, w)
|