def test(args):
    # Params / Config
    modalities = ['_CBV_reg1_downsampled', '_TTD_reg1_downsampled']
    labels = [
        '_CBVmap_subset_reg1_downsampled', '_TTDmap_subset_reg1_downsampled',
        '_FUCT_MAP_T_Samplespace_subset_reg1_downsampled'
    ]
    normalization_hours_penumbra = args.normalize
    pad = args.padding
    pad_value = 0

    for idx in range(len(args.path)):
        # Data
        transform = [
            data.ResamplePlaneXY(args.xyresample),
            data.PadImages(pad[0], pad[1], pad[2], pad_value=pad_value),
            data.ToTensor()
        ]
        ds_test = data.get_testdata(modalities=modalities,
                                    labels=labels,
                                    transform=transform,
                                    indices=args.fold[idx])

        print('Size test set:', len(ds_test.sampler.indices), '| # batches:',
              len(ds_test))

        # Single case evaluation
        tester = CaeReconstructionTester(ds_test, args.path[idx],
                                         args.outbasepath,
                                         normalization_hours_penumbra)
        tester.run_inference()
def train():
    args = util.get_args_shape_training()

    # Params / Config
    learning_rate = 1e-3
    momentums_cae = (0.99, 0.999)
    criterion = metrics.BatchDiceLoss([1.0])  # nn.BCELoss()
    path_training_metrics = args.continuetraining  # --continuetraining /share/data_zoe1/lucas/Linda_Segmentations/tmp/tmp_shape_f3.json
    path_saved_model = args.caepath
    channels_cae = args.channelscae
    n_globals = args.globals  # type(core/penu), tO_to_tA, NHISS, sex, age
    resample_size = int(args.xyoriginal * args.xyresample)
    pad = args.padding
    pad_value = 0
    leakage = 0.01
    cuda = True

    # CAE model
    enc = Enc3DCtp(size_input_xy=resample_size, size_input_z=args.zsize,
                   channels=channels_cae, n_ch_global=n_globals, leakage=leakage, padding=pad)
    dec = Dec3D(size_input_xy=resample_size, size_input_z=args.zsize,
                channels=channels_cae, n_ch_global=n_globals, leakage=leakage)
    cae = Cae3DCtp(enc, dec)
    if cuda:
        cae = cae.cuda()

    # Model params
    params = [p for p in cae.parameters() if p.requires_grad]
    print('# optimizing params', sum([p.nelement() * p.requires_grad for p in params]),
          '/ total: cae', sum([p.nelement() for p in cae.parameters()]))

    # Optimizer with scheduler
    optimizer = torch.optim.Adam(params, lr=learning_rate, weight_decay=1e-5, betas=momentums_cae)
    if args.lrsteps:
        scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, args.lrsteps)
    else:
        scheduler = None

    # Data
    common_transform = [data.ResamplePlaneXY(args.xyresample),
                        data.HemisphericFlipFixedToCaseId(split_id=args.hemisflipid),
                        data.PadImages(pad[0], pad[1], pad[2], pad_value=pad_value)]
    train_transform = common_transform + [data.ElasticDeform(), data.ToTensor()]
    valid_transform = common_transform + [data.ToTensor()]
    modalities = ['_CBV_reg1_downsampled', '_TTD_reg1_downsampled']
    labels = ['_CBVmap_subset_reg1_downsampled', '_TTDmap_subset_reg1_downsampled',
              '_FUCT_MAP_T_Samplespace_subset_reg1_downsampled']
    ds_train, ds_valid = data.get_stroke_shape_training_data(modalities, labels, train_transform, valid_transform,
                                                             args.fold, args.validsetsize, batchsize=args.batchsize)
    print('Size training set:', len(ds_train.sampler.indices), 'samples | Size validation set:', len(ds_valid.sampler.indices),
          'samples | Capacity batch:', args.batchsize, 'samples')
    print('# training batches:', len(ds_train), '| # validation batches:', len(ds_valid))

    # Training
    learner = CaeReconstructionLearner(ds_train, ds_valid, cae, path_saved_model, optimizer, scheduler,
                                       path_outputs_base=args.outbasepath)
    learner.run_training()
