def train_traffic_net():
    download_traffic_net()

    trainer = ModelTraining()
    trainer.setModelTypeAsResNet()
    trainer.setDataDirectory("trafficnet_dataset_v1")
    trainer.trainModel(num_objects=4, num_experiments=200, batch_size=32, save_full_model=True, enhance_data=True)
Example #2
0
def ImageREcognition():
    model_trainer = ModelTraining()
    model_trainer.setModelTypeAsResNet()
    model_trainer.setDataDirectory("idenprof")
    model_trainer.trainModel(num_objects=10,
                             num_experiments=100,
                             enhance_data=True,
                             batch_size=32,
                             show_network_summary=True)
def main():
    model_trainer = ModelTraining()
    model_trainer.setModelTypeAsResNet()
    model_trainer.setDataDirectory("data/images")
    model_trainer.trainModel(num_objects=2,
                             num_experiments=20,
                             enhance_data=True,
                             batch_size=16,
                             show_network_summary=True)
Example #4
0
def main(path_data):
    model_trainer = ModelTraining()
    model_trainer.setModelTypeAsResNet()
    model_trainer.setDataDirectory(path_data)
    model_trainer.trainModel(num_objects=10,
                             num_experiments=20,
                             enhance_data=True,
                             batch_size=32,
                             show_network_summary=True)
Example #5
0
def training(image_directory):
    model_trainer = ModelTraining()
    model_trainer.setModelTypeAsResNet()
    model_trainer.setDataDirectory(training_image_directory)
    model_trainer.trainModel(num_objects=1,
                             num_experiments=200,
                             enhance_data=True,
                             batch_size=5,
                             show_network_summary=True)
Example #6
0
def train_func(n):

    model_trainer = ModelTraining()
    model_trainer.setModelTypeAsResNet()
    model_trainer.setDataDirectory('Dataset')
    model_trainer.trainModel(num_objects=n,
                             num_experiments=100,
                             enhance_data=True,
                             batch_size=32,
                             show_network_summary=True)
Example #7
0
def modelIrain(dataDir='data', classNum=2, epochs=100, batch_size=32):
    '''
    模型训练部分
    '''
    #创建了ModelTraining类的新实例
    model_trainer = ModelTraining()
    #将模型类型设置为ResNet
    model_trainer.setModelTypeAsResNet()
    #设置我们想要训练的数据集的路径
    model_trainer.setDataDirectory(dataDir)
    #模型训练
    '''
    num_objects:该参数用于指定图像数据集中对象的数量
    num_experiments:该参数用于指定将对图像训练的次数,也称为epochs
    enhance_data(可选):该参数用于指定是否生成训练图像的副本以获得更好的性能
    batch_size:该参数用于指定批次数量。由于内存限制,需要分批训练,直到所有批次训练集都完成为止。
    show_network_summary:该参数用于指定是否在控制台中显示训练的过程
    '''
    model_trainer.trainModel(num_objects=classNum,
                             num_experiments=epochs,
                             enhance_data=True,
                             batch_size=batch_size,
                             show_network_summary=True)
    print('Model Train Finished!!!')

    def modelPredict(model_path='data/models/model_ex-001_acc-0.500000.h5',
                     class_path='data/json/model_class.json',
                     pic_path='a.jpg',
                     classNum=2,
                     resNum=5):
        '''

        模型预测部分
        prediction_speed[模型加载的速度]:fast faster fastest
        '''
        prediction = CustomImagePrediction()
        prediction.setModelTypeAsResNet()
        prediction.setModelPath(model_path)
        prediction.setJsonPath(class_path)
        prediction.loadModel(num_objects=classNum, prediction_speed='fastest')
        prediction, probabilities = prediction.predictImage(
            pic_path, result_count=resNum)
        for eachPrediction, eachProbability in zip(predictions, probabilities):
            print(eachPrediction + " : " + str(eachProbability))

    if __name__ == '__main__':
        #模型训练
        modelTrain(dataDir='data', classNum=2, epochs=10, batch_size=8)
        #模型识别预测
        modelPredict(model_path='data/models/model_ex-001_acc-0.500000.h5',
                     class_path='data/json/model_class.json',
                     pic_path='test.jpg',
                     classNum=2,
                     resNum=5)
 def train_model(foldername,Model_Type,num_objects=2, num_experiments=1, enhance_data=False, batch_size=1, show_network_summary=True):
     model_trainer = ModelTraining()
     if Model_Type in "ResNet":
         model_trainer.setModelTypeAsResNet()
     elif Model_Type in "SqueezeNet":
         model_trainer.setModelTypeAsSqueezeNet()
     elif Model_Type in "InceptionV3":
         model_trainer.setModelTypeAsInceptionV3()
     elif Model_Type in "DenseNet":
         model_trainer.setModelTypeAsDenseNet()
     model_trainer.setDataDirectory(foldername)
     model_trainer.trainModel(num_objects=num_objects, num_experiments=num_experiments, enhance_data=enhance_data, batch_size=batch_size, show_network_summary=show_network_summary)
Example #9
0
def test_resnet_training():

    trainer = ModelTraining()
    trainer.setModelTypeAsResNet()
    trainer.setDataDirectory(data_directory=sample_dataset)
    trainer.trainModel(num_objects=10,
                       num_experiments=1,
                       enhance_data=True,
                       batch_size=16,
                       show_network_summary=True)

    assert os.path.isdir(sample_dataset_json_folder)
    assert os.path.isdir(sample_dataset_models_folder)
    assert os.path.isfile(
        os.path.join(sample_dataset_json_folder, "model_class.json"))
    assert (len(os.listdir(sample_dataset_models_folder)) > 0)
    shutil.rmtree(os.path.join(sample_dataset_json_folder))
    shutil.rmtree(os.path.join(sample_dataset_models_folder))
