File size: 10,974 Bytes
2034096
 
7132b8a
 
 
2034096
7132b8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2034096
 
7132b8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2034096
7132b8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2034096
7132b8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2034096
7132b8a
 
2034096
7132b8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2034096
7132b8a
 
 
 
 
 
 
 
2034096
7132b8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2034096
7132b8a
2034096
7132b8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b31bba8
7132b8a
 
 
 
2034096
7132b8a
 
 
 
 
2034096
 
7132b8a
2034096
 
7132b8a
 
2034096
7132b8a
2034096
7132b8a
 
 
 
5527144
 
 
 
 
 
 
 
 
 
7132b8a
 
 
5527144
 
 
 
 
 
 
 
 
 
 
 
7132b8a
2034096
 
 
 
5527144
 
 
 
 
 
7132b8a
 
2034096
5527144
7132b8a
2034096
 
 
 
 
 
5527144
 
 
 
 
 
 
 
 
 
 
 
 
 
2034096
5527144
7132b8a
 
eead4ea
f1b6ca9
 
 
7132b8a
 
 
 
 
 
 
 
 
5527144
 
7132b8a
 
 
 
5527144
2034096
f1b6ca9
2034096
7132b8a
 
 
2034096
 
 
7132b8a
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import base64
from huggingface_hub import hf_hub_download
import streamlit as st
import io
import gc
import json

########################################################################################################
# The RWKV Language Model - https://github.com/BlinkDL/RWKV-LM
########################################################################################################

MODEL_REPO = 'BlinkDL/clip-guided-binary-autoencoder'

import torch, types
import numpy as np
from PIL import Image
import torch.nn as nn
from torch.nn import functional as F
import torchvision as vision
import torchvision.transforms as transforms
from torchvision.transforms import functional as VF

device = 'cuda' if torch.cuda.is_available() else 'cpu'

IMG_BITS = 13


class ResBlock(nn.Module):

    def __init__(self, c_x, c_hidden):
        super().__init__()
        self.B0 = nn.BatchNorm2d(c_x)
        self.C0 = nn.Conv2d(c_x, c_hidden, kernel_size=3, padding=1)
        self.C1 = nn.Conv2d(c_hidden, c_x, kernel_size=3, padding=1)
        self.C2 = nn.Conv2d(c_x, c_hidden, kernel_size=3, padding=1)
        self.C3 = nn.Conv2d(c_hidden, c_x, kernel_size=3, padding=1)

    def forward(self, x):
        ACT = F.mish
        x = x + self.C1(ACT(self.C0(ACT(self.B0(x)))))
        x = x + self.C3(ACT(self.C2(x)))
        return x


class REncoderSmall(nn.Module):

    def __init__(self):
        super().__init__()
        dd = 8
        self.Bxx = nn.BatchNorm2d(dd * 64)

        self.CIN = nn.Conv2d(3, dd, kernel_size=3, padding=1)
        self.Cx0 = nn.Conv2d(dd, 32, kernel_size=3, padding=1)
        self.Cx1 = nn.Conv2d(32, dd, kernel_size=3, padding=1)

        self.B00 = nn.BatchNorm2d(dd * 4)
        self.C00 = nn.Conv2d(dd * 4, 256, kernel_size=3, padding=1)
        self.C01 = nn.Conv2d(256, dd * 4, kernel_size=3, padding=1)
        self.C02 = nn.Conv2d(dd * 4, 256, kernel_size=3, padding=1)
        self.C03 = nn.Conv2d(256, dd * 4, kernel_size=3, padding=1)

        self.B10 = nn.BatchNorm2d(dd * 16)
        self.C10 = nn.Conv2d(dd * 16, 256, kernel_size=3, padding=1)
        self.C11 = nn.Conv2d(256, dd * 16, kernel_size=3, padding=1)
        self.C12 = nn.Conv2d(dd * 16, 256, kernel_size=3, padding=1)
        self.C13 = nn.Conv2d(256, dd * 16, kernel_size=3, padding=1)

        self.B20 = nn.BatchNorm2d(dd * 64)
        self.C20 = nn.Conv2d(dd * 64, 256, kernel_size=3, padding=1)
        self.C21 = nn.Conv2d(256, dd * 64, kernel_size=3, padding=1)
        self.C22 = nn.Conv2d(dd * 64, 256, kernel_size=3, padding=1)
        self.C23 = nn.Conv2d(256, dd * 64, kernel_size=3, padding=1)

        self.COUT = nn.Conv2d(dd * 64, IMG_BITS, kernel_size=3, padding=1)

    def forward(self, img):
        ACT = F.mish

        x = self.CIN(img)
        xx = self.Bxx(F.pixel_unshuffle(x, 8))
        x = x + self.Cx1(ACT(self.Cx0(x)))

        x = F.pixel_unshuffle(x, 2)
        x = x + self.C01(ACT(self.C00(ACT(self.B00(x)))))
        x = x + self.C03(ACT(self.C02(x)))

        x = F.pixel_unshuffle(x, 2)
        x = x + self.C11(ACT(self.C10(ACT(self.B10(x)))))
        x = x + self.C13(ACT(self.C12(x)))

        x = F.pixel_unshuffle(x, 2)
        x = x + self.C21(ACT(self.C20(ACT(self.B20(x)))))
        x = x + self.C23(ACT(self.C22(x)))

        x = self.COUT(x + xx)
        return torch.sigmoid(x)


