Skip to content

Commit b3185db

Browse files
authored
Merge pull request #80 from NetherlandsForensicInstitute/feature/eval-by-arch
Eval by architecture
2 parents be45190 + 4d81f58 commit b3185db

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

asmtransformers/scripts/evaluation.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def generate_triplets(dataset, pool_size, static_pool):
133133
yield {'anchor': anchors[i], 'pos': positives[i], 'negs': pool}
134134

135135

136-
def generate_test_pools(data_folder, pool_size, static_pool):
136+
def generate_test_pools(data_folder, pool_size, static_pool, architecture=None):
137137
"""order data in such a way that we can make triplets consisting of an anchor, a positive and pool_size * negative
138138
examples, then call generate_triplets() to generate said triplets
139139
:param data_folder: Path to data
@@ -143,6 +143,9 @@ def generate_test_pools(data_folder, pool_size, static_pool):
143143
return
144144
Generator yielding triplets: anchor, positive, numpy.array(negative_embeddings * POOL_SIZE)"""
145145
dataset = datasets.load_from_disk(data_folder) # .select(range(11000, 45000))
146+
if architecture:
147+
print(f'Selecting examples with architecture=={architecture}')
148+
dataset = dataset.filter(lambda x: x['architecture'] == architecture)
146149

147150
print('Adding columns')
148151
# Don't use all 3M examples because sort is really slow.
@@ -200,25 +203,26 @@ def calculate_all(test_pools, output_path, output_file):
200203
csvfile.flush()
201204

202205

203-
def run_tests(data_folder, output_path, pool_size, static_pool):
206+
def run_tests(data_folder, output_path, pool_size, static_pool, architecture):
204207
print('\ngenerate test_pools\n')
205-
test_pools = generate_test_pools(data_folder, pool_size, static_pool=static_pool)
208+
test_pools = generate_test_pools(data_folder, pool_size, static_pool=static_pool, architecture=architecture)
206209
print('\ncalculate cosine similarities\n')
207210
model_name = data_folder.split('/')[-1]
208211
output_file = f'{model_name}-{pool_size}-{static_pool}-{timestamp()}'
209212
calculate_all(test_pools, output_path, output_file)
210213
with open(os.path.join(output_path, output_file + '-parameters.txt'), 'w') as file:
211-
file.write(f'{data_folder=},\n {output_path=},\n {pool_size=},\n {static_pool=}\n')
214+
file.write(f'{data_folder=},\n {output_path=},\n {pool_size=},\n {static_pool=},\n {architecture=}\n')
212215

213216

214217
if __name__ == '__main__':
215218
parser = argparse.ArgumentParser(description='evaluation')
216219
parser.add_argument('--input-path', type=str, help='the path to the test data')
217220
parser.add_argument('--output-path', type=str, help='the path to write the final scores to')
218221
parser.add_argument('--pool-size', type=int, help='the poolsize to pick the positive example from')
222+
parser.add_argument('--architecture', type=str, help='only use examples from specified architecture')
219223
parser.add_argument(
220224
'--static-pool', action='store_true', help='keep the negatives pool or refresh for every anchor-pos pair'
221225
)
222226

223227
args = parser.parse_args()
224-
run_tests(args.input_path, args.output_path, args.pool_size, args.static_pool)
228+
run_tests(args.input_path, args.output_path, args.pool_size, args.static_pool, args.architecture)

0 commit comments

Comments
 (0)