Example #10
0
def TrainModel(files, Classes, Epochs, BatchSize):
    model_trainer = ModelTraining()  #create an instance for de model training
    model_trainer.setModelTypeAsResNet(
    )  #set model to ResNet NN (SqueezeNet, ResNet, InceptionV3 and DenseNet)
    model_trainer.setDataDirectory(
        files)  #folder that contains train and test sets
    '''
    train model function
    number_objects : number of clases
    num_experiments : number of iterations (epochs)
    Enhance_data (Optional) : if true, creates modified copies of the images to maximize accuracy (but more process cost)
    batch_size: number of images that the model trainer will study at once
    Show_network_summary (Optional) : if true, shows the structure of the model type
    '''
    model_trainer.trainModel(num_objects=Classes,
                             num_experiments=Epochs,
                             enhance_data=True,
                             batch_size=BatchSize,
                             show_network_summary=True)
from imageai.Prediction.Custom import ModelTraining

model_trainer = ModelTraining()
model_trainer.setModelTypeAsResNet()
model_trainer.setDataDirectory(r"D:/python/imageAI_model_train/pets")
model_trainer.trainModel(num_objects=2,
                         num_experiments=100,
                         enhance_data=True,
                         batch_size=16,
                         show_network_summary=True)
from imageai.Prediction.Custom import ModelTraining

model_trainer = ModelTraining(
)  # type of training algorithm in this case basic NN
model_trainer.setModelTypeAsResNet(
)  # defines the type of model that will be stored, in this case it will be a simple .h5 file
model_trainer.setDataDirectory(
    "idenprof")  # where the AI will look for data to train it
model_trainer.trainModel(num_objects=2,
                         num_experiments=200,
                         batch_size=32,
                         show_network_summary=True)
# num_objects is the total number of different types of objects, eg: chef, car, cat
# num_experiments is the total number of epochs or the total number of times an "experiment" is run
# batch_size is the total number of images tested in an epoch
Example #13
0
from imageai.Prediction.Custom import ModelTraining

# Train using the images found in the data subdirectory
trainer = ModelTraining()
trainer.setModelTypeAsResNet()
trainer.setDataDirectory("data")
trainer.trainModel(
    num_objects=15,
    num_experiments=50,
    enhance_data=True,
    save_full_model=True,
    batch_size=25,
    show_network_summary=True,
    transfer_from_model="resnet50_weights_tf_dim_ordering_tf_kernels.h5",
    initial_num_objects=1000,
    transfer_with_full_training=True)
Example #14
0
from imageai.Prediction.Custom import ModelTraining

model_trainer = ModelTraining()
model_trainer.setModelTypeAsResNet() # others include SqueezeNet, ResNet, InceptionV3 and DenseNet
model_trainer.setDataDirectory("idenprof") # dataset
model_trainer.trainModel(num_objects=10, # prediction classes
                         num_experiments=200,
                         enhance_data=True, # allow data augmentation
                         batch_size=32, # images per cycle
                         show_network_summary=True)
    extract1.extractall(DATASET_TRAIN_DIR)
    extract1.close()

    print("Extracting idenprof-train2.zip")
    extract2 = ZipFile(TRAIN_ZIP_TWO)
    extract2.extractall(DATASET_TRAIN_DIR)
    extract2.close()



if(len(os.listdir(DATASET_TEST_DIR)) < 10):
    if (os.path.exists(TEST_ZIP) == False):
        print("Downloading idenprof-test.zip")

        data = requests.get("https://github.com/OlafenwaMoses/IdenProf/releases/download/v1.0/idenprof-test.zip", stream=True)

        with open(TEST_ZIP, "wb") as file:
            shutil.copyfileobj(data.raw, file)
        del data

    print("Extracting idenprof-test.zip")
    extract = ZipFile(TEST_ZIP)
    extract.extractall(DATASET_TEST_DIR)
    extract.close()


model_trainer = ModelTraining()
model_trainer.setModelTypeAsResNet()
model_trainer.setDataDirectory(DATASET_DIR)
model_trainer.trainModel(num_objects=10, num_experiments=100, enhance_data=True, batch_size=32, show_network_summary=True)
from imageai.Prediction.Custom import ModelTraining

model_trainer = ModelTraining(
)  ##create an instance of the ModelTraining class

model_trainer.setModelTypeAsResNet(
)  ##set your instance property and start the traning process.
##this function sets the model type of the training instance you created to the ResNet
##model, which means the ResNet algorithm will be trained on your dataset

model_trainer.setDataDirectory(
    r"C:\Users\Andreas Thoma\Desktop\Mathimata\EPL445\Project\Tortillas")
## accepts a string which must be the path to the folder that contains the test and train subfolder of your image dataset

model_trainer.trainModel(num_objects=2,
                         num_experiments=5,
                         enhance_data=True,
                         batch_size=32,
                         show_network_summary=True)
##this is the function that starts the training process. Once it starts, it will create a JSON file in
##the dataset/json folder (e.g Tortillas/json) which contains the mapping of the classes of the dataset.
##The JSON file will be used during custom prediction to produce reults