Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request for Release of Bias Calculation Code #3

Open
Kqp1227 opened this issue Jul 14, 2024 · 4 comments
Open

Request for Release of Bias Calculation Code #3

Kqp1227 opened this issue Jul 14, 2024 · 4 comments

Comments

@Kqp1227
Copy link

Kqp1227 commented Jul 14, 2024

Hi! I wanted to express my appreciation for your team's excellent work. Recently, while conducting experiments on "exp1 debiasing gender", I calculated a gender bias of "0.0546" using my own bias calculation code, which notably outperformed the "0.23±0.16" reported in your paper. This discrepancy has prompted concerns about the correctness of my implementation. Could you please consider releasing your bias calculation code for comparison and validation? It'd help me compare and verify my results. Looking forward to hearing from you. Thanks!!😊

@Kqp1227
Copy link
Author

Kqp1227 commented Jul 19, 2024

Already solved, thx!

@pkulium
Copy link

pkulium commented Nov 8, 2024

can I get the code?

@pkulium
Copy link

pkulium commented Nov 12, 2024

Already solved, thx!

can I get the code?

@Kqp1227
Copy link
Author

Kqp1227 commented Nov 19, 2024

Already solved, thx!

can I get the code?

sure, i think is this one, maybe u can have a try.

import pickle as pkl
import torch
import numpy as np
import csv


def load_results(test_results_path):
    with open(test_results_path, "rb") as f:
        results = pkl.load(f)
    return results

def process_results(face_indicators_all, gender_logits_all, race_logits_all, num_groups):
    
    bias = 0
    csv_file_path = 'male_bias_results.csv'
    with open(csv_file_path, 'w', newline='') as csvfile:
        csvwriter = csv.writer(csvfile)
        csvwriter.writerow(['Bias'])
        for i in face_indicators_all.keys():
            bias_single = 0
            freqs = np.zeros(num_groups)
            count = 0
            for j in range(len(face_indicators_all[i])):
                if face_indicators_all[i][j]:
                    count += 1
                    if num_groups == 2:  # Gender
                        # print(gender_logits_all[i][j])
                        gender_pred = torch.argmax(gender_logits_all[i][j].clone().detach()).item()
                        # print(gender_pred)
                        freqs[gender_pred] += 1
                        # print(gender_pred, freqs[gender_pred])
                    elif num_groups == 4:  # Race
                        race_pred = torch.argmax(race_logits_all[i][j].clone().detach()).item()
                        freqs[race_pred] += 1
                    elif num_groups == 8:  # Intersection of gender and race
                        gender_pred = torch.argmax(gender_logits_all[i][j].clone().detach()).item()
                        race_pred = torch.argmax(race_logits_all[i][j].clone().detach()).item()
                        intersection_group = gender_pred * 4 + race_pred
                        freqs[intersection_group] += 1
        
            if count > 0:
                freqs /= count  # Normalize frequencies

            id = 0
            for m in range(len(freqs)):
                for n in range(m):
                    # print("m:", m, "n:", n)
                    
                    bias += abs(freqs[n] - freqs[m])
                    bias_single += abs(freqs[n] - freqs[m])
                    # print(freqs[n], freqs[m])
            print("bias:", bias_single)
            csvwriter.writerow([bias_single])
            
    
    bias /= (0.5 * num_groups * (num_groups - 1))
    bias /= i

    return bias

def main(test_results_path):
    # Load the results
    face_indicators_all, face_bboxs_all, gender_logits_all, race_logits_all, age_logits_all = load_results(test_results_path)
    # Calculate biases
    gender_bias = process_results(face_indicators_all, gender_logits_all, race_logits_all, 2)
    race_bias = process_results(face_indicators_all, gender_logits_all, race_logits_all, 4)
    intersection_bias = process_results(face_indicators_all, gender_logits_all, race_logits_all, 8)
    
    print(gender_bias)
    print(race_bias)
    print(intersection_bias)
    
if __name__ == "__main__":
    test_results_path = "./outputs/base/test_results.pkl" #base
    test_results_path = "./exp_gen/test_results.pkl"
    main(test_results_path)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants