|
|
|
""" |
|
Chain-of-Zoom 8-bit Complete Pipeline Usage Example |
|
""" |
|
|
|
from transformers import AutoModel, BitsAndBytesConfig |
|
from PIL import Image |
|
import torch |
|
|
|
def load_chain_of_zoom_pipeline(): |
|
"""Load the complete Chain-of-Zoom pipeline""" |
|
|
|
|
|
vlm_config = BitsAndBytesConfig(load_in_8bit=True) |
|
diffusion_config = BitsAndBytesConfig(load_in_8bit=True) |
|
ram_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4") |
|
lora_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4") |
|
|
|
print("π Loading Chain-of-Zoom components...") |
|
|
|
|
|
vlm = AutoModel.from_pretrained("./vlm", quantization_config=vlm_config) |
|
diffusion = AutoModel.from_pretrained("./diffusion", quantization_config=diffusion_config) |
|
ram = AutoModel.from_pretrained("./ram", quantization_config=ram_config) |
|
lora = AutoModel.from_pretrained("./lora", quantization_config=lora_config) |
|
|
|
print("β
All components loaded successfully!") |
|
|
|
return { |
|
'vlm': vlm, |
|
'diffusion': diffusion, |
|
'ram': ram, |
|
'lora': lora |
|
} |
|
|
|
def super_resolve_image(image_path, target_scale=8): |
|
"""Super-resolve an image using Chain-of-Zoom""" |
|
|
|
|
|
pipeline = load_chain_of_zoom_pipeline() |
|
|
|
|
|
image = Image.open(image_path) |
|
print(f"πΈ Input image: {image.size}") |
|
|
|
|
|
current_scale = 1 |
|
current_image = image |
|
|
|
while current_scale < target_scale: |
|
next_scale = min(current_scale * 2, target_scale) |
|
print(f"π Scaling {current_scale}x β {next_scale}x") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
current_scale = next_scale |
|
|
|
print(f"β
Super-resolution complete: {target_scale}x") |
|
return current_image |
|
|
|
if __name__ == "__main__": |
|
|
|
result = super_resolve_image("input.jpg", target_scale=8) |
|
result.save("output_8x.jpg") |
|
|