<jupyter_start><jupyter_text>SAA-Aggression Dataset Inference<jupyter_code>"""
Dataset      : Saag01
Epochs       : 80 (total)
Iterations   : 1361 (train), 341 (val), 190 (test)
Batch_size   : 48
Iters/Epoch  : 28

Arch         : BNInception
Modality     : ARP
""";
import os
import numpy as np
import pandas as pd
def extract_index(basename):
    index = basename.split('_')[-1]
    return index

# list all files' abspath in a directory
def abs_listdir(directory):
    directory = os.path.abspath(directory)
    return [
        os.path.join(directory, filename)
        for filename in os.listdir(directory)
    ]

# format indexing
def format_files_indexing(file_paths):
    formatted_filepaths = []
    for file_path in file_paths:
        # assuming digit is located at the end of file name
        basename, ext = os.path.splitext(file_path)
        index = extract_index(basename)
        if index.isdigit():
            formatted_filename = basename.replace(index, "{:05d}".format(int(index))) + ext

            os.rename(file_path, formatted_filename)
            formatted_filepaths.append(formatted_filename)

    return sorted(formatted_filepaths)
SCORES_DIR = 'Scores/'

SCORES_MAPDIR = {
    'SAAG01':{
        'ARP': {
            '3_segments': os.path.join('Scores/', 'saag01_bni_arp_seg_3_scores_split_0'),
            '7_segments': os.path.join('Scores/', 'saag01_bni_arp_seg_7_scores_split_0')
        },
        'Flow': {
            '3_segments': os.path.join('Scores/', 'saag01_bni_flow_seg_3_scores_split_0'),
            '7_segments': ''
        }
    }
}<jupyter_output><empty_output><jupyter_text>Approximated Rank Pooling<jupyter_code>SAAG01_ARP_3seg_dir = SCORES_MAPDIR['SAAG01']['ARP']['3_segments']  # choose the one with 3 segments
SAAG01_ARP_7seg_dir = SCORES_MAPDIR['SAAG01']['ARP']['7_segments']  # choose the one with 7 segments
# Index formatting for scores' files
# this is for sorting consistency
saag01_arp_3seg_files = format_files_indexing(abs_listdir(SAAG01_ARP_3seg_dir))
saag01_arp_7seg_files = format_files_indexing(abs_listdir(SAAG01_ARP_7seg_dir))

saag01_arp_3seg_files[10:15]
# Load labels
split_val_0_file = 'saag01_validation_split_0.txt'
label_df = pd.read_csv(split_val_0_file, 
                       delim_whitespace=True,
                       header=None,
                       names=['path', 'n_frames', 'label']
                      )

# drop last 5 items since we used droplast=True, 
# hence batchsize = 48, and valset = 341; then the remainder is 5 
labels = label_df['label'][:-5]

labels.shape
# Try one numpy score from a file
saag01_score_sample = np.load(saag01_arp_3seg_files[-1])

pd.DataFrame(saag01_score_sample).head()  # logits for two scores (before softmax)
saag01_score_sample.shape<jupyter_output><empty_output><jupyter_text>One-stream Evaluation  1. Validation results<jupyter_code># Num_segments: 3
for scorefile in saag01_arp_3seg_files:
    itername, _ = os.path.splitext(scorefile)
    itername = extract_index(itername)
    
    print("Iter: %s" % itername)
    scores = np.load(scorefile)
    pred1 = np.argmax(scores, axis=1)
    acc = np.mean(pred1 == labels)
    print("ARP Acc: %f" % acc)
# Num segments: 7
for scorefile in saag01_arp_7seg_files:
    itername, _ = os.path.splitext(scorefile)
    itername = extract_index(itername)
    
    print("Iter: %s" % itername)
    scores = np.load(scorefile)
    pred1 = np.argmax(scores, axis=1)
    acc = np.mean(pred1 == labels)
    print("ARP Acc: %f" % acc)<jupyter_output>Iter: 00028
ARP Acc: 0.750000
Iter: 00056
ARP Acc: 0.851190
Iter: 00084
ARP Acc: 0.889881
Iter: 00112
ARP Acc: 0.866071
Iter: 00140
ARP Acc: 0.872024
Iter: 00168
ARP Acc: 0.898810
Iter: 00196
ARP Acc: 0.892857
Iter: 00224
ARP Acc: 0.922619
Iter: 00252
ARP Acc: 0.928571
Iter: 00280
ARP Acc: 0.925595
Iter: 00308
ARP Acc: 0.925595
Iter: 00336
ARP Acc: 0.919643
Iter: 00364
ARP Acc: 0.925595
Iter: 00392
ARP Acc: 0.931548
Iter: 00420
ARP Acc: 0.946429
Iter: 00448
ARP Acc: 0.934524
Iter: 00476
ARP Acc: 0.922619
Iter: 00504
ARP Acc: 0.919643
Iter: 00532
ARP Acc: 0.931548
Iter: 00560
ARP Acc: 0.943452
Iter: 00588
ARP Acc: 0.952381
Iter: 00616
ARP Acc: 0.952381
Iter: 00644
ARP Acc: 0.958333
Iter: 00672
ARP Acc: 0.955357
Iter: 00700
ARP Acc: 0.949405
Iter: 00728
ARP Acc: 0.946429
Iter: 00756
ARP Acc: 0.946429
Iter: 00784
ARP Acc: 0.949405
Iter: 00812
ARP Acc: 0.952381
Iter: 00840
ARP Acc: 0.946429
Iter: 00868
ARP Acc: 0.946429
Iter: 00896
ARP Acc: 0.952381
Iter: 00924
ARP Acc: 0.946429
Iter: 0095[...]<jupyter_text>2. Test results<jupyter_code># load test labels
split_test_file = 'saag01_test_split.txt'
test_labels_df = pd.read_csv(split_test_file, 
                       delim_whitespace=True,
                       header=None,
                       names=['path', 'n_frames', 'label']
                      )
test_labels = test_labels_df['label']
# Num_segments: 3
test_score_file = os.path.join(SAAG01_ARP_3seg_dir,
                               'saag01_bni_arp_seg_3_test_scores.npy')
test_labels.shape
# Num_segments: 3
print("Test iterations")
scores = np.load(test_score_file)
pred1 = np.argmax(scores, axis=1)
acc = np.mean(pred1 == test_labels)
print("ARP Test Acc: %f" % acc)
# Num_segments: 7
test_score_file = os.path.join(SAAG01_ARP_7seg_dir,
                               'saag01_bni_arp_seg_7_test_scores.npy')
# Num_segments: 7
print("Test iterations")
scores = np.load(test_score_file)
pred1 = np.argmax(scores, axis=1)
acc = np.mean(pred1 == test_labels)
print("ARP Test Acc: %f" % acc)
inference_summary = {
    'Saa-Aggression': {
        'ARP (3 segments)': 0.957895,
        'ARP (7 segments)': 0.942105
    }
}

pd.DataFrame(inference_summary)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11167423977630199, 'fraction_numerical': 0.09629500174764069, 'mean_word_length': 3.5205371248025275, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '123404', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 2576, 'n_tokens_neox': 2167, 'n_words': 484, 'path': 'inference/Saag01 Single ARP Inference.ipynb', 'repo_name': 'iqDF/tsn-pytorch', 'size': '5722'}