Exemplo n.º 1
0
def construct_model_and_data(args):
    model = ImageModel(args.arch, args.dataset)
    loader = ImageData(args.data, args.dataset,
                       num_samples=args.num_samples).data_loader
    target, label = next(iter(loader))
    target, label = target.to(device), label.to(device)
    x_data, y_data = split_data(target, label, model, args.num_classes)
    result = {
        'data_model': model,
        'x_data': x_data,
        'y_data': y_data,
        'clip_min': 0.0,
        'clip_max': 1.0
    }
    if args.attack_type == 'targeted':
        np.random.seed(0)
        length = y_data.shape[0]
        idx = [
            np.random.choice([j for j in range(length) if j != label])
            for label in range(length)
        ]
        result['target_labels'] = y_data[idx]
        result['target_images'] = x_data[idx]

    return result
Exemplo n.º 2
0
def main(args):
    # Ensure training, testing, and manip are not all turned off
    assert (
        args.train or args.test or args.manip
    ), 'Cannot have train, test, and manip all set to 0, Nothing to do.'

    # Load the training, validation, and testing data
    try:
        train_list, val_list, test_list = load_data(args.data_root_dir,
                                                    args.split_num)
    except:
        # Create the training and test splits if not found
        logging.info(
            '\nNo existing training, validate, test files...System will generate it.'
        )
        split_data(args.data_root_dir, num_splits=args.Kfold)
        train_list, val_list, test_list = load_data(args.data_root_dir,
                                                    args.split_num)

    # Get image properties from first image. Assume they are all the same.
    logging.info('\nRead image files...%s' %
                 (join(args.data_root_dir, 'imgs', train_list[0][0])))
    # Get image shape from the first image.
    image = sitk.GetArrayFromImage(
        sitk.ReadImage(join(args.data_root_dir, 'imgs', train_list[0][0])))
    img_shape = image.shape  # # (x, y, channels)
    if args.dataset == 'luna16':
        net_input_shape = (img_shape[1], img_shape[2], args.slices)
    else:
        args.slices = 1
        if GRAYSCALE:
            net_input_shape = (RESOLUTION, RESOLUTION, 1)  # only one channel
        else:
            net_input_shape = (RESOLUTION, RESOLUTION, 3
                               )  # Only access RGB 3 channels.
    # Create the model for training/testing/manipulation
    # enable_decoder = False only for SegCaps R3 to disable recognition image output on evaluation model
    # to speed up performance.
    model_list = create_model(args=args,
                              input_shape=net_input_shape,
                              enable_decoder=True)
    print_summary(model=model_list[0], positions=[.38, .65, .75, 1.])

    args.output_name = 'split-' + str(args.split_num) + '_batch-' + str(args.batch_size) + \
                       '_shuff-' + str(args.shuffle_data) + '_aug-' + str(args.aug_data) + \
                       '_loss-' + str(args.loss) + '_slic-' + str(args.slices) + \
                       '_sub-' + str(args.subsamp) + '_strid-' + str(args.stride) + \
                       '_lr-' + str(args.initial_lr) + '_recon-' + str(args.recon_wei)

    #     args.output_name = 'sh-' + str(args.shuffle_data) + '_a-' + str(args.aug_data)

    args.time = time
    if platform.system() == 'Windows':
        args.use_multiprocessing = False
    else:
        args.use_multiprocessing = True
    args.check_dir = join(args.data_root_dir, 'saved_models', args.net)
    try:
        makedirs(args.check_dir)
    except:
        pass

    args.log_dir = join(args.data_root_dir, 'logs', args.net)
    try:
        makedirs(args.log_dir)
    except:
        pass

    args.tf_log_dir = join(args.log_dir, 'tf_logs')
    try:
        makedirs(args.tf_log_dir)
    except:
        pass

    args.output_dir = join(args.data_root_dir, 'plots', args.net)
    try:
        makedirs(args.output_dir)
    except:
        pass

    if args.train:
        from train import train
        # Run training
        train(args, train_list, val_list, model_list[0], net_input_shape)

    if args.test:
        from test import test
        # Run testing
        test(args, test_list, model_list, net_input_shape)

    if args.manip:
        from manip import manip
        # Run manipulation of segcaps
        manip(args, test_list, model_list, net_input_shape)
'''
The file is used to split the training dataset.
The test data of the original datset should not be touched.
'''
import utils.load_data as datama
import argparse

if __name__ == '__main__':
    ap = argparse.ArgumentParser()
    ap.add_argument("-t", "--toy", type=bool, default=False)
    args = vars(ap.parse_args())
    if args['toy']:
        print("Toy Data")
        datama.spli_toy_data()
    else:
        print("Not Toy Data")
        datama.split_data(10000)
Exemplo n.º 4
0
    fig.savefig('./{}.png'.format(save_path), bbox_inches='tight')
    print('saved')


def draw_boxplot(df, save_path=None, is_save=False):
    fig = plt.figure(figsize=(6, 6))
    ax = fig.add_subplot(111)
    ax.set_xlabel('Initial AUC', fontsize=18)
    ax.set_ylabel('lambda', fontsize=18)
    sns.boxplot(data=df, orient='h', palette='Pastel1', width=0.5, sym='')
    if save_path is not None and is_save == True:
        save_graph(fig, save_path)


# %%
train_X, test_X, train_y, test_y = split_data(df_insect, False)
print('{}insect{}'.format('-' * 10, '-' * 10))
df_wj = pd.DataFrame([])
for lambd in lambds:
    oracle_insect = Oracle(eta=eta, lambd=lambd)
    min_w = oracle_insect.estimate_min_w(pd.concat([train_X, test_X]),
                                         pd.concat([train_y, test_y]))
    # print('[min_w] {}: {}'.format(min_w, predict(test_X, test_y, min_w)))
    W_init = oracle_insect.make_W_init(J=J)
    tmp = pd.Series(predict_wj(test_X, test_y, W_init))
    df_wj = pd.concat([df_wj, tmp], axis=1)
    # print("lambd = {} : {}".format(lambd, predict_by_W(test_X, test_y, W_init)))

df_wj.columns = ['0', '1', '2', '3', '4', '5', '10']
draw_boxplot(df_wj)
# %%