def __init__(self, opt, conv=common.default_conv): super(DRN, self).__init__() self.opt = opt self.scale = opt.scale self.phase = len(opt.scale) n_blocks = opt.n_blocks n_feats = opt.n_feats kernel_size = 3 act = nn.ReLU(True) self.upsample = nn.Upsample(scale_factor=max(opt.scale), mode='bicubic', align_corners=False) rgb_mean = (0.4488, 0.4371, 0.4040) rgb_std = (1.0, 1.0, 1.0) self.sub_mean = common.MeanShift(opt.rgb_range, rgb_mean, rgb_std) self.head = conv(opt.n_colors, n_feats, kernel_size) self.head_two = conv(n_feats, n_feats * 4, kernel_size) self.down = [ common.DownBlock(opt, 2, n_feats * pow(2, p), n_feats * pow(2, p), n_feats * pow(2, p + 1)) for p in range(self.phase) ] self.down = nn.ModuleList(self.down) up_body_blocks = [[ common.RCAB(conv, n_feats * pow(2, p), kernel_size, act=act) for _ in range(n_blocks) ] for p in range(self.phase, 1, -1)] up_body_blocks.insert(0, [ common.RCAB( conv, n_feats * pow(2, self.phase), kernel_size, act=act) for _ in range(n_blocks) ]) # The fisrt upsample block up = [[ common.Upsampler(conv, 2, n_feats * pow(2, self.phase), act=False), conv(n_feats * pow(2, self.phase), n_feats * pow(2, self.phase - 1), kernel_size=1) ]] # The rest upsample blocks for p in range(self.phase - 1, 0, -1): up.append([ # common.Upsampler(conv, 2, 2 * n_feats * pow(2, p), act=False), # conv(2 * n_feats * pow(2, p), n_feats * pow(2, p - 1), kernel_size=1) common.Upsampler(conv, 2, n_feats * pow(2, self.phase), act=False), conv(n_feats * pow(2, self.phase), n_feats * pow(2, self.phase - 1), kernel_size=1) ]) self.up_blocks = nn.ModuleList() for idx in range(self.phase): self.up_blocks.append(nn.Sequential(*up_body_blocks[idx], *up[idx])) # tail conv that output sr imgs tail = [conv(n_feats * pow(2, self.phase), opt.n_colors, kernel_size)] for p in range(self.phase, 0, -1): tail.append(conv(n_feats * pow(2, p), opt.n_colors, kernel_size)) self.tail = nn.ModuleList(tail) self.add_mean = common.MeanShift(opt.rgb_range, rgb_mean, rgb_std, 1) self.RCAB_block_1 = common.RCAB(conv, n_feats * pow(2, 2), kernel_size, act=act) self.up_block_1 = [[ common.Upsampler(conv, 2, n_feats * pow(2, 2), act=False), conv(n_feats * pow(2, self.phase), n_feats * pow(2, self.phase - 1), kernel_size=1) ]] self.RCAB_block_2 = common.RCAB(conv, n_feats * pow(2, 1), kernel_size, act=act) self.up_block_2 = [[ common.Upsampler(conv, 2, n_feats * pow(2, 1), act=False), conv(n_feats * pow(2, 1), n_feats * pow(2, 0), kernel_size=1) ]] self.head_3 = conv(n_feats * pow(2, 0), 8, kernel_size) self.head_4 = conv(8, opt.n_colors, kernel_size)
def __init__(self, opt, conv=common.default_conv): super(DRN, self).__init__() self.opt = opt self.scale = opt.scale self.phase = len(opt.scale) n_blocks = opt.n_blocks n_feats = opt.n_feats kernel_size = 3 act = nn.ReLU(True) # 加True的意思就是将计算的值覆盖之前的值,不用重新赋给新的变量 self.upsample = nn.Upsample(scale_factor=max(opt.scale), mode='bicubic', align_corners=False) # 当align_corners = True时,像素被视为网格的格子上的点, 拐角处的像素对齐.可知是点之间是等间距的 # 当align_corners = False时, 像素被视为网格的交叉线上的点, 拐角处的点依然是原图像的拐角像素, 但是差值的点间却按照上图的取法取, 导致点与点之间是不等距的 rgb_mean = (0.4488, 0.4371, 0.4040) rgb_std = (1.0, 1.0, 1.0) self.sub_mean = common.MeanShift(opt.rgb_range, rgb_mean, rgb_std) self.head = conv(opt.n_colors, n_feats, kernel_size) self.down = [ common.DownBlock(opt, 2, n_feats * pow(2, p), n_feats * pow(2, p), n_feats * pow(2, p + 1)) for p in range(self.phase) ] self.down = nn.ModuleList(self.down) up_body_blocks = [[ common.RCAB(conv, n_feats * pow(2, p), kernel_size, act=act) for _ in range(n_blocks) ] for p in range(self.phase, 1, -1)] up_body_blocks.insert(0, [ common.RCAB( conv, n_feats * pow(2, self.phase), kernel_size, act=act) for _ in range(n_blocks) ]) # The fisrt upsample block up = [[ common.Upsampler(conv, 2, n_feats * pow(2, self.phase), act=False), conv(n_feats * pow(2, self.phase), n_feats * pow(2, self.phase - 1), kernel_size=1) ]] # The rest upsample blocks for p in range(self.phase - 1, 0, -1): up.append([ common.Upsampler(conv, 2, 2 * n_feats * pow(2, p), act=False), conv(2 * n_feats * pow(2, p), n_feats * pow(2, p - 1), kernel_size=1) ]) self.up_blocks = nn.ModuleList() for idx in range(self.phase): self.up_blocks.append(nn.Sequential(*up_body_blocks[idx], *up[idx])) # tail conv that output sr imgs tail = [conv(n_feats * pow(2, self.phase), opt.n_colors, kernel_size)] for p in range(self.phase, 0, -1): tail.append(conv(n_feats * pow(2, p), opt.n_colors, kernel_size)) self.tail = nn.ModuleList(tail) self.add_mean = common.MeanShift(opt.rgb_range, rgb_mean, rgb_std, 1)
def __init__(self, opt, conv=common.default_conv): super(DRN, self).__init__() self.opt = opt self.scale = opt.scale self.phase = len(opt.scale) n_blocks = opt.n_blocks n_feats = opt.n_feats kernel_size = 3 sf = 0 if (self.scale[0] % 2) == 0: sf = 2 elif self.scale[0] == 3: sf = 3 print('DRN scale >> ' + str(sf)) act = nn.ReLU(True) self.upsample = nn.Upsample(scale_factor=max(opt.scale), mode='bicubic', align_corners=False) rgb_mean = (0.4488, 0.4371, 0.4040) rgb_std = (1.0, 1.0, 1.0) self.sub_mean = common.MeanShift(opt.rgb_range, rgb_mean, rgb_std) self.head = conv(opt.n_colors, n_feats, kernel_size) self.down = [ common.DownBlock(opt, sf, n_feats * pow(2, p), n_feats * pow(2, p), n_feats * pow(2, p + 1)) for p in range(self.phase) ] self.down = nn.ModuleList(self.down) up_body_blocks = [[ common.RCAB(conv, n_feats * pow(2, p), kernel_size, act=act) for _ in range(n_blocks) ] for p in range(self.phase, 1, -1)] up_body_blocks.insert(0, [ common.RCAB( conv, n_feats * pow(2, self.phase), kernel_size, act=act) for _ in range(n_blocks) ]) # The first upsample block up = [[ common.Upsampler(conv, sf, n_feats * pow(2, self.phase), act=False), conv(n_feats * pow(2, self.phase), n_feats * pow(2, self.phase - 1), kernel_size=1) ]] # The rest upsample blocks for p in range(self.phase - 1, 0, -1): up.append([ common.Upsampler(conv, sf, 2 * n_feats * pow(2, p), act=False), conv(2 * n_feats * pow(2, p), n_feats * pow(2, p - 1), kernel_size=1) ]) self.up_blocks = nn.ModuleList() for idx in range(self.phase): self.up_blocks.append(nn.Sequential(*up_body_blocks[idx], *up[idx])) # tail conv that output sr imgs tail = [conv(n_feats * pow(2, self.phase), opt.n_colors, kernel_size)] for p in range(self.phase, 0, -1): tail.append(conv(n_feats * pow(2, p), opt.n_colors, kernel_size)) self.tail = nn.ModuleList(tail) self.P2W = Pos2Weight(inC=opt.n_colors, kernel_size=kernel_size, outC=opt.n_colors) self.add_mean = common.MeanShift(opt.rgb_range, rgb_mean, rgb_std, 1)