print('Model loaded')
                            #for eval_thresh in reversed([0.9, 0.91, 0.92, 0.93, 0.94,
                            #                             0.95, 0.96, 0.97, 0.98, 0.99,
                            #                             0.992, 0.994, 0.996, 0.998, 0.999,
                            #                             3.0]):

                            for eval_thresh in reversed([
                                    0.9, 0.91, 0.92, 0.93, 0.94, 0.95, 0.96,
                                    0.97, 0.98, 0.99, 0.992, 0.994, 0.996,
                                    0.998, 0.999, 0.999, 0.9991, 0.9992,
                                    0.9993, 0.9994, 0.9995, 0.9996, 0.9997,
                                    0.9998, 0.9999, 3.0
                            ]):
                                #for eval_thresh in reversed([0.9991, 0.9992, 0.9993, 0.9994, 0.9995, 0.9996, 0.9997, 0.9998, 0.9999, 3.0]):
                                #change_all_pca_layer_thresholds_and_inject_random_directions(eval_thresh, model, verbose=False)
                                sat, indims, fsdims, lnames = change_all_pca_layer_thresholds(
                                    eval_thresh, network=model)
                                start = time()
                                print('Changed model threshold to',
                                      eval_thresh)
                                #model = model.to(trainer.device)
                                #trainer.model = model
                                acc, loss = trainer.test(False)
                                print('InDims:', sum(indims), 'Acc:', acc,
                                      'Loss:', loss, 'for', model.name,
                                      'at threshold:', eval_thresh)

                                model_names.append(model.name)
                                accs.append(acc)
                                losses.append(loss)
                                dims.append(sum(indims))
                                inference_thresholds.append(eval_thresh)
Example #2
0
 print('Automatized experiment schedule enabled using', args.json_file)
 config_dict = json.load(open(args.json_file, 'r'))
 thresholds = [.99] if not 'threshs' in config_dict else config_dict['threshs']
 dss = config_dict['dataset'] if isinstance(config_dict['dataset'], list) else [config_dict['dataset']]
 optimizer = config_dict['optimizer']
 run_num = 0
 print(thresholds)
 for dataset in dss:
     for thresh in thresholds:
         for batch_size in config_dict['batch_sizes']:
             for model in config_dict['models']:
                 run_num += 1
                 print('Running Experiment', run_num, 'of', len(config_dict['batch_sizes'])*len(config_dict['models']*len(thresholds))*len(dss))
                 train_loader, test_loader, shape, num_classes = parse_dataset(dataset, batch_size)
                 model = parse_model(model, shape, num_classes)
                 change_all_pca_layer_thresholds(thresh, model, verbose=True)
                 if 'centering' in config_dict:
                     change_all_pca_layer_centering(centering=config_dict['centering'], network=model, verbose=True)
                 else:
                     change_all_pca_layer_centering(centering=False, network=model, verbose=True)
                 conv_method = 'channelwise' if 'conv_method' not in config_dict else config_dict['conv_method']
                 trainer = Trainer(model,
                                   train_loader,
                                   test_loader,
                                   logs_dir=args.output,
                                   device=args.device,
                                   run_id=args.run_id,
                                   epochs=config_dict['epochs'],
                                   batch_size=batch_size,
                                   optimizer=optimizer,
                                   plot=True,
    device = 'cuda:0'
    torch.cuda.set_device(device)



    cae = TinyCAE().to(device)
    thresh = 10.0
    _, test, _, _ = Food101(1, no_norm=True, shuffle_test=True)
    path = './GoldFood101Samples'
    if not exists(path):
        mkdir(path=path)

    cae.load_state_dict(torch.load(state_path)['model_state_dict'])

    cae.eval()
    change_all_pca_layer_thresholds(thresh, cae, verbose=True)
    cae.to(device)
    for i,(data, _) in enumerate(test):
        data = data.to(device)
        for thresh in [10.0, 0.9999, 0.999, 0.995, 0.99, 0.95, 0.9, 0.5]:
            change_all_pca_layer_thresholds(thresh, cae)
            d = data.squeeze().cpu().numpy().swapaxes(0, -1).swapaxes(0, 1)
            imshow(d)
            savefig(join(path, f'{i}sample-thresh{99999}-{8192}f-{8192}c-{100}s.jpg'))
            pred = cae(data)
            p = pred.squeeze().cpu().detach().numpy().swapaxes(0, -1).swapaxes(0, 1)
            #im = np.append(d, p, axis=1)
            im = p
            imshow(im)
            #title(f'Reconstruction with PCA-Threshold {round(thresh*100, 2) if thresh != 10 else 100}%')
            savefig(join(path, f'{i}sample-thresh{round(thresh*10000) if thresh != 10 else 10000}-{cae.pca_layer.reduced_transformation_matrix.shape[-1]}f-{8192}c-{round(cae.pca_layer.reduced_transformation_matrix.shape[-1] / 8192*100, 2)}s.jpg'))
Example #4
0
 print(thresholds)
 for dataset in dss:
     for thresh in thresholds:
         for batch_size in config_dict['batch_sizes']:
             for model in config_dict['models']:
                 run_num += 1
                 print(
                     'Running Experiment', run_num, 'of',
                     len(config_dict['batch_sizes']) *
                     len(config_dict['models'] * len(thresholds)) *
                     len(dss))
                 train_loader, test_loader, shape, num_classes = parse_dataset(
                     dataset, batch_size)
                 model = parse_model(model, shape, num_classes)
                 change_all_pca_layer_thresholds(thresh,
                                                 model,
                                                 verbose=True)
                 if 'centering' in config_dict:
                     change_all_pca_layer_centering(
                         centering=config_dict['centering'],
                         network=model,
                         verbose=True)
                 else:
                     change_all_pca_layer_centering(centering=False,
                                                    network=model,
                                                    verbose=True)
                 conv_method = 'channelwise' if 'conv_method' not in config_dict else config_dict[
                     'conv_method']
                 trainer = Trainer(
                     model,
                     train_loader,