-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcompress.py
More file actions
169 lines (149 loc) · 5.45 KB
/
compress.py
File metadata and controls
169 lines (149 loc) · 5.45 KB
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
import argparse
from PIL import Image
from pathlib import Path
import yaml
import zlib
import struct
from easydict import EasyDict as edict
from lib import image_utils
from lib.diffc.encode import encode
from lib.diffc.rcc.gaussian_channel_simulator import GaussianChannelSimulator
from lib.blip import BlipCaptioner
def parse_args():
parser = argparse.ArgumentParser(
description="Compress an image or folder of images using the DiffC algorithm."
)
parser.add_argument(
"--config",
help="Path to the compression config file",
required=True
)
parser.add_argument(
"--image_path",
default=None,
help="Path to a single image to compress"
)
parser.add_argument(
"--image_dir",
default=None,
help="Path to a directory containing one or more images to compress"
)
parser.add_argument(
"--output_dir",
required=True,
help="Directory to output the compressed files to"
)
parser.add_argument(
"--recon_timestep",
type=int,
required=True,
help="Timestep at which to save the encoded version"
)
return parser.parse_args()
def get_noise_prediction_model(model_name, config):
if model_name == "SD1.5":
from lib.models.SD15 import SD15Model
return SD15Model()
elif model_name == "SD2.1":
from lib.models.SD21 import SD21Model
return SD21Model()
elif model_name == "SDXL":
from lib.models.SDXL import SDXLModel
use_refiner = config.get("use_refiner", False)
return SDXLModel(use_refiner=use_refiner)
elif model_name == 'Flux':
from lib.models.Flux import FluxModel
return FluxModel()
else:
raise ValueError(f"Unrecognized model: {model_name}")
def write_diffc_file(caption, image_bytes, width, height, step_idx, output_path):
# Compress caption with zlib
compressed_caption = zlib.compress(caption.encode('utf-8'))
caption_length = len(compressed_caption)
# Write caption length (4 bytes), width (2 bytes), height (2 bytes), step_idx (2 bytes),
# compressed caption, then image data
with open(output_path, 'wb') as f:
f.write(struct.pack('<I', caption_length)) # Write length as 4-byte little-endian uint
f.write(struct.pack('<H', width)) # Write width as 2-byte little-endian uint
f.write(struct.pack('<H', height)) # Write height as 2-byte little-endian uint
f.write(struct.pack('<H', step_idx)) # Write step_idx as 2-byte little-endian uint
f.write(compressed_caption)
f.write(bytes(image_bytes))
def compress_image(image_path, output_path, noise_prediction_model,
gaussian_channel_simulator, config, caption=""):
# Load and preprocess image
img_pil = Image.open(image_path)
img_width, img_height = img_pil.size
gt_pt = image_utils.pil_to_torch_img(img_pil)
gt_latent = noise_prediction_model.image_to_latent(gt_pt)
# Configure model
noise_prediction_model.configure(
caption, config.encoding_guidance_scale, img_width, img_height
)
# Encode image
chunk_seeds_per_step, Dkl_per_step, _, recon_step_indices = encode(
gt_latent,
config.encoding_timesteps,
noise_prediction_model,
gaussian_channel_simulator,
config.manual_dkl_per_step,
[config.recon_timestep] # Only encode for the specified timestep
)
# Get the compressed representation
step_idx = recon_step_indices[0] # Only one step since we specified one timestep
bytes_data = gaussian_channel_simulator.compress_chunk_seeds(
chunk_seeds_per_step[: step_idx + 1],
Dkl_per_step[: step_idx + 1]
)
write_diffc_file(
caption,
bytes_data,
img_width,
img_height,
step_idx,
output_path)
def main():
args = parse_args()
# Load config
with open(args.config, "r") as f:
config = edict(yaml.safe_load(f))
config.recon_timestep = args.recon_timestep
# Set up output directory
output_dir = Path(args.output_dir)
output_dir.mkdir(exist_ok=True, parents=True)
# Get image paths
if not bool(args.image_path) ^ bool(args.image_dir):
raise ValueError("Must specify exactly one of --image_path or --image_dir")
image_paths = []
if args.image_path:
image_paths.append(Path(args.image_path))
else:
image_dir = Path(args.image_dir)
image_paths = list(image_dir.glob("*.jpg")) + list(image_dir.glob("*.png"))
image_paths = list(map(Path, image_paths))
# Initialize models
gaussian_channel_simulator = GaussianChannelSimulator(
config.max_chunk_size,
config.chunk_padding
)
noise_prediction_model = get_noise_prediction_model(config.model, config)
# Get captions if needed
captions = {}
if config.encoding_guidance_scale or config.denoising_guidance_scale:
captioner = BlipCaptioner()
captions = captioner.process_and_save(image_paths, output_dir)
del captioner
# Process each image
for image_path in image_paths:
output_path = output_dir / f"{image_path.stem}.diffc"
caption = captions.get(str(image_path), "")
compress_image(
image_path,
output_path,
noise_prediction_model,
gaussian_channel_simulator,
config,
caption
)
if __name__ == "__main__":
main()