-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodels_compressai.py
57 lines (53 loc) · 1.79 KB
/
models_compressai.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
from compressai.models import Cheng2020Attention
from compressai.models.utils import conv, deconv, update_registered_buffers
from compressai.entropy_models import EntropyBottleneck, GaussianConditional
from compressai.layers import GDN, MaskedConv2d
from compressai.layers import (
AttentionBlock,
ResidualBlock,
ResidualBlockUpsample,
ResidualBlockWithStride,
conv3x3,
subpel_conv3x3,
)
import warnings
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import sys
import torch
# import layers
from pytorch_msssim import ssim
# from layers_compander import PermEquivEncoder, PermEquivDecoder
def get_models(args):
if args.model_name == 'Cheng2020AttentionFull':
return Cheng2020AttentionCustom(192, args.orig_channels)
else:
print("Invalid model_name")
sys.exit(0)
class Cheng2020AttentionCustom(Cheng2020Attention):
def __init__(self, N, orig_channels=3, **kwargs):
super().__init__(N=N, **kwargs)
self.g_a = nn.Sequential(
ResidualBlockWithStride(orig_channels, N, stride=2),
ResidualBlock(N, N),
ResidualBlockWithStride(N, N, stride=2),
AttentionBlock(N),
ResidualBlock(N, N),
ResidualBlockWithStride(N, N, stride=2),
ResidualBlock(N, N),
conv3x3(N, N, stride=2),
AttentionBlock(N),
)
self.g_s = nn.Sequential(
AttentionBlock(N),
ResidualBlock(N, N),
ResidualBlockUpsample(N, N, 2),
ResidualBlock(N, N),
ResidualBlockUpsample(N, N, 2),
AttentionBlock(N),
ResidualBlock(N, N),
ResidualBlockUpsample(N, N, 2),
ResidualBlock(N, N),
subpel_conv3x3(N, orig_channels, 2),
)