File size: 7,750 Bytes
55866f4
 
 
 
 
 
 
e6d4da8
3a5de53
55866f4
 
 
 
 
 
 
 
 
3a5de53
55866f4
 
 
 
 
 
 
 
 
 
 
 
 
 
5f0abcc
55866f4
 
 
 
 
 
 
e6d4da8
55866f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a5de53
55866f4
3a5de53
 
 
 
 
55866f4
 
3a5de53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55866f4
 
 
 
 
 
 
 
 
 
 
 
3a5de53
 
 
 
 
 
 
 
 
55866f4
 
3a5de53
 
55866f4
 
3a5de53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55866f4
3a5de53
 
 
 
 
 
 
 
 
 
 
 
55866f4
3a5de53
55866f4
3a5de53
 
 
 
55866f4
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"""
    Wrapper pipeline for concept attention. 
"""
from dataclasses import dataclass
import PIL
import numpy as np
import matplotlib.pyplot as plt
import torch
import einops

from concept_attention.binary_segmentation_baselines.raw_cross_attention import RawCrossAttentionBaseline, RawCrossAttentionSegmentationModel
from concept_attention.binary_segmentation_baselines.raw_output_space import RawOutputSpaceBaseline, RawOutputSpaceSegmentationModel
from concept_attention.image_generator import FluxGenerator

@dataclass
class ConceptAttentionPipelineOutput():
    image: PIL.Image.Image | np.ndarray
    concept_heatmaps: list[PIL.Image.Image]
    cross_attention_maps: list[PIL.Image.Image]

