def _transformations_experiment(data_loader: ZTFOutlierLoader,
    transformer: AbstractTransformer, dataset_name: str, class_name: str,
    save_path: str):
  # Todo: pass as param_dict
  batch_size = 128
  transform_batch_size = 1024

  (x_train, y_train), (
  x_val, y_val), (
    x_test, y_test) = data_loader.get_outlier_detection_datasets()

  mdl = TransformODModel(
      data_loader=data_loader, transformer=transformer,
      input_shape=x_train.shape[1:], results_folder_name=save_path)

  mdl.fit(x_train=x_train, x_val=x_val, transform_batch_size=transform_batch_size,
          train_batch_size=batch_size,
          epochs=EPOCHS  # int(np.ceil(200 / transformer.n_transforms))
          )

  metrics_dict = mdl.evaluate_od(
      x_train, x_test, y_test, dataset_name, class_name, x_val,
      transform_batch_size=transform_batch_size,
      additional_score_save_path_list=[save_path, mdl.specific_model_folder],
      save_hist_folder_path=mdl.specific_model_folder)
  del mdl, x_train, x_val, x_test
def _transformations_experiment(data_loader: ZTFOutlierLoader,
                                transformer: AbstractTransformer,
                                dataset_name: str, class_name: str,
                                save_path: str):
    # Todo: pass as param_dict
    batch_size = 128
    transform_batch_size = 1024

    (x_train, y_train), (x_val, y_val), (
        x_test, y_test) = data_loader.get_outlier_detection_datasets()

    mdl = TransformODModel(data_loader=data_loader,
                           transformer=transformer,
                           input_shape=x_train.shape[1:],
                           results_folder_name=save_path)

    # mdl.fit(x_train=x_train, x_val=x_val, transform_batch_size=transform_batch_size,
    #         train_batch_size=batch_size,
    #         epochs=EPOCHS  # int(np.ceil(200 / transformer.n_transforms))
    #         )
    mdl.create_specific_model_paths()
    mdl.build(tuple([None] + list(x_train.shape[1:])))
    weights_path = os.path.join(
        PROJECT_PATH,
        'results/ztf-diri-val-refact/hits/Transformer_OD_Model/PlusKernel_transformer/Transformer_OD_Model_20191127-124219/checkpoints',
        'final_weights.h5')
    print(os.path.abspath(weights_path))
    mdl.load_weights(weights_path)

    metrics_dict = mdl.evaluate_od(
        x_train,
        x_test,
        y_test,
        dataset_name,
        class_name,
        x_val,
        transform_batch_size=transform_batch_size,
        additional_score_save_path_list=[save_path, mdl.specific_model_folder],
        save_hist_folder_path=mdl.specific_model_folder)
    del mdl
        tf.config.experimental.set_memory_growth(gpu, True)

    data_loader_params = {
        loader_keys.DATA_PATH:
        os.path.join(PROJECT_PATH, '../datasets/ztf_v1_bogus_added.pkl'),
        loader_keys.VAL_SET_INLIER_PERCENTAGE:
        0.1,
        loader_keys.USED_CHANNELS: [0, 1, 2],
        loader_keys.CROP_SIZE:
        21,
        general_keys.RANDOM_SEED:
        42,
        loader_keys.TRANSFORMATION_INLIER_CLASS_VALUE:
        1
    }
    ztf_od_loader = ZTFOutlierLoader(data_loader_params)
    (x_train, y_train), (x_val, y_val), (
        x_test, y_test) = ztf_od_loader.get_outlier_detection_datasets()
    transformer = Transformer()
    model = TransformODSimpleModel(data_loader=ztf_od_loader,
                                   transformer=transformer,
                                   input_shape=x_train.shape[1:])
    model.build(tuple([None] + list(x_train.shape[1:])))
    # print(model.network.model().summary())
    weight_path = os.path.join(PROJECT_PATH, 'results', model.name,
                               'my_checkpoint_simple.h5')
    if os.path.exists(weight_path):
        model.load_weights(weight_path)
    else:
        model.fit(x_train, x_val)
    N_RUNS = 1
    ztf_params = {
        loader_keys.DATA_PATH:
        os.path.join(PROJECT_PATH, '../datasets/ztf_v1_bogus_added.pkl'),
        loader_keys.VAL_SET_INLIER_PERCENTAGE:
        0.1,
        loader_keys.USED_CHANNELS: [0, 1, 2],
        loader_keys.CROP_SIZE:
        21,
        general_keys.RANDOM_SEED:
        42,
        loader_keys.TRANSFORMATION_INLIER_CLASS_VALUE:
        1
    }
    ztf_outlier_dataset = ZTFOutlierLoader(ztf_params)
    ztf_params[loader_keys.CROP_SIZE] = None
    ztf_outlier_dataset_63 = ZTFOutlierLoader(ztf_params)
    hits_params = {
        loader_keys.DATA_PATH:
        os.path.join(PROJECT_PATH, '../datasets/HiTS2013_300k_samples.pkl'),
        loader_keys.N_SAMPLES_BY_CLASS:
        10000,
        loader_keys.TEST_PERCENTAGE:
        0.2,
        loader_keys.VAL_SET_INLIER_PERCENTAGE:
        0.1,
        loader_keys.USED_CHANNELS: [0, 1, 2, 3],
        loader_keys.CROP_SIZE:
        21,
        general_keys.RANDOM_SEED:
Пример #5
0
def best_score_evaluation(result_folder_name, epochs, patience=0):
    trainer_params = {
        param_keys.RESULTS_FOLDER_NAME: result_folder_name,
        'epochs': epochs,
        'patience': patience,
    }
    # data loaders
    hits_params = {
        loader_keys.DATA_PATH:
        os.path.join(PROJECT_PATH, '../datasets/HiTS2013_300k_samples.pkl'),
        loader_keys.N_SAMPLES_BY_CLASS:
        10000,
        loader_keys.TEST_PERCENTAGE:
        0.2,
        loader_keys.VAL_SET_INLIER_PERCENTAGE:
        0.1,
        loader_keys.USED_CHANNELS: [0, 1, 2, 3],  # [2],#
        loader_keys.CROP_SIZE:
        21,
        general_keys.RANDOM_SEED:
        42,
        loader_keys.TRANSFORMATION_INLIER_CLASS_VALUE:
        1
    }
    hits_loader = HiTSOutlierLoader(hits_params)
    ztf_params = {
        loader_keys.DATA_PATH:
        os.path.join(PROJECT_PATH, '../datasets/ztf_v1_bogus_added.pkl'),
        loader_keys.VAL_SET_INLIER_PERCENTAGE:
        0.1,
        loader_keys.USED_CHANNELS: [0, 1, 2],
        loader_keys.CROP_SIZE:
        21,
        general_keys.RANDOM_SEED:
        42,
        loader_keys.TRANSFORMATION_INLIER_CLASS_VALUE:
        1
    }
    ztf_loader = ZTFOutlierLoader(ztf_params)
    # transformers
    transformer_72 = transformations_tf.Transformer()
    trans_transformer = transformations_tf.TransTransformer()
    kernel_transformer = transformations_tf.KernelTransformer()
    plus_kernel_transformer = transformations_tf.PlusKernelTransformer()
    # trainers
    hits_trainer = Trainer(hits_loader, trainer_params)
    ztf_trainer = Trainer(ztf_loader, trainer_params)

    model_constructors_list = (TransformODModel, )
    transformers_list = (
        plus_kernel_transformer,
        kernel_transformer,
        transformer_72,
        trans_transformer,
    )
    trainers_list = (hits_trainer, )  # (ztf_trainer, hits_trainer, )
    trainer_model_transformer_tuples = list(
        itertools.product(trainers_list, model_constructors_list,
                          transformers_list))

    for trainer, model_constructor, transformer in trainer_model_transformer_tuples:
        trainer.train_model_n_times(model_constructor,
                                    transformer,
                                    trainer_params,
                                    train_times=TRAIN_TIME)

    hits_trainer.create_tables_of_results_folders()
Пример #6
0
        tf.config.experimental.set_memory_growth(gpu, True)

    ztf_params = {
        loader_keys.DATA_PATH:
        os.path.join(PROJECT_PATH, '../datasets/ztf_v1_bogus_added.pkl'),
        loader_keys.VAL_SET_INLIER_PERCENTAGE:
        0.1,
        loader_keys.USED_CHANNELS: [0, 1, 2],
        loader_keys.CROP_SIZE:
        21,
        general_keys.RANDOM_SEED:
        42,
        loader_keys.TRANSFORMATION_INLIER_CLASS_VALUE:
        1
    }
    ztf_loader = ZTFOutlierLoader(ztf_params)
    (x_train,
     y_train), (x_val,
                y_val), (x_test,
                         y_test) = ztf_loader.get_outlier_detection_datasets()
    plus_kernel_transformer = transformations_tf.PlusKernelTransformer()
    model = TransformODModel(data_loader=ztf_loader,
                             transformer=plus_kernel_transformer,
                             input_shape=x_train.shape[1:])
    weight_path = os.path.join(
        PROJECT_PATH,
        'results/best_scores_refact/best_scores_resnet_1_epochs/Transformer_OD_Model/PlusKernel_Transformer',
        'Transformer_OD_Model_20191223-192249/checkpoints/final_weights.ckpt')
    # 'Transformer_OD_Model_20191223-193141/checkpoints/final_weights.ckpt')
    model.load_weights(weight_path).expect_partial()
    met_dict = model.evaluate_od(x_train, x_test, y_test, ztf_loader.name,