Пример #1
0
def get_accuracy_small_net(train_set_size_for_knn, class_name, class_path, random_seed, test_dataset):
    class_json_location = class_path
    max_epochs = 50
    classifier_parser = Classifier.get_args_parser()
    parse_args_list = [
        '--save_model',
        '--learning_rate', '0.0003200196036593708',
        '--total_dataset_size', f'{train_set_size_for_knn}',
        '--max_epochs', f'{max_epochs}',
        '--arch', 'BasicConvNetLatent',
        '--name', 'classifier',
        '--latent_space_size', '35',
        '--scale_exp',
        '--scale_exp_class_name', class_name,
        '--scale_exp_class_path', class_json_location,
        '--num_classes', '2',
        '--radii', f'[8]',
        '--use_gpu'
    ]

    random.seed(random_seed)
    torch.manual_seed(random_seed)
    cudnn.deterministic = True
    np.random.seed(random_seed)
    import pytorch_lightning as pl
    logger = TensorBoardLogger(EXP_LOGS_PATH, name=f'{random_seed}_{class_name}_train_size{train_set_size_for_knn}')

    trainer = pl.Trainer(max_epochs=max_epochs, logger=logger)
    classifier_current_args = classifier_parser.parse_args(parse_args_list +
                                                           ['--scale_exp_random_seed', str(random_seed),
                                                            '--original_radii', f'[[8]]',
                                                            ])
    model = Classifier(classifier_current_args)
    trainer.fit(model)
    test_accuracy = model.get_accuracy_for_small_dataset(test_dataset)
Пример #2
0
    def test_run(self):
        parser = Classifier.get_args_parser()
        autoencoder_check_args = parser.parse_args([
            '--save_model', '--learning_rate', '1e-2', '--max_epochs', '1',
            '--total_dataset_size', '1000', '--arch', 'BasicAmphibAutoencoder',
            '--svm_classify_latent_space', '--name', 'autoencoder',
            '--pytorch_module', 'Autoencoder', '--test_knn',
            '--random_set_size_for_svm', '10'
        ])

        lab = TaskHandler(autoencoder_check_args)
        lab.run()

        parser = Classifier.get_args_parser()
        autoencoder_check_args = parser.parse_args([
            '--save_model', '--learning_rate', '1e-2', '--max_epochs', '1',
            '--total_dataset_size', '1000', '--arch', 'BasicAmphibAutoencoder',
            '--svm_classify_latent_space', '--name', 'autoencoder',
            '--pytorch_module', 'Autoencoder', '--random_set_size_for_svm',
            '10', '--svm_classify_latent_space', '--test_knn'
        ])
        lab = TaskHandler(autoencoder_check_args)
        lab.run()

        classifier_parser = Classifier.get_args_parser()

        classifier_check_args = classifier_parser.parse_args([
            '--save_model',
            '--learning_rate',
            '1e-4',
            '--max_epochs',
            '1',
            '--total_dataset_size',
            '100',
            '--arch',
            'BasicConvNetLatent',
            '--svm_classify_latent_space',
            '--name',
            'classifier',
            '--pytorch_module',
            'Classifier',
            '--random_set_size_for_svm',
            '100',
            '--test_set_size_for_svm',
            '38',
            '--random_set_size_for_knn',
            '100',
            '--latent_space_size',
            '50',
            '--knn_method_for_typical_choosing',
            'group_from_file',
            '--random_set_size_for_svm_special',
            '1000',
            '--test_knn',
            '--svm_classify_latent_space',
        ])

        lab = TaskHandler(classifier_check_args)
        lab.run()
def std_mean_accuracy_radius_class(train_set_size_for_scales_experiment, random_seeds, MAX_EPOCHS,
                                   original_radii_to_check, EXP_LOGS_PATH, class_name, only_higher_than=-999):
    class_json_location = BASE_LOCATION + f'data/overpass_classes_data/{class_name}_(45,5,50,15).json'
    if class_name == 'streams':
        class_json_location = BASE_LOCATION + f'data/overpass_classes_data_all/overpass_classes_data/{class_name}_(45,5,50,15).json'

    classifier_parser = Classifier.get_args_parser()
    parse_args_list = [
        '--save_model',
        '--learning_rate', '0.0003200196036593708',
        '--total_dataset_size', f'{train_set_size_for_scales_experiment}',
        '--arch', arch,
        '--name', 'classifier',
        '--latent_space_size', '35',
        '--scale_exp',
        '--scale_exp_class_name', class_name,
        '--scale_exp_class_path', class_json_location,
        '--scale_exp_only_higher_than', str(only_higher_than),
        '--num_classes', '2',
        '--radii', f'[{RADII}]',
    ]
    validation_accuracies_means = []
    validation_accuracies_stds = []
    for original_radii in original_radii_to_check:
        resulted_accuracies_this_original_radiis_list = []
        for random_seed in random_seeds:
            random.seed(random_seed)
            torch.manual_seed(random_seed)
            cudnn.deterministic = True
            np.random.seed(random_seed)
            logger = TensorBoardLogger(EXP_LOGS_PATH, name=f'{random_seed}_{original_radii}')
            trainer = pl.Trainer(max_epochs=MAX_EPOCHS, logger=logger)
            classifier_current_args = classifier_parser.parse_args(parse_args_list +
                                                                   ['--scale_exp_random_seed', str(random_seed),
                                                                    '--original_radii', f'[[{original_radii}]]',
                                                                    ])
            model = Classifier(classifier_current_args)
            trainer.fit(model)
            random.seed(random_seed)
            torch.manual_seed(random_seed)
            cudnn.deterministic = True
            np.random.seed(random_seed)
            test_dataset = OneVsRandomDataset([[original_radii]], TEST_SIZE,
                                              VALIDATION_HALF,
                                              class_json_location,
                                              # f'scale_exp_{self.scale_exp_class_name}_vs_random_validation',
                                              radii=model.radii, random_seed=random_seed)
            test_accuracy = model.get_accuracy_for_small_dataset(test_dataset)

            resulted_accuracies_this_original_radiis_list.append(test_accuracy)
        validation_accuracies_means.append(np.mean(resulted_accuracies_this_original_radiis_list))
        validation_accuracies_stds.append(np.std(resulted_accuracies_this_original_radiis_list))
    return validation_accuracies_means, validation_accuracies_stds
