-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyse_latents.py
executable file
·243 lines (213 loc) · 8.95 KB
/
analyse_latents.py
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
from invokeai.invocation_api import (
BaseInvocation,
Input,
InputField,
InvocationContext,
invocation,
LatentsField,
ImageField,
ImageOutput,
)
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
@invocation("analyze_latents", title="Analyze Latents", tags=["analyze", "latents"], category="modular", version="1.0.0")
class AnalyzeLatentsInvocation(BaseInvocation):
""" Create an image of a histogram of the latents with averages marked """
latents: LatentsField = InputField(
default=None, input=Input.Connection
)
bins: int = InputField(
default=100,
description="Number of bins to use in the histogram",
title="Bins",
)
start_range: float = InputField(
default=-4,
input=Input.Direct,
description="Start of the range to use in the histogram",
title="Start Range",
)
end_range: float = InputField(
default=4,
input=Input.Direct,
description="End of the range to use in the histogram",
title="End Range",
)
image_title: str = InputField(
default="Latent Histogram",
input=Input.Direct,
description="Title of the image",
title="Image Title",
)
def invoke(self, context: InvocationContext) -> ImageOutput:
latents = context.tensors.load(self.latents.latents_name).half()
latents = latents.detach().cpu().numpy()
num_channels = latents.shape[1]
if num_channels not in [4, 16]:
raise ValueError("Latents must have either 4 or 16 channels")
# Create subplots based on the number of channels
if num_channels == 16:
fig, axs = plt.subplots(4, 4, figsize=(15, 15))
else:
fig, axs = plt.subplots(2, 2, figsize=(10, 10))
axs = axs.flatten()
for i in range(num_channels):
channel_data = latents[0, i, :, :]
axs[i].hist(channel_data.flatten(), bins=self.bins, range=(self.start_range, self.end_range))
axs[i].set_title(f'L{i}')
axs[i].axvline(x=channel_data.mean(), color='r', linestyle='dashed', linewidth=1)
# Add title
fig.suptitle(self.image_title)
plt.tight_layout() # Adjust subplot spacing
# Convert to PIL image
fig.canvas.draw()
w, h = fig.canvas.get_width_height()
buf = np.frombuffer(fig.canvas.tostring_argb(), dtype=np.uint8)
buf.shape = (h, w, 4)
buf = np.roll(buf, 3, axis=2) # Convert ARGB to RGBA
img = Image.fromarray(buf)
# Resize image if there are 16 channels
if num_channels == 16:
img = img.resize((1024, 1024))
# Return image
image_dto = context.images.save(image=img)
return ImageOutput(
image=ImageField(image_name=image_dto.image_name),
width=img.width,
height=img.height,
)
@invocation("analyze_image", title="Analyze Image", tags=["analyze", "image"], category="modular", version="1.1.0")
class AnalyzeImageInvocation(BaseInvocation):
""" Create an image of a histogram of the image with averages marked """
image: ImageField = InputField(
default=None, input=Input.Connection
)
bins: int = InputField(
default=255,
description="Number of bins to use in the histogram",
title="Bins",
)
start_range: float = InputField(
default=0,
input=Input.Direct,
description="Start of the range to use in the histogram",
title="Start Range",
)
end_range: float = InputField(
default=255,
input=Input.Direct,
description="End of the range to use in the histogram",
title="End Range",
)
image_title: str = InputField(
default="Image Histogram",
input=Input.Direct,
description="Title of the image",
title="Image Title",
)
greyscale_mode: bool = InputField(
default=False,
input=Input.Direct,
description="Load image in greyscale mode",
title="Greyscale Mode",
)
def invoke(self, context: InvocationContext) -> ImageOutput:
if self.greyscale_mode:
image = context.images.get_pil(self.image.image_name, mode="L")
image = np.array(image)
#create histogram for greyscale image
fig, ax = plt.subplots()
ax.hist(image.flatten(), bins=self.bins, range=(self.start_range, self.end_range))
ax.set_title('Greyscale Channel')
#add title
fig.suptitle(self.image_title)
plt.tight_layout() # Adjust subplot spacing
#add average line if within range
if self.start_range <= image.mean() <= self.end_range:
ax.axvline(x=image.mean(), color='r', linestyle='dashed', linewidth=1)
#convert to PIL image
fig.canvas.draw()
w, h = fig.canvas.get_width_height()
buf = np.frombuffer(fig.canvas.tostring_argb(), dtype=np.uint8)
buf.shape = (h, w, 4)
buf = np.roll(buf, 3, axis=2) # Convert ARGB to RGBA
img = Image.fromarray(buf)
else:
image = context.images.get_pil(self.image.image_name, mode="RGBA")
image = np.array(image)
#split individual channels
R = image[:,:,0]
G = image[:,:,1]
B = image[:,:,2]
A = image[:,:,3]
#create histogram
fig, axs = plt.subplots(2, 2)
axs[0, 0].hist(R.flatten(), bins=self.bins, range=(self.start_range, self.end_range))
axs[0, 0].set_title('Red Channel')
axs[0, 1].hist(G.flatten(), bins=self.bins, range=(self.start_range, self.end_range))
axs[0, 1].set_title('Green Channel')
axs[1, 0].hist(B.flatten(), bins=self.bins, range=(self.start_range, self.end_range))
axs[1, 0].set_title('Blue Channel')
axs[1, 1].hist(A.flatten(), bins=self.bins, range=(self.start_range, self.end_range))
axs[1, 1].set_title('Alpha Channel')
#add title
fig.suptitle(self.image_title)
plt.tight_layout() # Adjust subplot spacing
#add average lines if within range
if self.start_range <= R.mean() <= self.end_range:
axs[0, 0].axvline(x=R.mean(), color='r', linestyle='dashed', linewidth=1)
if self.start_range <= G.mean() <= self.end_range:
axs[0, 1].axvline(x=G.mean(), color='r', linestyle='dashed', linewidth=1)
if self.start_range <= B.mean() <= self.end_range:
axs[1, 0].axvline(x=B.mean(), color='r', linestyle='dashed', linewidth=1)
if self.start_range <= A.mean() <= self.end_range:
axs[1, 1].axvline(x=A.mean(), color='r', linestyle='dashed', linewidth=1)
#convert to PIL image
fig.canvas.draw()
w, h = fig.canvas.get_width_height()
buf = np.frombuffer(fig.canvas.tostring_argb(), dtype=np.uint8)
buf.shape = (h, w, 4)
buf = np.roll(buf, 3, axis=2) # Convert ARGB to RGBA
img = Image.fromarray(buf)
#return image
image_dto = context.images.save(image=img)
return ImageOutput(
image=ImageField(image_name=image_dto.image_name),
width=img.width,
height=img.height,
)
@invocation("image_difference", title="Image Difference", tags=["analyze", "image"], category="modular", version="1.0.0")
class ImageDifferenceInvocation(BaseInvocation):
""" Create an image of the difference between two images """
image1: ImageField = InputField(
default=None, input=Input.Connection
)
image2: ImageField = InputField(
default=None, input=Input.Connection
)
image_title: str = InputField(
default="Image Difference",
input=Input.Direct,
description="Title of the image",
title="Image Title",
)
def invoke(self, context: InvocationContext) -> ImageOutput:
image1 = context.images.get_pil(self.image1.image_name, mode="RGBA")
image2 = context.images.get_pil(self.image2.image_name, mode="RGBA")
image1 = np.array(image1)
image2 = np.array(image2)
#check if images are the same size
if image1.shape != image2.shape:
raise ValueError("Images must be the same size")
#calculate difference
diff = np.clip((image1 - image2) + 127, 0, 255).astype(np.uint8)
#convert to PIL image
img = Image.fromarray(diff)
#return image
image_dto = context.images.save(image=img)
return ImageOutput(
image=ImageField(image_name=image_dto.image_name),
width=img.width,
height=img.height,
)