Exemplo n.º 1
0
    def train(self, options):
        print('-' * 30)
        print('Loading and preprocessing train data...')
        print('-' * 30)

        imgs_train, imgs_mask_train_list = load_train_data(options)
        print('Creating and compiling model...')
        model = self.get_unet()

        for imgs_mask_train in imgs_mask_train_list:
            if not imgs_mask_train.endswith('.npy'):
                print('Ignored: ' + imgs_mask_train)
                print('-' * 30)
                continue
            print('Loading mask: ' + imgs_mask_train)
            model_name = imgs_mask_train.split('.')[0]
            imgs_mask_train = np.load(
                os.path.join(options.inputdir,
                             imgs_mask_train)).astype('float32')
            imgs_train = imgs_train.astype('float32')
            imgs_mask_train /= 255.  # scale masks to [0, 1]
            imgs_train /= 255.  # scale masks to [0, 1]

            print('-' * 30)
            print('Training for', model_name)

            weight_dir = options.outputdir + '/weights'
            if not os.path.exists(weight_dir):
                os.mkdir(weight_dir)
            model_checkpoint = ModelCheckpoint(os.path.join(
                weight_dir, model_name + '.h5'),
                                               monitor='val_loss',
                                               save_best_only=True)

            log_dir = options.outputdir + '/logs'
            if not os.path.exists(log_dir):
                os.mkdir(log_dir)
            csv_logger = CSVLogger(os.path.join(log_dir, model_name + '.txt'),
                                   separator=',',
                                   append=False)

            print('Fitting model...')
            print('-' * 30)

            model.fit(imgs_train,
                      imgs_mask_train,
                      batch_size=1,
                      epochs=options.epochs,
                      verbose=1,
                      shuffle=True,
                      validation_split=0.10,
                      callbacks=[model_checkpoint, csv_logger])
            print('Finished training: ', model_name)

        print('-' * 30)
        print('Training finished, total: ' + str(len(imgs_mask_train_list)) +
              ' models generated.')
        print('-' * 30)
Exemplo n.º 2
0
    def train(self, options):
        print('-' * 30)
        print('Loading and preprocessing train data...')
        print('-' * 30)

        imgs_train, imgs_mask_train = load_train_data(options)
        imgs_mask_train = imgs_mask_train.astype('float32')
        imgs_train = imgs_train.astype('float32')
        imgs_mask_train /= 255.  # scale masks to [0, 1]
        imgs_train /= 255.  # scale masks to [0, 1]

        print('-' * 30)
        print('Creating and compiling model...')
        print('-' * 30)

        model = self.get_unet()
        weight_dir = options.outputdir + '/weights'
        if not os.path.exists(weight_dir):
            os.mkdir(weight_dir)
        model_checkpoint = ModelCheckpoint(os.path.join(
            weight_dir, project_name + '.h5'),
                                           monitor='val_loss',
                                           save_best_only=True)

        log_dir = options.outputdir + '/logs'
        if not os.path.exists(log_dir):
            os.mkdir(log_dir)
        csv_logger = CSVLogger(os.path.join(log_dir, project_name + '.txt'),
                               separator=',',
                               append=False)

        print('-' * 30)
        print('Fitting model...')
        print('-' * 30)

        model.fit(imgs_train,
                  imgs_mask_train,
                  batch_size=1,
                  epochs=options.epochs,
                  verbose=1,
                  shuffle=True,
                  validation_split=0.10,
                  callbacks=[model_checkpoint, csv_logger])

        print('-' * 30)
        print('Training finished')
        print('-' * 30)