deepfaketar / python /calculateaccuracy.py
blorg469's picture
Upload folder using huggingface_hub
90cd92a verified
import os
import json
def create_word_mapping():
word_to_number = {
"eye": 0,
"lip": 1,
"nose": 2,
"hair": 3,
"eyebrow": 4,
}
return word_to_number
# def create_word_mapping():
# word_to_number = {
# "bangs": 0,
# "beard": 1,
# "young": 2,
# "eyeglasses": 3,
# "smiling": 4,
# }
# return word_to_number
def words_to_numbers(sentence, word_to_number):
# Remove the trailing period and split the sentence into words based on ". " delimiter
words = sentence.lower().strip().rstrip('.').split(". ")
# Convert words to numbers using the mapping
numbers = [word_to_number[word] for word in words if word in word_to_number]
return numbers
def strip_path_and_map_words(file_path, word_to_number):
# Extract the relevant part of the file path
relevant_part = os.path.basename(os.path.dirname(file_path))
# Split the relevant part into words based on "-" delimiter
words = relevant_part.lower().split('-')
# Convert words to numbers using the mapping
numbers = [word_to_number[word] for word in words if word in word_to_number]
return numbers
def read_jsonl(file_path):
with open(file_path, 'r') as file:
return [json.loads(line) for line in file]
def process_files(image_jsonl_path, text_jsonl_path):
image_data = read_jsonl(image_jsonl_path)
text_data = read_jsonl(text_jsonl_path)
if len(image_data) != len(text_data):
raise ValueError("The two JSONL files must have the same number of entries.")
accuracies=[]
totalCorrect=0
totalTotal=0
accuracyC = [0,0,0,0,0]
accuracyT = [0,0,0,0,0]
for img_entry, text_entry in zip(image_data, text_data):
img_path = img_entry.get('image')
text_seq = text_entry.get('text')
if img_path is None or text_seq is None:
raise ValueError("Entries must contain 'image' and 'text' fields respectively.")
a = words_to_numbers(text_seq, word_to_number)
b = strip_path_and_map_words(img_path, word_to_number)
correct = 0
# total = len(b)
# for i in b:
# accuracyT[i]+=1
# i = 0
# while(i<len(a) and i<len(b)):
# if (a[i]==b[i]):
# accuracyC[b[i]]+=1
# correct+=1
# i+=1
#//////////////////////////////////
i=0
total=len(a)
for i in a:
for j in b:
if (i==j):
accuracyC[i]+=1
correct+=1
break
accuracyT[i]+=1
if (total==0): continue
accuracies.append(correct/total)
totalCorrect+=correct
totalTotal+=total
for i in range(5):
print(f"{accuracyC[i]/accuracyT[i]} ", end ="")
print()
# print(len(accuracies))
# print(accuracies)
print(totalCorrect/totalTotal)
if __name__ == "__main__":
word_to_number = create_word_mapping()
# sentence = "Eyeglasses. Bangs. Smiling."
# result = words_to_numbers(sentence, word_to_number)
# file_path = "/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Bangs-Smiling/007240.jpg"
# result2 = strip_path_and_map_words(file_path, word_to_number)
image_jsonl_path = "/mnt/user/myang/LLaVA-2/my_code/json/llavatestquestions5.jsonl"
text_jsonl_path = "/mnt/user/myang/LLaVA-2/my_code/json/llavafinetune13b5epochlorafctestv4"
process_files(image_jsonl_path, text_jsonl_path)
# print(result2) # Output: [1, 0]