|
|
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 words_to_numbers(sentence, word_to_number): |
|
|
|
|
|
words = sentence.lower().strip().rstrip('.').split(". ") |
|
|
|
|
|
|
|
|
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): |
|
|
|
|
|
relevant_part = os.path.basename(os.path.dirname(file_path)) |
|
|
|
|
|
|
|
|
words = relevant_part.lower().split('-') |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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(totalCorrect/totalTotal) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
word_to_number = create_word_mapping() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|