class ConceptAttentionFluxPipeline():
    """
        This is an object that allows you to generate images with flux, and
        'encode' images with flux.  
    """

    def __init__(
        self, 
        model_name: str = "flux-schnell", 
        offload_model=False,
        device="cuda:0"
    ):
        self.model_name = model_name
        self.offload_model = offload_model
        # Load the generator
        self.flux_generator = FluxGenerator(
            model_name=model_name,
            offload=offload_model,
            device=device
        )

    @torch.no_grad()
    def generate_image(
        self, 
        prompt: str,
        concepts: list[str],
        width: int = 1024,
        height: int = 1024,
        return_cross_attention = False,
        layer_indices = list(range(15, 19)),
        return_pil_heatmaps = True,
        seed: int = 0,
        num_inference_steps: int = 4,
        guidance: float = 0.0,
        timesteps=None,
        softmax: bool = True,
        cmap="plasma"
    ) -> ConceptAttentionPipelineOutput:
        """
            Generate an image with flux, given a list of concepts.
        """
        assert return_cross_attention is False, "Not supported yet"
        assert all([layer_index >= 0 and layer_index < 19 for layer_index in layer_indices]), "Invalid layer index"
        assert height == width, "Height and width must be the same for now"

        if timesteps is None:
            timesteps = list(range(num_inference_steps))
        # Run the raw output space object
        image, cross_attention_maps, concept_heatmaps = self.flux_generator.generate_image(
            width=width,
            height=height,
            prompt=prompt,
            num_steps=num_inference_steps,
            concepts=concepts,
            seed=seed,
            guidance=guidance,
        )
        # Concept heamaps extraction
        if softmax:
            concept_heatmaps = torch.nn.functional.softmax(concept_heatmaps, dim=-2)

        concept_heatmaps = concept_heatmaps[:, layer_indices]
        concept_heatmaps = einops.reduce(
            concept_heatmaps,
            "time layers batch concepts patches -> batch concepts patches",
            reduction="mean"
        )
        concept_heatmaps = einops.rearrange(
            concept_heatmaps,
            "batch concepts (h w) -> batch concepts h w",
            h=64,
            w=64
        )
        # Cross attention maps 
        if softmax:
            cross_attention_maps = torch.nn.functional.softmax(cross_attention_maps, dim=-2)

        cross_attention_maps = cross_attention_maps[:, layer_indices]
        cross_attention_maps = einops.reduce(
            cross_attention_maps,
            "time layers batch concepts patches -> batch concepts patches",
            reduction="mean"
        )
        cross_attention_maps = einops.rearrange(
            cross_attention_maps,
            "batch concepts (h w) -> batch concepts h w",
            h=64,
            w=64
        )
        
        concept_heatmaps = concept_heatmaps.to(torch.float32).detach().cpu().numpy()[0]
        cross_attention_maps = cross_attention_maps.to(torch.float32).detach().cpu().numpy()[0]
        # Convert the torch heatmaps to PIL images.
        if return_pil_heatmaps:
            # Convert to a matplotlib color scheme
            colored_heatmaps = []
            for concept_heatmap in concept_heatmaps:
                concept_heatmap = (concept_heatmap - concept_heatmap.min()) / (concept_heatmap.max() - concept_heatmap.min())
                colored_heatmap = plt.get_cmap(cmap)(concept_heatmap)
                rgb_image = (colored_heatmap[:, :, :3] * 255).astype(np.uint8)
                colored_heatmaps.append(rgb_image)

            concept_heatmaps = [PIL.Image.fromarray(concept_heatmap) for concept_heatmap in colored_heatmaps]

            colored_cross_attention_maps = []
            for cross_attention_map in cross_attention_maps:
                cross_attention_map = (cross_attention_map - cross_attention_map.min()) / (cross_attention_map.max() - cross_attention_map.min())
                colored_cross_attention_map = plt.get_cmap(cmap)(cross_attention_map)
                rgb_image = (colored_cross_attention_map[:, :, :3] * 255).astype(np.uint8)
                colored_cross_attention_maps.append(rgb_image)

            cross_attention_maps = [PIL.Image.fromarray(cross_attention_map) for cross_attention_map in colored_cross_attention_maps]

        return ConceptAttentionPipelineOutput(
            image=image,
            concept_heatmaps=concept_heatmaps,
            cross_attention_maps=cross_attention_maps
        )

    # def encode_image(
    #     self,
    #     image: PIL.Image.Image,
    #     concepts: list[str],
    #     prompt: str = "", # Optional
    #     width: int = 1024,
    #     height: int = 1024,
    #     return_cross_attention = False,
    #     layer_indices = list(range(15, 19)),
    #     num_samples: int = 1,
    #     device: str = "cuda:0",
    #     return_pil_heatmaps: bool = True,
    #     seed: int = 0,
    #     cmap="plasma"
    # ) -> ConceptAttentionPipelineOutput:
    #     """
    #         Encode an image with flux, given a list of concepts.
    #     """
    #     assert return_cross_attention is False, "Not supported yet"
    #     assert all([layer_index >= 0 and layer_index < 19 for layer_index in layer_indices]), "Invalid layer index"
    #     assert height == width, "Height and width must be the same for now"
    #     # Run the raw output space object
    #     concept_heatmaps, _ = self.output_space_segmentation_model.segment_individual_image(
    #         image=image,
    #         concepts=concepts,
    #         caption=prompt,
    #         device=device,
    #         softmax=True,
    #         layers=layer_indices,
    #         num_samples=num_samples,
    #         height=height,
    #         width=width
    #     )
    #     concept_heatmaps = concept_heatmaps.detach().cpu().numpy().squeeze()
       
    #     # Convert the torch heatmaps to PIL images. 
    #     if return_pil_heatmaps:
    #         min_val = concept_heatmaps.min()
    #         max_val = concept_heatmaps.max()
    #         # Convert to a matplotlib color scheme
    #         colored_heatmaps = []
    #         for concept_heatmap in concept_heatmaps:
    #             # concept_heatmap = (concept_heatmap - concept_heatmap.min()) / (concept_heatmap.max() - concept_heatmap.min())
    #             concept_heatmap = (concept_heatmap - min_val) / (max_val - min_val)
    #             colored_heatmap = plt.get_cmap(cmap)(concept_heatmap)
    #             rgb_image = (colored_heatmap[:, :, :3] * 255).astype(np.uint8)
    #             colored_heatmaps.append(rgb_image)

    #         concept_heatmaps = [PIL.Image.fromarray(concept_heatmap) for concept_heatmap in colored_heatmaps]

    #     return ConceptAttentionPipelineOutput(
    #         image=image,
    #         concept_heatmaps=concept_heatmaps
    #     )