File size: 7,598 Bytes
c24d39e
ba2bd35
d5a4886
 
cedc10c
29caf0d
c24d39e
 
d5a4886
 
 
 
3ebf54f
d5a4886
 
 
 
 
 
 
 
471186c
d5a4886
c24d39e
 
d5a4886
 
277a2e9
89d1722
d5a4886
 
ba2bd35
c30bbb7
d5a4886
ba2bd35
 
d5a4886
 
c0fb24c
ba2bd35
d5a4886
 
 
 
 
 
 
 
72bc1c2
d5a4886
 
 
 
c0fb24c
ba2bd35
d5a4886
 
72bc1c2
 
d5a4886
 
 
 
 
 
277a2e9
 
d5a4886
 
 
 
ba2bd35
d5a4886
 
 
29caf0d
d5a4886
 
 
 
 
 
 
 
 
 
 
 
 
3bbf214
d5a4886
ef5107a
3ebf54f
c24d39e
 
 
 
 
 
 
6bf73bb
 
3ebf54f
97ba26a
29caf0d
6bf73bb
 
3ebf54f
97ba26a
d5a4886
 
c24d39e
 
3bbf214
c24d39e
6c9b00c
c24d39e
3bbf214
d5a4886
6c9b00c
3bbf214
d5a4886
 
c24d39e
d5a4886
 
 
 
 
 
 
 
 
ba2bd35
d5a4886
c0fb24c
 
 
 
 
 
 
 
c24d39e
cedc10c
c24d39e
 
3bbf214
c24d39e
 
3bbf214
c24d39e
cedc10c
c24d39e
 
3bbf214
c24d39e
 
3bbf214
c24d39e
cedc10c
c24d39e
ba2bd35
d5a4886
 
 
c0fb24c
 
 
d5a4886
c0fb24c
 
 
 
 
 
 
 
 
c24d39e
 
 
 
 
 
 
 
 
 
3bbf214
c24d39e
 
3bbf214
c24d39e
 
3bbf214
c24d39e
 
3bbf214
c24d39e
 
3bbf214
c24d39e
 
 
 
 
 
 
8d96cbf
c24d39e
3bbf214
d5a4886
 
 
277a2e9
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
import os
import pathlib
import torch
import torch.hub
from torchvision.transforms.functional import convert_image_dtype, pil_to_tensor
from torchvision.io.image import encode_png
from PIL import Image
import PIL

from mcquic import Config
from mcquic.modules.compressor import BaseCompressor, Compressor
from mcquic.datasets.transforms import AlignedCrop
from mcquic.utils.specification import File
from mcquic.utils.vision import DeTransform

try:
    import streamlit as st
except:
    raise ImportError("To run `mcquic service`, please install Streamlit by `pip install streamlit` firstly.")


MODELS_URL = "https://github.com/xiaosu-zhu/McQuic/releases/download/generic/qp_2_msssim_8e954998.mcquic"

HF_SPACE = "HF_SPACE" in os.environ


@st.experimental_singleton
def loadModel(device):
    ckpt = torch.hub.load_state_dict_from_url(MODELS_URL, map_location=device, check_hash=True)

    config = Config.deserialize(ckpt["config"])
    model = Compressor(**config.Model.Params).to(device)
    model.QuantizationParameter = "qp_2_msssim"
    model.load_state_dict(ckpt["model"])
    return model



@st.cache
def compressImage(image: torch.Tensor, model: BaseCompressor, crop: bool) -> File:
    image = convert_image_dtype(image)

    if crop:
        image = AlignedCrop()(image)

    # [c, h, w]
    image = (image - 0.5) * 2

    codes, binaries, headers = model.compress(image[None, ...])

    return File(headers[0], binaries[0])


@st.cache
def decompressImage(sourceFile: File, model: BaseCompressor) -> torch.ByteTensor:
    binaries = sourceFile.Content

    # [1, c, h, w]
    restored = model.decompress([binaries], [sourceFile.FileHeader])

    # [c, h, w]
    return DeTransform()(restored[0])



