def eval(data_directory="plates", model_path="plates/models", json_path="plates/json/detection_config.json", iou_threshold=0.5, object_threshold=0.3, nms_threshold=0.5): trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory) trainer.evaluateModel(model_path, json_path, iou_threshold, object_threshold, nms_threshold)
def evaluate_yolov3(data_path, models_path, json_path, iou_thresh, object_thresh, nms_thresh): model = DetectionModelTrainer() model.setModelTypeAsYOLOv3() model.setDataDirectory(data_directory=data_path) model.evaluateModel(model_path=models_path, json_path=os.path.join(json_path, "detection_config.json"), iou_threshold=iou_thresh, object_threshold=object_thresh, nms_threshold=nms_thresh)
def train_yolo(lst_y, train_path="fs/training_yolo/", transfer_modelfile=""): ## setup yolo = DetectionModelTrainer() yolo.setModelTypeAsYOLOv3() yolo.setDataDirectory(data_directory=train_path) yolo.setTrainConfig(object_names_array=lst_y, batch_size=4, num_experiments=1, train_from_pretrained_model=transfer_modelfile) ## train print("--- training ---") yolo.trainModel() ## evaluate print("--- metrics ---") metrics = yolo.evaluateModel(model_path=train_path + "models", json_path=train_path + "json/detection_config.json", iou_threshold=0.5, object_threshold=0.5, nms_threshold=0.5) print(metrics) ## laod model print("--- loading model ---") modelfile = os.listdir(train_path + "models/")[0] return load_yolo(modelfile=train_path + "models/" + modelfile, confjson=train_path + "/json/detection_config.json")
def evaluarModelo(): trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory="2_RESPUESTA") metrics = trainer.evaluateModel(model_path="models", json_path="detection_config.json", iou_threshold=0.5, object_threshold=0.3, nms_threshold=0.5) print(metrics)
def main(): # Will we train? if DOTRAIN: # Use last trained model as base for transferlearning? last_model = '' # Ohmy. This means None. if DOTRANSFERLEARN: last_model = MODEL_PATH if DORESUMELAST: last_model = get_last_model() else: if os.path.exists(os.path.join(DATA_PATH, 'cache')): shutil.rmtree(os.path.join(DATA_PATH, 'cache')) shutil.rmtree(os.path.join(DATA_PATH, 'json')) shutil.rmtree(os.path.join(DATA_PATH, 'logs')) shutil.rmtree(os.path.join(DATA_PATH, 'models')) trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory=DATA_PATH) trainer.setTrainConfig(object_names_array=OBJECT_NAMES, batch_size=BATCHCOUNT, num_experiments=EXPIREMENTSCOUNT, train_from_pretrained_model=last_model) trainer.trainModel() # Will we evaluate? if DOEVALUATE: # Show evaluations for the last model metrics = trainer.evaluateModel(model_path=get_last_model(), json_path=os.path.join( DATA_PATH, 'json', 'detection_config.json'), iou_threshold=.5, object_threshold=.3, nms_threshold=.5) print(metrics)
from imageai.Detection.Custom import DetectionModelTrainer trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory="datasets") trainer.evaluateModel(model_path="datasets/models", json_path="datasets/json/detection_config.json", iou_threshold=0.5, object_threshold=0.3, nms_threshold=0.5)
from imageai.Detection.Custom import DetectionModelTrainer trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory="smoker") trainer.evaluateModel( model_path="smoker/models/detection_model-ex-143--loss-0007.288.h5", json_path="smoker/json/detection_config.json", iou_threshold=0.1, object_threshold=0.35, nms_threshold=0.1) # download JSON file via https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/detection_config.json # download detection model via https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/hololens-ex-60--loss-2.76.h5 """ SAMPLE RESULT Model File: hololens_detection_model-ex-09--loss-4.01.h5 Using IoU : 0.5 Using Object Threshold : 0.3 Using Non-Maximum Suppression : 0.5 hololens: 0.9613 mAP: 0.9613 =============================== """
from imageai.Detection.Custom import DetectionModelTrainer trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory="qr") metrics = trainer.evaluateModel(model_path="detection_model-ex-001--loss-0000.000.h5", json_path="detection_config.json", iou_threshold=0.5, object_threshold=0.3, nms_threshold=0.5)
trainer.setDataDirectory(data_directory="dataset_yolo_v3/") trainer.setTrainConfig(object_names_array=["corn", "weed"], batch_size=2, num_experiments=200, train_from_pretrained_model="pretrained-yolov3.h5") trainer.trainModel() """ Evaluate model - evaluate how every model checkpoint performs via its mAP. """ trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory="dataset_yolo_v3") metrics = trainer.evaluateModel( model_path="dataset_yolo_v3/models", json_path="dataset_yolo_v3/json/detection_config.json", iou_threshold=0.5, object_threshold=0.9, nms_threshold=0.5) """ Use model for detection """ detector = CustomObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath( "dataset_yolo_v3/models/detection_model-ex-005--loss-0014.777.h5") detector.setJsonPath("dataset_yolo_v3/json/detection_config.json") detector.loadModel() detections = detector.detectObjectsFromImage( input_image="test.jpg", output_image_path="ima-detected.jpg",
import os import glob from imageai.Detection.Custom import DetectionModelTrainer execution_path = os.getcwd() models_path = os.path.join(execution_path, "doge-identification/models/") data_path = os.path.join(execution_path, "doge-identification/") # This will force tensorflow to run on the cpu os.environ["CUDA_VISIBLE_DEVICES"] = "-1" list_of_files = glob.glob(models_path + "*.h5") latest_model_path = max(list_of_files, key=os.path.getctime) path, newest_model = os.path.split(latest_model_path) print("Newest Model: ", newest_model) trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory=data_path) metrics = trainer.evaluateModel( model_path=latest_model_path, json_path=os.path.join(execution_path, "doge-identification/json/detection_config.json"), iou_threshold=0.5, object_threshold=0.3, nms_threshold=0.5)
from imageai.Detection.Custom import DetectionModelTrainer trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory="Vier_Onderdelen_Wit") trainer.setTrainConfig( object_names_array=["Schoepen_groot", "Schijf_zilver", "Kom", "Rotor"], batch_size=4, num_experiments=100, train_from_pretrained_model="pretrained-yolov3.h5") trainer.trainModel() trainer.setDataDirectory(data_directory="Vier_Onderdelen_Wit") evaluate = trainer.evaluateModel( model_path="Vier_Onderdelen_Wit/models", json_path="Vier_Onderdelen_Wit/json/detection_config.json", iou_threshold=0.5, object_threshold=0.3, nms_threshold=0.5) x = [] for i in range(len(evaluate)): map = evaluate[i]['map'] experiment = evaluate[i]['model_file'] x.append((map, experiment)) x.sort() text_file = open("maps_vier_onderdelen_wit.txt", "wt") for i in x: e = text_file.writelines(str(i) + "\n") text_file.close()
# -*- coding: utf-8 -*- #Run this in colab or in a jupyter notebook """Prerequisites""" !pip3 install tensorflow-gpu==1.13.1 !pip3 install imageai --upgrade !wget https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/pretrained-yolov3.h5 """Change data_directory and object_names_array appropriately""" from imageai.Detection.Custom import DetectionModelTrainer trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory="/content/drive/My Drive/Football/") trainer.setTrainConfig(object_names_array=["Bundesliga", "EPL", "LaLiga"], batch_size=4, num_experiments=7, train_from_pretrained_model="pretrained-yolov3.h5") trainer.trainModel() """Pick the model with maximum mAP""" from imageai.Detection.Custom import DetectionModelTrainer trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory="/content/drive/My Drive/Football") trainer.evaluateModel(model_path="/content/drive/My Drive/Football/models", json_path="/content/drive/My Drive/Football/json/detection_config.json", iou_threshold=0.5, object_threshold=0.3, nms_threshold=0.5)
from imageai.Detection.Custom import DetectionModelTrainer trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory( data_directory="/content/drive/My Drive/project data sets/data_cleaned") trainer.evaluateModel( model_path="/content/drive/My Drive/project data sets/data_cleaned/models", json_path= "/content/drive/My Drive/project data sets/data_cleaned/json/detection_config.json", iou_threshold=0.5, object_threshold=0.3, nms_threshold=0.5)
from imageai.Detection.Custom import DetectionModelTrainer trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory="../data/") metrics = trainer.evaluateModel( model_path="../models/detection_model-ex-015--loss-0004.540.h5", json_path="../json/detection_config.json", iou_threshold=0.5, object_threshold=0.3, nms_threshold=0.5, ) print(metrics)
from imageai.Detection.Custom import DetectionModelTrainer trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory( data_directory="C:/Users/ABC/Desktop/New folder/TDAI/Data/hololens") trainer.evaluateModel( model_path="C:/Users/ABC/Desktop/New folder/TDAI/Data/hololens/models", json_path= "C:/Users/ABC/Desktop/New folder/TDAI/Data/hololens/json/detection_config.json", iou_threshold=0.5, object_threshold=0.3, nms_threshold=0.5)
from imageai.Detection.Custom import DetectionModelTrainer trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory="SymDescribeImages") metrics = trainer.evaluateModel( model_path="SymDescribeImages/models", json_path="SymDescribeImages/json/detection_config.json", iou_threshold=0.5, object_threshold=0.3, nms_threshold=0.5) print(metrics)
from imageai.Detection.Custom import DetectionModelTrainer trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory="characters") trainer.setTrainConfig(object_names_array=[ 'aeolianDune', 'beach', 'crevasseRiverBedLagDeposit', 'crevasseRiverChannelBar', 'distalBar', 'distributaryChannelFill', 'distributaryMouthBar', 'floodPlain', 'floodSwamp', 'interdistributaryEstuary', 'majorRiverBedLagDeposit', 'majorRiverChannelBar', 'meanderPointBar', 'mouthBar', 'proximalDeepseaFan', 'underwaterDistributaryChannel', 'underwaterNaturalLevee' ], batch_size=2, num_experiments=10, train_from_pretrained_model="pretrained-yolov3.h5") trainer.trainModel() metrics = trainer.evaluateModel( model_path="characters/models", json_path="characters/json/detection_config.json", iou_threshold=0.5, object_threshold=0.3, nms_threshold=0.5) print(metrics)
''' After running the training file, object_detection_custom_training.py, Run this file to evaluate the generated model from training ''' from imageai.Detection.Custom import DetectionModelTrainer trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory="hololens") trainer.evaluateModel( model_path="hololens/models", # where model is json_path="hololens/json/detection_config.json", iou_threshold= 0.5, # minimum desired inersect over union of the Mean Average Precision object_threshold=0.3, #desired minimum class score for the mAP computation nms_threshold=0.5 ) #desired Non-maximum suppression for the mAP computation.
from imageai.Detection.Custom import DetectionModelTrainer trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory="Dataset2") trainer.setTrainConfig(object_names_array=["Vehicle"], batch_size=4, num_experiments=20, train_from_pretrained_model="pretrained-yolov3.h5") trainer.trainModel() # Evaluation from imageai.Detection.Custom import DetectionModelTrainer trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory="Dataset2") metrics = trainer.evaluateModel(model_path="Dataset2/models", json_path="Dataset2/json/detection_config.json", iou_threshold=0.5, object_threshold=0.3, nms_threshold=0.5) # Detection from imageai.Detection.Custom import CustomObjectDetection def deet(fold,best): detector = CustomObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath("Dataset{}/models/{}.h5".format(fold,best)) detector.setJsonPath("Dataset{}/json/detection_config.json".format(fold)) detector.loadModel() for ikl in [1,2,3,4,5,6]: detections = detector.detectObjectsFromImage("Dataset{}/test/T{}.jpg".format(fold,ikl), output_image_path="Dataset{}/test/T{}_detected.jpg".format(fold,ikl),nms_treshold=0.1, minimum_percentage_probability=30)
from imageai.Detection.Custom import DetectionModelTrainer trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory="hololens") trainer.evaluateModel(model_path="hololens-ex-60--loss-2.76.h5", json_path="detection_config.json", iou_threshold=0.5, object_threshold=0.3, nms_threshold=0.5) # download JSON file via https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/detection_config.json # download detection model via https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/hololens-ex-60--loss-2.76.h5 """ SAMPLE RESULT Model File: hololens_detection_model-ex-09--loss-4.01.h5 Using IoU : 0.5 Using Object Threshold : 0.3 Using Non-Maximum Suppression : 0.5 hololens: 0.9613 mAP: 0.9613 =============================== """