def create_train_test_valid_folders(dataset_train_test_valid_directory,
                                        dataset_folders_to_create,
                                        dataset_classes):
        for dirname in dataset_folders_to_create:
            Utils.create_folder_if_not_exists(
                os.path.join(dataset_train_test_valid_directory, dirname))

            for class_name in dataset_classes:
                Utils.create_folder_if_not_exists(
                    os.path.join(dataset_train_test_valid_directory, dirname,
                                 class_name))
Esempio n. 2
0
    def train_models(dataset_dataloader, dataset_test_dir_name,
                     dataset_train_dir_name, dataset_valid_dir_name,
                     training_cnn_models_to_train, training_criterion,
                     training_dropout, training_epochs_early_stopping,
                     training_epochs_count, training_feature_extract,
                     training_learning_rate, training_lr_gamma,
                     training_lr_step_size, training_model_output_directory,
                     training_momentum, training_optimizer,
                     training_save_best_model_enabled, training_scheduler,
                     training_use_gpu, training_use_early_stopping,
                     training_use_pretrained_models, training_use_softmax,
                     training_weight_decay, signals):
        for i, model_id in enumerate(training_cnn_models_to_train):
            signals['label_training_model_name_text_changed'].emit(model_id)
            signals['console_append'].emit('----------\n' + model_id +
                                           '\n----------')

            model, accuracy_loss_history = ModelTraining.train_model(
                model_id=model_id,
                dataset_dataloader=dataset_dataloader,
                dataset_test_dir_name=dataset_test_dir_name,
                dataset_train_dir_name=dataset_train_dir_name,
                dataset_valid_dir_name=dataset_valid_dir_name,
                training_criterion=training_criterion,
                training_dropout=training_dropout,
                training_epochs_count=training_epochs_count,
                training_epochs_early_stopping=training_epochs_early_stopping,
                training_use_gpu=training_use_gpu,
                training_use_early_stopping=training_use_early_stopping,
                training_feature_extract=training_feature_extract,
                training_learning_rate=training_learning_rate,
                training_lr_gamma=training_lr_gamma,
                training_lr_step_size=training_lr_step_size,
                training_momentum=training_momentum,
                training_optimizer=training_optimizer,
                training_scheduler=training_scheduler,
                training_use_pretrained_models=training_use_pretrained_models,
                training_use_softmax=training_use_softmax,
                signals=signals,
                training_weight_decay=training_weight_decay)

            if training_save_best_model_enabled:
                path = Utils.create_folder_if_not_exists(
                    training_model_output_directory)
                ModelIO.save(
                    model=model,
                    model_id=model_id,
                    output_dir=path,
                    classes=dataset_dataloader[dataset_train_dir_name].dataset.
                    classes,
                    feature_extract=training_feature_extract,
                    use_pretrained=training_use_pretrained_models,
                    training_dropout=training_dropout,
                    training_use_softmax=training_use_softmax)

            signals['plot_train_valid_acc_loss_graph'].emit(
                accuracy_loss_history, model_id)
            signals['progressBar_training_set_value_changed'].emit(
                (i + 1) / len(training_cnn_models_to_train) * 100)
Esempio n. 3
0
    def save(model, output_dir, model_id, classes, feature_extract,
             use_pretrained, training_dropout, training_use_softmax):
        output_dir = Utils.create_folder_if_not_exists(output_dir)

        filename = model_id + DELIMITER + datetime.now().strftime(
            "%Y%m%d_%H%M%S") + '.pth'
        torch.save(model, os.path.join(output_dir, filename))
        torch.save(
            {
                'classes': classes,
                'feature_extract': feature_extract,
                'model_id': model_id,
                'model_state_dict': model.state_dict(),
                'use_pretrained': use_pretrained,
                'training_dropout': training_dropout,
                'training_use_softmax': training_use_softmax
            }, os.path.join(output_dir, filename))
Esempio n. 4
0
    def train_valid_acc_loss_graph(history, model_name, save,
                                   training_evaluation_directory):
        history = pd.DataFrame(
            history,
            columns=['train_acc', 'train_loss', 'valid_acc', 'valid_loss'])

        fig = plt.figure(figsize=(15, 6))
        locator = matplotlib.ticker.MultipleLocator(1)
        fig.suptitle(model_name)

        subfig = fig.add_subplot(122)
        fig.gca().xaxis.set_major_locator(locator)
        subfig.plot(history['train_acc'], label="Training")
        subfig.plot(history['valid_acc'], label="Validation")
        subfig.set_title('Model Accuracy')
        subfig.set_xlabel('Epoch')
        subfig.set_ylabel('Percentage')
        subfig.legend(loc='upper left')

        subfig = fig.add_subplot(121)
        fig.gca().xaxis.set_major_locator(locator)
        subfig.plot(history['train_loss'], label="Training")
        subfig.plot(history['valid_loss'], label="Validation")
        subfig.set_title('Model Loss')
        subfig.set_xlabel('Epoch')
        subfig.set_ylabel('Loss')
        subfig.legend(loc='upper right')

        plt.show()

        if save:
            path = os.path.join(training_evaluation_directory, model_name)
            path = Utils.create_folder_if_not_exists(path)

            plt.savefig(os.path.join(
                path, 'valid_acc_loss_graph_' +
                datetime.now().strftime("%Y%m%d_%H%M%S") + '.png'),
                        bbox_inches='tight')