def load_dataset(self,
                     data_subset_id,
                     size=160,
                     sample='Normal',
                     res=0.5,
                     rating_scale='none',
                     configuration=None,
                     dataset_type='Clean'):
        # prepare test data
        self.images_test, self.labels_test, self.classes_test, self.masks_test, self.meta_test, _ = \
            prepare_data_direct(
                load_nodule_dataset(size=size, res=res, sample=sample, dataset_type=dataset_type, configuration=configuration)[data_subset_id],
                size=size,
                return_meta=True, objective="rating", rating_scale=rating_scale, verbose=1,
                balanced=False)

        print("Data ready: images({}), labels({})".format(
            self.images_test[0].shape, self.labels_test.shape))
        print("Range = [{:.2f},{:.2f}]".format(np.min(self.images_test[0]),
                                               np.max(self.images_test[0])))

        self.images_test = np.array([
            crop_center(im, msk, size=self.in_size)[0]
            for im, msk in zip(self.images_test, self.masks_test)
        ])

        print("Image size changed to {}".format(self.images_test.shape))
        print('Mask not updated')
    def predict_malignancy(self,
                           weights_file,
                           out_filename,
                           data_subset_id,
                           dataset_type='Clean',
                           configuration=None):

        input_shape = (self.model_size, self.model_size, 1)

        # prepare model
        model = DirectArch(miniXception_loader,
                           input_shape,
                           objective="malignancy",
                           pooling=self.pooling,
                           output_size=self.out_size,
                           normalize=True)
        if weights_file is not None:
            model.load_weights(weights_file)
            print('Load from: {}'.format(weights_file))
        else:
            print('Model without weights')

        # prepare test data
        images_test, labels_test, classes_test, masks_test, meta_test = \
            prepare_data_direct(
                load_nodule_dataset(size=self.data_size, res=self.res, sample=self.sample, dataset_type=dataset_type, configuration=configuration)[data_subset_id],
                    size=self.model_size, return_meta=True, objective="malignancy", verbose=1, balanced=False)

        print("Data ready: images({}), labels({})".format(
            images_test[0].shape, labels_test.shape))
        print("Range = [{:.2f},{:.2f}]".format(np.min(images_test[0]),
                                               np.max(images_test[0])))

        images_test = np.array([
            crop_center(im, msk, size=self.model_size)[0]
            for im, msk in zip(images_test, masks_test)
        ])

        print("Image size changed to {}".format(images_test.shape))
        print('Mask not updated')

        # eval
        print("Begin Predicting...")
        pred = model.predict(images_test, round=False)
        print("Predication Ready")
        print("\tshape = {}\n\trange [{}, {}]".format(pred.shape, np.min(pred),
                                                      np.max(pred)))

        pickle.dump((images_test, pred, meta_test, labels_test, masks_test),
                    open(out_filename, 'bw'))
        print("Saved to: {}".format(out_filename))

        return (images_test, pred, meta_test, labels_test,
                masks_test), out_filename
Exemple #3
0
    def prepare_data(self, data_subset_id, dataset_type='Clean', configuration=None):

        images, labels, classes, masks, meta, conf = \
            prepare_data(load_nodule_dataset(size=self.data_size, res=self.data_res, sample=self.data_sample, dataset_type=dataset_type,
                                             configuration=configuration)[data_subset_id],
                         reshuffle=False,
                         return_meta=True,
                         verbose=1)
        self.images = np.array([crop_center(im, msk, size=self.net_in_size)[0]
                           for im, msk in zip(images, masks)])
        self.meta   = meta
        self.labels = labels
        self.masks  = masks
        print("Image size changed to {}".format(self.images.shape))
        print('Mask not updated')
Exemple #4
0
DataSubSet = 2

if DataSubSet == 0:
    print("Test Set Analysis")
elif DataSubSet == 1:
    print("Validation Set Analysis")
elif DataSubSet == 2:
    print("Training Set Analysis")
else:
    assert False

dataset = load_nodule_raw_dataset()[DataSubSet]
print("Raw Data Loaded: {} entries".format(len(dataset)))

# prepare test data
images_test, labels_test = prepare_data(load_nodule_dataset()[0],
                                        classes=2,
                                        size=size)
print("Data ready: images({}), labels({})".format(images_test.shape,
                                                  labels_test.shape))
print("Range = [{},{}]".format(np.min(images_test), np.max(images_test)))

assert len(dataset) == images_test.shape[0]

#model = miniXception(None, (size, size,1),'avg', weights='w_002_37-0.95-0.82.h5')
#compile(model, learning_rate=0.01)