def main():
    if not torch.cuda.is_available():
        device = torch.device("cpu")
    else:
        device = torch.device("cuda")

    model = loadModel(device).eval()

    st.sidebar.markdown("""
<p align="center">
  <a href="https://github.com/xiaosu-zhu/McQuic" target="_blank">
    <img src="https://raw.githubusercontent.com/xiaosu-zhu/McQuic/main/assets/McQuic-light.svg" alt="McQuic" title="McQuic" width="45%"/>
  </a>
  <br/>
  <span>
    <i>a.k.a.</i> <b><i>M</i></b>ulti-<b><i>c</i></b>odebook <b><i>Qu</i></b>antizers for neural <b><i>i</i></b>mage <b><i>c</i></b>ompression
  </span>
</p>

<p align="center">
  Compressing images on-the-fly.
</p>


<img src="https://img.shields.io/badge/NOTE-yellow?style=for-the-badge" alt="NOTE"/>

> Due to resources limitation, I only provide compression service with model `qp = 2` targeted `ms-ssim`.

<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>

<p align="center">
<a href="https://github.com/xiaosu-zhu/McQuic" target="_blank">
  <img src="https://raw.githubusercontent.com/xiaosu-zhu/McQuic/main/assets/GitHub_Logo.png" height="16px" alt="Github"/>
  <img src="https://img.shields.io/github/stars/xiaosu-zhu/McQuic?style=social" height="20px" alt="Github"/>
</a>
</p>
""", unsafe_allow_html=True)

    if HF_SPACE:
        st.markdown("""
<img src="https://img.shields.io/badge/NOTE-yellow?style=for-the-badge" alt="NOTE"/>

> Due to resources limitation of HF spaces, upload image size is restricted to smaller than `3000 x 3000`. Also, this demo is CPU-only and may be slow.

<img src="https://img.shields.io/badge/NOTE-yellow?style=for-the-badge" alt="NOTE"/>

> This demo is synced with main branch of `McQuic`. Some features may be unstable and changed frequently.
""", unsafe_allow_html=True)
    with st.form("SubmitForm"):
        uploadedFile = st.file_uploader("Try running McQuic to compress or restore images!", type=["png", "jpg", "jpeg", "mcq"], help="Upload your image or compressed `.mcq` file here.")
        cropping = st.checkbox("Cropping image to align grids.", help="If checked, the image is cropped to align feature map grids. This will make compressed file smaller.")
        submitted = st.form_submit_button("Submit", help="Click to start compress/restore.")
    if submitted and uploadedFile is not None:
        if uploadedFile.name.endswith(".mcq"):
            uploadedFile.flush()

            binaryFile = File.deserialize(uploadedFile.read())

            st.text(str(binaryFile))

            result = decompressImage(binaryFile, model)
            st.image(result.cpu().permute(1, 2, 0).numpy())

            downloadButton = st.empty()

            done = downloadButton.download_button("Click to download restored image", data=bytes(encode_png(result.cpu()).tolist()), file_name=".".join(uploadedFile.name.split(".")[:-1] + ["png"]), mime="image/png")

            if done:
              downloadButton.empty()
        elif uploadedFile.name.lower().endswith((".png", ".jpg", ".jpeg")):
            try:
                image = Image.open(uploadedFile)
            except PIL.UnidentifiedImageError:
                st.markdown("""
<img src="https://img.shields.io/badge/ERROR-red?style=for-the-badge" alt="ERROR"/>

> Image open failed. Please try other images.
""", unsafe_allow_html=True)
                return
            w, h = image.size
            if HF_SPACE and (h > 3000 or w > 3000):
                st.markdown("""
<img src="https://img.shields.io/badge/ERROR-red?style=for-the-badge" alt="ERROR"/>

> Image is too large. Please try other images.
""", unsafe_allow_html=True)
                return
            image = pil_to_tensor(image.convert("RGB")).to(device)
            # st.image(image.cpu().permute(1, 2, 0).numpy())
            result = compressImage(image, model, cropping)

            st.text(str(result))

            downloadButton = st.empty()

            done = st.download_button("Click to download compressed file", data=result.serialize(), file_name=".".join(uploadedFile.name.split(".")[:-1] + ["mcq"]), mime="image/mcq")

            if done:
              downloadButton.empty()
        else:
            st.markdown("""
<img src="https://img.shields.io/badge/ERROR-red?style=for-the-badge" alt="ERROR"/>

> Not supported image formate. Please try other images.
""", unsafe_allow_html=True)
            return
    st.markdown("""
<br/>
<br/>
<br/>
<br/>
<br/>


<p align="center">
  <a href="https://www.python.org/" target="_blank">
    <img src="https://img.shields.io/badge/python-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54" alt="Python"/>
  </a>
  <a href="https://pytorch.org/" target="_blank">
    <img src="https://img.shields.io/badge/PyTorch-%23EE4C2C.svg?style=for-the-badge&logo=PyTorch&logoColor=white" alt="PyTorch"/>
  </a>
  <a href="https://github.com/xiaosu-zhu/McQuic/stargazers" target="_blank">
    <img src="https://img.shields.io/github/stars/xiaosu-zhu/McQuic?logo=github&style=for-the-badge" alt="Github stars"/>
  </a>
  <a href="https://github.com/xiaosu-zhu/McQuic/network/members" target="_blank">
    <img src="https://img.shields.io/github/forks/xiaosu-zhu/McQuic?logo=github&style=for-the-badge" alt="Github forks"/>
  </a>
  <a href="https://github.com/xiaosu-zhu/McQuic/blob/main/LICENSE" target="_blank">
    <img src="https://img.shields.io/github/license/xiaosu-zhu/McQuic?logo=github&style=for-the-badge" alt="Github license"/>
  </a>
</p>

<br/>
<br/>
<br/>

<p align="center"><a href="localhost" target="_blank">CVF Open Access</a> | <a href="https://arxiv.org/abs/2203.10897" target="_blank">arXiv</a> | <a href="https://github.com/xiaosu-zhu/McQuic#citation" target="_blank">BibTex</a> | <a href="https://huggingface.co/spaces/xiaosu-zhu/McQuic" target="_blank">Demo</a></p>

""", unsafe_allow_html=True)

if __name__ == "__main__":
    with torch.inference_mode():
        main()