class RDecoderSmall(nn.Module):

    def __init__(self):
        super().__init__()
        dd = 8
        self.CIN = nn.Conv2d(IMG_BITS, dd * 64, kernel_size=3, padding=1)

        self.B00 = nn.BatchNorm2d(dd * 64)
        self.C00 = nn.Conv2d(dd * 64, 256, kernel_size=3, padding=1)
        self.C01 = nn.Conv2d(256, dd * 64, kernel_size=3, padding=1)
        self.C02 = nn.Conv2d(dd * 64, 256, kernel_size=3, padding=1)
        self.C03 = nn.Conv2d(256, dd * 64, kernel_size=3, padding=1)

        self.B10 = nn.BatchNorm2d(dd * 16)
        self.C10 = nn.Conv2d(dd * 16, 256, kernel_size=3, padding=1)
        self.C11 = nn.Conv2d(256, dd * 16, kernel_size=3, padding=1)
        self.C12 = nn.Conv2d(dd * 16, 256, kernel_size=3, padding=1)
        self.C13 = nn.Conv2d(256, dd * 16, kernel_size=3, padding=1)

        self.B20 = nn.BatchNorm2d(dd * 4)
        self.C20 = nn.Conv2d(dd * 4, 256, kernel_size=3, padding=1)
        self.C21 = nn.Conv2d(256, dd * 4, kernel_size=3, padding=1)
        self.C22 = nn.Conv2d(dd * 4, 256, kernel_size=3, padding=1)
        self.C23 = nn.Conv2d(256, dd * 4, kernel_size=3, padding=1)

        self.Cx0 = nn.Conv2d(dd, 32, kernel_size=3, padding=1)
        self.Cx1 = nn.Conv2d(32, dd, kernel_size=3, padding=1)
        self.COUT = nn.Conv2d(dd, 3, kernel_size=3, padding=1)

    def forward(self, code):
        ACT = F.mish
        x = self.CIN(code)

        x = x + self.C01(ACT(self.C00(ACT(self.B00(x)))))
        x = x + self.C03(ACT(self.C02(x)))
        x = F.pixel_shuffle(x, 2)

        x = x + self.C11(ACT(self.C10(ACT(self.B10(x)))))
        x = x + self.C13(ACT(self.C12(x)))
        x = F.pixel_shuffle(x, 2)

        x = x + self.C21(ACT(self.C20(ACT(self.B20(x)))))
        x = x + self.C23(ACT(self.C22(x)))
        x = F.pixel_shuffle(x, 2)

        x = x + self.Cx1(ACT(self.Cx0(x)))
        x = self.COUT(x)

        return torch.sigmoid(x)


class REncoderLarge(nn.Module):

    def __init__(self, dd, ee, ff):
        super().__init__()
        self.CXX = nn.Conv2d(3, dd, kernel_size=3, padding=1)
        self.BXX = nn.BatchNorm2d(dd)
        self.CX0 = nn.Conv2d(dd, ee, kernel_size=3, padding=1)
        self.CX1 = nn.Conv2d(ee, dd, kernel_size=3, padding=1)
        self.R0 = ResBlock(dd * 4, ff)
        self.R1 = ResBlock(dd * 16, ff)
        self.R2 = ResBlock(dd * 64, ff)
        self.CZZ = nn.Conv2d(dd * 64, IMG_BITS, kernel_size=3, padding=1)

    def forward(self, x):
        ACT = F.mish
        x = self.BXX(self.CXX(x))

        x = x + self.CX1(ACT(self.CX0(x)))
        x = F.pixel_unshuffle(x, 2)
        x = self.R0(x)
        x = F.pixel_unshuffle(x, 2)
        x = self.R1(x)
        x = F.pixel_unshuffle(x, 2)
        x = self.R2(x)

        x = self.CZZ(x)
        return torch.sigmoid(x)


class RDecoderLarge(nn.Module):

    def __init__(self, dd, ee, ff):
        super().__init__()
        self.CZZ = nn.Conv2d(IMG_BITS, dd * 64, kernel_size=3, padding=1)
        self.BZZ = nn.BatchNorm2d(dd * 64)
        self.R0 = ResBlock(dd * 64, ff)
        self.R1 = ResBlock(dd * 16, ff)
        self.R2 = ResBlock(dd * 4, ff)
        self.CX0 = nn.Conv2d(dd, ee, kernel_size=3, padding=1)
        self.CX1 = nn.Conv2d(ee, dd, kernel_size=3, padding=1)
        self.CXX = nn.Conv2d(dd, 3, kernel_size=3, padding=1)

    def forward(self, x):
        ACT = F.mish
        x = self.BZZ(self.CZZ(x))

        x = self.R0(x)
        x = F.pixel_shuffle(x, 2)
        x = self.R1(x)
        x = F.pixel_shuffle(x, 2)
        x = self.R2(x)
        x = F.pixel_shuffle(x, 2)
        x = x + self.CX1(ACT(self.CX0(x)))

        x = self.CXX(x)
        return torch.sigmoid(x)


