File size: 3,646 Bytes
90cd92a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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]