diff --git a/scripts/GPT4V.py b/scripts/GPT4V.py index 07ef1c7..1048ae5 100644 --- a/scripts/GPT4V.py +++ b/scripts/GPT4V.py @@ -23,7 +23,7 @@ def save_json(json_list,save_path): json.dump(json_list, file,indent=4) def _get_args(): parser = ArgumentParser() - parser.add_argument("--image_folder", type=str, default="./data") + parser.add_argument("--image_folder", type=str, default="./OCRBench_Images") parser.add_argument("--output_path", type=str, default="./results") parser.add_argument("--OCRBench_file", type=str, default="./OCRBench/OCRBench.json") parser.add_argument("--OPENAI_API_KEY", type=str, default="") diff --git a/scripts/Genimi.py b/scripts/Genimi.py index 9942f16..638e76b 100644 --- a/scripts/Genimi.py +++ b/scripts/Genimi.py @@ -21,7 +21,7 @@ def save_json(json_list,save_path): json.dump(json_list, file,indent=4) def _get_args(): parser = ArgumentParser() - parser.add_argument("--image_folder", type=str, default="./data") + parser.add_argument("--image_folder", type=str, default="./OCRBench_Images") parser.add_argument("--output_path", type=str, default="./results") parser.add_argument("--OCRBench_file", type=str, default="./OCRBench/OCRBench.json") parser.add_argument("--GOOGLE_API_KEY", type=str, default="") diff --git a/scripts/LLaVA1_5.py b/scripts/LLaVA1_5.py index 33042a3..2f87273 100644 --- a/scripts/LLaVA1_5.py +++ b/scripts/LLaVA1_5.py @@ -32,7 +32,7 @@ def save_json(json_list,save_path): def _get_args(): parser = ArgumentParser() - parser.add_argument("--image_folder", type=str, default="./data") + parser.add_argument("--image_folder", type=str, default="./OCRBench_Images") parser.add_argument("--output_folder", type=str, default="./results") parser.add_argument("--OCRBench_file", type=str, default="./OCRBench/OCRBench.json") parser.add_argument("--model_path", type=str, default="liuhaotian/llava-v1.5-7b") diff --git a/scripts/blip2.py b/scripts/blip2.py index 89005dc..c709f6c 100644 --- a/scripts/blip2.py +++ b/scripts/blip2.py @@ -28,7 +28,7 @@ def save_json(json_list,save_path): def _get_args(): parser = ArgumentParser() - parser.add_argument("--image_folder", type=str, default="./data") + parser.add_argument("--image_folder", type=str, default="./OCRBench_Images") parser.add_argument("--output_folder", type=str, default="./results") parser.add_argument("--OCRBench_file", type=str, default="./OCRBench/OCRBench.json") parser.add_argument("--model_path", type=str, default="./model_weights/blip2-opt-6.7b") diff --git a/scripts/blip2_vicuna_instruct.py b/scripts/blip2_vicuna_instruct.py index 45d5cbd..59c14e6 100644 --- a/scripts/blip2_vicuna_instruct.py +++ b/scripts/blip2_vicuna_instruct.py @@ -27,7 +27,7 @@ def save_json(json_list,save_path): def _get_args(): parser = ArgumentParser() - parser.add_argument("--image_folder", type=str, default="./data") + parser.add_argument("--image_folder", type=str, default="./OCRBench_Images") parser.add_argument("--output_folder", type=str, default="./results") parser.add_argument("--OCRBench_file", type=str, default="./OCRBench/OCRBench.json") parser.add_argument("--model_path", type=str, default="./model_weights/instructblip-vicuna-7b") diff --git a/scripts/bliva.py b/scripts/bliva.py index c76a0fe..4058689 100644 --- a/scripts/bliva.py +++ b/scripts/bliva.py @@ -36,7 +36,7 @@ def save_json(json_list,save_path): def _get_args(): parser = ArgumentParser() - parser.add_argument("--image_folder", type=str, default="./data") + parser.add_argument("--image_folder", type=str, default="./OCRBench_Images") parser.add_argument("--output_folder", type=str, default="./results") parser.add_argument("--OCRBench_file", type=str, default="./OCRBench/OCRBench.json") parser.add_argument("--model_path", type=str, default="bliva_vicuna") diff --git a/scripts/interlm.py b/scripts/interlm.py new file mode 100644 index 0000000..85eed5e --- /dev/null +++ b/scripts/interlm.py @@ -0,0 +1,161 @@ +import json +from argparse import ArgumentParser +import torch +import os +import json +from tqdm import tqdm +from PIL import Image +import math +import multiprocessing +from multiprocessing import Pool, Queue, Manager +from transformers import AutoModel, AutoTokenizer +# https://github.com/InternLM/InternLM-XComposer/tree/main/InternLM-XComposer-1.0 +def split_list(lst, n): + length = len(lst) + avg = length // n # 每份的大小 + result = [] # 存储分割后的子列表 + for i in range(n - 1): + result.append(lst[i*avg:(i+1)*avg]) + result.append(lst[(n-1)*avg:]) + return result + +def save_json(json_list,save_path): + with open(save_path, 'w') as file: + json.dump(json_list, file,indent=4) + +def _get_args(): + parser = ArgumentParser() + parser.add_argument("--image_folder", type=str, default="./OCRBench_Images") + parser.add_argument("--output_folder", type=str, default="./results") + parser.add_argument("--OCRBench_file", type=str, default="./OCRBench/OCRBench.json") + parser.add_argument("--model_path", type=str, default='internlm/internlm-xcomposer-7b')#TODO Set the address of your model's weights + parser.add_argument("--save_name", type=str, default="internlm-xcomposer-7b") #TODO Set the name of the JSON file you save in the output_folder. + parser.add_argument("--num_workers", type=int, default=1) + args = parser.parse_args() + return args + +OCRBench_score = {"Regular Text Recognition":0,"Irregular Text Recognition":0,"Artistic Text Recognition":0,"Handwriting Recognition":0, +"Digit String Recognition":0,"Non-Semantic Text Recognition":0,"Scene Text-centric VQA":0,"Doc-oriented VQA":0,"Doc-oriented VQA":0, +"Key Information Extraction":0,"Handwritten Mathematical Expression Recognition":0} +AllDataset_score = {"IIIT5K":0,"svt":0,"IC13_857":0,"IC15_1811":0,"svtp":0,"ct80":0,"cocotext":0,"ctw":0,"totaltext":0,"HOST":0,"WOST":0,"WordArt":0,"IAM":0,"ReCTS":0,"ORAND":0,"NonSemanticText":0,"SemanticText":0, +"STVQA":0,"textVQA":0,"ocrVQA":0,"ESTVQA":0,"ESTVQA_cn":0,"docVQA":0,"infographicVQA":0,"ChartQA":0,"ChartQA_Human":0,"FUNSD":0,"SROIE":0,"POIE":0,"HME100k":0} +num_all = {"IIIT5K":0,"svt":0,"IC13_857":0,"IC15_1811":0,"svtp":0,"ct80":0,"cocotext":0,"ctw":0,"totaltext":0,"HOST":0,"WOST":0,"WordArt":0,"IAM":0,"ReCTS":0,"ORAND":0,"NonSemanticText":0,"SemanticText":0, +"STVQA":0,"textVQA":0,"ocrVQA":0,"ESTVQA":0,"ESTVQA_cn":0,"docVQA":0,"infographicVQA":0,"ChartQA":0,"ChartQA_Human":0,"FUNSD":0,"SROIE":0,"POIE":0,"HME100k":0} + +def eval_worker(args, data, eval_id, output_queue): + print(f"Process {eval_id} start.") + checkpoint = args.model_path + + torch.set_grad_enabled(False) + + # init model and tokenizer + model = AutoModel.from_pretrained(checkpoint, trust_remote_code=True).cuda().eval() + tokenizer = AutoTokenizer.from_pretrained(checkpoint, trust_remote_code=True) + model.tokenizer = tokenizer + + for i in tqdm(range(len(data))): + img_path = os.path.join(args.image_folder, data[i]['image_path']) + qs = data[i]['question'] + response = model.generate(qs, img_path) + data[i]['predict'] = response + output_queue.put({eval_id: data}) + print(f"Process {eval_id} has completed.") + +if __name__=="__main__": + multiprocessing.set_start_method('spawn') + args = _get_args() + + if os.path.exists(os.path.join(args.output_folder,f"{args.save_name}.json")): + data_path = os.path.join(args.output_folder,f"{args.save_name}.json") + print(f"output_path:{data_path} exist! Only generate the results that were not generated in {data_path}.") + else: + data_path = args.OCRBench_file + + with open(data_path, "r") as f: + data = json.load(f) + + data_list = split_list(data, args.num_workers) + + output_queue = Manager().Queue() + + pool = Pool(processes=args.num_workers) + for i in range(len(data_list)): + pool.apply_async(eval_worker, args=(args, data_list[i], i, output_queue)) + pool.close() + pool.join() + + results = {} + while not output_queue.empty(): + result = output_queue.get() + results.update(result) + data = [] + for i in range(len(data_list)): + data.extend(results[i]) + + for i in range(len(data)): + data_type = data[i]["type"] + dataset_name = data[i]["dataset_name"] + answers = data[i]["answers"] + if data[i].get('predict',0)==0: + continue + predict = data[i]['predict'] + data[i]['result'] = 0 + if dataset_name == "HME100k": + if type(answers)==list: + for j in range(len(answers)): + answer = answers[j].strip().replace("\n"," ").replace(" ","") + predict = predict.strip().replace("\n"," ").replace(" ","") + if answer in predict: + data[i]['result'] = 1 + else: + answers = answers.strip().replace("\n"," ").replace(" ","") + predict = predict.strip().replace("\n"," ").replace(" ","") + if answers in predict: + data[i]['result'] = 1 + else: + if type(answers)==list: + for j in range(len(answers)): + answer = answers[j].lower().strip().replace("\n"," ") + predict = predict.lower().strip().replace("\n"," ") + if answer in predict: + data[i]['result'] = 1 + else: + answers = answers.lower().strip().replace("\n"," ") + predict = predict.lower().strip().replace("\n"," ") + if answers in predict: + data[i]['result'] = 1 + save_json(data, os.path.join(args.output_folder,f"{args.save_name}.json")) + if len(data)==1000: + for i in range(len(data)): + if data[i].get("result",100)==100: + continue + OCRBench_score[data[i]['type']] += data[i]['result'] + recognition_score = OCRBench_score['Regular Text Recognition']+OCRBench_score['Irregular Text Recognition']+OCRBench_score['Artistic Text Recognition']+OCRBench_score['Handwriting Recognition']+OCRBench_score['Digit String Recognition']+OCRBench_score['Non-Semantic Text Recognition'] + Final_score = recognition_score+OCRBench_score['Scene Text-centric VQA']+OCRBench_score['Doc-oriented VQA']+OCRBench_score['Key Information Extraction']+OCRBench_score['Handwritten Mathematical Expression Recognition'] + print("###########################OCRBench##############################") + print(f"Text Recognition(Total 300):{recognition_score}") + print("------------------Details of Recognition Score-------------------") + print(f"Regular Text Recognition(Total 50): {OCRBench_score['Regular Text Recognition']}") + print(f"Irregular Text Recognition(Total 50): {OCRBench_score['Irregular Text Recognition']}") + print(f"Artistic Text Recognition(Total 50): {OCRBench_score['Artistic Text Recognition']}") + print(f"Handwriting Recognition(Total 50): {OCRBench_score['Handwriting Recognition']}") + print(f"Digit String Recognition(Total 50): {OCRBench_score['Digit String Recognition']}") + print(f"Non-Semantic Text Recognition(Total 50): {OCRBench_score['Non-Semantic Text Recognition']}") + print("----------------------------------------------------------------") + print(f"Scene Text-centric VQA(Total 200): {OCRBench_score['Scene Text-centric VQA']}") + print("----------------------------------------------------------------") + print(f"Doc-oriented VQA(Total 200): {OCRBench_score['Doc-oriented VQA']}") + print("----------------------------------------------------------------") + print(f"Key Information Extraction(Total 200): {OCRBench_score['Key Information Extraction']}") + print("----------------------------------------------------------------") + print(f"Handwritten Mathematical Expression Recognition(Total 100): {OCRBench_score['Handwritten Mathematical Expression Recognition']}") + print("----------------------Final Score-------------------------------") + print(f"Final Score(Total 1000): {Final_score}") + else: + for i in range(len(data)): + num_all[data[i]['dataset_name']] += 1 + if data[i].get("result",100)==100: + continue + AllDataset_score[data[i]['dataset_name']] += data[i]['result'] + for key in AllDataset_score.keys(): + print(f"{key}: {AllDataset_score[key]/float(num_all[key])}") diff --git a/scripts/interlm2.py b/scripts/interlm2.py new file mode 100644 index 0000000..7b920bd --- /dev/null +++ b/scripts/interlm2.py @@ -0,0 +1,162 @@ +import json +from argparse import ArgumentParser +import torch +import os +import json +from tqdm import tqdm +from PIL import Image +import math +import multiprocessing +from multiprocessing import Pool, Queue, Manager +from transformers import AutoModel, AutoTokenizer +#https://github.com/InternLM/InternLM-XComposer/tree/main + +def split_list(lst, n): + length = len(lst) + avg = length // n # 每份的大小 + result = [] # 存储分割后的子列表 + for i in range(n - 1): + result.append(lst[i*avg:(i+1)*avg]) + result.append(lst[(n-1)*avg:]) + return result + +def save_json(json_list,save_path): + with open(save_path, 'w') as file: + json.dump(json_list, file,indent=4) + +def _get_args(): + parser = ArgumentParser() + parser.add_argument("--image_folder", type=str, default="./OCRBench_Images") + parser.add_argument("--output_folder", type=str, default="./results") + parser.add_argument("--OCRBench_file", type=str, default="./OCRBench/OCRBench.json") + parser.add_argument("--model_path", type=str, default='internlm/internlm-xcomposer2-vl-7b')#TODO Set the address of your model's weights + parser.add_argument("--save_name", type=str, default="internlm-xcomposer2-vl-7b") #TODO Set the name of the JSON file you save in the output_folder. + parser.add_argument("--num_workers", type=int, default=1) + args = parser.parse_args() + return args + +OCRBench_score = {"Regular Text Recognition":0,"Irregular Text Recognition":0,"Artistic Text Recognition":0,"Handwriting Recognition":0, +"Digit String Recognition":0,"Non-Semantic Text Recognition":0,"Scene Text-centric VQA":0,"Doc-oriented VQA":0,"Doc-oriented VQA":0, +"Key Information Extraction":0,"Handwritten Mathematical Expression Recognition":0} +AllDataset_score = {"IIIT5K":0,"svt":0,"IC13_857":0,"IC15_1811":0,"svtp":0,"ct80":0,"cocotext":0,"ctw":0,"totaltext":0,"HOST":0,"WOST":0,"WordArt":0,"IAM":0,"ReCTS":0,"ORAND":0,"NonSemanticText":0,"SemanticText":0, +"STVQA":0,"textVQA":0,"ocrVQA":0,"ESTVQA":0,"ESTVQA_cn":0,"docVQA":0,"infographicVQA":0,"ChartQA":0,"ChartQA_Human":0,"FUNSD":0,"SROIE":0,"POIE":0,"HME100k":0} +num_all = {"IIIT5K":0,"svt":0,"IC13_857":0,"IC15_1811":0,"svtp":0,"ct80":0,"cocotext":0,"ctw":0,"totaltext":0,"HOST":0,"WOST":0,"WordArt":0,"IAM":0,"ReCTS":0,"ORAND":0,"NonSemanticText":0,"SemanticText":0, +"STVQA":0,"textVQA":0,"ocrVQA":0,"ESTVQA":0,"ESTVQA_cn":0,"docVQA":0,"infographicVQA":0,"ChartQA":0,"ChartQA_Human":0,"FUNSD":0,"SROIE":0,"POIE":0,"HME100k":0} + +def eval_worker(args, data, eval_id, output_queue): + print(f"Process {eval_id} start.") + checkpoint = args.model_path + torch.set_grad_enabled(False) + + # init model and tokenizer + model = AutoModel.from_pretrained(checkpoint, trust_remote_code=True).cuda().eval() + tokenizer = AutoTokenizer.from_pretrained(checkpoint, trust_remote_code=True) + + for i in tqdm(range(len(data))): + img_path = os.path.join(args.image_folder, data[i]['image_path']) + qs = data[i]['question'] + text = f'{qs}' + with torch.cuda.amp.autocast(): + response, _ = model.chat(tokenizer, query=text, image=img_path, history=[], do_sample=False) + data[i]['predict'] = response + output_queue.put({eval_id: data}) + print(f"Process {eval_id} has completed.") + +if __name__=="__main__": + multiprocessing.set_start_method('spawn') + args = _get_args() + + if os.path.exists(os.path.join(args.output_folder,f"{args.save_name}.json")): + data_path = os.path.join(args.output_folder,f"{args.save_name}.json") + print(f"output_path:{data_path} exist! Only generate the results that were not generated in {data_path}.") + else: + data_path = args.OCRBench_file + + with open(data_path, "r") as f: + data = json.load(f) + + data_list = split_list(data, args.num_workers) + + output_queue = Manager().Queue() + + pool = Pool(processes=args.num_workers) + for i in range(len(data_list)): + pool.apply_async(eval_worker, args=(args, data_list[i], i, output_queue)) + pool.close() + pool.join() + + results = {} + while not output_queue.empty(): + result = output_queue.get() + results.update(result) + data = [] + for i in range(len(data_list)): + data.extend(results[i]) + + for i in range(len(data)): + data_type = data[i]["type"] + dataset_name = data[i]["dataset_name"] + answers = data[i]["answers"] + if data[i].get('predict',0)==0: + continue + predict = data[i]['predict'] + data[i]['result'] = 0 + if dataset_name == "HME100k": + if type(answers)==list: + for j in range(len(answers)): + answer = answers[j].strip().replace("\n"," ").replace(" ","") + predict = predict.strip().replace("\n"," ").replace(" ","") + if answer in predict: + data[i]['result'] = 1 + else: + answers = answers.strip().replace("\n"," ").replace(" ","") + predict = predict.strip().replace("\n"," ").replace(" ","") + if answers in predict: + data[i]['result'] = 1 + else: + if type(answers)==list: + for j in range(len(answers)): + answer = answers[j].lower().strip().replace("\n"," ") + predict = predict.lower().strip().replace("\n"," ") + if answer in predict: + data[i]['result'] = 1 + else: + answers = answers.lower().strip().replace("\n"," ") + predict = predict.lower().strip().replace("\n"," ") + if answers in predict: + data[i]['result'] = 1 + save_json(data, os.path.join(args.output_folder,f"{args.save_name}.json")) + if len(data)==1000: + for i in range(len(data)): + if data[i].get("result",100)==100: + continue + OCRBench_score[data[i]['type']] += data[i]['result'] + recognition_score = OCRBench_score['Regular Text Recognition']+OCRBench_score['Irregular Text Recognition']+OCRBench_score['Artistic Text Recognition']+OCRBench_score['Handwriting Recognition']+OCRBench_score['Digit String Recognition']+OCRBench_score['Non-Semantic Text Recognition'] + Final_score = recognition_score+OCRBench_score['Scene Text-centric VQA']+OCRBench_score['Doc-oriented VQA']+OCRBench_score['Key Information Extraction']+OCRBench_score['Handwritten Mathematical Expression Recognition'] + print("###########################OCRBench##############################") + print(f"Text Recognition(Total 300):{recognition_score}") + print("------------------Details of Recognition Score-------------------") + print(f"Regular Text Recognition(Total 50): {OCRBench_score['Regular Text Recognition']}") + print(f"Irregular Text Recognition(Total 50): {OCRBench_score['Irregular Text Recognition']}") + print(f"Artistic Text Recognition(Total 50): {OCRBench_score['Artistic Text Recognition']}") + print(f"Handwriting Recognition(Total 50): {OCRBench_score['Handwriting Recognition']}") + print(f"Digit String Recognition(Total 50): {OCRBench_score['Digit String Recognition']}") + print(f"Non-Semantic Text Recognition(Total 50): {OCRBench_score['Non-Semantic Text Recognition']}") + print("----------------------------------------------------------------") + print(f"Scene Text-centric VQA(Total 200): {OCRBench_score['Scene Text-centric VQA']}") + print("----------------------------------------------------------------") + print(f"Doc-oriented VQA(Total 200): {OCRBench_score['Doc-oriented VQA']}") + print("----------------------------------------------------------------") + print(f"Key Information Extraction(Total 200): {OCRBench_score['Key Information Extraction']}") + print("----------------------------------------------------------------") + print(f"Handwritten Mathematical Expression Recognition(Total 100): {OCRBench_score['Handwritten Mathematical Expression Recognition']}") + print("----------------------Final Score-------------------------------") + print(f"Final Score(Total 1000): {Final_score}") + else: + for i in range(len(data)): + num_all[data[i]['dataset_name']] += 1 + if data[i].get("result",100)==100: + continue + AllDataset_score[data[i]['dataset_name']] += data[i]['result'] + for key in AllDataset_score.keys(): + print(f"{key}: {AllDataset_score[key]/float(num_all[key])}") diff --git a/scripts/intervl.py b/scripts/intervl.py new file mode 100644 index 0000000..92a6c55 --- /dev/null +++ b/scripts/intervl.py @@ -0,0 +1,178 @@ +import json +from argparse import ArgumentParser +import torch +import os +import json +from tqdm import tqdm +from PIL import Image +import math +import multiprocessing +from multiprocessing import Pool, Queue, Manager +from PIL import Image +from transformers import AutoModel, CLIPImageProcessor +from transformers import AutoTokenizer + +#https://github.com/OpenGVLab/InternVL + +def split_list(lst, n): + length = len(lst) + avg = length // n # 每份的大小 + result = [] # 存储分割后的子列表 + for i in range(n - 1): + result.append(lst[i*avg:(i+1)*avg]) + result.append(lst[(n-1)*avg:]) + return result + +def save_json(json_list,save_path): + with open(save_path, 'w') as file: + json.dump(json_list, file,indent=4) + +def _get_args(): + parser = ArgumentParser() + parser.add_argument("--image_folder", type=str, default="./OCRBench_Images") + parser.add_argument("--output_folder", type=str, default="./results") + parser.add_argument("--OCRBench_file", type=str, default="./OCRBench/OCRBench.json") + parser.add_argument("--model_path", type=str, default='OpenGVLab/InternVL-Chat-Chinese-V1-1')#TODO Set the address of your model's weights + parser.add_argument("--save_name", type=str, default="InternVL-Chat-Chinese-V1-1") #TODO Set the name of the JSON file you save in the output_folder. + parser.add_argument("--num_workers", type=int, default=1) + args = parser.parse_args() + return args + +OCRBench_score = {"Regular Text Recognition":0,"Irregular Text Recognition":0,"Artistic Text Recognition":0,"Handwriting Recognition":0, +"Digit String Recognition":0,"Non-Semantic Text Recognition":0,"Scene Text-centric VQA":0,"Doc-oriented VQA":0,"Doc-oriented VQA":0, +"Key Information Extraction":0,"Handwritten Mathematical Expression Recognition":0} +AllDataset_score = {"IIIT5K":0,"svt":0,"IC13_857":0,"IC15_1811":0,"svtp":0,"ct80":0,"cocotext":0,"ctw":0,"totaltext":0,"HOST":0,"WOST":0,"WordArt":0,"IAM":0,"ReCTS":0,"ORAND":0,"NonSemanticText":0,"SemanticText":0, +"STVQA":0,"textVQA":0,"ocrVQA":0,"ESTVQA":0,"ESTVQA_cn":0,"docVQA":0,"infographicVQA":0,"ChartQA":0,"ChartQA_Human":0,"FUNSD":0,"SROIE":0,"POIE":0,"HME100k":0} +num_all = {"IIIT5K":0,"svt":0,"IC13_857":0,"IC15_1811":0,"svtp":0,"ct80":0,"cocotext":0,"ctw":0,"totaltext":0,"HOST":0,"WOST":0,"WordArt":0,"IAM":0,"ReCTS":0,"ORAND":0,"NonSemanticText":0,"SemanticText":0, +"STVQA":0,"textVQA":0,"ocrVQA":0,"ESTVQA":0,"ESTVQA_cn":0,"docVQA":0,"infographicVQA":0,"ChartQA":0,"ChartQA_Human":0,"FUNSD":0,"SROIE":0,"POIE":0,"HME100k":0} + +def eval_worker(args, data, eval_id, output_queue): + print(f"Process {eval_id} start.") + checkpoint = args.model_path + model = AutoModel.from_pretrained( + checkpoint, + torch_dtype=torch.bfloat16, + low_cpu_mem_usage=True, + trust_remote_code=True, + device_map='cuda').eval() + + tokenizer = AutoTokenizer.from_pretrained(checkpoint) + + for i in tqdm(range(len(data))): + img_path = os.path.join(args.image_folder, data[i]['image_path']) + qs = data[i]['question'] + image = Image.open(img_path).convert('RGB') + image = image.resize((448, 448)) + image_processor = CLIPImageProcessor.from_pretrained(checkpoint) + + pixel_values = image_processor(images=image, return_tensors='pt').pixel_values + pixel_values = pixel_values.to(torch.bfloat16).cuda() + + generation_config = dict( + num_beams=1, + max_new_tokens=512, + do_sample=False, + ) + response = model.chat(tokenizer, pixel_values, qs, generation_config) + data[i]['predict'] = response + output_queue.put({eval_id: data}) + print(f"Process {eval_id} has completed.") + +if __name__=="__main__": + multiprocessing.set_start_method('spawn') + args = _get_args() + + if os.path.exists(os.path.join(args.output_folder,f"{args.save_name}.json")): + data_path = os.path.join(args.output_folder,f"{args.save_name}.json") + print(f"output_path:{data_path} exist! Only generate the results that were not generated in {data_path}.") + else: + data_path = args.OCRBench_file + + with open(data_path, "r") as f: + data = json.load(f) + + data_list = split_list(data, args.num_workers) + + output_queue = Manager().Queue() + + pool = Pool(processes=args.num_workers) + for i in range(len(data_list)): + pool.apply_async(eval_worker, args=(args, data_list[i], i, output_queue)) + pool.close() + pool.join() + + results = {} + while not output_queue.empty(): + result = output_queue.get() + results.update(result) + data = [] + for i in range(len(data_list)): + data.extend(results[i]) + + for i in range(len(data)): + data_type = data[i]["type"] + dataset_name = data[i]["dataset_name"] + answers = data[i]["answers"] + if data[i].get('predict',0)==0: + continue + predict = data[i]['predict'] + data[i]['result'] = 0 + if dataset_name == "HME100k": + if type(answers)==list: + for j in range(len(answers)): + answer = answers[j].strip().replace("\n"," ").replace(" ","") + predict = predict.strip().replace("\n"," ").replace(" ","") + if answer in predict: + data[i]['result'] = 1 + else: + answers = answers.strip().replace("\n"," ").replace(" ","") + predict = predict.strip().replace("\n"," ").replace(" ","") + if answers in predict: + data[i]['result'] = 1 + else: + if type(answers)==list: + for j in range(len(answers)): + answer = answers[j].lower().strip().replace("\n"," ") + predict = predict.lower().strip().replace("\n"," ") + if answer in predict: + data[i]['result'] = 1 + else: + answers = answers.lower().strip().replace("\n"," ") + predict = predict.lower().strip().replace("\n"," ") + if answers in predict: + data[i]['result'] = 1 + save_json(data, os.path.join(args.output_folder,f"{args.save_name}.json")) + if len(data)==1000: + for i in range(len(data)): + if data[i].get("result",100)==100: + continue + OCRBench_score[data[i]['type']] += data[i]['result'] + recognition_score = OCRBench_score['Regular Text Recognition']+OCRBench_score['Irregular Text Recognition']+OCRBench_score['Artistic Text Recognition']+OCRBench_score['Handwriting Recognition']+OCRBench_score['Digit String Recognition']+OCRBench_score['Non-Semantic Text Recognition'] + Final_score = recognition_score+OCRBench_score['Scene Text-centric VQA']+OCRBench_score['Doc-oriented VQA']+OCRBench_score['Key Information Extraction']+OCRBench_score['Handwritten Mathematical Expression Recognition'] + print("###########################OCRBench##############################") + print(f"Text Recognition(Total 300):{recognition_score}") + print("------------------Details of Recognition Score-------------------") + print(f"Regular Text Recognition(Total 50): {OCRBench_score['Regular Text Recognition']}") + print(f"Irregular Text Recognition(Total 50): {OCRBench_score['Irregular Text Recognition']}") + print(f"Artistic Text Recognition(Total 50): {OCRBench_score['Artistic Text Recognition']}") + print(f"Handwriting Recognition(Total 50): {OCRBench_score['Handwriting Recognition']}") + print(f"Digit String Recognition(Total 50): {OCRBench_score['Digit String Recognition']}") + print(f"Non-Semantic Text Recognition(Total 50): {OCRBench_score['Non-Semantic Text Recognition']}") + print("----------------------------------------------------------------") + print(f"Scene Text-centric VQA(Total 200): {OCRBench_score['Scene Text-centric VQA']}") + print("----------------------------------------------------------------") + print(f"Doc-oriented VQA(Total 200): {OCRBench_score['Doc-oriented VQA']}") + print("----------------------------------------------------------------") + print(f"Key Information Extraction(Total 200): {OCRBench_score['Key Information Extraction']}") + print("----------------------------------------------------------------") + print(f"Handwritten Mathematical Expression Recognition(Total 100): {OCRBench_score['Handwritten Mathematical Expression Recognition']}") + print("----------------------Final Score-------------------------------") + print(f"Final Score(Total 1000): {Final_score}") + else: + for i in range(len(data)): + num_all[data[i]['dataset_name']] += 1 + if data[i].get("result",100)==100: + continue + AllDataset_score[data[i]['dataset_name']] += data[i]['result'] + for key in AllDataset_score.keys(): + print(f"{key}: {AllDataset_score[key]/float(num_all[key])}") diff --git a/scripts/llavar.py b/scripts/llavar.py index fa53ede..4298936 100644 --- a/scripts/llavar.py +++ b/scripts/llavar.py @@ -71,7 +71,7 @@ def save_json(json_list,save_path): def _get_args(): parser = ArgumentParser() - parser.add_argument("--image_folder", type=str, default="./data") + parser.add_argument("--image_folder", type=str, default="./OCRBench_Images") parser.add_argument("--output_folder", type=str, default="./results") parser.add_argument("--OCRBench_file", type=str, default="./OCRBench/OCRBench.json") parser.add_argument("--model_path", type=str, default="./model_weights/LLaVar") diff --git a/scripts/mPLUG-owl.py b/scripts/mPLUG-owl.py index 8c7173d..567b76b 100644 --- a/scripts/mPLUG-owl.py +++ b/scripts/mPLUG-owl.py @@ -31,7 +31,7 @@ def save_json(json_list,save_path): def _get_args(): parser = ArgumentParser() - parser.add_argument("--image_folder", type=str, default="./data") + parser.add_argument("--image_folder", type=str, default="./OCRBench_Images") parser.add_argument("--output_folder", type=str, default="./results") parser.add_argument("--OCRBench_file", type=str, default="./OCRBench/OCRBench.json") parser.add_argument("--model_path", type=str, default="./model_weights/mplug-owl") diff --git a/scripts/mPLUG-owl2.py b/scripts/mPLUG-owl2.py index d3e0a61..0bb5db6 100644 --- a/scripts/mPLUG-owl2.py +++ b/scripts/mPLUG-owl2.py @@ -31,7 +31,7 @@ def save_json(json_list,save_path): def _get_args(): parser = ArgumentParser() - parser.add_argument("--image_folder", type=str, default="./data") + parser.add_argument("--image_folder", type=str, default="./OCRBench_Images") parser.add_argument("--output_folder", type=str, default="./results") parser.add_argument("--OCRBench_file", type=str, default="./OCRBench/OCRBench.json") parser.add_argument("--model_path", type=str, default="./model_weights/mplug-owl2") diff --git a/scripts/minigpt4v2.py b/scripts/minigpt4v2.py index 1c0b872..bb0da78 100644 --- a/scripts/minigpt4v2.py +++ b/scripts/minigpt4v2.py @@ -33,7 +33,7 @@ def save_json(json_list,save_path): def _get_args(): parser = ArgumentParser() - parser.add_argument("--image_folder", type=str, default="./data") + parser.add_argument("--image_folder", type=str, default="./OCRBench_Images") parser.add_argument("--output_folder", type=str, default="./results") parser.add_argument("--OCRBench_file", type=str, default="./OCRBench/OCRBench.json") parser.add_argument("--cfg-path", default='./scripts/MiniGPT-4/eval_configs/minigptv2_eval.yaml') diff --git a/scripts/monkey.py b/scripts/monkey.py index c7b334e..9224c11 100644 --- a/scripts/monkey.py +++ b/scripts/monkey.py @@ -28,7 +28,7 @@ def save_json(json_list,save_path): def _get_args(): parser = ArgumentParser() - parser.add_argument("--image_folder", type=str, default="./data") + parser.add_argument("--image_folder", type=str, default="./OCRBench_Images") parser.add_argument("--output_folder", type=str, default="./results") parser.add_argument("--OCRBench_file", type=str, default="./OCRBench/OCRBench.json") parser.add_argument("--model_path", type=str, default="echo840/Monkey")