概述
- ImageAI本着简洁的原则,支持最先进的机器学习算法
- 用于图像预测,自定义图像预测,物体检测,视频检测,视频对象跟踪和图像预测训练
- ImageAI目前支持使用在ImageNet-1000数据集上训练的4种不同机器学习算法进行图像预测和训练
- ImageAI还支持使用在COCO数据集上训练的RetinaNet进行对象检测,视频检测和对象跟踪
- 除了由官方提供的图像模型外,自定义模型是被允许的
安装
pip install imageai
- ImageAI文档涉及内容
- 依赖
- 安装
- 图像预测
- 对象检测
- 视频对象检测和跟踪
- 自定义预测模型训练
- 自定义图像预测
- 实时和高性能实施
- 样本应用
- AI实践建议
- ImageAI:图像预测
- FirstPrediction.py
- 多图像预测
- 预测速度
- 图像输入类型
- 多线程预测
- 文档
- ImageAI:对象检测
- FirstObjectDetection.py
- 物体检测,提取和微调
- 自定义对象检测
- 检测速度
- 图像输入和输出类型
- ImageAI:视频对象检测和跟踪(预览版)
- FirstVideoObjectDetection.py
- 自定义视频对象检测
- 视频检测速度
- 帧检测间隔
- ImageAI:自定义预测模型训练
- 在 IdenProf 数据集上训练
- ImageAI:自定义图像预测
- FirstCustomPrediction.py
自定义图像分类之初体验
from tensorflow.keras.datasets import mnist
import abc
import os
import numpy as np
import requests
import cv2
import pathlib
class ABCDatasets(metaclass=abc.ABCMeta):
"""数据集抽象类,以下方法必须全部重新复写"""
datasets_dir: str
@property
@abc.abstractmethod
def load_data(self):
"""加载数据"""
pass
@property
@abc.abstractmethod
def label_mapping(self):
"""标签映射关系"""
return {}
class MnistDataSetsToDir(ABCDatasets):
"""
配置数据集,以及标签
"""
datasets_dir = "mnist_datasets"
def __init__(self):
self.x_train = self.y_train = self.x_test = self.y_test = None
def load_data(self):
"""加载官方的手写数据集"""
(self.x_train, self.y_train), (self.x_test, self.y_test) = mnist.load_data()
# print(self.x_train.shape)
# print(self.y_train.shape)
# print(self.x_train[0].shape)
# (60000, 28, 28)
# (60000,)
# (28, 28)
# 这里输入可知,数据集包含了60000张图片,且素材是一个单通道28x28
for label in self.label_mapping.keys():
os.makedirs(name=f"{self.datasets_dir}/train/{label}", exist_ok=True)
os.makedirs(name=f"{self.datasets_dir}/test/{label}", exist_ok=True)
train_length = self.x_train.shape[0]
train_length = 3000
test_length = self.x_test.shape[0]
test_length = 1000
import time
for index in range(train_length):
cv2.imwrite(filename=f"{self.datasets_dir}/train/{self.y_train[index]}/{time.time()}.jpg",
img=self.x_train[index])
# break
for index in range(test_length):
cv2.imwrite(filename=f"{self.datasets_dir}/test/{self.y_test[index]}/{time.time()}.jpg",
img=self.x_test[index])
# break
@property
def label_mapping(self):
"""标签映射关系"""
return {1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 0: 0}
def get_online_test_data(self):
"""
在线获取一张手写体图片,并做前处理
:return:
"""
label = 3
url = "https://img1.baidu.com/it/u=3472197447,93830654&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=281"
image = requests.get(url).content
nparr = np.fromstring(image, np.uint8)
gray = cv2.imdecode(nparr, cv2.IMREAD_GRAYSCALE)
gray = cv2.resize(gray, (28, 28))
_, gray = cv2.threshold(gray, thresh=165, maxval=255, type=cv2.THRESH_BINARY)
return gray, label
from imageai.Prediction.Custom import ModelTraining
from imageai.Classification.Custom import CustomImageClassification
class ClassifierModel():
""""""
datasets_dir = "mnist_datasets"
def __init__(self):
pass
def train(self):
self.modeler = ModelTraining()
self.modeler.setModelTypeAsResNet50()
self.modeler.setDataDirectory(self.datasets_dir)
self.modeler.trainModel(num_objects=10, num_experiments=5, enhance_data=True, batch_size=16,
show_network_summary=True, training_image_size=28)
def predict(self):
from imageai.Prediction.Custom import CustomImagePrediction
execution_path = os.getcwd()
multiple_prediction = CustomImagePrediction()
multiple_prediction.setModelTypeAsResNet50()
multiple_prediction.setModelPath("./resnet.h5")
multiple_prediction.setJsonPath("model_class.json")
multiple_prediction.loadModel(classification_speed="fastest")
files = os.listdir("./mnist_datasets/test/3/")
print(files)
for i in files:
i = "./mnist_datasets/test/0/" + i
predictions, probabilities = multiple_prediction.predictImage(i,
result_count=10)
for eachPrediction, eachProbability in zip(predictions, probabilities):
print(str(eachPrediction) + " : " + str(eachProbability))
def load_data():
MnistDataSetsToDir().load_data()
def train_classifier():
ClassifierModel().train()
def test_classifier():
ClassifierModel().predict()
if __name__ == '__main__':
import fire
fire.Fire()