def train_model(): trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory="examples/specjale") trainer.setTrainConfig(object_names_array=["specjal"], batch_size=4, num_experiments=10, train_from_pretrained_model="models/yolo.h5") trainer.trainModel()
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 train(): trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory="") trainer.setTrainConfig( object_names_array=["Container", "Dent", "Rust", "Crack"], batch_size=4, num_experiments=100, train_from_pretrained_model="pretrained-yolov3.h5")
def train(self, data_dir, classes, batch_size, epochs): trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory=data_dir) trainer.setTrainConfig(object_names_array=classes, batch_size=batch_size, num_experiments=epochs, train_from_pretrained_model=PRETRAINED_MODEL) trainer.trainModel()
def train_detection_model(): from imageai.Detection.Custom import DetectionModelTrainer trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory="fire-dataset") trainer.setTrainConfig(object_names_array=["fire"], batch_size=8, num_experiments=100, train_from_pretrained_model="pretrained-yolov3.h5") trainer.trainModel()
def entrenarModelo(): trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory="2_RESPUESTA") trainer.setTrainConfig(object_names_array=["RESPUESTA"], batch_size=2, num_experiments=50, train_from_pretrained_model="pretrained-yolov3.h5") trainer.trainModel()
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)
class Trainer(): def __init__(self): self.trainer = DetectionModelTrainer() self.trainer.setModelTypeAsYOLOv3() # Set name of dataset directory self.trainer.setDataDirectory(data_directory="dog_dataset") self.trainer.setTrainConfig(object_names_array=['dog'], batch_size=4, num_experiments=10, train_from_pretrained_model='yolov3-dogs.h5') def train(self): self.trainer.trainModel()
class Trainer(): def __init__(self): self.trainer = DetectionModelTrainer() self.trainer.setModelTypeAsYOLOv3() # Set name of dataset directory self.trainer.setDataDirectory(data_directory="wheels_dataset") self.trainer.setTrainConfig(object_names_array=['wheelchair', 'pull trolley', 'push trolley'], batch_size=8, num_experiments=10, train_from_pretrained_model='wheels-yolov3.h5') def train(self): self.trainer.trainModel()
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 trainer(): trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory=data_directory) trainer.setTrainConfig( object_names_array=["seal", "no_seal"], batch_size=4, num_experiments=50, train_from_pretrained_model="pretrained-yolov3.h5", ) trainer.trainModel()
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 train_detection_model(): from imageai.Detection.Custom import DetectionModelTrainer trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory="fire-dataset") trainer.setTrainConfig(object_names_array=["fire"], batch_size=8, num_experiments=100, train_from_pretrained_model="pretrained-yolov3.h5") # download 'pretrained-yolov3.h5' from the link below # https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/pretrained-yolov3.h5 trainer.trainModel()
def train(): trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory=os.path.join('drive','MyDrive','train_data', 'circle')) trainer.setTrainConfig( object_names_array=['circle'], batch_size=4, num_experiments=200, train_from_pretrained_model='pretrained-yolov3.h5' ) trainer.trainModel()
def trainer( data_directory="plates", object_names_array=["LP"], batch_size=4, num_experiments=2, train_from_pretrained_model="plates/models/detection_model-ex-002--loss-0007.222.h5" ): trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory) trainer.setTrainConfig(object_names_array, batch_size, num_experiments, train_from_pretrained_model) trainer.trainModel()
def test_detection_training(clear_keras_session): trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory=sample_dataset) trainer.setTrainConfig(object_names_array=["hololens"], batch_size=2, num_experiments=1, train_from_pretrained_model=pretrained_model) trainer.trainModel() assert os.path.isdir(sample_dataset_json_folder) assert os.path.isdir(sample_dataset_models_folder) assert os.path.isdir(sample_dataset_cache_folder) assert os.path.isfile(os.path.join(sample_dataset_json_folder, "detection_config.json")) shutil.rmtree(os.path.join(sample_dataset_json_folder)) shutil.rmtree(os.path.join(sample_dataset_models_folder)) shutil.rmtree(os.path.join(sample_dataset_cache_folder))
def keurmerk_train(data, model): """Trains YOLOv3 model from pretrained model. Arguments: data {string} -- Directory where the image and annotation data is stored according to the specified structure. model {string} -- Directory where the chosen model is stored. """ # Set model type trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() labels = [ "fairtrade", "planetproof", "utz", "organic", "vegan", "weidemelk", "bio", "eko", "asc", "msc", "ebio", "beterleven", ] # Data folder trainer.setDataDirectory(data_directory=DATA_DIR) # Training configuration trainer.setTrainConfig( object_names_array=labels, batch_size=4, num_experiments=100, train_from_pretrained_model=MODEL_PATH, ) trainer.trainModel()
def train_model(self, data_dir, user, objs_array, model=None, new_model=False): """ Train a specific model according with a set of data Args: data_dir (str): directory with the YOLO training data (pictures and annotations) user (str): registered user which the model belongs objs_array (list): list containing the names of unique objects to be trained for model (str): full path of the model file to be loaded new_model (bool): True if after training a new model file will be generated Returns: object: trained model """ if new_model: model_path = '' else: self.load_model(user=user, model_path=model) model_path = self.model_path model_trainer = DetectionModelTrainer() model_trainer.setModelTypeAsYOLOv3() model_trainer.setDataDirectory(data_directory=data_dir) model_trainer.setTrainConfig(object_names_array=list(set(objs_array)), batch_size=4, num_experiments=200, train_from_pretrained_model=model_path) model_trainer.trainModel() if new_model: self.version_model(user=user) self.model = model_trainer return self.model
def train_model(self, model_pretrained, label_list, batch_size=2, epoch=10, use_pretrained_model=False): self.__use_pretrained_model = use_pretrained_model if self.__use_pretrained_model: trainer = DetectionModelTrainer() # fixe le type du model comme etant yoloV3 trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(self.__data_dir) trainer.setTrainConfig( object_names_array=label_list, batch_size=batch_size, num_experiments=epoch, train_from_pretrained_model=model_pretrained) trainer.trainModel()
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)
def main(): if (len(sys.argv) < 3): print("Usage: " + sys.argv[0] + " <dataset_dir> <pre_trained_model>") exit(-1) dataset_dir = sys.argv[1] pre_trained_model = sys.argv[2] if (len(sys.argv) > 2): image_name = sys.argv[2] else: print("Enter image or directory name: ") image_name = input() trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory=dataset_dir) trainer.setTrainConfig(object_names_array=[ "Ripe Strawberry", "Green Strawberry", "Bad Strawberry" ], batch_size=4, num_experiments=40, train_from_pretrained_model=pre_trained_model) trainer.trainModel()
from imageai.Detection.Custom import DetectionModelTrainer trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory="bottles") trainer.setTrainConfig( object_names_array=["PET", "HDPE(1)", "HDPE(2)", "PVC", "Opaque", "PP"], batch_size=2, num_experiments=200, train_from_pretrained_model="pretrained-yolov3.h5") trainer.trainModel()
from imageai.Detection.Custom import DetectionModelTrainer trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() # others can include RetinaNet trainer.setDataDirectory(data_directory="inputs/hololens") trainer.setTrainConfig( object_names_array=["hololens" ], # an array of names of all objects in your dataset batch_size=4, num_experiments=100, train_from_pretrained_model="models/pretrained-yolov3.h5" ) # use transfer learning from a pretrained YOLO model trainer.trainModel()
# get the absolute image path for the image from the annotation img_abs_path = os.path.join(images_directory, img_filename) if os.path.isfile(img_abs_path): # image corresponding to the annotation exists # print(img_abs_path + " exists") pass else: print(img_abs_path + " does not exists") os.remove(abs_annotation_file_path) print(abs_annotation_file_path + " Deleted") print(count) !wget https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/pretrained-yolov3.h5 -P '/gdrive/My Drive/BE Project 2019 - 2020/final_training_folder/models/' import os from imageai.Detection.Custom import DetectionModelTrainer model_trainer = DetectionModelTrainer() model_trainer.setModelTypeAsYOLOv3() model_trainer.setDataDirectory(data_directory_path) yolo_path = os.path.join(os.path.join(execution_path, "models"), "pretrained-yolov3.h5") model_trainer.setTrainConfig(object_names_array=["glass", "metal", "paper", "plastic"], batch_size=10, num_experiments=50, train_from_pretrained_model=yolo_path) model_trainer.trainModel()
from imageai.Detection.Custom import DetectionModelTrainer manaf = DetectionModelTrainer() manaf.setModelTypeAsYOLOv3() manaf.setDataDirectory(data_directory="AI-ny") manaf.setTrainConfig(object_names_array=["Kartong", "Plast", "Metall"], batch_size=2, num_experiments=180, train_from_pretrained_model="pretrained-yolov3.h5") manaf.trainModel()