RamAnanth1 commited on
Commit
05833ed
β€’
1 Parent(s): d796b5a

Create adapter.py

Browse files
Files changed (1) hide show
  1. lvdm/models/modules/adapter.py +105 -0
lvdm/models/modules/adapter.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ from collections import OrderedDict
4
+ from lvdm.models.modules.util import (
5
+ zero_module,
6
+ conv_nd,
7
+ avg_pool_nd
8
+ )
9
+
10
+ class Downsample(nn.Module):
11
+ """
12
+ A downsampling layer with an optional convolution.
13
+ :param channels: channels in the inputs and outputs.
14
+ :param use_conv: a bool determining if a convolution is applied.
15
+ :param dims: determines if the signal is 1D, 2D, or 3D. If 3D, then
16
+ downsampling occurs in the inner-two dimensions.
17
+ """
18
+
19
+ def __init__(self, channels, use_conv, dims=2, out_channels=None, padding=1):
20
+ super().__init__()
21
+ self.channels = channels
22
+ self.out_channels = out_channels or channels
23
+ self.use_conv = use_conv
24
+ self.dims = dims
25
+ stride = 2 if dims != 3 else (1, 2, 2)
26
+ if use_conv:
27
+ self.op = conv_nd(
28
+ dims, self.channels, self.out_channels, 3, stride=stride, padding=padding
29
+ )
30
+ else:
31
+ assert self.channels == self.out_channels
32
+ self.op = avg_pool_nd(dims, kernel_size=stride, stride=stride)
33
+
34
+ def forward(self, x):
35
+ assert x.shape[1] == self.channels
36
+ return self.op(x)
37
+
38
+
39
+ class ResnetBlock(nn.Module):
40
+ def __init__(self, in_c, out_c, down, ksize=3, sk=False, use_conv=True):
41
+ super().__init__()
42
+ ps = ksize // 2
43
+ if in_c != out_c or sk == False:
44
+ self.in_conv = nn.Conv2d(in_c, out_c, ksize, 1, ps)
45
+ else:
46
+ # print('n_in')
47
+ self.in_conv = None
48
+ self.block1 = nn.Conv2d(out_c, out_c, 3, 1, 1)
49
+ self.act = nn.ReLU()
50
+ self.block2 = nn.Conv2d(out_c, out_c, ksize, 1, ps)
51
+ if sk == False:
52
+ self.skep = nn.Conv2d(in_c, out_c, ksize, 1, ps)
53
+ else:
54
+ self.skep = None
55
+
56
+ self.down = down
57
+ if self.down == True:
58
+ self.down_opt = Downsample(in_c, use_conv=use_conv)
59
+
60
+ def forward(self, x):
61
+ if self.down == True:
62
+ x = self.down_opt(x)
63
+ if self.in_conv is not None: # edit
64
+ x = self.in_conv(x)
65
+
66
+ h = self.block1(x)
67
+ h = self.act(h)
68
+ h = self.block2(h)
69
+ if self.skep is not None:
70
+ return h + self.skep(x)
71
+ else:
72
+ return h + x
73
+
74
+
75
+ class Adapter(nn.Module):
76
+ def __init__(self, channels=[320, 640, 1280, 1280], nums_rb=3, cin=64, ksize=3, sk=False, use_conv=True):
77
+ super(Adapter, self).__init__()
78
+ self.unshuffle = nn.PixelUnshuffle(8)
79
+ self.channels = channels
80
+ self.nums_rb = nums_rb
81
+ self.body = []
82
+ for i in range(len(channels)):
83
+ for j in range(nums_rb):
84
+ if (i != 0) and (j == 0):
85
+ self.body.append(
86
+ ResnetBlock(channels[i - 1], channels[i], down=True, ksize=ksize, sk=sk, use_conv=use_conv))
87
+ else:
88
+ self.body.append(
89
+ ResnetBlock(channels[i], channels[i], down=False, ksize=ksize, sk=sk, use_conv=use_conv))
90
+ self.body = nn.ModuleList(self.body)
91
+ self.conv_in = nn.Conv2d(cin, channels[0], 3, 1, 1)
92
+
93
+ def forward(self, x):
94
+ # unshuffle
95
+ x = self.unshuffle(x)
96
+ # extract features
97
+ features = []
98
+ x = self.conv_in(x)
99
+ for i in range(len(self.channels)):
100
+ for j in range(self.nums_rb):
101
+ idx = i * self.nums_rb + j
102
+ x = self.body[idx](x)
103
+ features.append(x)
104
+
105
+ return features