Spaces:
Runtime error
Runtime error
Commit
·
c24d39e
1
Parent(s):
7d5ab3d
Auto deploy
Browse files- stCompressService.py +78 -8
stCompressService.py
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
|
|
| 1 |
import pathlib
|
| 2 |
import torch
|
| 3 |
import torch.hub
|
| 4 |
from torchvision.transforms.functional import convert_image_dtype
|
| 5 |
from torchvision.io.image import ImageReadMode, encode_png, decode_image
|
|
|
|
|
|
|
| 6 |
|
| 7 |
from mcquic import Config
|
| 8 |
from mcquic.modules.compressor import BaseCompressor, Compressor
|
|
@@ -18,6 +21,8 @@ except:
|
|
| 18 |
|
| 19 |
MODELS_URL = "https://github.com/xiaosu-zhu/McQuic/releases/download/generic/qp_3_msssim_fcc58b73.mcquic"
|
| 20 |
|
|
|
|
|
|
|
| 21 |
|
| 22 |
@st.experimental_singleton
|
| 23 |
def loadModel(qp: int, local: pathlib.Path, device, mse: bool):
|
|
@@ -60,7 +65,7 @@ def decompressImage(sourceFile: File, model: BaseCompressor) -> torch.ByteTensor
|
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
-
def main(debug: bool, quiet: bool, disable_gpu: bool):
|
| 64 |
if disable_gpu or not torch.cuda.is_available():
|
| 65 |
device = torch.device("cpu")
|
| 66 |
else:
|
|
@@ -84,23 +89,38 @@ def main(debug: bool, quiet: bool, disable_gpu: bool):
|
|
| 84 |
</p>
|
| 85 |
|
| 86 |
|
| 87 |
-
<
|
| 88 |
-
<image src="https://img.shields.io/badge/NOTE-yellow?style=for-the-badge" alt="NOTE"/>
|
| 89 |
-
</a>
|
| 90 |
|
| 91 |
> Due to resources limitation, I only provide compression service with model `qp = 3`.
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
-
<a href="
|
|
|
|
| 95 |
<image src="https://img.shields.io/github/stars/xiaosu-zhu/McQuic?style=social" alt="Github"/>
|
| 96 |
</a>
|
| 97 |
|
| 98 |
""", unsafe_allow_html=True)
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
|
|
|
|
|
|
| 101 |
with st.form("SubmitForm"):
|
| 102 |
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.")
|
| 103 |
-
cropping = st.checkbox("Cropping image to align grids.", help="If checked, the image is cropped to align
|
| 104 |
submitted = st.form_submit_button("Submit", help="Click to start compress/restore.")
|
| 105 |
if submitted and uploadedFile is not None:
|
| 106 |
if uploadedFile.name.endswith(".mcq"):
|
|
@@ -114,16 +134,66 @@ def main(debug: bool, quiet: bool, disable_gpu: bool):
|
|
| 114 |
st.image(result.cpu().permute(1, 2, 0).numpy())
|
| 115 |
st.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")
|
| 116 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
raw = torch.ByteTensor(torch.ByteStorage.from_buffer(uploadedFile.read())) # type: ignore
|
| 118 |
image = decode_image(raw, ImageReadMode.RGB).to(device)
|
| 119 |
-
st.image(image.cpu().permute(1, 2, 0).numpy())
|
| 120 |
result = compressImage(image, model, cropping)
|
| 121 |
|
| 122 |
st.text(str(result))
|
| 123 |
|
| 124 |
st.download_button("Click to download compressed file", data=result.serialize(), file_name=".".join(uploadedFile.name.split(".")[:-1] + ["mcq"]), mime="image/mcq")
|
| 125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
if __name__ == "__main__":
|
| 128 |
with torch.inference_mode():
|
| 129 |
-
main(False, False, False)
|
|
|
|
| 1 |
+
import os
|
| 2 |
import pathlib
|
| 3 |
import torch
|
| 4 |
import torch.hub
|
| 5 |
from torchvision.transforms.functional import convert_image_dtype
|
| 6 |
from torchvision.io.image import ImageReadMode, encode_png, decode_image
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import PIL
|
| 9 |
|
| 10 |
from mcquic import Config
|
| 11 |
from mcquic.modules.compressor import BaseCompressor, Compressor
|
|
|
|
| 21 |
|
| 22 |
MODELS_URL = "https://github.com/xiaosu-zhu/McQuic/releases/download/generic/qp_3_msssim_fcc58b73.mcquic"
|
| 23 |
|
| 24 |
+
HF_SPACE = "HF_SPACE" in os.environ
|
| 25 |
+
|
| 26 |
|
| 27 |
@st.experimental_singleton
|
| 28 |
def loadModel(qp: int, local: pathlib.Path, device, mse: bool):
|
|
|
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
+
def main(debug: bool, quiet: bool, qp: int, disable_gpu: bool):
|
| 69 |
if disable_gpu or not torch.cuda.is_available():
|
| 70 |
device = torch.device("cpu")
|
| 71 |
else:
|
|
|
|
| 89 |
</p>
|
| 90 |
|
| 91 |
|
| 92 |
+
<image src="https://img.shields.io/badge/NOTE-yellow?style=for-the-badge" alt="NOTE"/>
|
|
|
|
|
|
|
| 93 |
|
| 94 |
> Due to resources limitation, I only provide compression service with model `qp = 3`.
|
| 95 |
|
| 96 |
+
<br/>
|
| 97 |
+
<br/>
|
| 98 |
+
<br/>
|
| 99 |
+
<br/>
|
| 100 |
+
<br/>
|
| 101 |
+
<br/>
|
| 102 |
+
<br/>
|
| 103 |
|
| 104 |
+
<a href="https://github.com/xiaosu-zhu/McQuic">
|
| 105 |
+
<image src="https://raw.githubusercontent.com/xiaosu-zhu/McQuic/main/assets/GitHub-Mark-120px-plus.png" alt="Github"/>
|
| 106 |
<image src="https://img.shields.io/github/stars/xiaosu-zhu/McQuic?style=social" alt="Github"/>
|
| 107 |
</a>
|
| 108 |
|
| 109 |
""", unsafe_allow_html=True)
|
| 110 |
|
| 111 |
+
if HF_SPACE:
|
| 112 |
+
st.markdown("""
|
| 113 |
+
<image src="https://img.shields.io/badge/NOTE-yellow?style=for-the-badge" alt="NOTE"/>
|
| 114 |
+
|
| 115 |
+
> Due to resources limitation of HF spaces, the upload image size is restricted to lower than `3000 x 3000`.
|
| 116 |
+
|
| 117 |
+
<image src="https://img.shields.io/badge/NOTE-yellow?style=for-the-badge" alt="NOTE"/>
|
| 118 |
|
| 119 |
+
> Also, this demo running on HF space is GPU-disabled. So it may be slow.
|
| 120 |
+
""")
|
| 121 |
with st.form("SubmitForm"):
|
| 122 |
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.")
|
| 123 |
+
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.")
|
| 124 |
submitted = st.form_submit_button("Submit", help="Click to start compress/restore.")
|
| 125 |
if submitted and uploadedFile is not None:
|
| 126 |
if uploadedFile.name.endswith(".mcq"):
|
|
|
|
| 134 |
st.image(result.cpu().permute(1, 2, 0).numpy())
|
| 135 |
st.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")
|
| 136 |
else:
|
| 137 |
+
try:
|
| 138 |
+
a = Image.open(uploadedFile)
|
| 139 |
+
except PIL.UnidentifiedImageError:
|
| 140 |
+
st.markdown("""
|
| 141 |
+
<image src="https://img.shields.io/badge/ERROR-red?style=for-the-badge" alt="ERROR"/>
|
| 142 |
+
|
| 143 |
+
> Image open failed. Please try other images.
|
| 144 |
+
""")
|
| 145 |
+
return
|
| 146 |
+
w, h = a.size
|
| 147 |
+
if HF_SPACE and (h > 3000 or w > 3000):
|
| 148 |
+
st.markdown("""
|
| 149 |
+
<image src="https://img.shields.io/badge/ERROR-red?style=for-the-badge" alt="ERROR"/>
|
| 150 |
+
|
| 151 |
+
> Image is too large. Please try other images.
|
| 152 |
+
""")
|
| 153 |
+
return
|
| 154 |
raw = torch.ByteTensor(torch.ByteStorage.from_buffer(uploadedFile.read())) # type: ignore
|
| 155 |
image = decode_image(raw, ImageReadMode.RGB).to(device)
|
| 156 |
+
# st.image(image.cpu().permute(1, 2, 0).numpy())
|
| 157 |
result = compressImage(image, model, cropping)
|
| 158 |
|
| 159 |
st.text(str(result))
|
| 160 |
|
| 161 |
st.download_button("Click to download compressed file", data=result.serialize(), file_name=".".join(uploadedFile.name.split(".")[:-1] + ["mcq"]), mime="image/mcq")
|
| 162 |
|
| 163 |
+
st.markdown("""
|
| 164 |
+
<br/>
|
| 165 |
+
<br/>
|
| 166 |
+
<br/>
|
| 167 |
+
<br/>
|
| 168 |
+
<br/>
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
<p align="center">
|
| 172 |
+
<a href="https://www.python.org/" target="_blank">
|
| 173 |
+
<image src="https://img.shields.io/badge/python-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54" alt="Python"/>
|
| 174 |
+
</a>
|
| 175 |
+
<a href="https://pytorch.org/" target="_blank">
|
| 176 |
+
<image src="https://img.shields.io/badge/PyTorch-%23EE4C2C.svg?style=for-the-badge&logo=PyTorch&logoColor=white" alt="PyTorch"/>
|
| 177 |
+
</a>
|
| 178 |
+
<a href="https://github.com/xiaosu-zhu/McQuic/stargazers" target="_blank">
|
| 179 |
+
<image src="https://img.shields.io/github/stars/xiaosu-zhu/McQuic?logo=github&style=for-the-badge" alt="Github stars"/>
|
| 180 |
+
</a>
|
| 181 |
+
<a href="https://github.com/xiaosu-zhu/McQuic/network/members" target="_blank">
|
| 182 |
+
<image src="https://img.shields.io/github/forks/xiaosu-zhu/McQuic?logo=github&style=for-the-badge" alt="Github forks"/>
|
| 183 |
+
</a>
|
| 184 |
+
<a href="https://github.com/xiaosu-zhu/McQuic/blob/main/LICENSE" target="_blank">
|
| 185 |
+
<image src="https://img.shields.io/github/license/xiaosu-zhu/McQuic?logo=github&style=for-the-badge" alt="Github license"/>
|
| 186 |
+
</a>
|
| 187 |
+
</p>
|
| 188 |
+
|
| 189 |
+
<br/>
|
| 190 |
+
<br/>
|
| 191 |
+
<br/>
|
| 192 |
+
|
| 193 |
+
<p align="center"><a href="localhost" target="_blank">CVF Open Access</a> | <a href="localhost" 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">Demo</a></p>
|
| 194 |
+
|
| 195 |
+
""")
|
| 196 |
|
| 197 |
if __name__ == "__main__":
|
| 198 |
with torch.inference_mode():
|
| 199 |
+
main(False, False, 3, False)
|