AI

ImageAI - ②Prediction Classes

으뜸점2 2020. 6. 9. 17:01
prediction.loadModel(0

Prediction Classes

ImageAI는 매우 강력하지만 사용하기 쉬운 클래스를 제공하여 이미지 인식 작업을 수행합니다. 5 줄에서 12 줄 사이의 python 코드를 사용하여 이러한 최첨단 컴퓨터 비전 작업을 모두 수행 할 수 있습니다. 컴퓨터 시스템에 Python, 기타 종속성 및 ImageAI가 설치되면 만들 수있는 놀라운 응용 프로그램에 제한이 없습니다.

 

ImagePrediction 클래스는 ImageNet-1000 데이터 세트에서 사전 훈련된 SqueezeNet, ResNet, InceptionV3 및 DenseNet과 같은 최첨단 이미지 인식 모델을 사용하는 기능을 제공합니다. 모든 이미지 또는 여러 이미지에서 1000 개의 서로 다른 물체를 인식합니다

 

.setModelTypeAsSqueezeNet ()

생성 한 이미지 인식 인스턴스의 모델 유형을 SqueezeNet 모델로 설정합니다. 

prediction.setModelTypeAsSqueezeNet()

.setModelTypeAsResNet ()

생성 한 이미지 인식 인스턴스의 모델 유형을 ResNet 모델로 설정합니다. 

prediction.setModelTypeAsResNet()

.setModelTypeAsInceptionV3 ()

생성 한 이미지 인식 인스턴스의 모델 유형을 InecptionV3 모델로 설정합니다

prediction.setModelTypeAsInceptionV3()

.setModelTypeAsDenseNet ()

생성 한 이미지 인식 인스턴스의 모델 유형을 DenseNet 모델로 설정합니다

prediction.setModelTypeAsDenseNet()

.setModelPath()

다운로드 한 모델 파일의 경로 여야하며 이미지 예측 인스턴스에 설정 한 모델 유형과 일치해야하는 문자열을 허용합니다.

prediction.setModelPath("resnet50_weights_tf_dim_ordering_tf_kernels.h5")

예> 학습한 모델의 경로 : resnet50_weights_tf_dim_ordering_tf_kernels.h5

 

.loadModel ()

이 함수는 함수 호출에서 지정한 경로에서 모델을 이미지 예측 인스턴스로로드합니다.

prediction.loadModel()

– parameter prediction_speed (선택 사항) :이 매개 변수를 사용하면 이미지에서 예측하는 데 걸리는 시간을 최대 80 %까지 줄일 수있어 정확도가 약간 떨어집니다. 이 매개 변수는 문자열 값을 승인합니다. 사용 가능한 값은 "normal", "fast", "faster"및 "fastest"입니다. 기본값은“normal”입니다.

 

.predictImage ()

이미지의 실제 예측을 수행하는 함수입니다. 모델이 예측 인스턴스에로드 된 후 여러 이미지에서 여러 번 호출 될 수 있습니다.

predictions, probabilities = prediction.predictImage("image1.jpg", result_count=10)

 

 

Sample Codes

#One Image

from imageai.Prediction import ImagePrediction
import os

execution_path = os.getcwd()

prediction = ImagePrediction()
prediction.setModelTypeAsResNet()
prediction.setModelPath(os.path.join(execution_path, "resnet50_weights_tf_dim_ordering_tf_kernels.h5"))
prediction.loadModel()

predictions, probabilities = prediction.predictImage(os.path.join(execution_path, "image1.jpg"), result_count=10)
for eachPrediction, eachProbability in zip(predictions, probabilities):
    print(eachPrediction , " : " , eachProbability)

 

#Multiple Images

from imageai.Prediction import ImagePrediction
import os

execution_path = os.getcwd()

multiple_prediction = ImagePrediction()
multiple_prediction.setModelTypeAsResNet()
multiple_prediction.setModelPath(os.path.join(execution_path, "resnet50_weights_tf_dim_ordering_tf_kernels.h5"))
multiple_prediction.loadModel()

all_images_array = []

all_files = os.listdir(execution_path)
for each_file in all_files:
    if(each_file.endswith(".jpg") or each_file.endswith(".png")):
        all_images_array.append(each_file)

results_array = multiple_prediction.predictMultipleImages(all_images_array, result_count_per_image=5)

for each_result in results_array:
    predictions, percentage_probabilities = each_result["predictions"], each_result["percentage_probabilities"]
    for index in range(len(predictions)):
        print(predictions[index] , " : " , percentage_probabilities[index])
    print("-----------------------")