model = DirectArch(miniXception_loader, input_shape, 2)
#model.summary()
#model.compile()
model.load_weights('w_007_36-0.91-0.86.h5')
Exemple #5
0
def run(choose_model="DIR",
        epochs=200,
        config=0,
        skip_validation=False,
        no_training=False):

    np.random.seed(1337)
    random.seed(1337)
    tf.set_random_seed(1234)
    K.set_session(tf.Session(graph=tf.get_default_graph()))

    ## --------------------------------------- ##
    ## ------- General Setup ----------------- ##
    ## --------------------------------------- ##

    #data
    dataset_type = 'Primary'
    data_size = 160
    if no_training:
        data_size = 160
    res = 0.5  # 'Legacy' #0.7 #0.5 #'0.5I'
    sample = 'Normal'  # 'UniformNC' #'Normal' #'Uniform'
    use_gen = True
    #model
    model_size = 128
    input_shape = (model_size, model_size, 1)
    normalize = True
    out_size = 128
    do_augment = True
    if no_training:
        do_augment = False
    preload_weight = None

    print("-" * 30)
    print("Running {} for --** {} **-- model, with #{} configuration".format(
        "training" if not no_training else "validation", choose_model, config))
    print(
        "\tdata_size = {},\n\tmodel_size = {},\n\tres = {},\n\tdo_augment = {}"
        .format(data_size, model_size, res, do_augment))
    print("\tdataset_type = {}".format(dataset_type))
    print("-" * 30)

    model = None

    data_augment_params = {
        'max_angle': 30,
        'flip_ratio': 0.5,
        'crop_stdev': 0.15,
        'epoch': 0
    }

    data_loader = build_loader(size=data_size,
                               res=res,
                               sample=sample,
                               dataset_type=dataset_type,
                               configuration=config)

    ## --------------------------------------- ##
    ## ------- Prepare Direct Architecture ------- ##
    ## --------------------------------------- ##

    if choose_model is "DIR":
        # run = '300'  # SPIE avg-pool (data-aug, balanced=False,class_weight=True)
        # run = '301'  # SPIE max-pool (data-aug, balanced=False,class_weight=True)
        # run = '302'  # SPIE rmac-pool (data-aug, balanced=False,class_weight=True)

        # run = 'zzz'

        model = DirectArch(miniXception_loader,
                           input_shape,
                           output_size=out_size,
                           normalize=normalize,
                           pooling='msrmac')
        model.model.summary()
        model.compile(learning_rate=1e-3, decay=0)
        if use_gen:
            generator = DataGeneratorDir(
                data_loader,
                val_factor=0 if skip_validation else 1,
                balanced=False,
                data_size=data_size,
                model_size=model_size,
                batch_size=32,
                do_augment=do_augment,
                augment=data_augment_params,
                use_class_weight=True,
                use_confidence=False)
            model.load_generator(generator)
        else:
            dataset = load_nodule_dataset(size=data_size,
                                          res=res,
                                          sample=sample)
            images_train, labels_train, class_train, masks_train, _ = prepare_data_direct(
                dataset[2], num_of_classes=2)
            images_valid, labels_valid, class_valid, masks_valid, _ = prepare_data_direct(
                dataset[1], num_of_classes=2)
            images_train = np.array([
                crop_center(im, msk, size=model_size)[0]
                for im, msk in zip(images_train, masks_train)
            ])
            images_valid = np.array([
                crop_center(im, msk, size=model_size)[0]
                for im, msk in zip(images_valid, masks_valid)
            ])
            model.load_data(images_train,
                            labels_train,
                            images_valid,
                            labels_valid,
                            batch_size=32)

    if choose_model is "DIR_RATING":

        ### CLEAN SET
        # run = '800'  # rmac conf:size
        # run = '801'  # rmac conf:none
        # run = '802'  # rmac conf:rating-std
        # run = '803'  # max conf:none

        ### PRIMARY SET
        # run = '810'  # rmac conf:size
        # run = '811'  # rmac conf:none
        # run = '812'  # rmac conf:rating-std
        run = '813'  # max conf:none

        # run = 'zzz'

        obj = 'rating'  # 'distance-matrix' 'rating' 'rating-size'

        rating_scale = 'none'
        reg_loss = None  # {'SampleCorrelation': 0.0}  # 'Dispersion', 'Std', 'FeatureCorrelation', 'SampleCorrelation'
        batch_size = 32

        epoch_pre = 20
        preload_weight = None  # FileManager.Weights('dirR', output_dir=input_dir).name(run='251c{}'.format(config), epoch=epoch_pre)

        model = DirectArch(miniXception_loader,
                           input_shape,
                           output_size=out_size,
                           objective=obj,
                           separated_prediction=False,
                           normalize=normalize,
                           pooling='max',
                           l1_regularization=None,
                           regularization_loss=reg_loss,
                           batch_size=batch_size)
        model.model.summary()

        if preload_weight is not None:
            model.load_core_weights(preload_weight)

        # scheduale 02
        should_use_scheduale = (reg_loss is not None) or (obj == 'rating_size')
        sched = [{'epoch': 00, 'weights': [0.1, 0.9]},
                 {'epoch': 20, 'weights': [0.4, 0.6]},
                 {'epoch': 40, 'weights': [0.6, 0.4]},
                 {'epoch': 60, 'weights': [0.9, 0.1]},
                 {'epoch': 80, 'weights': [1.0, 0.0]}] \
            if should_use_scheduale else []

        loss = 'logcosh' if obj is not 'distance-matrix' else pearson_correlation
        model.compile(
            learning_rate=1e-3, decay=0, loss=loss, scheduale=sched
        )  # mean_squared_logarithmic_error, binary_crossentropy, logcosh

        if use_gen:
            generator = DataGeneratorDir(
                data_loader,
                val_factor=0 if skip_validation else 1,
                data_size=data_size,
                model_size=model_size,
                batch_size=batch_size,
                objective=obj,
                rating_scale=rating_scale,
                weighted_rating=(obj == 'distance-matrix'),
                balanced=False,
                do_augment=do_augment,
                augment=data_augment_params,
                use_class_weight=False,
                use_confidence=False)
            model.load_generator(generator)
        else:
            dataset = load_nodule_dataset(size=data_size,
                                          res=res,
                                          sample=sample,
                                          dataset_type=dataset_type)
            images_train, labels_train, masks_train = prepare_data_direct(
                dataset[2], objective='rating', rating_scale=rating_scale)
            images_valid, labels_valid, masks_valid = prepare_data_direct(
                dataset[1], objective='rating', rating_scale=rating_scale)
            images_train = np.array([
                crop_center(im, msk, size=model_size)[0]
                for im, msk in zip(images_train, masks_train)
            ])
            images_valid = np.array([
                crop_center(im, msk, size=model_size)[0]
                for im, msk in zip(images_valid, masks_valid)
            ])
            model.load_data(images_train,
                            labels_train,
                            images_valid,
                            labels_valid,
                            batch_size=batch_size)

    ## --------------------------------------- ##
    ## ------- Prepare Siamese Architecture ------ ##
    ## --------------------------------------- ##

    if choose_model is "SIAM":
        # run = '300'  # l1, avg-pool (data-aug, balanced=True, class_weight=False)
        # run = '301'  # l1, max-pool (data-aug, balanced=True, class_weight=False)
        # run = '302'  # l1, rmac-pool (data-aug, balanced=True, class_weight=False)
        # run = '310'  # l2, avg-pool (data-aug, balanced=True, class_weight=False)
        # run = '311'  # l2, max-pool (data-aug, balanced=True, class_weight=False)
        # run = '312'  # l2, rmac-pool (data-aug, balanced=True, class_weight=False)
        # run = '320'  # cos, avg-pool (data-aug, balanced=True, class_weight=False)
        # run = '321'  # cos, max-pool (data-aug, balanced=True, class_weight=False)
        # run = '322b'  # cos, rmac-pool (data-aug, balanced=True, class_weight=False)

        # b/c - changed margin-loss params
        # run = '313c'  # l2, max-pool MARGINAL-LOSS (data-aug, balanced=True, class_weight=False)
        # run = '314c'  # l2, rmac-pool MARGINAL-LOSS (data-aug, balanced=True, class_weight=False)
        # run = '323c'  # cos, max-pool MARGINAL-LOSS (data-aug, balanced=True, class_weight=False)
        # run = '324c'  # cos, rmac-pool MARGINAL-LOSS (data-aug, balanced=True, class_weight=False)

        # run = 'zzz'

        batch_size = 64 if local else 128

        # model
        generator = DataGeneratorSiam(data_loader,
                                      data_size=data_size,
                                      model_size=model_size,
                                      batch_size=batch_size,
                                      val_factor=0 if skip_validation else 3,
                                      balanced=True,
                                      objective="malignancy",
                                      do_augment=do_augment,
                                      augment=data_augment_params,
                                      use_class_weight=False)

        model = SiamArch(miniXception_loader,
                         input_shape,
                         output_size=out_size,
                         batch_size=batch_size,
                         distance='l2',
                         normalize=normalize,
                         pooling='msrmac')
        model.model.summary()
        model.compile(learning_rate=1e-3, decay=0)
        if use_gen:
            model.load_generator(generator)
        else:
            imgs_trn, lbl_trn = generator.next_train().__next__()
            imgs_val, lbl_val = generator.next_val().__next__()
            model.load_data(imgs_trn, lbl_trn, imgs_val, lbl_val)

    if choose_model is "SIAM_RATING":
        ### clean set
        # run = '400'  # l2-rmac no-conf
        # run = '401'  # cosine-rmac no-conf
        # run = '402'  # l2-rmac conf
        # run = '403'  # cosine-rmac conf
        # run = '404'  # l2-max no-conf
        # run = '405'  # cosine-max no-conf

        ### primary set
        # run = '410'  # l2-rmac no-conf
        # run = '411'  # cosine-rmac no-conf
        # run = '412'  # l2-rmac conf
        # run = '413'  # cosine-rmac conf
        # run = '414'  # l2-max no-conf
        # run = '415'  # cosine-max no-conf

        # run = 'zzz'

        obj = 'rating'  # rating / size / rating_size
        batch_size = 16 if local else 64
        reg_loss = None  # {'SampleCorrating_clusters_distance_and_stdrelation': 0.1}  # 'Dispersion', 'Std', 'FeatureCorrelation', 'SampleCorrelation'

        epoch_pre = 60
        preload_weight = None  # FileManager.Weights('dirR', output_dir=input_dir).name(run='251c{}'.format(config), epoch=70)

        should_use_scheduale = (reg_loss is not None) or (obj == 'rating_size')
        '''
        sched = [{'epoch': 00, 'weights': [0.1, 0.9]},
                 {'epoch': 30, 'weights': [0.4, 0.6]},
                 {'epoch': 60, 'weights': [0.6, 0.4]},
                 {'epoch': 80, 'weights': [0.9, 0.1]},
                 {'epoch': 100, 'weights': [1.0, 0.0]}] \
            if should_use_scheduale else []
        '''
        sched = [{'epoch': 00, 'weights': [0.1, 0.9]},
                 {'epoch': 20, 'weights': [0.4, 0.6]},
                 {'epoch': 30, 'weights': [0.6, 0.4]},
                 {'epoch': 50, 'weights': [0.9, 0.1]},
                 {'epoch': 80, 'weights': [1.0, 0.0]}] \
            if should_use_scheduale else []
        # model
        generator = DataGeneratorSiam(data_loader,
                                      data_size=data_size,
                                      model_size=model_size,
                                      batch_size=batch_size,
                                      train_facotr=2,
                                      val_factor=0 if skip_validation else 3,
                                      balanced=False,
                                      objective=obj,
                                      weighted_rating=True,
                                      do_augment=do_augment,
                                      augment=data_augment_params,
                                      use_class_weight=False,
                                      use_confidence=False)

        model = SiamArch(miniXception_loader,
                         input_shape,
                         output_size=out_size,
                         objective=obj,
                         batch_size=batch_size,
                         distance='cosine',
                         normalize=normalize,
                         pooling='rmac',
                         regularization_loss=reg_loss,
                         l1_regularization=False)

        if preload_weight is not None:
            model.load_core_weights(preload_weight)
        model.model.summary()
        model.compile(learning_rate=1e-3,
                      decay=0,
                      loss='logcosh',
                      scheduale=sched)  # mean_squared_error, logcosh
        model.load_generator(generator)

    ## --------------------------------------- ##
    ## ------- Prepare Triplet Architecture ------ ##
    ## --------------------------------------- ##

    if choose_model is "TRIPLET":
        #run = 'trip011XXX'  # mrg-loss, decay(0.01), max-pool
        #run = 'trip012X'  # mrg-loss, decay(0.05), rmac-pool
        #run = 'trip013'  # cosine
        #run = 'trip014' # ortogonal initialization
        #run = 'trip015X'  # objective rating
        #run = 'trip016XXXX'  # softplus-loss
        #run = 'trip017'  # softplus-loss, no decay
        #run = 'trip018'  # binary
        #run = 'trip019'  # categorize
        #run = 'trip020X'  # rating-conf-tryout

        #run = 'trip021' # pretrained
        #run = 'trip022XXX'  # pretrained rmac
        #run = 'trip023X'  # pretrained categorize
        #run = 'trip024'  # pretrained confidence
        #run = 'trip025'  # pretrained cat,conf
        #run = 'trip026Z'  # class_weight='rating_distance', cat

        #run = 'trip027'  # obj:malig, rmac, categorize, no-decay
        #run = 'trip028'  # obj:malig, max, categorize, no-decay

        run = 'trip_100'  # obj:malig, msrmac, softplus-loss
        #run = 'trip101'  # obj:malig, msrmac, rank-loss

        objective = 'malignancy'
        use_rank_loss = False

        gen = True
        preload_weight = None  #'./Weights/w_dirR011X_50.h5'

        # model
        model = TripArch(miniXception_loader,
                         input_shape,
                         objective=objective,
                         output_size=out_size,
                         distance='l2',
                         normalize=True,
                         pooling='msrmac',
                         categorize=use_rank_loss)

        if preload_weight is not None:
            model.load_core_weights(preload_weight)
        model.model.summary()
        model.compile(learning_rate=1e-3, decay=0)  #0.05

        generator = DataGeneratorTrip(data_loader,
                                      data_size=data_size,
                                      model_size=model_size,
                                      batch_size=16,
                                      objective=objective,
                                      balanced=(objective == 'malignancy'),
                                      categorize=True,
                                      val_factor=0 if skip_validation else 3,
                                      train_factor=1,
                                      do_augment=do_augment,
                                      augment=data_augment_params,
                                      use_class_weight=False,
                                      use_confidence=False)
        if gen:
            model.load_generator(generator)
        else:
            imgs_trn, lbl_trn = generator.next_train().__next__()
            imgs_val, lbl_val = generator.next_val().__next__()
            model.load_data(imgs_trn, lbl_trn, imgs_val, lbl_val)

    ## --------------------------------------- ##
    ## -------      RUN             ------ ##
    ## --------------------------------------- ##

    print('Current Run: {}{}c{}'.format('', run, config))

    if no_training:
        model.last_epoch = epochs
        model.run = '{}{}c{}'.format('', run, config)
    else:
        model.train(run='{}{}c{}'.format('', run, config),
                    epoch=(0 if preload_weight is None else epoch_pre),
                    n_epoch=epochs,
                    gen=use_gen,
                    do_graph=False)

    return model
