{ "cells": [ { "cell_type": "markdown", "id": "66a65080-4234-43c8-ac3a-1e7e03acd8cf", "metadata": {}, "source": [ "# 1. TrOCR 훈련 이미지 생성\n", "\n", "--- \n", "\n", "0번 모듈에서 생성한 `ocr_dataset_poc.csv`를 사용하여 OCR 훈련 이미지를 생성합니다. 훈련 이미지 생성은 오픈 소스로 공개되어 있는 TextRecognitionDataGenerator를 사용합니다.\n", "\n", "- 참조: https://github.com/Belval/TextRecognitionDataGenerator" ] }, { "cell_type": "code", "execution_count": null, "id": "1abbdeb2-4261-4408-8366-231165958f42", "metadata": {}, "outputs": [], "source": [ "!pip install -r requirements.txt" ] }, { "cell_type": "code", "execution_count": 1, "id": "99005511-1fc6-4caf-9cb0-393b5372ac77", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
documentcategory
398118누리호 발사 성공을 축하합니다news
447804그동안 두 자릿수였던 수출 증가율이 지난달 한 자릿수로 떨어지는 등 둔화세가 본격화...news
464420동원참치 캔 개발·생산 경험이 있는 동원시스템즈는 차별화된 기술을 적용한 4680 ...news
534239봉고 III EV 냉동탑차는 135kW 모터와 588kWh 배터리를 탑재해 완충 시...news
235107아 점깍은이유는 네이버가 영화 분류를 코믹으로 안하고 공포에 집어넣은 실수 때문에nsmc
618045유경준 의원은 개회사에서 “전 세계적으로 성장률 저하와 물가 상승이 동반되는 스테그...news
633123사랑은 변하고 사람은 안 변해요chatbot
20017꼴깝 떨고 앉았있네 별 시덥잖은 의미nsmc
184000청춘의지표는 여기서부터 시작이아닐까 싶다nsmc
115726차화연의 빛나는 미모nsmc
\n", "
" ], "text/plain": [ " document category\n", "398118 누리호 발사 성공을 축하합니다 news\n", "447804 그동안 두 자릿수였던 수출 증가율이 지난달 한 자릿수로 떨어지는 등 둔화세가 본격화... news\n", "464420 동원참치 캔 개발·생산 경험이 있는 동원시스템즈는 차별화된 기술을 적용한 4680 ... news\n", "534239 봉고 III EV 냉동탑차는 135kW 모터와 588kWh 배터리를 탑재해 완충 시... news\n", "235107 아 점깍은이유는 네이버가 영화 분류를 코믹으로 안하고 공포에 집어넣은 실수 때문에 nsmc\n", "618045 유경준 의원은 개회사에서 “전 세계적으로 성장률 저하와 물가 상승이 동반되는 스테그... news\n", "633123 사랑은 변하고 사람은 안 변해요 chatbot\n", "20017 꼴깝 떨고 앉았있네 별 시덥잖은 의미 nsmc\n", "184000 청춘의지표는 여기서부터 시작이아닐까 싶다 nsmc\n", "115726 차화연의 빛나는 미모 nsmc" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import pandas as pd\n", "import multiprocessing\n", "\n", "df = pd.read_csv('https://raw.githubusercontent.com/aws-samples/sm-kornlp/main/trocr/ocr_dataset_poc.csv')\n", "display(df.sample(n=10, random_state=42))\n", "df['document'].to_csv('ocr_dataset_poc.txt', header=False, index=False)" ] }, { "cell_type": "markdown", "id": "cfc92037-7824-43b8-bbcc-75ffaaa8dd18", "metadata": {}, "source": [ "전체 데이터셋은 60만건을 초과하지만, 핸즈온 시에는 1000건의 샘플만 사용합니다." ] }, { "cell_type": "code", "execution_count": null, "id": "f3484867-11ed-4c76-951f-28ae3eb0c183", "metadata": {}, "outputs": [], "source": [ "df['category'].value_counts()" ] }, { "cell_type": "code", "execution_count": null, "id": "facf996e-d0bc-466b-949d-c83240b70059", "metadata": {}, "outputs": [], "source": [ "FULL_TRAINING = False\n", "\n", "if not FULL_TRAINING:\n", " df = df.sample(n=1000)" ] }, { "cell_type": "code", "execution_count": null, "id": "b856cdbb-d865-447d-96fb-dcf625564888", "metadata": {}, "outputs": [], "source": [ "num_cores = multiprocessing.cpu_count()\n", "dataset_dir = 'train'\n", "num_samples = len(df)\n", "\n", "!rm -rf {dataset_dir}\n", "!python3 ./trdg/run.py -i ocr_dataset_poc.txt -w 5 -t {num_cores} -f 64 -l ko -c {num_samples} -na 2 --output_dir {dataset_dir}\n", "!cp {dataset_dir}/labels.txt ./" ] }, { "cell_type": "markdown", "id": "d445385d-92fa-4570-89e3-5b0ef2fb34ca", "metadata": {}, "source": [ "선택적으로 랜덤하게 생성한 문장을 훈련 데이터셋에 추가합니다. 좀 더 다양한 형태의 데이터로 훈련해야 할 때 참고하세요. " ] }, { "cell_type": "code", "execution_count": null, "id": "23bf369c-2b00-495f-8eab-204d243c1a19", "metadata": {}, "outputs": [], "source": [ "# rand_dataset_dir = 'rand_train'\n", "# num_rand_samples = 500000\n", "\n", "# !rm -rf {rand_dataset_dir}\n", "# !python3 ./trdg/run.py -w 5 -t {num_cores} -f 64 -l ko -c {num_rand_samples} -na 2 --output_dir {rand_dataset_dir}\n", "# !cp {rand_dataset_dir}/labels.txt ./rand_labels.txt" ] } ], "metadata": { "kernelspec": { "display_name": "conda_pytorch_p38", "language": "python", "name": "conda_pytorch_p38" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.12" } }, "nbformat": 4, "nbformat_minor": 5 }