Spaces:
Runtime error
Runtime error
updated color names
Browse files- DisentanglementBase.py +54 -38
DisentanglementBase.py
CHANGED
|
@@ -366,6 +366,7 @@ class DisentanglementBase:
|
|
| 366 |
axs[i].set_title(np.round(lambd, 2))
|
| 367 |
plt.tight_layout()
|
| 368 |
plt.savefig(join(self.repo_folder, 'figures', name+'.jpg'))
|
|
|
|
| 369 |
return images, lambdas
|
| 370 |
|
| 371 |
def get_verification_score(self, separation_vector, feature_id, samples=10, lambd=1, savefig=False, feature=None, method=None):
|
|
@@ -377,21 +378,26 @@ class DisentanglementBase:
|
|
| 377 |
|
| 378 |
for seed in tqdm(items):
|
| 379 |
images, lambdas = self.generate_changes(seed, separation_vector, min_epsilon=-lambd, max_epsilon=lambd, count=3, savefig=savefig, feature=feature, method=method)
|
| 380 |
-
|
| 381 |
-
|
|
|
|
| 382 |
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 388 |
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
else:
|
| 392 |
-
if (h0 > hue_low and h0 < hue_high) or (h2 > hue_low and h2 < hue_high):
|
| 393 |
-
matches += 1
|
| 394 |
|
|
|
|
| 395 |
return np.round(matches / samples, 2)
|
| 396 |
|
| 397 |
|
|
@@ -408,18 +414,18 @@ def main():
|
|
| 408 |
with dnnlib.util.open_url(model_file) as f:
|
| 409 |
model = legacy.load_network_pkl(f)['G_ema'] # type: ignore
|
| 410 |
|
| 411 |
-
colors_list = ['
|
| 412 |
-
'Kelly Green', 'Green Blue Seafoam', 'Blue
|
| 413 |
-
'Warm Blue', 'Indigo
|
| 414 |
|
| 415 |
scores = []
|
| 416 |
-
kwargs = {'CL method':['LR', 'SVM'], 'C':[0.1, 1], 'sign':[True, False], 'num_factors':[1, 10, 20
|
| 417 |
|
| 418 |
for space in ['w', 'z', 's']:
|
| 419 |
print('Launching experiment with space:', space)
|
| 420 |
disentanglemnet_exp = DisentanglementBase(repo_folder, model, annotations, df, space=space, colors_list=colors_list, compute_s=False)
|
| 421 |
|
| 422 |
-
for method in ['
|
| 423 |
if space != 's' and method == 'InterFaceGAN':
|
| 424 |
print('Now obtaining separation vector for using InterfaceGAN')
|
| 425 |
for met in kwargs['CL method']:
|
|
@@ -427,17 +433,22 @@ def main():
|
|
| 427 |
separation_vectors = disentanglemnet_exp.InterFaceGAN_separation_vector(method=met, C=c)
|
| 428 |
for i, color in enumerate(colors_list):
|
| 429 |
print('Generating images with variations')
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
|
|
|
| 434 |
print('Finally obtaining verification score')
|
| 435 |
for verif in kwargs['lambda_verif']:
|
| 436 |
-
score = disentanglemnet_exp.get_verification_score(separation_vectors[i], i, samples=kwargs['samples'], lambd=verif, savefig=
|
| 437 |
print('Score for method', method, 'on space', space, 'for color', color, ':', score)
|
| 438 |
|
| 439 |
-
scores.append([space, method, color, score, 'classification method:' + met + ', regularization: ' + str(c) + ', verification lambda:' + str(verif)])
|
| 440 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 441 |
elif method == 'StyleSpace':
|
| 442 |
print('Now obtaining separation vector for using StyleSpace')
|
| 443 |
for sign in kwargs['sign']:
|
|
@@ -446,35 +457,40 @@ def main():
|
|
| 446 |
separation_vectors = disentanglemnet_exp.StyleSpace_separation_vector(sign=sign, num_factors=num_factors, cutout=cutout)
|
| 447 |
for i, color in enumerate(colors_list):
|
| 448 |
print('Generating images with variations')
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
|
|
|
| 453 |
print('Finally obtaining verification score')
|
| 454 |
for verif in kwargs['lambda_verif']:
|
| 455 |
-
score = disentanglemnet_exp.get_verification_score(separation_vectors[i], i, samples=kwargs['samples'], lambd=verif, savefig=
|
| 456 |
print('Score for method', method, 'on space', space, 'for color', color, ':', score)
|
| 457 |
|
| 458 |
-
scores.append([space, method, color, score, 'using sign:' + str(sign) + ', number of factors: ' + str(num_factors) + ', using cutout: ' + str(cutout) + ', verification lambda:' + str(verif)])
|
| 459 |
-
|
|
|
|
|
|
|
|
|
|
| 460 |
if space == 'w' and method == 'GANSpace':
|
| 461 |
print('Now obtaining separation vector for using GANSpace')
|
| 462 |
separation_vectors = disentanglemnet_exp.GANSpace_separation_vectors(100)
|
| 463 |
-
for
|
| 464 |
print('Generating images with variations')
|
| 465 |
seed = random.randint(0,100000)
|
| 466 |
-
for
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
|
|
|
| 471 |
else:
|
| 472 |
print('Skipping', method, 'on space', space)
|
| 473 |
continue
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
-
score_df = pd.DataFrame(scores, columns=['space', 'method', 'color', 'score', 'kwargs'])
|
| 478 |
print(score_df)
|
| 479 |
score_df.to_csv(join(repo_folder, 'data/scores.csv'))
|
| 480 |
|
|
|
|
| 366 |
axs[i].set_title(np.round(lambd, 2))
|
| 367 |
plt.tight_layout()
|
| 368 |
plt.savefig(join(self.repo_folder, 'figures', name+'.jpg'))
|
| 369 |
+
plt.close()
|
| 370 |
return images, lambdas
|
| 371 |
|
| 372 |
def get_verification_score(self, separation_vector, feature_id, samples=10, lambd=1, savefig=False, feature=None, method=None):
|
|
|
|
| 378 |
|
| 379 |
for seed in tqdm(items):
|
| 380 |
images, lambdas = self.generate_changes(seed, separation_vector, min_epsilon=-lambd, max_epsilon=lambd, count=3, savefig=savefig, feature=feature, method=method)
|
| 381 |
+
try:
|
| 382 |
+
colors_negative = extract_color(images[0], 5, 1, None)
|
| 383 |
+
h0, s0, v0 = ImageColor.getcolor(colors_negative[0], 'HSV')
|
| 384 |
|
| 385 |
+
colors_orig = extract_color(images[1], 5, 1, None)
|
| 386 |
+
h1, s1, v1 = ImageColor.getcolor(colors_orig[0], 'HSV')
|
| 387 |
+
|
| 388 |
+
colors_positive = extract_color(images[2], 5, 1, None)
|
| 389 |
+
h2, s2, v2 = ImageColor.getcolor(colors_positive[0], 'HSV')
|
| 390 |
+
|
| 391 |
+
if h1 > hue_low and h1 < hue_high:
|
| 392 |
+
samples -= 1
|
| 393 |
+
else:
|
| 394 |
+
if (h0 > hue_low and h0 < hue_high) or (h2 > hue_low and h2 < hue_high):
|
| 395 |
+
matches += 1
|
| 396 |
|
| 397 |
+
except Exception as e:
|
| 398 |
+
print(e)
|
|
|
|
|
|
|
|
|
|
| 399 |
|
| 400 |
+
|
| 401 |
return np.round(matches / samples, 2)
|
| 402 |
|
| 403 |
|
|
|
|
| 414 |
with dnnlib.util.open_url(model_file) as f:
|
| 415 |
model = legacy.load_network_pkl(f)['G_ema'] # type: ignore
|
| 416 |
|
| 417 |
+
colors_list = ['Red', 'Orange', 'Yellow', 'Yellow Green', 'Chartreuse Green',
|
| 418 |
+
'Kelly Green', 'Green Blue Seafoam', 'Cyan Blue',
|
| 419 |
+
'Warm Blue', 'Indigo', 'Purple Magenta', 'Magenta Pink']
|
| 420 |
|
| 421 |
scores = []
|
| 422 |
+
kwargs = {'CL method':['LR', 'SVM'], 'C':[0.1, 1], 'sign':[True, False], 'num_factors':[1, 10, 20], 'cutout': [None], 'max_lambda':[18, 3], 'samples':50, 'lambda_verif':[10, 5, 3]}
|
| 423 |
|
| 424 |
for space in ['w', 'z', 's']:
|
| 425 |
print('Launching experiment with space:', space)
|
| 426 |
disentanglemnet_exp = DisentanglementBase(repo_folder, model, annotations, df, space=space, colors_list=colors_list, compute_s=False)
|
| 427 |
|
| 428 |
+
for method in ['InterFaceGAN', 'StyleSpace', 'GANSpace']:
|
| 429 |
if space != 's' and method == 'InterFaceGAN':
|
| 430 |
print('Now obtaining separation vector for using InterfaceGAN')
|
| 431 |
for met in kwargs['CL method']:
|
|
|
|
| 433 |
separation_vectors = disentanglemnet_exp.InterFaceGAN_separation_vector(method=met, C=c)
|
| 434 |
for i, color in enumerate(colors_list):
|
| 435 |
print('Generating images with variations')
|
| 436 |
+
for s in range(30):
|
| 437 |
+
seed = random.randint(0,100000)
|
| 438 |
+
for eps in kwargs['max_lambda']:
|
| 439 |
+
disentanglemnet_exp.generate_changes(seed, separation_vectors[i], min_epsilon=-eps, max_epsilon=eps, savefig=True, feature=color, method=str(method) + '_' + str(met) + '_' + str(c))
|
| 440 |
+
|
| 441 |
print('Finally obtaining verification score')
|
| 442 |
for verif in kwargs['lambda_verif']:
|
| 443 |
+
score = disentanglemnet_exp.get_verification_score(separation_vectors[i], i, samples=kwargs['samples'], lambd=verif, savefig=False, feature=color, method=method)
|
| 444 |
print('Score for method', method, 'on space', space, 'for color', color, ':', score)
|
| 445 |
|
| 446 |
+
scores.append([space, method, color, score, 'classification method:' + met + ', regularization: ' + str(c) + ', verification lambda:' + str(verif), ', '.join(list(separation_vectors[i].astype(str)))])
|
| 447 |
+
score_df = pd.DataFrame(scores, columns=['space', 'method', 'color', 'score', 'kwargs', 'vector'])
|
| 448 |
+
print(score_df)
|
| 449 |
+
score_df.to_csv(join(repo_folder, 'data/scores.csv'))
|
| 450 |
+
|
| 451 |
+
|
| 452 |
elif method == 'StyleSpace':
|
| 453 |
print('Now obtaining separation vector for using StyleSpace')
|
| 454 |
for sign in kwargs['sign']:
|
|
|
|
| 457 |
separation_vectors = disentanglemnet_exp.StyleSpace_separation_vector(sign=sign, num_factors=num_factors, cutout=cutout)
|
| 458 |
for i, color in enumerate(colors_list):
|
| 459 |
print('Generating images with variations')
|
| 460 |
+
for s in range(30):
|
| 461 |
+
seed = random.randint(0,100000)
|
| 462 |
+
for eps in kwargs['max_lambda']:
|
| 463 |
+
disentanglemnet_exp.generate_changes(seed, separation_vectors[i], min_epsilon=-eps, max_epsilon=eps, savefig=True, feature=color, method=method + '_' + str(num_factors) + '_' + str(cutout) + '_' + str(sign))
|
| 464 |
+
|
| 465 |
print('Finally obtaining verification score')
|
| 466 |
for verif in kwargs['lambda_verif']:
|
| 467 |
+
score = disentanglemnet_exp.get_verification_score(separation_vectors[i], i, samples=kwargs['samples'], lambd=verif, savefig=False, feature=color, method=method)
|
| 468 |
print('Score for method', method, 'on space', space, 'for color', color, ':', score)
|
| 469 |
|
| 470 |
+
scores.append([space, method, color, score, 'using sign:' + str(sign) + ', number of factors: ' + str(num_factors) + ', using cutout: ' + str(cutout) + ', verification lambda:' + str(verif), ', '.join(list(separation_vectors[i].astype(str)))])
|
| 471 |
+
score_df = pd.DataFrame(scores, columns=['space', 'method', 'color', 'score', 'kwargs', 'vector'])
|
| 472 |
+
print(score_df)
|
| 473 |
+
score_df.to_csv(join(repo_folder, 'data/scores.csv'))
|
| 474 |
+
|
| 475 |
if space == 'w' and method == 'GANSpace':
|
| 476 |
print('Now obtaining separation vector for using GANSpace')
|
| 477 |
separation_vectors = disentanglemnet_exp.GANSpace_separation_vectors(100)
|
| 478 |
+
for s in range(30):
|
| 479 |
print('Generating images with variations')
|
| 480 |
seed = random.randint(0,100000)
|
| 481 |
+
for i in range(100):
|
| 482 |
+
for eps in kwargs['max_lambda']:
|
| 483 |
+
disentanglemnet_exp.generate_changes(seed, separation_vectors[i], min_epsilon=-eps, max_epsilon=eps, savefig=True, feature=color, method=method)
|
| 484 |
+
|
| 485 |
+
score = None
|
| 486 |
+
scores.append([space, method, color, score, '100', ', '.join(list(separation_vectors[i].astype(str)))])
|
| 487 |
else:
|
| 488 |
print('Skipping', method, 'on space', space)
|
| 489 |
continue
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
+
score_df = pd.DataFrame(scores, columns=['space', 'method', 'color', 'score', 'kwargs', 'vector'])
|
| 494 |
print(score_df)
|
| 495 |
score_df.to_csv(join(repo_folder, 'data/scores.csv'))
|
| 496 |
|