boostcampaitech4lv23nlp1 / final-project-level3-nlp-03

Multi-Modal Model for DocVQA(Document Visual Question Answering)
3 stars 0 forks source link

Cases that OCR results don't include ground truth #9

Closed chanmuzi closed 1 year ago

chanmuzi commented 1 year ago

배경

문제점

사례 1 (snbx0223_14.png)

사례 2 (hyhk0037_6.png)

접근 방법

  1. OCR 성능을 개선하여 OCR results에 ground truth를 포함할 수 있도록 한다. -> 난이도가 너무 높은 방식. 그리고 특정 이미지에 대한 OCR results는 사람이 직접 보더라도 그럴싸한 것들이 있음.
  2. 따라서 test set에 대한 category classification을 기준으로 OCR results가 ground truth를 포함하지 않는 경우들을 해결하는 것에 대해 낮은 우선순위를 부여 -> answer가 OCR result에 포함되지만 정확한 예측이 되지 않는 것들을 중심으로 개선 시도 -> 이를 위해 각 question에 대한 answer가 OCR result에 포함되어 있는지 먼저 분류
chanmuzi commented 1 year ago
# 데이터 불러오기
import pandas as pd
import json
import os
from pprint import pprint

df = pd.read_csv('result_category.csv',encoding='UTF-8')

with open('/opt/ml/data/test/test_v1.0.json') as f:
    tests = json.load(f)
tests = tests['data']
# 정답이 ocr result에 포함되어 있으면 'yes', 그렇지 않으면 'no'를 inanswer라는 list에 담기
inanswer = []

for i in range(len(df)):
    qid = df.loc[i,'question_id'] # 각 행별로 처리
    answer = df.loc[i,'answer'][1:-1].split(', ') # 작은 따옴표를 제외하고 정답만 뽑아 오기
    for j in range(len(tests)): # test의 모든 결과를 확인
        if tests[j]['questionId'] == qid: # question ID를 찾으면 break
            test = tests[j]
            break 
    ocr_path = 'data/test/ocr_results/' + test['image'][10:-3] + 'json' # OCR result 부러오기
    with open(ocr_path) as f:
        ocr_result = json.load(f)
    ocr_result = ocr_result['recognitionResults'][0]['lines']

    flag = False # flag가 False이면 들어 있지 않은 경우, True이면 들어 있는 경우
    for ans in answer:  # 정답 리스트의 각 정답에 대해 확인  
        for ocr in ocr_result:
            if ans == ocr['text']: # 정답이 존재하는 경우 flag=True 후 break
                flag = True
                break

    if flag == True:
        inanswer.append('yes')
    else: inanswer.append('no')

print(len(inanswer))
# inanswer를 concat하여 csv파일로 저장
inanswer = pd.Series(inanswer,name='inanswer') # series로 생성
df = pd.concat([df,inanswer],axis=1,ignore_index=True) # 그리고 concat
df = df.rename(columns={0:'question_id',1:'score',2:'question',3:'prediction',4:'answer',5:'category',7:'inanswer'}) # column명 변경
df.to_csv('result_category_inanswer.csv',index=False) # csv로 저장
image

위와 같은 방식으로 csv파일이 생성된 것을 확인할 수 있다. 이를 토대로 결과 분석을 진행할 예정이다.