dn6 HF Staff commited on
Commit
95e2523
·
verified ·
1 Parent(s): d994f25

Upload folder using huggingface_hub

Browse files
Files changed (5) hide show
  1. README.md +19 -0
  2. __pycache__/block.cpython-311.pyc +0 -0
  3. block.py +106 -0
  4. config.json +7 -0
  5. requirements.txt +0 -0
README.md ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Florence2 Image Annotator
2
+
3
+ ```python
4
+ import torch
5
+ from diffusers.modular_pipelines import ModularPipelineBlocks, SequentialPipelineBlocks
6
+ from diffusers.modular_pipelines.stable_diffusion_xl import INPAINT_BLOCKS
7
+ from diffusers.utils import load_image
8
+
9
+ # fetch the Florence2 image annotator block that will create our mask
10
+ image_annotator_block = ModularPipelineBlocks.from_pretrained("diffusers/florence2-image-annotator", trust_remote_code=True)
11
+
12
+ my_blocks = INPAINT_BLOCKS.copy()
13
+ # insert the annotation block before the image encoding step
14
+ my_blocks.insert("image_annotator", image_annotator_block, 1)
15
+
16
+ # Create our initial set of inpainting blocks
17
+ blocks = SequentialPipelineBlocks.from_blocks_dict(my_blocks)
18
+ pipe = blocks.init_pipeline()
19
+ ```
__pycache__/block.cpython-311.pyc ADDED
Binary file (5.05 kB). View file
 
block.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List, Union
2
+ from PIL import Image, ImageDraw
3
+ import torch
4
+
5
+ from diffusers.modular_pipelines import (
6
+ PipelineState,
7
+ ModularPipelineBlocks,
8
+ InputParam,
9
+ ComponentSpec,
10
+ OutputParam,
11
+ )
12
+ from transformers import AutoProcessor, AutoModelForCausalLM
13
+
14
+
15
+ class Florence2ImageAnnotatorBlock(ModularPipelineBlocks):
16
+ @property
17
+ def expected_components(self):
18
+ return [
19
+ ComponentSpec(
20
+ name="image_annotator",
21
+ type_hint=AutoModelForCausalLM,
22
+ repo="microsoft/Florence-2-large",
23
+ ),
24
+ ComponentSpec(
25
+ name="image_annotator_processor",
26
+ type_hint=AutoProcessor,
27
+ repo="microsoft/Florence-2-large",
28
+ ),
29
+ ]
30
+
31
+ @property
32
+ def inputs(self) -> List[InputParam]:
33
+ return [
34
+ InputParam(
35
+ "image",
36
+ Image,
37
+ required=True,
38
+ description="Image(s) to annotate",
39
+ ),
40
+ InputParam(
41
+ "annotation_task_prompt",
42
+ Union[str, List[str]],
43
+ required=True,
44
+ description="""Annotation Task to perform on the image.
45
+ """,
46
+ ),
47
+ ]
48
+
49
+ @property
50
+ def intermediates_outputs(self) -> List[OutputParam]:
51
+ return [
52
+ OutputParam(
53
+ "mask",
54
+ type_hint=torch.Tensor,
55
+ description="Depth Map(s) of input Image(s)",
56
+ ),
57
+ ]
58
+
59
+ def annotate_image(self, image, prompt):
60
+ inputs = self.image_annotator_processor(
61
+ text=prompt, images=image, return_tensors="pt"
62
+ )
63
+ generated_ids = self.annotator.generate(
64
+ input_ids=inputs["input_ids"],
65
+ pixel_values=inputs["pixel_values"],
66
+ max_new_tokens=1024,
67
+ early_stopping=False,
68
+ do_sample=False,
69
+ num_beams=3,
70
+ )
71
+ annotations = self.image_annotator_processor.batch_decode(
72
+ generated_ids, skip_special_tokens=False
73
+ )[0]
74
+ annotations = self.image_annotator_processor.post_process_generation(
75
+ annotations, task=prompt, image_size=(image.height, image.width)
76
+ )
77
+
78
+ return annotations
79
+
80
+ def prepare_mask(self, images, annotations):
81
+ masks = []
82
+ for image, annotation in zip(images, annotations):
83
+ mask_image = Image.new("L", image.size, 0)
84
+ draw = ImageDraw.Draw(mask_image)
85
+ draw.polygon(annotation["polygon"], fill="white")
86
+ masks.append(mask_image)
87
+
88
+ return masks
89
+
90
+ @torch.no_grad()
91
+ def __call__(self, pipeline, state: PipelineState) -> PipelineState:
92
+ block_state = self.get_block_state(state)
93
+
94
+ images = block_state.image
95
+ annotation_task_prompt = block_state.annotation_task_prompt
96
+
97
+ if not isinstance(annotation_task_prompt, list):
98
+ annotation_task_prompt = [annotation_task_prompt]
99
+
100
+ if len(images) != len(annotation_task_prompt):
101
+ raise ValueError("Number of images and annotation prompts must match")
102
+
103
+ annotations = self.annotate_image(images, annotation_task_prompt)
104
+ block_state.mask = self.prepare_mask(images, annotations)
105
+
106
+ self.set_block_state(block_state)
config.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "_class_name": "Florence2ImageAnnotatorBlock",
3
+ "_diffusers_version": "0.35.0.dev0",
4
+ "auto_map": {
5
+ "ModularPipelineBlocks": "block.Florence2ImageAnnotatorBlock"
6
+ }
7
+ }
requirements.txt ADDED
File without changes