Skip to content

Commit

Permalink
inital file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
felixrosberg committed Oct 20, 2022
1 parent 35f2d6e commit 788f5e8
Show file tree
Hide file tree
Showing 61 changed files with 6,694 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/FaceDancer.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added arcface_model/__pycache__/resnet.cpython-38.pyc
Binary file not shown.
118 changes: 118 additions & 0 deletions arcface_model/interpret_similarity_statistics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import numpy as np
import tensorflow as tf
import argparse
import os
import pandas as pd

from scipy.spatial.distance import cosine
from tensorflow.keras.models import load_model, Model

from tqdm import tqdm
from PIL import Image


def main(opt):
gpus = tf.config.experimental.list_physical_devices('GPU')
tf.config.set_visible_devices(gpus[opt.device_id], 'GPU')

blocks = ['conv5_block3_out',
'conv5_block2_out',
'conv5_block1_out',
'conv4_block6_out',
'conv4_block5_out',
'conv4_block4_out',
'conv4_block3_out',
'conv4_block2_out',
'conv4_block1_out',
'conv3_block4_out',
'conv3_block3_out',
'conv3_block2_out',
'conv3_block1_out',
'conv2_block3_out',
'conv2_block2_out',
'conv2_block1_out',
'conv1_relu'
]

arcface = load_model(opt.arcface_path)
extractor = arcface.layers[1]
extractor.summary()
extractor.trainable = False

arcface_p = Model(extractor.input, [extractor.get_layer(layer_name).output for layer_name in blocks])

data_path = opt.data_path

name = opt.arcface_path.split("/")[0] + "/"
print(name)

if not os.path.isdir(name + "block_distances/" + opt.data_name):
os.mkdir(name + "block_distances/")
os.mkdir(name + "block_distances/" + opt.data_name)

# data dicts
cos_sim_t_s = {}
cos_sim_t_c = {}
cos_sim_c_s = {}
for block in blocks:
cos_sim_t_s[block] = []
cos_sim_t_c[block] = []
cos_sim_c_s[block] = []

print("[*] Calculating perceptual similarity between targets, sources and changes...")
for file_name in tqdm(os.listdir(data_path + 'target/'), total=len(os.listdir(data_path + 'target/'))):
target = np.asarray(Image.open(data_path + 'target/' + file_name).resize((112, 112))) / 255.0
source = np.asarray(Image.open(data_path + 'source/' + file_name).resize((112, 112))) / 255.0
change = np.asarray(Image.open(data_path + 'change/' + file_name).resize((112, 112))) / 255.0

target_features = arcface_p.predict(np.expand_dims(target, axis=0))
source_features = arcface_p.predict(np.expand_dims(source, axis=0))
change_features = arcface_p.predict(np.expand_dims(change, axis=0))

for t_f, s_f, c_f, block_name in zip(target_features, source_features, change_features, blocks):
t_f = tf.reshape(t_f, (1, -1))
s_f = tf.reshape(s_f, (1, -1))
c_f = tf.reshape(c_f, (1, -1))

t_s = cosine(t_f, s_f)
t_c = cosine(t_f, c_f)
c_s = cosine(c_f, s_f)

cos_sim_t_s[block_name].append(t_s)
cos_sim_t_c[block_name].append(t_c)
cos_sim_c_s[block_name].append(c_s)

print("[*] Saving distances as data frame pickles...")
for block in blocks:
df_t_s = pd.DataFrame(cos_sim_t_s[block], columns=['cos_sim'])
df_t_s.to_pickle(name + "block_distances/" + opt.data_name + "/arcface_" + block + "_t_s.pkl")

df_t_c = pd.DataFrame(cos_sim_t_c[block], columns=['cos_sim'])
df_t_c.to_pickle(name + "block_distances/" + opt.data_name + "/arcface_" + block + "_t_c.pkl")

df_c_s = pd.DataFrame(cos_sim_c_s[block], columns=['cos_sim'])
df_c_s.to_pickle(name + "block_distances/" + opt.data_name + "/arcface_" + block + "_c_s.pkl")