Exemple #6
0
def run(choose_model="DIR",
        epochs=200,
        config=0,
        skip_validation=False,
        no_training=False):

    np.random.seed(1337)
    random.seed(1337)
    tf.set_random_seed(1234)
    K.set_session(tf.Session(graph=tf.get_default_graph()))

    ## --------------------------------------- ##
    ## ------- General Setup ----------------- ##
    ## --------------------------------------- ##

    net_type = 'flat'  # 'flat', 'rmac'

    #data
    dataset_type = '3d'
    res = 0.5  # 'Legacy' #0.7 #0.5 #'0.5I'
    sample = 'Normal'  # 'UniformNC' #'Normal' #'Uniform'
    use_gen = True
    data_size = 160

    data_loader = build_loader_3d(configuration=config,
                                  net_type='dirR',
                                  run='251',
                                  epoch=60)

    # model
    out_size = 128
    if net_type == 'flat':
        model_size = 8 * 8 * 128
    elif net_type == 'rmac':
        model_size = 128
    else:
        assert False
    input_shape = (None, model_size)
    do_augment = False
    normalize = True

    print("-" * 30)
    print("Running Sequence {} for --** {} **-- model, with #{} configuration".
          format("training" if not no_training else "validation", choose_model,
                 config))
    print(
        "\tdata_size = {},\n\tmodel_size = {},\n\tres = {},\n\tdo_augment = {}"
        .format(data_size, model_size, res, do_augment))
    print("-" * 30)

    model = None

    ## --------------------------------------- ##
    ## ------- Prepare Direct Architecture ------- ##
    ## --------------------------------------- ##

    if choose_model is "DIR_RATING":

        #run = '0004'
        run = '0005'  # new dataset (Train-Valid-Test)
        obj = 'rating'  # 'distance-matrix' 'rating' 'rating-size'

        rating_scale = 'none'
        reg_loss = None  # {'SampleCorrelation': 0.0}  # 'Dispersion', 'Std', 'FeatureCorrelation', 'SampleCorrelation'
        batch_size = 16

        epoch_pre = 20
        preload_weight = None  # FileManager.Weights('dirR', output_dir=input_dir).name(run='251c{}'.format(config), epoch=epoch_pre)

        model = DirectArch(gru3d_loader,
                           input_shape,
                           output_size=out_size,
                           objective=obj,
                           separated_prediction=False,
                           normalize=normalize,
                           pooling='msrmac',
                           l1_regularization=None,
                           regularization_loss=reg_loss,
                           batch_size=batch_size)
        model.model.summary()

        if preload_weight is not None:
            model.load_core_weights(preload_weight)

        # scheduale 02
        should_use_scheduale = (reg_loss is not None) or (obj == 'rating_size')
        sched = [{'epoch': 00, 'weights': [0.1, 0.9]},
                 {'epoch': 20, 'weights': [0.4, 0.6]},
                 {'epoch': 40, 'weights': [0.6, 0.4]},
                 {'epoch': 60, 'weights': [0.9, 0.1]},
                 {'epoch': 80, 'weights': [1.0, 0.0]}] \
            if should_use_scheduale else []

        loss = 'logcosh' if obj is not 'distance-matrix' else pearson_correlation
        model.compile(
            learning_rate=1e-3,
            decay=0,
            loss=loss,
            scheduale=sched,
            temporal_weights=False
        )  # mean_squared_logarithmic_error, binary_crossentropy, logcosh

        if use_gen:
            generator = DataGeneratorDir(
                data_loader,
                val_factor=0 if skip_validation else 1,
                data_size=data_size,
                model_size=model_size,
                batch_size=batch_size,
                objective=obj,
                rating_scale=rating_scale,
                weighted_rating=False,
                seq_model=True,
                balanced=False,
                do_augment=do_augment,
                use_class_weight=False,
                use_confidence=False)
            model.load_generator(generator)
        else:
            dataset = load_nodule_dataset(size=data_size,
                                          res=res,
                                          sample=sample,
                                          dataset_type=dataset_type)
            images_train, labels_train, masks_train = prepare_data_direct(
                dataset[2], objective='rating', rating_scale=rating_scale)
            images_valid, labels_valid, masks_valid = prepare_data_direct(
                dataset[1], objective='rating', rating_scale=rating_scale)
            images_train = np.array([
                crop_center(im, msk, size=model_size)[0]
                for im, msk in zip(images_train, masks_train)
            ])
            images_valid = np.array([
                crop_center(im, msk, size=model_size)[0]
                for im, msk in zip(images_valid, masks_valid)
            ])
            model.load_data(images_train,
                            labels_train,
                            images_valid,
                            labels_valid,
                            batch_size=batch_size)

    ## --------------------------------------- ##
    ## ------- Prepare Siamese Architecture ------ ##
    ## --------------------------------------- ##

    if choose_model is "SIAM_RATING":
        #run = 'siamR001'  # mse-loss, rating-scaled
        #run = 'siamR002'  # mse-loss, rating-scaled, repeated-epochs
        #run = 'siamR003'  # mse-loss, 0.25*rating-scaled, repeated-epochs(3)
        #run = 'siamR004X'  # mse-loss, 0.25*rating-scaled, repeated-epochs(5)
        #run = 'siamR005'  # mse-loss, 0.25*rating-scaled, repeated-epochs(1)
        #run = 'siamR006XX'  # rmac, mse-loss, 0.25*rating-scaled, repeated-epochs(1)
        #run = 'siamR007'  # rmac, logcosh-loss, 0.25*rating-scaled, repeated-epochs(1)
        #run = 'siamR008X'  # data-aug
        #run = 'siamR009'  # cosine
        #run = '100c'  # l2, max-pooling, train_factor=1
        #run = '101b'  # l2, max-pooling, train_factor=2
        #run = '102'  # l2, max-pooling, train_factor=3
        #run = '103'  # l2, max-pooling, train_factor=1, mse
        #run = '110'  # l2, max-pooling, train_factor=3
        #run = '112'  # l2, msrmac-pooling, train_factor=3
        #run = '122'  # l2, msrmac-pooling, train_factor=2, data-aug
        #run = '132'  # l2, msrmac-pooling, train_factor=2, data-aug, primary
        #run = '142'  # l2, msrmac-pooling, train_factor=2, out=64
        #run = '152'  # l2, msrmac-pooling, train_factor=2, out=32
        #run = '162'  # l2, msrmac-pooling, train_factor=2, out=8
        #run = '172'  # l2, msrmac-pooling, train_factor=2, out=256
        #run = '135'  # l2, msrmac-pooling, train_factor=2, data-aug, primary
        #run = '180'  # baseline, b64
        #run = '181'  # baseline, FeatCorr.1
        #run = '182'  # baseline, SampCorr.1

        run = '200'  # pretrain with dirR251-70

        #run = '300'   # obj: size
        #run = '311'  # obj: rating-size

        #run = 'zzz'  #

        dataset_type = 'Primary'
        obj = 'rating'  # rating / size / rating_size
        batch_size = 16 if local else 64
        reg_loss = None  # {'SampleCorrelation': 0.1}  # 'Dispersion', 'Std', 'FeatureCorrelation', 'SampleCorrelation'

        epoch_pre = 60
        preload_weight = None  # FileManager.Weights('dirR', output_dir=input_dir).name(run='251c{}'.format(config), epoch=70)

        should_use_scheduale = (reg_loss is not None) or (obj == 'rating_size')
        '''
        sched = [{'epoch': 00, 'weights': [0.1, 0.9]},
                 {'epoch': 30, 'weights': [0.4, 0.6]},
                 {'epoch': 60, 'weights': [0.6, 0.4]},
                 {'epoch': 80, 'weights': [0.9, 0.1]},
                 {'epoch': 100, 'weights': [1.0, 0.0]}] \
            if should_use_scheduale else []
        '''
        sched = [{'epoch': 00, 'weights': [0.1, 0.9]},
                 {'epoch': 20, 'weights': [0.4, 0.6]},
                 {'epoch': 30, 'weights': [0.6, 0.4]},
                 {'epoch': 50, 'weights': [0.9, 0.1]},
                 {'epoch': 80, 'weights': [1.0, 0.0]}] \
            if should_use_scheduale else []
        # model
        generator = DataGeneratorSiam(data_loader,
                                      data_size=data_size,
                                      model_size=model_size,
                                      batch_size=batch_size,
                                      train_facotr=2,
                                      val_factor=0 if skip_validation else 3,
                                      balanced=False,
                                      objective=obj,
                                      do_augment=do_augment,
                                      augment=data_augment_params,
                                      use_class_weight=False,
                                      use_confidence=False)

        model = SiamArch(miniXception_loader,
                         input_shape,
                         output_size=out_size,
                         objective=obj,
                         batch_size=batch_size,
                         distance='l2',
                         normalize=normalize,
                         pooling='msrmac',
                         regularization_loss=reg_loss,
                         l1_regularization=False)

        if preload_weight is not None:
            model.load_core_weights(preload_weight)
        model.model.summary()
        model.compile(learning_rate=1e-3,
                      decay=0,
                      loss='logcosh',
                      scheduale=sched)  # mean_squared_error, logcosh
        model.load_generator(generator)

    ## --------------------------------------- ##
    ## ------- Prepare Triplet Architecture ------ ##
    ## --------------------------------------- ##

    if choose_model is "TRIPLET":
        #run = 'trip011XXX'  # mrg-loss, decay(0.01), max-pool
        #run = 'trip012X'  # mrg-loss, decay(0.05), rmac-pool
        #run = 'trip013'  # cosine
        #run = 'trip014' # ortogonal initialization
        #run = 'trip015X'  # objective rating
        #run = 'trip016XXXX'  # softplus-loss
        #run = 'trip017'  # softplus-loss, no decay
        #run = 'trip018'  # binary
        #run = 'trip019'  # categorize
        #run = 'trip020X'  # rating-conf-tryout

        #run = 'trip021' # pretrained
        #run = 'trip022XXX'  # pretrained rmac
        #run = 'trip023X'  # pretrained categorize
        #run = 'trip024'  # pretrained confidence
        #run = 'trip025'  # pretrained cat,conf
        #run = 'trip026Z'  # class_weight='rating_distance', cat

        #run = 'trip027'  # obj:malig, rmac, categorize, no-decay
        #run = 'trip028'  # obj:malig, max, categorize, no-decay

        run = 'trip_100'  # obj:malig, msrmac, softplus-loss
        #run = 'trip101'  # obj:malig, msrmac, rank-loss

        dataset_type = 'Primary'
        objective = 'malignancy'
        use_rank_loss = False

        gen = True
        preload_weight = None  #'./Weights/w_dirR011X_50.h5'

        # model
        model = TripArch(miniXception_loader,
                         input_shape,
                         objective=objective,
                         output_size=out_size,
                         distance='l2',
                         normalize=True,
                         pooling='msrmac',
                         categorize=use_rank_loss)

        if preload_weight is not None:
            model.load_core_weights(preload_weight)
        model.model.summary()
        model.compile(learning_rate=1e-3, decay=0)  #0.05

        generator = DataGeneratorTrip(data_loader,
                                      data_size=data_size,
                                      model_size=model_size,
                                      batch_size=16,
                                      objective=objective,
                                      balanced=(objective == 'malignancy'),
                                      categorize=True,
                                      val_factor=0 if skip_validation else 3,
                                      train_factor=1,
                                      do_augment=do_augment,
                                      augment=data_augment_params,
                                      use_class_weight=False,
                                      use_confidence=False)
        if gen:
            model.load_generator(generator)
        else:
            imgs_trn, lbl_trn = generator.next_train().__next__()
            imgs_val, lbl_val = generator.next_val().__next__()
            model.load_data(imgs_trn, lbl_trn, imgs_val, lbl_val)

    ## --------------------------------------- ##
    ## -------      RUN             ------ ##
    ## --------------------------------------- ##

    if no_training:
        model.last_epoch = epochs
        model.run = '{}{}c{}'.format('', run, config)
    else:
        model.train(run='{}{}c{}'.format('', run, config),
                    epoch=(0 if preload_weight is None else epoch_pre),
                    n_epoch=epochs,
                    gen=use_gen,
                    do_graph=False)

    return model
