File size: 4,210 Bytes
139d7b2
827021c
 
 
 
 
 
 
 
139d7b2
 
827021c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66754a0
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import gradio as gr
import torch.nn as nn
from torch import tanh, Tensor
from abc import ABC, abstractmethod
from huggingface_hub import hf_hub_download
import torch
import json
from omegaconf import OmegaConf
from model import Generator


class BaseGenerator(ABC, nn.Module):
    def __init__(self, channels: int = 3):
        super().__init__()
        self.channels = channels

    @abstractmethod
    def forward(self, x: Tensor) -> Tensor:
        pass


class Generator(BaseGenerator):
    def __init__(self, cfg: DictConfig):
        super().__init__(cfg.channels)
        self.cfg = cfg
        self.model = self._construct_model()

    def _construct_model(self):
        initial_layer = nn.Sequential(
            nn.Conv2d(
                self.cfg.channels,
                self.cfg.num_features,
                kernel_size=7,
                stride=1,
                padding=3,
                padding_mode="reflect",
            ),
            nn.ReLU(inplace=True),
        )

        down_blocks = nn.Sequential(
            ConvBlock(
                self.cfg.num_features,
                self.cfg.num_features * 2,
                kernel_size=3,
                stride=2,
                padding=1,
            ),
            ConvBlock(
                self.cfg.num_features * 2,
                self.cfg.num_features * 4,
                kernel_size=3,
                stride=2,
                padding=1,
            ),
        )

        residual_blocks = nn.Sequential(
            *[
                ResidualBlock(self.cfg.num_features * 4)
                for _ in range(self.cfg.num_residuals)
            ]
        )

        up_blocks = nn.Sequential(
            ConvBlock(
                self.cfg.num_features * 4,
                self.cfg.num_features * 2,
                down=False,
                kernel_size=3,
                stride=2,
                padding=1,
                output_padding=1,
            ),
            ConvBlock(
                self.cfg.num_features * 2,
                self.cfg.num_features,
                down=False,
                kernel_size=3,
                stride=2,
                padding=1,
                output_padding=1,
            ),
        )

        last_layer = nn.Conv2d(
            self.cfg.num_features,
            self.cfg.channels,
            kernel_size=7,
            stride=1,
            padding=3,
            padding_mode="reflect",
        )

        return nn.Sequential(
            initial_layer, down_blocks, residual_blocks, up_blocks, last_layer
        )

    def forward(self, x: Tensor) -> Tensor:
        return tanh(self.model(x))


class ConvBlock(nn.Module):
    def __init__(
        self, in_channels, out_channels, down=True, use_activation=True, **kwargs
    ):
        super().__init__()
        self.conv = nn.Sequential(
            nn.Conv2d(in_channels, out_channels, padding_mode="reflect", **kwargs)
            if down
            else nn.ConvTranspose2d(in_channels, out_channels, **kwargs),
            nn.InstanceNorm2d(out_channels),
            nn.ReLU(inplace=True) if use_activation else nn.Identity(),
        )

    def forward(self, x: Tensor) -> Tensor:
        return self.conv(x)


class ResidualBlock(nn.Module):
    def __init__(self, channels: int):
        super().__init__()
        self.block = nn.Sequential(
            ConvBlock(channels, channels, kernel_size=3, padding=1),
            ConvBlock(
                channels, channels, use_activation=False, kernel_size=3, padding=1
            ),
        )

    def forward(self, x: Tensor) -> Tensor:
        return x + self.block(x)


repo_id = "Kiwinicki/sat2map-generator"
generator_path = hf_hub_download(repo_id=repo_id, filename="generator.pth")
config_path = hf_hub_download(repo_id=repo_id, filename="config.json")
model_path = hf_hub_download(repo_id=repo_id, filename="model.py")


with open(config_path, "r") as f:
    config_dict = json.load(f)
cfg = OmegaConf.create(config_dict)

generator = Generator(cfg)
generator.load_state_dict(torch.load(generator_path))
generator.eval()



def greet(iamge):
    return image

iface = gr.Interface(fn=greet, inputs="image", outputs="image")
iface.launch()