if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--arcface_path', type=str, default="arcface/arc_res50.h5",
help='path to arcface model')
parser.add_argument('--data_path', type=str, default="C:/path/to/facial_recognition_data/",
help='path to data to run comparisons,'
'structure should be: data_path/change, target, source/0.png, 1.png, ... n.png')
parser.add_argument('--device_id', type=int, default=0,
help='which device to use')
parser.add_argument('--data_name', type=str, default='FF',
help='name of the data set.')

opt = parser.parse_args()

main(opt)







96 changes: 96 additions & 0 deletions arcface_model/plot_block_distances.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import pandas as pd
import numpy as np
import argparse
import os
from matplotlib import pyplot as plt


def main(opt):
pd.set_option('display.max_rows', 1000)
pd.set_option('display.max_columns', 151)

dataset = opt.data_name
name = opt.arcface_path.split("/")[0] + "/"

meta_dict = {'af': 'ArcFace', 'fn': 'FaceNet', 'l2': 'L2',
'sl2': 'squared L2', 'cos': 'cosine similarity', 'l1': 'L1'}
columns = ['cos_sim']

blocks = ['conv5_block3_out',
'conv5_block2_out',
'conv5_block1_out',
'conv4_block6_out',
'conv4_block5_out',
'conv4_block4_out',
'conv4_block3_out',
'conv4_block2_out',
'conv4_block1_out',
'conv3_block4_out',
'conv3_block3_out',
'conv3_block2_out',
'conv3_block1_out',
'conv2_block3_out',
'conv2_block2_out',
'conv2_block1_out',
'conv1_relu'
]

t_c_margins = []

if not os.path.isdir(name + "block_distances_plots/"):
os.mkdir(name + "block_distances_plots")
if not os.path.isdir(name + "block_distances_plots/" + opt.data_name):
os.mkdir(name + "block_distances_plots/" + opt.data_name)

for block in blocks:

#positive_samples = pd.read_pickle('block_distances/imagenet_pos_neg_c2t_c2s/arcface_' + block + '_distance_df_pos.pkl')
t_s = pd.read_pickle(name + 'block_distances/' + dataset + '/arcface_' + block + '_t_s.pkl')
t_c = pd.read_pickle(name + 'block_distances/' + dataset + '/arcface_' + block + '_t_c.pkl')
c_s = pd.read_pickle(name + 'block_distances/' + dataset + '/arcface_' + block + '_c_s.pkl')

print(block)
print(t_s.mean())
print(t_c.mean())
print(c_s.mean())
print('==========================================================')
print()

for col in columns:
bins = np.linspace(0, 1.5, 100)

fig = plt.figure(dpi=500)
t_s_s, _, _ = plt.hist(t_s[col].values, bins, alpha=0.5, label='y')
t_c_s, _, _ = plt.hist(t_c[col].values, bins, alpha=0.5, label='y')
c_s_s, _, _ = plt.hist(c_s[col].values, bins, alpha=0.5, label='y')
plt.legend(['t2s', 't2c', 'c2s'], loc='upper right')

plt.axvline(t_c[col].values.mean(), color='k', linestyle='dashed', linewidth=1)

min_ylim, max_ylim = plt.ylim()
plt.text(t_c[col].values.mean() * 1.1, max_ylim * 0.9, 'mean {:.2f}'.format(t_c[col].values.mean()))

plt.title('ArcFace ResNet50 - ' + block + ' feature cosine similarity distance.')
plt.xlabel('distance')
plt.ylabel('facial comparisons')
plt.savefig(name + 'block_distances_plots/' + dataset + '/' + block + '.png')
plt.close()

t_c_margins.append(t_c[col].values.mean())

np.save(name + 'block_distances_plots/' + dataset + '/perceptual_similarity_margins.npy', np.asarray(t_c_margins))


if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--arcface_path', type=str, default="arcface/arc_res50.h5",
help='path to arcface model')
parser.add_argument('--data_path', type=str, default="D:/forensic_face_swap_data/SimSwap/",
help='path to data to run comparisons,'
'structure should be: data_path/change, target, source/0.png, 1.png, ... n.png')
parser.add_argument('--data_name', type=str, default='FF',
help='name of the data set.')

opt = parser.parse_args()

main(opt)
Loading

0 comments on commit 788f5e8

Please sign in to comment.