@st.cache
def prepare_model(model_prefix):
    gc.collect()

    if model_prefix == 'out-v7c_d8_256-224-13bit-OB32x0.5-745':
        R_ENCODER, R_DECODER = REncoderSmall(), RDecoderSmall()
    else:
        if 'd16_512' in model_prefix:
            dd, ee, ff = 16, 64, 512
        elif 'd32_1024' in model_prefix:
            dd, ee, ff = 32, 128, 1024
        R_ENCODER = REncoderLarge(dd, ee, ff)
        R_DECODER = RDecoderLarge(dd, ee, ff)

    encoder = R_ENCODER.eval().to(device)
    decoder = R_DECODER.eval().to(device)

    encoder.load_state_dict(
        torch.load(hf_hub_download(MODEL_REPO, f'{model_prefix}-E.pth')))
    decoder.load_state_dict(
        torch.load(hf_hub_download(MODEL_REPO, f'{model_prefix}-D.pth')))

    return encoder, decoder


def compute_padding(img_shape):
    hsize, vsize = (img_shape[1] + 7) // 8 * 8, (img_shape[0] + 7) // 8 * 8
    hpad, vpad = hsize - img_shape[1], vsize - img_shape[0]
    left, top = hpad // 2, vpad // 2
    right, bottom = hpad - left, vpad - top
    return left, top, right, bottom


def encode(model_prefix, img, keep_shape):
    gc.collect()
    encoder, _ = prepare_model(model_prefix)

    with torch.no_grad():
        img = VF.pil_to_tensor(img.convert("RGB"))
        img = VF.convert_image_dtype(img)
        img = img.unsqueeze(0).to(device)
        img_shape = img.shape[2:]

        if keep_shape:
            left, top, right, bottom = compute_padding(img_shape)
            img = VF.pad(img, [left, top, right, bottom], padding_mode='edge')
        else:
            img = VF.resize(img, [224, 224])

        z = torch.floor(encoder(img) + 0.5)

    with io.BytesIO() as buffer:
        np.save(buffer, np.packbits(z.cpu().numpy().astype('bool')))
        z_b64 = base64.b64encode(buffer.getvalue()).decode()

    return json.dumps({
        "img_shape": img_shape,
        "z_shape": z.shape[2:],
        "keep_shape": keep_shape,
        "data": z_b64,
    })


def decode(model_prefix, z_str):
    gc.collect()
    _, decoder = prepare_model(model_prefix)

    z_json = json.loads(z_str)
    with io.BytesIO() as buffer:
        buffer.write(base64.b64decode(z_json["data"]))
        buffer.seek(0)
        z = np.load(buffer)
    img_shape = z_json["img_shape"]
    z_shape = z_json["z_shape"]
    keep_shape = z_json["keep_shape"]

    z = np.unpackbits(z)[:IMG_BITS * z_shape[0] * z_shape[1]].astype('float')
    z = z.reshape([1, IMG_BITS] + z_shape)

    img = decoder(torch.Tensor(z).to(device))

    if keep_shape:
        left, top, right, bottom = compute_padding(img_shape)
        img = img[0, :, top:img.shape[2] - bottom, left:img.shape[3] - right]
    else:
        img = img[0]

    return VF.to_pil_image(img)


st.title("Clip Guided Binary Autoencoder")
st.write(
    "Model is from [@BlinkDL](https://huggingface.co/BlinkDL/clip-guided-binary-autoencoder)"
)
model_prefix = st.selectbox('The model to use',
                            ('out-v7c_d8_256-224-13bit-OB32x0.5-745',
                             'out-v7d_d16_512-224-13bit-OB32x0.5-2487',
                             'out-v7d_d32_1024-224-13bit-OB32x0.5-5560'))

encoder_tab, decoder_tab = st.tabs(["Encode", "Decode"])

with encoder_tab:
    col_in, col_out = st.columns(2)
    keep_shape = col_in.checkbox(
        'Use original size of input image instead of rescaling (Experimental)')
    uploaded_file = col_in.file_uploader('Choose an Image')
    if uploaded_file is not None:
        image = Image.open(uploaded_file)
        col_in.image(image, 'Input Image')
        z_str = encode(model_prefix, image, keep_shape)
        col_out.write("Encoded to:")
        col_out.code(z_str, language=None)
        col_out.image(decode(model_prefix, z_str), 'Output Image preview')

with decoder_tab:
    col_in, col_out = st.columns(2)
    z_str = col_in.text_area('Paste encoded string here:')
    if len(z_str) > 0:
        image = decode(model_prefix, z_str)
        col_out.image(image, 'Output Image')