m7mdal7aj commited on
Commit
8b0c813
1 Parent(s): 950fed1

Update my_model/results/evaluation.py

Browse files
Files changed (1) hide show
  1. my_model/results/evaluation.py +89 -1
my_model/results/evaluation.py CHANGED
@@ -1 +1,89 @@
1
- import pandas
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ from fuzzywuzzy import fuzz
3
+ from collections import Counter
4
+ from nltk.stem import PorterStemmer
5
+ from ast import literal_eval
6
+ from typing import Union, List
7
+ import streamlit as st
8
+
9
+ class KBVQAEvaluator:
10
+ def __init__(self):
11
+ """
12
+ Initialize the VQA Processor with the dataset and configuration settings.
13
+ """
14
+ self.data_path = 'Files/evaluation_results_final.xlsx'
15
+ self.use_fuzzy = False
16
+ self.stemmer = PorterStemmer()
17
+ self.scores_df = pd.read_excel(self.data_path, sheet_name="Scores")
18
+ self.df = pd.read_excel(self.data_path, sheet_name="Main Data")
19
+ self.vqa_scores = {}
20
+ self.exact_match_scores = {}
21
+
22
+ def stem_answers(self, answers: Union[str, List[str]]) -> Union[str, List[str]]:
23
+ """
24
+ Apply Porter Stemmer to either a single string or a list of strings.
25
+ """
26
+ if isinstance(answers, list):
27
+ return [" ".join(self.stemmer.stem(word.strip()) for word in answer.split()) for answer in answers]
28
+ else:
29
+ words = answers.split()
30
+ return " ".join(self.stemmer.stem(word.strip()) for word in words)
31
+
32
+ def calculate_vqa_score(self, ground_truths, model_answer):
33
+ """
34
+ Calculate VQA score based on the number of matching answers, with optional fuzzy matching.
35
+ """
36
+ if self.use_fuzzy:
37
+ fuzzy_matches = sum(fuzz.partial_ratio(model_answer, gt) >= 80 for gt in ground_truths)
38
+ return min(fuzzy_matches / 3, 1)
39
+ else:
40
+ count = Counter(ground_truths)
41
+ return min(count.get(model_answer, 0) / 3, 1)
42
+
43
+ def calculate_exact_match_score(self, ground_truths, model_answer):
44
+ """
45
+ Calculate Exact Match score, with optional fuzzy matching.
46
+ """
47
+ if self.use_fuzzy:
48
+ return int(any(fuzz.partial_ratio(model_answer, gt) >= 80 for gt in ground_truths))
49
+ else:
50
+ return int(model_answer in ground_truths)
51
+
52
+ def evaluate(self):
53
+ """
54
+ Process the DataFrame: stem answers, calculate scores, and store results.
55
+ """
56
+ self.df['raw_answers_stemmed'] = self.df['raw_answers'].apply(literal_eval).apply(self.stem_answers)
57
+ model_configurations = ['caption+detic', 'caption+yolov5', 'only_caption', 'only_detic', 'only_yolov5']
58
+ model_names = ['13b', '7b']
59
+
60
+ for name in model_names:
61
+ for config in model_configurations:
62
+ full_config = f'{name}_{config}'
63
+ self.df[f'{full_config}_stemmed'] = self.df[full_config].apply(self.stem_answers)
64
+
65
+ self.df[f'vqa_score_{full_config}'] = self.df.apply(lambda x: self.calculate_vqa_score(x['raw_answers_stemmed'], x[f'{full_config}_stemmed']), axis=1)
66
+ self.df[f'exact_match_score_{full_config}'] = self.df.apply(lambda x: self.calculate_exact_match_score(x['raw_answers_stemmed'], x[f'{full_config}_stemmed']), axis=1)
67
+
68
+ self.vqa_scores[full_config] = round(self.df[f'vqa_score_{full_config}'].mean()*100, 2)
69
+ self.exact_match_scores[full_config] = round(self.df[f'exact_match_score_{full_config}'].mean()*100, 2)
70
+
71
+ def save_results(self):
72
+ # Create a DataFrame for the scores
73
+ scores_data = {
74
+ 'Model Configuration': list(self.vqa_scores.keys()),
75
+ 'VQA Score': list(self.vqa_scores.values()),
76
+ 'Exact Match Score': list(self.exact_match_scores.values())
77
+ }
78
+ scores_df = pd.DataFrame(scores_data)
79
+
80
+ # Saving the scores DataFrame to an Excel file
81
+ with pd.ExcelWriter('evaluation_results_final.xlsx', engine='openpyxl', mode='w') as writer:
82
+ scores_df.to_excel(writer, sheet_name='Scores', index=False)
83
+
84
+ def run_evaluator(self):
85
+ #evaluator.evaluate()
86
+ st.table(self.scores_df)
87
+ st.dataframe(self.scores_df, width=900, height=500)
88
+ #print(evaluator.exact_match_scores)
89
+ #evaluator.save_results()