def run(choose_model="DIR",
        epochs=200,
        config=0,
        skip_validation=False,
        no_training=False,
        config_name='LEGACY',
        load_data_from_predications=False):

    np.random.seed(1337)
    random.seed(1337)
    tf.set_random_seed(1234)
    K.set_session(tf.Session(graph=tf.get_default_graph()))

    ## --------------------------------------- ##
    ## ------- General Setup ----------------- ##
    ## --------------------------------------- ##

    #data
    dataset_type = 'Primary'
    data_size = 160
    if no_training:
        data_size = 160
    res = 0.5  # 'Legacy' #0.7 #0.5 #'0.5I'
    sample = 'Normal'  # 'UniformNC' #'Normal' #'Uniform'
    data_run = '813'
    data_epoch = 70
    return_predicted_ratings = not no_training
    use_gen = True
    #model
    model_size = 128
    input_shape = (model_size, model_size, 1)
    normalize = True
    out_size = 128
    do_augment = True
    if no_training:
        do_augment = False
    preload_weight = None

    print("-" * 30)
    print("Running {} for --** {} **-- model, with #{} configuration".format(
        "training" if not no_training else "validation", choose_model, config))
    if load_data_from_predications:
        print(
            "\tdata_run = {}, \n\tdata_epoch = {}, return_predicted_ratings = {}"
            .format(data_run, data_epoch, return_predicted_ratings))
    else:
        print(
            "\tdata_size = {},\n\tmodel_size = {},\n\tres = {},\n\tdo_augment = {}"
            .format(data_size, model_size, res, do_augment))
        print("\tdataset_type = {}".format(dataset_type))
    print("-" * 30)

    model = None

    data_augment_params = {
        'max_angle': 30,
        'flip_ratio': 0.5,
        'crop_stdev': 0.15,
        'epoch': 0
    }

    data_loader = build_loader(
        size=data_size,
        res=res,
        sample=sample,
        dataset_type=dataset_type,
        config_name=config_name,
        configuration=config,
        run=data_run,
        epoch=data_epoch,
        load_data_from_predictions=load_data_from_predications,
        return_predicted_ratings=return_predicted_ratings)

    ## --------------------------------------- ##
    ## ------- Prepare Direct Architecture ------- ##
    ## --------------------------------------- ##

    if choose_model is "DIR":
        # run = '300'  # SPIE avg-pool (data-aug, balanced=False,class_weight=True)
        # run = '301'  # SPIE max-pool (data-aug, balanced=False,class_weight=True)
        # run = '302'  # SPIE rmac-pool (data-aug, balanced=False,class_weight=True)

        # run = 'zzz'

        model = DirectArch(miniXception_loader,
                           input_shape,
                           output_size=out_size,
                           normalize=normalize,
                           pooling='msrmac')
        model.model.summary()
        model.compile(learning_rate=1e-3, decay=0)
        if use_gen:
            generator = DataGeneratorDir(
                data_loader,
                val_factor=0 if skip_validation else 1,
                balanced=False,
                data_size=data_size,
                model_size=model_size,
                batch_size=32,
                do_augment=do_augment,
                augment=data_augment_params,
                use_class_weight=True,
                use_confidence=False)
            model.load_generator(generator)
        else:
            dataset = load_nodule_dataset(size=data_size,
                                          res=res,
                                          sample=sample)
            images_train, labels_train, class_train, masks_train, _ = prepare_data_direct(
                dataset[2], num_of_classes=2)
            images_valid, labels_valid, class_valid, masks_valid, _ = prepare_data_direct(
                dataset[1], num_of_classes=2)
            images_train = np.array([
                crop_center(im, msk, size=model_size)[0]
                for im, msk in zip(images_train, masks_train)
            ])
            images_valid = np.array([
                crop_center(im, msk, size=model_size)[0]
                for im, msk in zip(images_valid, masks_valid)
            ])
            model.load_data(images_train,
                            labels_train,
                            images_valid,
                            labels_valid,
                            batch_size=32)

    if choose_model is "DIR_RATING":

        ### CLEAN SET
        # run = '800'  # rmac conf:size
        # run = '801'  # rmac conf:none
        # run = '802'  # rmac conf:rating-std
        # run = '803'  # max conf:none

        ### PRIMARY SET
        # run = '810'  # rmac conf:size
        # run = '811'  # rmac conf:none
        # run = '812'  # rmac conf:rating-std
        # run = '813'  # max conf:none
        # run = '814'  # max separated_prediction

        # run = '820'  # dirD, max, logcoh-loss
        # run = '821'  # dirD, max, pearson-loss
        # run = '822'  # dirD, max, KL-rank-loss
        # run = '823'  # dirD, max, poisson-rank-loss
        # run = '824'  # dirD, max, categorical-cross-entropy-loss
        # run = '825'  # dirD, max, ranked-pearson-loss
        # run = '826'  # dirD, max, KL-normalized-rank-loss
        # run = '827'  # dirD, max, KL-normalized-rank-loss (local-scaled) softmax
        # run = '828'  # dirD, max, KL-normalized-rank-loss (local-scaled) l2
        # run = '829'  # dirD, max, ranked-pearson-loss (local-scaled)

        # run = '830'  # dirD, rmac, logcoh-loss
        # run = '831'  # dirD, rmac, pearson-loss
        # run = '832'  # dirD, rmac, KL-rank-loss
        # run = '833'  # dirD, rmac, poisson-rank-loss
        # run = '834'  # dirD, rmac, categorical-cross-entropy-loss
        # run = '835'  # dirD, rmac, ranked-pearson-loss
        # run = '836'  # dirD, rmac, KL-normalized-rank-loss

        # run = '841'  # dirD, max, pearson-loss    pre:dirR813-50
        # run = '842b'  # dirD, max, KL-rank-loss    pre:dirR813-50  (b:lr-4)
        # run = '846'  # dirD, max, KL-norm-loss    pre:dirR813-50

        # run = '851'  # dirD, rmac, pearson-loss   pre:dirR813-50
        # run = '852'  # dirD, rmac, KL-rank-loss   pre:dirR813-50
        # run = '856'  # dirD, rmac, KL-norm-loss   pre:dirR813-50

        # run = '860'  # dirD, max, KL-loss    pre:dirR813-50  (b:lr-4, freeze:7)
        # run = '861'  # dirD, max, KL-loss    pre:dirR813-50  (b:lr-4, freeze:17)
        # run = '862'  # dirD, max, KL-loss    pre:dirR813-50  (b:lr-4, freeze:28)
        # run = '863'  # dirD, max, KL-loss    pre:dirR813-50  (b:lr-4, freeze:39)

        # run = '870'  # dirRD, max, KL-loss    schd: 00
        # run = '871'  # dirRD, max, KL-loss    schd: 01
        # run = '872'  # dirRD, max, KL-loss    schd: 02
        # run = '873'  # dirRD, max, KL-loss    schd: 03
        # run = '874'  # dirRD, max, KL-loss    schd: 04
        # run = '875'  # dirRD, max, KL-loss    schd: 05
        # run = '876'  # dirRD, max, KL-loss    schd: 06
        # run = '877b'  # dirRD, max, KL-loss    schd: 07b
        # run = '878'  # dirRD, max, KL-loss    schd: 08
        # run = '879'  # dirRD, max, KL-loss    schd: 09

        # run = '888'  # dirRD, max, KL-loss    schd: 08, on partial data SUP
        # run = '882'  # dirRD, max, KL-loss    schd:

        run = '898b'  # dirRD, max, KL-loss    schd: 08, on partial data UNSUP
        # run = '890b'  # dirR
        # run = '892b'  # dirRD, max, KL-loss

        # run = 'ccc'

        obj = 'rating_distance-matrix'  # 'distance-matrix' 'rating' 'rating-size'

        rating_scale = 'none'
        reg_loss = None  # {'SampleCorrelation': 0.0}  # 'Dispersion', 'Std', 'FeatureCorrelation', 'SampleCorrelation'
        batch_size = 32

        epoch_pre = 50
        preload_weight = None
        # FileManager.Weights('dirR', output_dir=input_dir).name(run='813c{}'.format(config), epoch=epoch_pre)
        # FileManager.Weights('dirR', output_dir=input_dir).name(run='251c{}'.format(config), epoch=epoch_pre)

        model = DirectArch(miniXception_loader,
                           input_shape,
                           output_size=out_size,
                           objective=obj,
                           separated_prediction=False,
                           normalize=normalize,
                           pooling='max',
                           l1_regularization=None,
                           regularization_loss=reg_loss,
                           batch_size=batch_size)

        if preload_weight is not None:
            model.load_core_weights(preload_weight, 39)
            # 7:    freeze 1 blocks
            # 17:   freeze 2 blocks
            # 28:   freeze 3 blocks
            # 39:   freeze 4 blocks

        model.model.summary()

        should_use_scheduale = (reg_loss is not None) or (obj in [
            'rating_size', 'rating_distance-matrix'
        ])

        # scheduale 00:     870
        # sched = [{'epoch': 00, 'weights': [0.9, 0.1]},
        #         {'epoch': 40, 'weights': [0.5, 0.5]},
        #         {'epoch': 80, 'weights': [0.1, 0.9]}] \
        #    if should_use_scheduale else []

        # scheduale 01:     871
        # sched = [{'epoch': 00, 'weights': [1.0, 0.0]},
        #         {'epoch': 50, 'weights': [0.0, 1.0]}] \
        #    if should_use_scheduale else []

        # scheduale 02:     872
        # sched = [{'epoch': 00, 'weights': [0.9, 0.1]},
        #       {'epoch': 50, 'weights': [0.1, 0.9]}] \
        #   if should_use_scheduale else []

        # scheduale 03:     873
        # sched = [{'epoch': 00, 'weights': [0.9, 0.1]},
        #        {'epoch': 50, 'weights': [0.5, 0.5]},
        #         {'epoch': 100, 'weights': [0.1, 0.9]}] \
        #    if should_use_scheduale else []

        # scheduale 04:     874
        # sched = [{'epoch': 00, 'weights': [1.0, 0.0]},
        #        {'epoch': 50, 'weights': [0.0, 0.1]}] \
        #   if should_use_scheduale else []

        # scheduale 05:     875
        # sched = [{'epoch': 00, 'weights': [1.0, 0.0]},
        #        {'epoch': 50, 'weights': [0.0, 1.0]},
        #         {'epoch': 100, 'weights': [0.0, 0.1]}] \
        #    if should_use_scheduale else []

        # scheduale 06:     876
        # sched = [{'epoch': 00, 'weights': [0.9, 0.1]},
        #         {'epoch': 40, 'weights': [0.5, 0.5]},
        #         {'epoch': 60, 'weights': [0.1, 0.1]},
        #         {'epoch': 80, 'weights': [0.0, 0.1]},
        #         {'epoch': 100, 'weights': [0.0, 0.05]}] \
        #    if should_use_scheduale else []

        # scheduale 07b:     877b
        # sched = [{'epoch': 00,  'weights': [1.0, 0.0]},
        #         {'epoch': 50,  'weights': [0.0, 1.0]},
        #         {'epoch': 80,  'weights': [0.0, 0.1]},
        #         {'epoch': 100, 'weights': [0.0, 0.05]}] \
        #    if should_use_scheduale else []

        # scheduale 08b:     878
        # sched = [{'epoch': 00, 'weights': [0.9, 0.1]},
        #         {'epoch': 40, 'weights': [0.5, 0.5]},
        #         {'epoch': 80, 'weights': [0.0, 0.1]}] \
        #    if should_use_scheduale else []

        # scheduale 09:     879
        # sched = [{'epoch': 00, 'weights': [0.9, 0.1]},
        #         {'epoch': 20, 'weights': [0.7, 0.3]},
        #         {'epoch': 40, 'weights': [0.5, 0.5]},
        #         {'epoch': 60, 'weights': [0.3, 0.3]},
        #         {'epoch': 80, 'weights': [0.0, 0.1]}] \
        #    if should_use_scheduale else []

        # scheduale      892/882
        sched = [{'epoch': 00, 'weights': [0.9, 0.1]},
                 {'epoch': 80, 'weights': [0.5, 0.5]},
                 {'epoch': 120, 'weights': [0.0, 0.1]}] \
            if should_use_scheduale else []

        loss = dict()
        loss['predictions'] = 'logcosh'
        loss['predictions_size'] = 'logcosh'
        loss['distance_matrix'] = distance_matrix_rank_loss_adapter(
            K_losses.kullback_leibler_divergence, 'KL')
        # distance_matrix_logcosh
        # pearson_correlation
        # distance_matrix_rank_loss_adapter(K_losses.kullback_leibler_divergence, 'KL')
        # distance_matrix_rank_loss_adapter(K_losses.poisson, 'poisson')
        # distance_matrix_rank_loss_adapter(K_losses.categorical_crossentropy, 'entropy')
        model.compile(
            learning_rate=1e-3 if (preload_weight is None) else 1e-4,
            loss=loss,
            scheduale=sched
        )  # mean_squared_logarithmic_error, binary_crossentropy, logcosh

        if use_gen:
            generator = DataGeneratorDir(
                data_loader,
                val_factor=0 if skip_validation else 1,
                data_size=data_size,
                model_size=model_size,
                batch_size=batch_size,
                objective=obj,
                rating_scale=rating_scale,
                weighted_rating=('distance-matrix' in obj),
                balanced=False,
                do_augment=do_augment,
                augment=data_augment_params,
                use_class_weight=False,
                use_confidence=False)
            model.load_generator(generator)
        else:
            dataset = load_nodule_dataset(size=data_size,
                                          res=res,
                                          sample=sample,
                                          dataset_type=dataset_type)
            images_train, labels_train, masks_train = prepare_data_direct(
                dataset[2], objective='rating', rating_scale=rating_scale)
            images_valid, labels_valid, masks_valid = prepare_data_direct(
                dataset[1], objective='rating', rating_scale=rating_scale)
            images_train = np.array([
                crop_center(im, msk, size=model_size)[0]
                for im, msk in zip(images_train, masks_train)
            ])
            images_valid = np.array([
                crop_center(im, msk, size=model_size)[0]
                for im, msk in zip(images_valid, masks_valid)
            ])
            model.load_data(images_train,
                            labels_train,
                            images_valid,
                            labels_valid,
                            batch_size=batch_size)

    ## --------------------------------------- ##
    ## ------- Prepare Siamese Architecture ------ ##
    ## --------------------------------------- ##

    if choose_model is "SIAM":
        # run = '300'  # l1, avg-pool (data-aug, balanced=True, class_weight=False)
        # run = '301'  # l1, max-pool (data-aug, balanced=True, class_weight=False)
        # run = '302'  # l1, rmac-pool (data-aug, balanced=True, class_weight=False)
        # run = '310'  # l2, avg-pool (data-aug, balanced=True, class_weight=False)
        # run = '311'  # l2, max-pool (data-aug, balanced=True, class_weight=False)
        # run = '312'  # l2, rmac-pool (data-aug, balanced=True, class_weight=False)
        # run = '320'  # cos, avg-pool (data-aug, balanced=True, class_weight=False)
        # run = '321'  # cos, max-pool (data-aug, balanced=True, class_weight=False)
        # run = '322b'  # cos, rmac-pool (data-aug, balanced=True, class_weight=False)

        # b/c - changed margin-loss params
        # run = '313c'  # l2, max-pool MARGINAL-LOSS (data-aug, balanced=True, class_weight=False)
        # run = '314c'  # l2, rmac-pool MARGINAL-LOSS (data-aug, balanced=True, class_weight=False)
        # run = '323c'  # cos, max-pool MARGINAL-LOSS (data-aug, balanced=True, class_weight=False)
        # run = '324c'  # cos, rmac-pool MARGINAL-LOSS (data-aug, balanced=True, class_weight=False)

        # run = 'zzz'

        batch_size = 64 if local else 128

        # model
        generator = DataGeneratorSiam(data_loader,
                                      data_size=data_size,
                                      model_size=model_size,
                                      batch_size=batch_size,
                                      val_factor=0 if skip_validation else 3,
                                      balanced=True,
                                      objective="malignancy",
                                      do_augment=do_augment,
                                      augment=data_augment_params,
                                      use_class_weight=False)

        model = SiamArch(miniXception_loader,
                         input_shape,
                         output_size=out_size,
                         batch_size=batch_size,
                         distance='l2',
                         normalize=normalize,
                         pooling='msrmac')
        model.model.summary()
        model.compile(learning_rate=1e-3, decay=0)
        if use_gen:
            model.load_generator(generator)
        else:
            imgs_trn, lbl_trn = generator.next_train().__next__()
            imgs_val, lbl_val = generator.next_val().__next__()
            model.load_data(imgs_trn, lbl_trn, imgs_val, lbl_val)

    if choose_model is "SIAM_RATING":
        ### clean set
        # run = '400'  # l2-rmac no-conf
        # run = '401'  # cosine-rmac no-conf
        # run = '402'  # l2-rmac conf
        # run = '403'  # cosine-rmac conf
        # run = '404'  # l2-max no-conf
        # run = '405'  # cosine-max no-conf

        ### primary set
        # run = '410'  # l2-rmac no-conf
        # run = '411'  # cosine-rmac no-conf
        # run = '412'  # l2-rmac conf
        # run = '413'  # cosine-rmac conf
        # run = '414'  # l2-max no-conf
        # run = '415'  # cosine-max no-conf

        run = 'zzz'

        obj = 'rating'  # rating / size / rating_size
        batch_size = 16 if local else 64
        reg_loss = None  # {'SampleCorrating_clusters_distance_and_stdrelation': 0.1}  # 'Dispersion', 'Std', 'FeatureCorrelation', 'SampleCorrelation'

        epoch_pre = 60
        preload_weight = None  # FileManager.Weights('dirR', output_dir=input_dir).name(run='251c{}'.format(config), epoch=70)

        should_use_scheduale = (reg_loss is not None) or (obj == 'rating_size')
        '''
        sched = [{'epoch': 00, 'weights': [0.1, 0.9]},
                 {'epoch': 30, 'weights': [0.4, 0.6]},
                 {'epoch': 60, 'weights': [0.6, 0.4]},
                 {'epoch': 80, 'weights': [0.9, 0.1]},
                 {'epoch': 100, 'weights': [1.0, 0.0]}] \
            if should_use_scheduale else []
        '''
        sched = [{'epoch': 00, 'weights': [0.1, 0.9]},
                 {'epoch': 20, 'weights': [0.4, 0.6]},
                 {'epoch': 30, 'weights': [0.6, 0.4]},
                 {'epoch': 50, 'weights': [0.9, 0.1]},
                 {'epoch': 80, 'weights': [1.0, 0.0]}] \
            if should_use_scheduale else []
        # model
        generator = DataGeneratorSiam(data_loader,
                                      data_size=data_size,
                                      model_size=model_size,
                                      batch_size=batch_size,
                                      train_facotr=2,
                                      val_factor=0 if skip_validation else 3,
                                      balanced=False,
                                      objective=obj,
                                      weighted_rating=True,
                                      do_augment=do_augment,
                                      augment=data_augment_params,
                                      use_class_weight=False,
                                      use_confidence=False)

        model = SiamArch(miniXception_loader,
                         input_shape,
                         output_size=out_size,
                         objective=obj,
                         batch_size=batch_size,
                         distance='cosine',
                         normalize=normalize,
                         pooling='rmac',
                         regularization_loss=reg_loss,
                         l1_regularization=False)

        if preload_weight is not None:
            model.load_core_weights(preload_weight)
        model.model.summary()
        model.compile(learning_rate=1e-3,
                      decay=0,
                      loss='logcosh',
                      scheduale=sched)  # mean_squared_error, logcosh
        model.load_generator(generator)

    ## --------------------------------------- ##
    ## ------- Prepare Triplet Architecture ------ ##
    ## --------------------------------------- ##

    if choose_model is "TRIPLET":

        # run = '000'  # rmac softplus, b16
        # run = '001'  # rmac hinge, b16, pre:dirR813-50
        # run = '002'  # rmac hinge, b32, pre:dirR813-50
        # run = '003'  # rmac hinge, b64, pre:dirR813-50
        # run = '004'  # rmac hinge, b128, pre:dirR813-50
        # run = '005'  # rmac hinge, b64, pre:dirR813-50
        run = '006'  # rmac rank, b64, pre:dirR813-50

        # run = 'zzz'

        objective = 'rating'
        use_rank_loss = True

        batch_size = 16 if local else 64

        gen = True
        epoch_pre = 50
        preload_weight = FileManager.Weights(
            'dirR', output_dir=input_dir).name(run='813c{}'.format(config),
                                               epoch=epoch_pre)

        # model
        model = TripArch(miniXception_loader,
                         input_shape,
                         objective=objective,
                         output_size=out_size,
                         distance='l2',
                         normalize=True,
                         pooling='msrmac',
                         categorize=use_rank_loss)

        if preload_weight is not None:
            model.load_core_weights(preload_weight)
        model.model.summary()
        model.compile(learning_rate=1e-3, decay=0)

        generator = DataGeneratorTrip(data_loader,
                                      data_size=data_size,
                                      model_size=model_size,
                                      batch_size=batch_size,
                                      objective=objective,
                                      balanced=(objective == 'malignancy'),
                                      categorize=use_rank_loss,
                                      val_factor=0 if skip_validation else 1,
                                      train_factor=2,
                                      do_augment=do_augment,
                                      augment=data_augment_params,
                                      use_class_weight=False,
                                      use_confidence=False)
        if gen:
            model.load_generator(generator)
        else:
            imgs_trn, lbl_trn = generator.next_train().__next__()
            imgs_val, lbl_val = generator.next_val().__next__()
            model.load_data(imgs_trn, lbl_trn, imgs_val, lbl_val)

    ## --------------------------------------- ##
    ## -------      RUN             ------ ##
    ## --------------------------------------- ##

    cnf_id = config if config_name == 'LEGACY' else CrossValidationManager(
        config_name).get_run_id(config)
    run_name = '{}{}c{}'.format('', run, cnf_id)
    print('Current Run: {}'.format(run_name))
    if no_training:
        model.last_epoch = epochs
        model.run = run_name
    else:
        model.train(run=run_name,
                    epoch=(0 if preload_weight is None else epoch_pre),
                    n_epoch=epochs,
                    gen=use_gen,
                    do_graph=False)

    return model
