forked from njbbaer/progrockdiffusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBatchSettingsExplorer.py
137 lines (110 loc) · 3.99 KB
/
BatchSettingsExplorer.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
import argparse
import json
import os
import numpy as np
parser = argparse.ArgumentParser()
parser.add_argument(
'-s',
'--settings',
required=False,
default=['blending.json'],
help='A settings JSON file to use, best to put in quotes.'
)
parser.add_argument(
'-p',
'--prd',
help='The PRD command to use (minus "python prd.py"). Put the *whole thing* in single quotes, using escaped double quotes within if needed. Example: -p=\'-s settings.json -p=\\"my prompt\\"\''
)
parser.add_argument("-x", '--xDimension', action='store_true',
help="Batch every possible setting in multidimensionnal grid.")
args = parser.parse_args()
print(args)
# open the json file from the command line
try:
with open(args.settings) as json_file:
data = json.load(json_file)
except FileNotFoundError:
print("File not found")
exit()
def ConvertBooleanToFloat(value):
if value:
return 1.0
else:
return 0.0
# Lerp function
def lerp(a, b, t):
booltype = False
if type(a) == bool and type(b) == bool:
a = ConvertBooleanToFloat(a)
b = ConvertBooleanToFloat(b)
booltype = True
result = (1. - t) * a + t * b
if type(a) == int:
result = int(round(result))
elif booltype:
if round(result) > 0.5:
return True
else:
return False
return result
# Lerp function for dictionaries
def LerpDictionary(dict1, dict2, t):
result = {}
for key in dict1.keys():
if key in dict2.keys():
if type(dict1[key]) == dict:
result[key] = LerpDictionary(dict1[key], dict2[key], t)
else:
result[key] = lerp(dict1[key], dict2[key], t)
else:
result[key] = dict1[key]
return result
def ExecuteProgrockdiffusionWith(temporarySetting):
# write a temporary setting file to override the base setting
print("\nCurrent Setting:" + temporarySetting.__str__() + "\n\n")
with open('tmp.json', 'w', encoding='utf-8') as f:
json.dump(temporarySetting, f, ensure_ascii=False, indent=4)
f.close()
temporarySetting.clear()
# Execute progrockdiffusion with the temporary setting file
os.system(f'python prd.py {args.prd} -s tmp.json')
def N_DimensionalSetting():
jsonData = []
for dimension in range(len(data["settings"])):
steps = data['settings'][dimension]['interpolation_steps']
step_size = 1.0 / (steps - 1)
jsonData.append([])
for i in range(steps):
current_step = i * step_size
jsonData[dimension].append(
LerpDictionary(data['settings'][dimension]['start'], data['settings'][dimension]['end'], current_step))
meshgridSettings = np.array(np.meshgrid(*jsonData)).T.reshape(-1, len(data["settings"]))
print("\n\Settings to be performed:" + meshgridSettings.__str__())
for i in range(0, len(meshgridSettings)):
setting = meshgridSettings[i][0]
for j in range(1, len(meshgridSettings[i])):
setting = {**setting, **meshgridSettings[i][j]}
ExecuteProgrockdiffusionWith(setting)
def SequenceSettings():
interpolatedSetting = {}
for dimension in range(len(data["settings"])):
# get the settings
setting = data["settings"][dimension]
steps = setting["interpolation_steps"]
for step in range(steps):
step_size = 1.0 / (steps - 1)
current_step = step * step_size
# Add the interpolated setting
for key in setting["start"].keys():
start = setting["start"][key]
end = setting["end"][key]
current_value = lerp(start, end, current_step)
interpolatedSetting[key] = current_value
ExecuteProgrockdiffusionWith(interpolatedSetting)
if args.xDimension:
print("\n Batch every possible setting in multidimensionnal grid. \n")
N_DimensionalSetting()
else:
print("\n Batch settings in sequence.\n")
SequenceSettings()
quit()