Пример #4
0
def load_final_model(final_model_name: str):
    load_path = os.path.join(FINAL_MODEL_DIR, final_model_name)
    global final_model_classifier
    final_model_classifier = Classifier(SUPERRESOLUTION_HPARAMS)
    final_model_classifier.load_state_dict(
        torch.load(load_path, map_location=torch.device('cpu')))
    final_model_classifier.eval()
Пример #5
0
random.seed(FINAL_SEED)
torch.manual_seed(FINAL_SEED)
cudnn.deterministic = True
np.random.seed(FINAL_SEED)

################################################################################
# Load the final model
################################################################################
FINAL_MODEL_DIR = BASE_LOCATION + 'data/final_model'
FINAL_HPARAMS = Classifier.get_args_parser().parse_args([
    '--total_dataset_size',
    '2500',
    '--arch',
    'BasicConvNetLatent',
    '--name',
    'final_model',
    '--pytorch_module',
    'Classifier',
    '--latent_space_size',
    '35',
    '--num_classes',
    '4',
])

SUPERRESOLUTION_HPARAMS = Classifier.get_args_parser().parse_args([
    '--save_model', '--index_in', '1', '--index_out', '0', '--learning_rate',
    '0.0015', '--max_epochs', '300', '--total_dataset_size', '100000',
    '--arch', 'UNet', '--svm_classify_latent_space',
    '--knn_method_for_typical_choosing', 'regular', '--name',
    'Superresolution', '--pytorch_module', 'Superresolution',
    '--random_set_size_for_svm', '2000', '--latent_space_size', '600',
    '--svm_classify_latent_space', '--test_knn', '--original_radiis',
from topo2vec.background import LOAD_CLASSES_LARGE
from topo2vec.constants import MULTICLASS_LOGS_PATH
from topo2vec.task_handler import TaskHandler
from topo2vec.modules import Classifier

classifier_parser = Classifier.get_args_parser()

######################################################################################################
# an ordinary classifier experiment - change the "classifier_regular_args" to make other experiments #
######################################################################################################

classifier_regular_args = classifier_parser.parse_args([
    '--save_model', '--learning_rate', '1e-4', '--max_epochs', '100',
    '--total_dataset_size', '1000', '--arch', 'BasicConvNetLatent',
    '--svm_classify_latent_space', '--name', 'classifier', '--pytorch_module',
    'Classifier', '--random_set_size_for_svm', '100',
    '--random_set_size_for_svm_special', '10', '--test_set_size_for_svm',
    '100', '--svm_classify_latent_space', '--latent_space_size', '35',
    '--special_classes_for_validation', '["alpine_huts", "antenas"]',
    '--original_radiis', '[[8]]', '--radii', '[8]', '--different_scales',
    '--logs_path', MULTICLASS_LOGS_PATH, '--use_gpu'
])
lab = TaskHandler(classifier_regular_args)
lab.run()
Пример #7
0
import os

import torch

from topo2vec.constants import BASE_LOCATION
from topo2vec.models.basic_conv_net_latent_dtm import ResNet18
from topo2vec.modules import Classifier, Autoencoder, Superresolution, pix2pix
from topo2vec.models import BasicConvNetLatentDTM
from topo2vec.modules import Classifier, Autoencoder, Superresolution, pix2pix

FINAL_MODEL_DIR = BASE_LOCATION + 'data/final_model'
FINAL_HPARAMS = Classifier.get_args_parser().parse_args([
    '--total_dataset_size', '2500', '--arch', 'BasicConvNetLatent', '--name',
    'final_model', '--pytorch_module', 'Classifier', '--latent_space_size',
    '35', '--num_classes', '4', '--svm_classify_latent_space',
    '--original_radii', '[[8]]', '--radii', '[8]'
])


def load_model_from_file(
        final_model_name='classifier_BasicConvNetLatent_[8, 16, 24]_lr_0.0009704376798307045_size_10000_num_classes_4_latent_size_35.pt',
        hparams=FINAL_HPARAMS,
        final_dir=True):
    if final_dir:
        load_path = os.path.join(FINAL_MODEL_DIR, final_model_name)
    else:
        load_path = final_model_name
    final_model_classifier = eval(hparams.pytorch_module)(hparams)

    final_model_classifier.load_state_dict(
        torch.load(load_path, map_location=torch.device('cpu')))