Exemple #8
0
    if DataSubSet == 0:
        post = "Test"
    elif DataSubSet == 1:
        post = "Valid"
    elif DataSubSet == 2:
        post = "Train"
    elif DataSubSet == -1:
        post = "All"
    else:
        assert False
    print("{} Set Analysis".format(post))
    print('=' * 15)

    if DataSubSet == -1:
        data = load_nodule_dataset(size=160,
                                   sample='Normal',
                                   res=0.5,
                                   configuration=0)
        data = data[0] + data[1] + data[2]
    else:
        data = load_nodule_dataset(size=160)[DataSubSet]

    Ret = Retriever(title='Rating', dset=post)
    Ret.load_rating(data)

    idx_hubness = np.zeros(len(metrics))
    idx_hubness_std_p = np.zeros(len(metrics))
    idx_hubness_std_m = np.zeros(len(metrics))
    idx_symmetry = np.zeros(len(metrics))
    idx_symmetry_std = np.zeros(len(metrics))
    idx_concentration = np.zeros(len(metrics))
    idx_concentration_std = np.zeros(len(metrics))
# model
model_size = 128
input_shape = (model_size, model_size, 1)
out_size = 128

pooling_options = ['max', 'avg', 'msrmac']
metric = 'l2'
repeatitions = 200  # 10 rep ~ 1:20 hour

