Initial commit of script
Browse files- sd3-purturbed-2.py +32 -0
sd3-purturbed-2.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from safetensors.torch import save_file, safe_open
|
3 |
+
|
4 |
+
# Open the safetensors file
|
5 |
+
model = safe_open('F:/sd3_test/sd3_medium.safetensors', framework='pt') # Updated the backslashes to forward slashes for compatibility
|
6 |
+
|
7 |
+
# Retrieve keys and create a dictionary of tensors
|
8 |
+
keys = model.keys()
|
9 |
+
dic = {key: model.get_tensor(key) for key in keys}
|
10 |
+
|
11 |
+
# Part of the key to filter by
|
12 |
+
parts = ['diffusion_model']
|
13 |
+
count = 0
|
14 |
+
|
15 |
+
# Loop through the keys
|
16 |
+
for k in keys:
|
17 |
+
# Check if all parts are in the key
|
18 |
+
if all(part in k for part in parts):
|
19 |
+
v = dic[k] # Get the tensor
|
20 |
+
print(f'{k}: {v.std()}') # Corrected the print statement to use 'v'
|
21 |
+
|
22 |
+
# Add noise to the tensor
|
23 |
+
noise = torch.normal(torch.zeros_like(v) * v.mean(), torch.ones_like(v) * v.std() * 0.02)
|
24 |
+
dic[k] += noise
|
25 |
+
|
26 |
+
count += 1
|
27 |
+
|
28 |
+
# Print the count of modified tensors
|
29 |
+
print(count)
|
30 |
+
|
31 |
+
# Save the modified tensors to a new file
|
32 |
+
save_file(dic, 'F:/sd3_test/sd3_medium_perturbed_marduk191-3.safetensors', metadata=model.metadata())
|