Exemple #1
0
def train(classes,
          dataset_path,
          pretrained_model="mask_rcnn_coco.h5",
          output_path="./Test",
          batch_size=1,
          num_epochs=300,
          network_type="resnet101"):

    if not os.path.exists(output_path):
        os.mkdir(output_path)

    classes = ['BG'] + classes
    num_classes = len(classes) - 1
    print(num_classes)

    train_maskrcnn = instance_custom_training()
    train_maskrcnn.modelConfig(network_backbone=network_type,
                               num_classes=num_classes,
                               batch_size=batch_size)
    train_maskrcnn.config.class_names = classes
    train_maskrcnn.load_pretrained_model(pretrained_model)
    train_maskrcnn.load_dataset(dataset_path)

    train_maskrcnn.config.NUM_CLASSES = num_classes
    # train_maskrcnn.config.IMAGE_META_SIZE=14

    train_maskrcnn.train_model(num_epochs=num_epochs,
                               augmentation=True,
                               path_trained_models=output_path)
    print("Done training")
Exemple #2
0
 def train_model(self):
     train_maskrcnn = instance_custom_training()
     train_maskrcnn.modelConfig(network_backbone="resnet101", num_classes=1, batch_size=5)
     train_maskrcnn.load_pretrained_model("../model/mask_rcnn_model.036-0.139239.h5")
     self.log.info('Starting training model detection...')
     train_maskrcnn.load_dataset("images")
     train_maskrcnn.train_model(num_epochs=100, augmentation=True,  path_trained_models="model")
     train_maskrcnn.evaluate_model("model/mask_rcnn_model.003-0.700727.h5")
     self.log.info('Finished training')
Exemple #3
0
def trainFromFolder(model, input, output):
  """
  Trains AI models from the input folder and puts the most successful models in the output folder.
  Note that for this to work, there needs to be both images and JSON files in a Train folder and Test folder in the input folder
  This format is easily made using labelme

  Args:
    model: The file path to the model to start training from
    input: The file path to the data to train from
    output: The file path to store new models in
  """
  train_maskrcnn = instance_custom_training()
  train_maskrcnn.modelConfig(network_backbone = "resnet101", num_classes= 1, batch_size = 2)
  train_maskrcnn.load_pretrained_model(model)
  train_maskrcnn.load_dataset(input)
  train_maskrcnn.train_model(num_epochs = 300, augmentation=True,  path_trained_models = output)
Exemple #4
0
def train(dataset, batch, valid, classes):
    ds = Dataset(dataset)

    print("Splitting up data...")
    # Split set into validation and train
    for folder in ['train', 'test']:
        path = os.path.join(ds.seg_anno_path, folder)
        if os.path.isdir(path):
            shutil.rmtree(path)
        os.mkdir(path)

    jsons = [x for x in os.listdir(ds.seg_anno_path) if x.endswith('.json') and 'test' not in x and 'train' not in x]
    random.shuffle(jsons)

    valid_size = int(len(jsons) * valid)
    valid_list = jsons[:valid_size]
    train_list = jsons[valid_size:]

    for lst, folder in zip([valid_list, train_list],['test','train']):
        path = os.path.join(ds.seg_anno_path, folder)
        for file in lst:
            shutil.copy2(os.path.join(ds.seg_anno_path, file), os.path.join(path, file))
            shutil.copy2(os.path.join(ds.seg_anno_path, file.replace('.json','.png')), os.path.join(path, file.replace('.json','.png')))

    print("Data Split.")
    default_model_path = r'models/segmentation/mask_rcnn_coco.h5'

    if not os.path.isfile(default_model_path):
        print("Base model not found.\nDownloading...")
        url = "https://github.com/ayoolaolafenwa/PixelLib/releases/download/1.2/mask_rcnn_coco.h5"
        r = requests.get(url, allow_redirects=True)
        open(default_model_path, 'wb').write(r.content)

    train_maskrcnn = instance_custom_training()
    train_maskrcnn.modelConfig(network_backbone = "resnet101", num_classes = classes, batch_size = batch)
    train_maskrcnn.load_pretrained_model(default_model_path)


    pth = os.path.abspath(p().SEG_MODELS)     
    if classes > 1:
        pth = os.path.join(os.path.abspath(p().SEG_MODELS)  ,'multi')
    #Train
    train_maskrcnn.load_dataset(ds.seg_anno_path)
    train_maskrcnn.train_model(num_epochs = 300, augmentation=True,  path_trained_models = pth)
Exemple #5
0
import pixellib
from pixellib.custom_train import instance_custom_training

vis_img = instance_custom_training()
vis_img.load_dataset("jy")
vis_img.visualize_sample()
Exemple #6
0
import pixellib
from pixellib.custom_train import instance_custom_training
import tensorflow as tf
import warnings
warnings.filterwarnings("ignore")

gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
    try:
        for gpu in gpus:
            tf.config.experimental.set_memory_growth(gpu, True)
    except RuntimeError as e:
        print(e)

train_maskrcnn = instance_custom_training()
train_maskrcnn.modelConfig(network_backbone="resnet50",
                           num_classes=2,
                           batch_size=4)  #network_backbone = "resnet50"
train_maskrcnn.load_pretrained_model("mask_rcnn_coco.h5")
train_maskrcnn.load_dataset("jy2")
train_maskrcnn.train_model(num_epochs=300,
                           augmentation=True,
                           path_trained_models="mask_rcnn_models")