merge_range = lambda rng1, rng2: (np.minimum(rng1[0], rng2[0]), np.maximum(rng1[1], rng2[1]))

# =====================
#       Load Data
# =====================

dataset = load_nodule_dataset(size=data_size, res=res, sample=sample, configuration=0)
dataset = reduce(lambda x, y: x + y, dataset)

N = len(dataset)

images, masks, classes, meta, size, rating = zip(*dataset[:N])
images = np.array(images)

# =====================
#       Evaluate
# =====================

H_lim, S_lim, Tau_lim, Conc_lim, Crst_lim = (1e6, 0), (1e6, 0), (1e6, 0), (1e6, 0), (1e6, 0)

for p, pooling in enumerate(pooling_options):
    print('Evaluating {} pooling'.format(pooling))
Exemple #10
0
                                    distance_norm='euclidean',
                                    weights=None):
    n = len(ratings)
    distance_matrix = np.zeros((n, n))
    for i in range(n):
        for j in range(n):
            if i <= j:
                continue
            if weights is None:
                distance = rating_clusters_distance(ratings[i], ratings[j],
                                                    distance_norm)
            else:
                distance = rating_clusters_distance(ratings[i], ratings[j],
                                                    distance_norm, weights[i],
                                                    weights[j])
            distance_matrix[i, j] = distance
            distance_matrix[j, i] = distance

    return distance_matrix


