KT 에이블스쿨 38일차(1)

박기범·2023년 3월 25일
0

에이블스쿨

목록 보기
44/95

어제에 이어서 오늘 모델링까지 진행했습니다.



진행 현황

어제는 가이드 라인에 맞춰 전처리를 진행했었고 오늘은 해당 데이터를 가지고 모델링을 진행하겠습니다.



YOLO 설치

먼저 해당 프로젝트는 YOLO을 활용해서 진행하기 때문에 먼저 YOLO를 설치해줍니다.

	!git clone https://github.com/ultralytics/yolov5

욜로 설치에 앞서 에러가 발생할 수 있으니 아래 코드로 에러를 예방하기 위해 아래의 코드를 직성해줍니다.

    temp_str = 'setuptools<=64.0.2\n' 

    f = open('/content/yolov5/requirements.txt', 'r')
    f_str = f.readlines()
    f.close()

    f2 = open('/content/yolov5/requirements.txt', 'w')

    for idx, val in enumerate(f_str) :
        if 'setuptools' in val :
            idx_v = idx
            f_str.remove(val)
            f_str.insert(idx_v, temp_str)

    for val in f_str :
        f2.write(val)
    f2.close()
	

마지막으로 필요 txt파일을 실행시켜서 마저 설치해주면 YOLO의 사용준비는 끝납니다.

	!cd yolov5; pip install -r requirements.txt

저는 YOLOV5s모델을 사용했습니다.



모델링 환경준비

먼저 학습 가중치 파일을 코랩 내에 가상폴더로 저장하고 사용하기 위해서 폴더를 하나 만듭니다.

	!mkdir /content/yolov5/pretrained

폴더를 하나 만든 후에 yolov5s 학습모델을 다운받아줍니다.

	!wget -O /content/yolov5/pretrained/yolov5s.pt https://github.com/ultralytics/yolov5/releases/download/v7.0/yolov5s.pt



모델링 시작

이제 학습모델까지 모두 다운을 받았으니 학습을 시작합니다.

	!cd yolov5; python train.py \
    --data '/content/drive/MyDrive/경로/Dataset/money_yaml.yaml' \
    --cfg '/content/yolov5/models/yolov5s.yaml' \
    --weights '/content/yolov5/pretrained/yolov5s.pt' \
    --hyp hyp.scratch-low.yaml \
    --epochs 1000 \
    --patience 7 \
    --img 640 \
    --project 'money' \
    --name 'show_me_the_money' \
    --exist-ok

이제 전과 다른 점이 있다면 옵션으로 --hyp hyp.scratch-low.yaml옵션이 추가되었는데 해당 옵션을 사용해주면 Data Augementation을 진행할 수 있습니다.

해당 파일은 경로가 /content/yolov5/data/hyps/hyp.scratch-low.yaml에 있습니다. 해당 경로로 찾아가서 파일을 여러보면 여러 옵션이 있는데 옵션을 조율해서 Data Augementation을 진행할 수 있습니다.

모델 학습이 끝나면 혹시 다시 사용할 때를 대비해 저는 크롬 드라이버에 저장해두었습니다.

	!cp /content/yolov5/money/show_me_the_money/weights/best.pt '/content/drive/MyDrive/경로/Dataset/모델가중치저장소/'

Detection 시작

이제 모델까지 완성이 되었으니 모델 Detection을 실행했습니다.

	!cd yolov5; python detect.py \
    --weights '/content/yolov5/money/show_me_the_money/weights/best.pt' \
    --source '/content/drive/MyDrive/경로/Dataset/money_test/' \
    --project '/content/drive/MyDrive/경로/Dataset/money_test_pred' \
    --img 640 \
    --conf-thres 0.3\
    --iou-thres 0.3\
    --line-thickness 2 \
    --exist-ok

해당 코드는 이미지에 대해 Detection한 코드입니다.

	!cd yolov5; python detect.py \
    --weights '/content/yolov5/money/show_me_the_money/weights/best.pt' \
    --source '/content/drive/MyDrive/경로/Dataset/money_test_video/' \
    --project '/content/drive/MyDrive/경로/Dataset/money_test_video_pred/' \
    --img 640 \
    --conf-thres 0.3\
    --iou-thres 0.3\
    --line-thickness 2 \
    --exist-ok

위 코드는 영상에 대해서 Detection한 코드입니다.

이렇게 YOLO를 사용한 Detection을 모두 마쳤습니다.



결론

원래는 Data Augmentation을 하지않고 주어진 데이터로만 학습을 진행한 후에 Detection을 했는데 거의 분류를 극악적으로 못했습니다. 보니깐 주어진 데이터가 대부분 반듯하게 놓여져있거나 90도로 회전 된 것들이기 때문에 45도로 회전되어 있으면 전혀 Detection하지을 못하거나 다른 물건과 같이 있거나 접혀있어도 Detection하지 못했습니다.

이런 문제를 해결하고자 YOLO 내에서 지원하는 --hyp hyp.scratch-low.yaml Data Detection을 진행하니 훨씬 잘 Detection을 했습니다. 역시 데이터 전처리와 데이터가 중요하다고 생각했습니다.







큰 폭풍이 지나간 CNN 미니프로젝트의 한 주 였습니다.




※공부하고 있어 다소 틀린점이 있을 수 있습니다. 언제든지 말해주시면 수정하도록 하겠습니다.
※용어에 대해 조금 공부 더 해서 수정하겠습니다.

profile
개발자가 되기 위한 한걸음

0개의 댓글