Upload folder using huggingface_hub
Browse files- README.md +30 -0
- block.py +72 -0
- modular_config.json +7 -0
- modular_model_index.json +15 -0
README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
---
|
| 4 |
+
|
| 5 |
+
default image resizing method for wan 14B image-to-video pipelines (for both wan2.1 and wan 2.2 14B)
|
| 6 |
+
```py
|
| 7 |
+
from diffusers import ModularPipeline
|
| 8 |
+
image_processor = ModularPipeline.from_pretrained("YiYiXu/WanImageProcessor14B", trust_remote_code=True)
|
| 9 |
+
image = image_processor(
|
| 10 |
+
image="https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/wan_i2v_input.JPG",
|
| 11 |
+
output="processed_image"
|
| 12 |
+
)
|
| 13 |
+
```
|
| 14 |
+
|
| 15 |
+
for wan 2.2 5B, the default method is here https://huggingface.co/YiYiXu/WanImageProcessor
|
| 16 |
+
|
| 17 |
+
this is the code to resize
|
| 18 |
+
|
| 19 |
+
```py
|
| 20 |
+
|
| 21 |
+
image = load_image(
|
| 22 |
+
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/astronaut.jpg"
|
| 23 |
+
)
|
| 24 |
+
max_area = 720 * 1280
|
| 25 |
+
aspect_ratio = image.height / image.width
|
| 26 |
+
mod_value = pipe.vae_scale_factor_spatial * pipe.transformer.config.patch_size[1]
|
| 27 |
+
height = round(np.sqrt(max_area * aspect_ratio)) // mod_value * mod_value
|
| 28 |
+
width = round(np.sqrt(max_area / aspect_ratio)) // mod_value * mod_value
|
| 29 |
+
image = image.resize((width, height))
|
| 30 |
+
```
|
block.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from diffusers.modular_pipelines import (
|
| 2 |
+
ModularPipelineBlocks,
|
| 3 |
+
InputParam,
|
| 4 |
+
OutputParam,
|
| 5 |
+
ConfigSpec,
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
from diffusers.utils import load_image
|
| 9 |
+
from PIL import Image
|
| 10 |
+
from typing import Union, Tuple
|
| 11 |
+
import numpy as np
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class Wan14BImageProcessor(ModularPipelineBlocks):
|
| 16 |
+
|
| 17 |
+
@property
|
| 18 |
+
def description(self):
|
| 19 |
+
return "default Image Processor for wan14B i2v (for both Wan2.1 and Wan2.2, it resizes image"
|
| 20 |
+
|
| 21 |
+
@property
|
| 22 |
+
def inputs(self):
|
| 23 |
+
return [
|
| 24 |
+
InputParam(name="image", type_hint=Union[Image.Image, str], description= "the Image to process"),
|
| 25 |
+
InputParam(name="max_area", type_hint=int, description= "the maximum area of the Image to process")
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
+
@property
|
| 29 |
+
def intermediate_outputs(self):
|
| 30 |
+
return [
|
| 31 |
+
OutputParam(name="processed_image", type_hint=Image.Image, description= "the processed Image"),
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
@property
|
| 35 |
+
def expected_configs(self):
|
| 36 |
+
return [
|
| 37 |
+
ConfigSpec(name="patch_size", default=(1, 2, 2)),
|
| 38 |
+
ConfigSpec(name="vae_stride", default=(4, 8, 8)),
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
def __call__(self, components, state):
|
| 42 |
+
|
| 43 |
+
block_state = self.get_block_state(state)
|
| 44 |
+
|
| 45 |
+
if isinstance(block_state.image, str):
|
| 46 |
+
image = load_image(block_state.image).convert("RGB")
|
| 47 |
+
elif isinstance(block_state.image, Image.Image):
|
| 48 |
+
image = block_state.image
|
| 49 |
+
else:
|
| 50 |
+
raise ValueError(f"Invalid image type: {type(block_state.image)}; only support PIL Image or url string")
|
| 51 |
+
|
| 52 |
+
if block_state.max_area is None:
|
| 53 |
+
max_area = 480 * 832
|
| 54 |
+
else:
|
| 55 |
+
max_area = block_state.max_area
|
| 56 |
+
|
| 57 |
+
aspect_ratio = image.height / image.width
|
| 58 |
+
mod_value_height = components.vae_stride[1] * components.patch_size[1]
|
| 59 |
+
mod_value_width = components.vae_stride[2] * components.patch_size[2]
|
| 60 |
+
height = round(np.sqrt(max_area * aspect_ratio)) // mod_value_height * mod_value_height
|
| 61 |
+
width = round(np.sqrt(max_area / aspect_ratio)) // mod_value_width * mod_value_width
|
| 62 |
+
resized_image = image.resize((width, height))
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
block_state.processed_image = resized_image
|
| 66 |
+
|
| 67 |
+
print(f" initial image size: {image.size}")
|
| 68 |
+
print(f" processed image size: {resized_image.size}")
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
self.set_block_state(state, block_state)
|
| 72 |
+
return components, state
|
modular_config.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_class_name": "Wan14BImageProcessor",
|
| 3 |
+
"_diffusers_version": "0.35.0.dev0",
|
| 4 |
+
"auto_map": {
|
| 5 |
+
"ModularPipelineBlocks": "block.Wan14BImageProcessor"
|
| 6 |
+
}
|
| 7 |
+
}
|
modular_model_index.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_blocks_class_name": "Wan14BImageProcessor",
|
| 3 |
+
"_class_name": "ModularPipeline",
|
| 4 |
+
"_diffusers_version": "0.35.0.dev0",
|
| 5 |
+
"patch_size": [
|
| 6 |
+
1,
|
| 7 |
+
2,
|
| 8 |
+
2
|
| 9 |
+
],
|
| 10 |
+
"vae_stride": [
|
| 11 |
+
4,
|
| 12 |
+
8,
|
| 13 |
+
8
|
| 14 |
+
]
|
| 15 |
+
}
|