AI

ImageAI - ④Custom Training : Prediction

으뜸점2 2020. 6. 9. 17:30

ImageAI는 매우 강력하면서도 사용하기 쉬운 클래스를 제공하여 SqueezeNet, ResNet, InceptionV3 및 DenseNet과 같은 최첨단 딥 러닝 알고리즘을 5 줄의 코드를 사용하여 사용자 정의 모델을 생성하는 자체 이미지 데이터 세트에서 학습합니다. 고유 한 사용자 지정 모델을 학습 한 후에는 ImageAI에서 제공하는 CustomImagePrediction 클래스를 사용하여 고유 한 모델을 사용하여 이미지 또는 이미지 집합을 인식 / 예측할 수 있습니다.

 

======= imageai.Prediction.Custom.ModelTraining =======

ModelTraining 클래스를 사용하면 고유 한 이미지 데이터 세트에서 지원되는 4 가지 딥 러닝 알고리즘 (SqueezeNet, ResNet, InceptionV3 및 DenseNet) 중 하나를 학습하여 고유 한 사용자 지정 모델을 생성 할 수 있습니다. 이미지 데이터 세트에는 2 가지 이상의 다른 클래스 / 유형 이미지 (예 : 고양이와 개)가 포함되어야하며 최대 정확도를 달성하려면 각 클래스에 대해 500 개 이상의 이미지를 수집해야합니다.

– Create a dataset folder with the name you will like your dataset to be called (e.g pets)

—In the dataset folder, create a folder by the name train

– In the dataset folder, create a folder by the name test

—In the train folder, create a folder for each object you want to the model to predict and give the folder a name that corresponds to the respective object name (e.g dog, cat, squirrel, snake)

– In the test folder, create a folder for each object you want to the model to predict and give the folder a name that corresponds to the respective object name (e.g dog, cat, squirrel, snake)

—In each folder present in the train folder, put the images of each object in its respective folder. This images are the ones to be used to train the model

– To produce a model that can perform well in practical applications, I recommend you about 500 or more images per object. 1000 images per object is just great

—In each folder present in the test folder, put about 100 to 200 images of each object in its respective folder. These images are the ones to be used to test the model as it trains

Data 세트가 준비됨녀 ModelTraining Class의 인스턴스를 생성 할 수 있습니다.

from imageai.Prediction.Custom import ModelTraining

model_trainer = ModelTraining()
model_trainer.setModelTypeAsResNet()

.setDataDirectory ()

이미지 데이터 세트의 테스트 및 기차 하위 폴더가 포함 된 폴더의 경로 여야하는 문자열을 허용합니다

prediction.setDataDirectory(r"C:/Users/Moses/Documents/Moses/AI/Custom Datasets/pets")

.trainModel ()

훈련 과정을 시작하는 함수입니다. 시작되면 dataset / json 폴더 (예 : pets / json)에 JSON 파일을 만들어 데이터 세트 클래스의 매핑을 포함합니다. JSON 파일은 사용자 정의 예측 중에 결과를 생성하는 데 사용됩니다.

model_trainer.trainModel(num_objects=4, num_experiments=100, enhance_data=True, batch_size=32, show_network_summary=True)

– 매개 변수 num_objects (필수) : 이미지 데이터 세트의 여러 클래스 수를 나타냅니다.

—parameter num_experiments (필수) : 알고리즘이 이미지 데이터 세트에 대해 훈련되는 횟수입니다. 훈련 횟수는 훈련 횟수가 증가함에 따라 증가합니다. 그러나 일정 횟수의 교육을받은 후에는 최고 수준이며 데이터 포인트의 크기와 특성에 따라 다릅니다.

– parameter enhance_data (선택 사항) :이 매개 변수는 훈련을 위해 더 많은 샘플을 생성하기 위해 이미지 데이터 세트를 변환하는 데 사용됩니다. 기본적으로 False로 설정되어 있습니다. 그러나 이미지 데이터 세트에 클래스 당 1000 개 미만의 이미지가 포함되어 있으면 True로 설정하는 것이 좋습니다.

—parameter batch_size (선택 사항) : 학습 중에 알고리즘이 일련의 이미지에 대해 병렬로 학습됩니다. 이로 인해 기본값은 32로 설정됩니다. 훈련에 사용중인 시스템의 용량을 충분히 알고 있으면이 값을 늘리거나 줄일 수 있습니다. 이 값을 모으려면 훈련 과정을 최적화하기 위해 값을 8의 배수로 설정해야합니다.

– 매개 변수 show_network_summary (선택 사항) :이 매개 변수는 True로 설정되면 학습을 시작하기 전에 CLI에서 이미지 데이터 세트에 대해 학습중인 알고리즘의 구조를 표시합니다. 기본적으로 False로 설정되어 있습니다.

 

Sample Code for Custom Model Training

from imageai.Prediction.Custom import ModelTraining

model_trainer = ModelTraining()
model_trainer.setModelTypeAsResNet()
model_trainer.setDataDirectory(r"C:/Users/Moses/Documents/Moses/AI/Custom Datasets/pets")
model_trainer.trainModel(num_objects=10, num_experiments=100, enhance_data=True, batch_size=32, show_network_summary=True)