|
|
@ -28,6 +28,25 @@ class ClassificationAI:
|
|
|
|
def ConvertImage(cv_img: cv2.Mat) -> Image.Image:
|
|
|
|
def ConvertImage(cv_img: cv2.Mat) -> Image.Image:
|
|
|
|
return Image.fromarray(cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)).resize((460, 460))
|
|
|
|
return Image.fromarray(cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)).resize((460, 460))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
|
|
|
def ConvertClassifyResult(cla: str) -> str:
|
|
|
|
|
|
|
|
if cla == 'ForeignV':
|
|
|
|
|
|
|
|
return '外籍车辆'
|
|
|
|
|
|
|
|
elif cla == 'In-fieldV':
|
|
|
|
|
|
|
|
return '场内车辆'
|
|
|
|
|
|
|
|
elif cla == 'large-scaleNewenergyV':
|
|
|
|
|
|
|
|
return '大型新能源车辆'
|
|
|
|
|
|
|
|
elif cla == 'MediumLarge-sizedV':
|
|
|
|
|
|
|
|
return '中/大型车辆'
|
|
|
|
|
|
|
|
elif cla == 'MilitaryPoliceEmergencyV':
|
|
|
|
|
|
|
|
return '军/警/应急车辆'
|
|
|
|
|
|
|
|
elif cla == 'SmallCar':
|
|
|
|
|
|
|
|
return '小型轿车'
|
|
|
|
|
|
|
|
elif cla == 'SmallNewEnergyV':
|
|
|
|
|
|
|
|
return '小型新能源轿车'
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
return '未知'
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
@classmethod
|
|
|
|
def TrainAI(cls, data_set_path: str, export_path: str) -> None:
|
|
|
|
def TrainAI(cls, data_set_path: str, export_path: str) -> None:
|
|
|
|
blocks = (ImageBlock, CategoryBlock)
|
|
|
|
blocks = (ImageBlock, CategoryBlock)
|
|
|
@ -39,7 +58,7 @@ class ClassificationAI:
|
|
|
|
get_y=parent_label,
|
|
|
|
get_y=parent_label,
|
|
|
|
item_tfms=Resize(460),
|
|
|
|
item_tfms=Resize(460),
|
|
|
|
batch_tfms=[*aug_transforms(size=224, min_scale=0.75), Normalize.from_stats(*imagenet_stats)]
|
|
|
|
batch_tfms=[*aug_transforms(size=224, min_scale=0.75), Normalize.from_stats(*imagenet_stats)]
|
|
|
|
).dataloaders(data_set_path, num_workers=4, bs=batch_size)
|
|
|
|
).dataloaders(data_set_path, num_workers=0, bs=batch_size)
|
|
|
|
model = vision_learner(dls, resnet34, metrics=error_rate)
|
|
|
|
model = vision_learner(dls, resnet34, metrics=error_rate)
|
|
|
|
model.fine_tune(5, freeze_epochs=3) # 5 - 训练的轮次, 3 - 冻结的轮次
|
|
|
|
model.fine_tune(5, freeze_epochs=3) # 5 - 训练的轮次, 3 - 冻结的轮次
|
|
|
|
model.export(Path(export_path) / 'model.pkl')
|
|
|
|
model.export(Path(export_path) / 'model.pkl')
|
|
|
@ -58,4 +77,5 @@ class ClassificationAI:
|
|
|
|
confidence = float(outputs)
|
|
|
|
confidence = float(outputs)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
confidence = float(outputs[pred_idx])
|
|
|
|
confidence = float(outputs[pred_idx])
|
|
|
|
|
|
|
|
pred_class = cls.ConvertClassifyResult(pred_class)
|
|
|
|
return pred_class, confidence
|
|
|
|
return pred_class, confidence
|
|
|
|