-
딥러닝 NVIDIA DIGITS를 활용한 Classifier 만들기 #5데이터 분석 2020. 11. 29. 11:04
딥 뉴럴 네트워크 훈련과 배포 마지막 과제를 풀어보겠습니다. 이것을 완료하면 글로벌 공인 인증서를 받을 수 있습니다.
Assessment Project 1
신경망을 학습시키고 적용하기
이제 여러분은 딥러닝 작업 흐름에 대해 모두 살펴보았습니다. 데이터 세트를 불러들이고, 모델을 학습시키고, 여러분의 모델을 간단한 애플리케이션에 적용하였습니다. 이제 새로운 문제를 가지고 이상의 작업 흐름을 되풀이해 봄으로써 여러분이 학습한 내용을 점검해 보도록 하겠습니다.
두 개의 클래스를 가지는 데이터 세트가 있습니다.
1) whale: 고래의 얼굴을 포함하는 이미지들
2) not whale: 고래의 얼굴을 포함하지 않는 이미지들
데이터 세트는 /dli/data/whale/data/train에 위치합니다.
여러분의 과제:
1) DIGITS를 사용하여 새로운 고래 얼굴을 80% 이상의 정확도로 인식할 수 있도록 모델을 학습시킵니다.
2) 이미지가 고래 얼굴을 포함하면 "whale"을, 포함하지 않으면 "not whale"을 리턴하도록 파이썬 파일 [submission.py](../../../../edit/tasks/task- assessment/task/submission.py)를 수정 및 저장하여 모델을 적용합니다.
리소스:
1) 모델 학습시키기
2) 새로운 목표 데이터
3) 배포
힌트:
- 문제를 푸는 데 필요한 정보를 찾기 위한 빈 코드블록을 사용하세요. 예: !ls [directorypath] prints the files in a given directory
- 아래의 처음 두 셀을 이용하여 테스트 이미지를 가지고 파이선 스크립트를 실행하세요. 처음 것은 "whale"이, 두 번째 것은 "not whale"이 출력되어야 합니다.우선 DIGITS에 접속합니다. 접속하면 Dataset, Models가 모두 존재하지 않아 모두 만들어야 합니다.
Dataset을 먼저 만들도록 하겠습니다.
Datasets탭을 클릭한 후 우측 상단에서 Images > Classification을 선택합니다. User Name은 자유롭게 입력하면, New Image Classification Dataset 페이지에 진입하게 됩니다.
Traning Images에는 /dli/data/whale/data/train을 입력하고, 아래 Dataset Name에는 whale을 입력하고 "Create" 버튼을 클릭하면 Dataset이 만들어지게 됩니다.
New Image Classification Dataset 화면 다시 DIGITS 홈에 접속해서 Models탭을 클릭한 후 우측 상단에서 Images > Classification을 선택합니다. New Image Classification Model 페이지에 진입하게 되면, Select Dataset에서 방금 만든 whale을 선택하고, 나머지는 기본값으로 둡니다.
New Image Classification Model 화면 하단의 Caffe 탭에서 AlexNet을 선택하고, Model Name을 자유롭게 입력하고 "Create"버튼을 클릭합니다.
학습이 완료되었습니다. 다음 작업인 배포를 해보도록 하겠습니다.
submission.py 파일 안에 /dli/data/whale/data/train/face/w_1.jpg, /dli/data/whale/data/train/not_face/w_1.jpg를 각각 whale, not whale값이 나와야 합격입니다.
!python submission.py '/dli/data/whale/data/train/face/w_1.jpg' #This should return "whale" at the very bottom of the output.
!python submission.py '/dli/data/whale/data/train/not_face/w_1.jpg' #This should return "not whale" at the very bottom of the output.
아마존 인스턴스 주소/edit/tasks/task-assessment/task/submission.py를 브라우저 주소창에 입력하여 파이썬 파일 내용을 확인합니다.
submission.py
import caffe import cv2 import sys def deploy(img_path): caffe.set_mode_gpu() # Initialize the Caffe model using the model trained in DIGITS. Which two files constitute your trained model? net = caffe.Classifier('##REPLACE WITH ARCHITECTURE FILE##', '##REPLACE WITH WEIGHTS FILE##', channel_swap=(2,1,0), raw_scale=255, image_dims=(256, 256)) # Create an input that the network expects. This is different for each project, so don't worry about the exact steps, but find the dataset job directory to show you know that whatever preprocessing is done during training must also be done during deployment. input_image= caffe.io.load_image(img_path) input_image = cv2.resize(input_image, (256,256)) mean_image = caffe.io.load_image('##REPLACE WITH DATASET JOB DIRECTORY##/mean.jpg') input_image = input_image-mean_image # Make prediction. What is the function and the input to the function needed to make a prediction? prediction = net.##REPLACE WITH THE FUNCTION THAT RETURNS THE OUTPUT OF THE NETWORK##([##REPLACE WITH THE INPUT TO THE FUNCTION##]) # Create an output that is useful to a user. What is the condition that should return "whale" vs. "not whale"? if ##REPLACE WITH THE CONDITION THAT WOULD MAKE THE FUNCTION RETURN WHALE##: return "whale" else: return "not whale" ##Ignore this part if __name__ == '__main__': print(deploy(sys.argv[1]))
학습중인 페이지에서 Job Directory에 있는 경로를 복사합니다.
##REPLACE WITH ARCHITECTURE FILE##
- 지난 번 deploy.prototxt를 참고하여 Job Directory와 조합합니다.
- 예: /dli/data/digits/20201129-011056-1852/deploy.prototxt
##REPLACE WITH WEIGHTS FILE##
- 마찬가지로 위에서 찾은 링크를 조합해서 입력합니다.
- 예: /dli/data/digits/20201129-011056-1852/snapshot_iter_486.caffemodel
##REPLACE WITH DATASET JOB DIRECTORY##/mean.jpg
- "DIGITS"의 Dataset에서 Job Directory의 경로를 복사합니다.
- 예: /dli/data/digits/20201129-010451-fab4/mean.jpg
해당 주소에 mean.jpg가 있는지 확인해보려면 앞에 !ls를 입력하여 하위 파일들을 확인하면 됩니다.
!ls /dli/data/digits/20201129-010451-fab4/
첫번째 줄 오른쪽에서 3번째에 mean.jpg가 있는 것을 확인할 수 있습니다. 그렇다면 정상적으로 입력한 것입니다.
prediction = net.##REPLACE WITH THE FUNCTION THAT RETURNS THE OUTPUT OF THE NETWORK##([##REPLACE WITH THE INPUT TO THE FUNCTION##])
- 기존 소스코드인 prediction = net.predict([ready_image])를 참고하여 수정하겠습니다.
- 예: prediction = net.predict([input_image])
if ##REPLACE WITH THE CONDITION THAT WOULD MAKE THE FUNCTION RETURN WHALE##:
- 기존 소스코드인 if prediction.argmax() == 0:를 그대로 가져옵니다.
- 예: if prediction.argmax() == 0:
submission.py를 그대로 저장합니다.
Assessment1-kr파일로 돌아와서 다시 실행해보겠습니다.
!python submission.py '/dli/data/whale/data/train/face/w_1.jpg' #This should return "whale" at the very bottom of the output.
결과값이 whale로 나온다. !python submission.py '/dli/data/whale/data/train/not_face/w_1.jpg' #This should return "not whale" at the very bottom of the output.
결과값이 not whale로 나온다. 완성된 코드
import caffe import cv2 import sys def deploy(img_path): caffe.set_mode_gpu() # Initialize the Caffe model using the model trained in DIGITS. Which two files constitute your trained model? net = caffe.Classifier('/dli/data/digits/20201129-011056-1852/deploy.prototxt', '/dli/data/digits/20201129-011056-1852/snapshot_iter_486.caffemodel', channel_swap=(2,1,0), raw_scale=255, image_dims=(256, 256)) # Create an input that the network expects. This is different for each project, so don't worry about the exact steps, but find the dataset job directory to show you know that whatever preprocessing is done during training must also be done during deployment. input_image= caffe.io.load_image(img_path) input_image = cv2.resize(input_image, (256,256)) mean_image = caffe.io.load_image('/dli/data/digits/20201129-010451-fab4/mean.jpg') input_image = input_image-mean_image # Make prediction. What is the function and the input to the function needed to make a prediction? prediction = net.predict([input_image]) # Create an output that is useful to a user. What is the condition that should return "whale" vs. "not whale"? if prediction.argmax() == 0: return "whale" else: return "not whale" ##Ignore this part if __name__ == '__main__': print(deploy(sys.argv[1]))
NVIDIA Courses로 이동 후 ACCESS TASK를 클릭하여 Assessment 평가를 진행합니다.
고래분류기를 정확히 훈련하였습니다. 인증서가 발행되었습니다. '데이터 분석' 카테고리의 다른 글
딥러닝 NVIDIA DIGITS를 활용한 Classifier 만들기 #4 (0) 2020.11.21 딥러닝 NVIDIA DIGITS를 활용한 Classifier 만들기 #3 (0) 2020.11.18 딥러닝 NVIDIA DIGITS를 활용한 Classifier 만들기 #2 (0) 2020.11.18 딥러닝 NVIDIA DIGITS를 활용한 Classifier 만들기 #1 (0) 2020.11.17