Exemplo n.º 3
0
def test():
    args = util.get_args_shape_testing()

    assert len(args.fold) == len(
        args.path
    ), 'You must provide as many --fold arguments as caepath model arguments\
                                                in the exact same order!'

    # Params / Config
    modalities = ['_CBV_reg1_downsampled', '_TTD_reg1_downsampled']
    labels = [
        '_CBVmap_subset_reg1_downsampled', '_TTDmap_subset_reg1_downsampled',
        '_FUCT_MAP_T_Samplespace_subset_reg1_downsampled'
    ]
    normalization_hours_penumbra = args.normalize
    steps = range(6)  # fixed steps for tAdmission-->tReca: 0-5 hrs
    pad = args.padding
    pad_value = 0

    # Data
    transform = [
        data.ResamplePlaneXY(args.xyresample),
        data.PadImages(pad[0], pad[1], pad[2], pad_value=pad_value),
        data.ToTensor()
    ]

    # Fold-wise evaluation according to fold indices and fold model for all folds and model path provided as arguments:
    for i, path in enumerate(args.path):
        print('Model ' + path + ' of fold ' + str(i + 1) + '/' +
              str(len(args.fold)) + ' with indices: ' + str(args.fold[i]))
        ds_test = data.get_testdata(modalities=modalities,
                                    labels=labels,
                                    transform=transform,
                                    indices=args.fold[i])
        print('Size test set:', len(ds_test.sampler.indices), '| # batches:',
              len(ds_test))
        # Single case evaluation for all cases in fold
        tester = CaeReconstructionTesterCurve(ds_test, path, args.outbasepath,
                                              normalization_hours_penumbra,
                                              steps)
        tester.run_inference()
def test(args):

    # Params / Config
    modalities = ['_CBV_reg1_downsampled', '_TTD_reg1_downsampled']
    labels = ['_CBVmap_subset_reg1_downsampled', '_TTDmap_subset_reg1_downsampled']
    path_saved_model = args.unetpath
    pad = args.padding
    pad_value = 0

    # Data
    # Trained on patches, but fully convolutional approach let us apply on bigger image (thus, omit patch transform)
    transform = [data.ResamplePlaneXY(args.xyresample),
                 data.PadImages(pad[0], pad[1], pad[2], pad_value=pad_value),
                 data.ToTensor()]
    ds_test = data.get_testdata(modalities=modalities, labels=labels, transform=transform, indices=args.fold)

    print('Size test set:', len(ds_test.sampler.indices), '| # batches:', len(ds_test))

    # Single case evaluation
    tester = UnetSegmentationTester(ds_test, path_saved_model, args.outbasepath, None)
    tester.run_inference()
def train():
    args = util.get_args_unet_training()

    # Params / Config
    batchsize = 6  # 17 training, 6 validation
    learning_rate = 1e-3
    momentums_cae = (0.99, 0.999)
    criterion = metrics.BatchDiceLoss([1.0])  # nn.BCELoss()
    path_saved_model = args.unetpath
    channels = args.channels
    pad = args.padding
    cuda = True

    # Unet model
    unet = Unet3D(channels)
    if cuda:
        unet = unet.cuda()

    # Model params
    params = [p for p in unet.parameters() if p.requires_grad]
    print('# optimizing params',
          sum([p.nelement() * p.requires_grad for p in params]),
          '/ total: unet', sum([p.nelement() for p in unet.parameters()]))

    # Optimizer with scheduler
    optimizer = torch.optim.Adam(params,
                                 lr=learning_rate,
                                 weight_decay=1e-5,
                                 betas=momentums_cae)
    if args.lrsteps:
        scheduler = torch.optim.lr_scheduler.MultiStepLR(
            optimizer, args.lrsteps)
    else:
        scheduler = None

    # Data
    train_transform = [
        data.ResamplePlaneXY(args.xyresample),
        data.HemisphericFlipFixedToCaseId(split_id=args.hemisflipid),
        data.PadImages(pad[0], pad[1], pad[2], pad_value=0),
        data.RandomPatch(104, 104, 68, pad[0], pad[1], pad[2]),
        data.ToTensor()
    ]
    valid_transform = [
        data.ResamplePlaneXY(args.xyresample),
        data.HemisphericFlipFixedToCaseId(split_id=args.hemisflipid),
        data.PadImages(pad[0], pad[1], pad[2], pad_value=0),
        data.RandomPatch(104, 104, 68, pad[0], pad[1], pad[2]),
        data.ToTensor()
    ]
    ds_train, ds_valid = data.get_stroke_shape_training_data(
        train_transform,
        valid_transform,
        args.fold,
        args.validsetsize,
        batchsize=batchsize)
    print('Size training set:',
          len(ds_train.sampler.indices), 'samples | Size validation set:',
          len(ds_valid.sampler.indices), 'samples | Capacity batch:',
          batchsize, 'samples')
    print('# training batches:', len(ds_train), '| # validation batches:',
          len(ds_valid))

    # Training
    learner = UnetSegmentationLearner(ds_train,
                                      ds_valid,
                                      unet,
                                      path_saved_model,
                                      optimizer,
                                      scheduler,
                                      criterion,
                                      path_previous_base=args.inbasepath,
                                      path_outputs_base=args.outbasepath)
    learner.run_training()