| 112 | |
| 113 | |
| 114 | class XLSR_quantization(nn.Module): |
| 115 | def __init__(self, SR_rate): |
| 116 | super(XLSR_quantization, self).__init__() |
| 117 | self.quant = QuantStub() |
| 118 | self.dequant = DeQuantStub() |
| 119 | |
| 120 | self.conv0_0 = ConvRelu(in_channels=3, out_channels=8, kernel_size=3) |
| 121 | self.conv0_1 = ConvRelu(in_channels=3, out_channels=8, kernel_size=3) |
| 122 | self.conv0_2 = ConvRelu(in_channels=3, out_channels=8, kernel_size=3) |
| 123 | self.conv0_3 = ConvRelu(in_channels=3, out_channels=8, kernel_size=3) |
| 124 | |
| 125 | self.conv1 = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, padding=1) |
| 126 | self.conv2 = nn.Conv2d(in_channels=32, out_channels=32, kernel_size=1, padding=0) |
| 127 | self.conv3 = ConvRelu(in_channels=48, out_channels=32, kernel_size=1) |
| 128 | self.conv4 = nn.Conv2d(in_channels=32, out_channels=3 * SR_rate ** 2, kernel_size=3, padding=1) |
| 129 | |
| 130 | self.Gblocks = nn.Sequential(Gblock(32, 32, 4), Gblock(32, 32, 4), Gblock(32, 32, 4)) |
| 131 | self.depth2spcae = nn.PixelShuffle(SR_rate) |
| 132 | self.clippedReLU = ClippedReLU() |
| 133 | self.cat1 = nn.quantized.FloatFunctional() |
| 134 | self.cat2 = nn.quantized.FloatFunctional() |
| 135 | |
| 136 | # init weights |
| 137 | for m in self.modules(): |
| 138 | if isinstance(m, nn.Conv2d): |
| 139 | nn.init.kaiming_normal_(m.weight.data, mode='fan_out', nonlinearity='relu') |
| 140 | if m.bias is not None: |
| 141 | nn.init.constant_(m.bias.data, 0.01) |
| 142 | |
| 143 | def forward(self, x): |
| 144 | |
| 145 | x = self.quant(x) |
| 146 | res_conv0_0 = self.conv0_0(x) |
| 147 | res_conv0_1 = self.conv0_1(x) |
| 148 | res_conv0_2 = self.conv0_2(x) |
| 149 | res_conv0_3 = self.conv0_3(x) |
| 150 | |
| 151 | res = self.cat1.cat((res_conv0_0, res_conv0_1, res_conv0_2, res_conv0_3), dim=1) |
| 152 | |
| 153 | res = self.conv2(res) |
| 154 | res = self.Gblocks(res) |
| 155 | |
| 156 | res_conv1 = self.conv1(x) |
| 157 | res = self.cat2.cat((res, res_conv1), dim=1) |
| 158 | |
| 159 | res = self.conv3(res) |
| 160 | res = self.conv4(res) |
| 161 | |
| 162 | res = self.depth2spcae(res) |
| 163 | res = self.clippedReLU(res) |
| 164 | |
| 165 | res = self.dequant(res) |
| 166 | return res |
| 167 | |
| 168 | def fuse_model(self): |
| 169 | for m in self.modules(): |
| 170 | if type(m) == ConvRelu: |
| 171 | # print("fuse conv and relu in ConvRelu") |