if __name__ == "__main__":

    try:
        from Network.data_loader import load_nodule_dataset
    except:
        from data import load_nodule_dataset

    data = load_nodule_dataset()
    test_augment(data[2])
                         output_size=out_size,
                         normalize=normalize)
        if WeightsFile is not None:
            model.load_weights(WeightsFile)
            print('Load from: {}'.format(WeightsFile))
        else:
            print('Model without weights')

        pred_all = []
        labels_test_all = []
        meta_all_0 = []
        meta_all_1 = []
        for i in range(1):
            # prepare test data
            images_test, labels_test,  masks_test, confidence, meta = \
                prepare_data_siamese_simple(load_nodule_dataset(size=size, res=res, sample=sample)[DataSubSet], size=size,
                                     return_meta=True, objective="rating", verbose=1, balanced=True)
            print("Data ready: images({}), labels({})".format(
                images_test[0].shape, labels_test.shape))
            print("Range = [{:.2f},{:.2f}]".format(np.min(images_test[0]),
                                                   np.max(images_test[0])))

            images_test = (
                np.array([
                    crop_center(im, msk, size=in_size)[0]
                    for im, msk in zip(images_test[0], masks_test[0])
                ]),
                np.array([
                    crop_center(im, msk, size=in_size)[0]
                    for im, msk in zip(images_test[1], masks_test[1])
                ]))
DataSubSet = 1
dsets = ['Test', 'Valid', 'Train']

Weights = FileManager.Weights('siam')

wRuns = ['078X']
wEpchs= [24]

run = wRuns[0]
epoch = wEpchs[0]

# Load Data
# =================

images, labels, masks, meta = \
                    prepare_data(load_nodule_dataset(size=inp_size, res=res, sample=sample)[DataSubSet],
                                 categorize=False,
                                 reshuffle=False,
                                 return_meta=True,
                                 verbose=1)

images = np.array([crop_center(im, msk, size=net_size)[0] for im, msk in zip(images, masks)])

# Run
# =================

siam_model = SiamArch(miniXception_loader, input_shape, distance='l2', output_size=out_size, normalize=True)
embed_model = siam_model.extract_core(weights=Weights(run=run, epoch=epoch))
embed_model.layers[1].summary()

layer_names = ['block13_sepconv2_bn'] #['block1_conv1', 'block1_conv1_bn', 'block1_conv1_act']