loss = 'binary_crossentropy'
        metrics = 'binary_accuracy'
    else:
        trainLabels = to_categorical(trainLabels)
        validationLabels = to_categorical(validationLabels)

        activation = 'softmax'
        nClasses = len(classNames)

        loss = 'categorical_crossentropy'
        metrics = 'categorical_accuracy'
else:
    nClasses = len(classNames)

    metrics = IOUScore(threshold=0.5)

    if nClasses == 1:
        activation = 'sigmoid'
        loss = binary_focal_dice_loss
    else:
        activation = 'softmax'
        loss = categorical_focal_dice_loss

# Configura el generador de datos para usar data augmentation y redimensión
trainData = DataGenerator(trainImages,
                          trainLabels,
                          batchSize=batchSize,
                          resize=resize(imageDimensions[:-1]),
                          augmenters=augmenters,
                          shuffle=True)
Exemple #2
0
                        classes=cfg.CLASSES,
                        augmentation=get_validation_augmentation(
                            cfg.image_size),
                        preprocessing=train_preprocessing)

train_dataloader = Dataloader(train_dataset,
                              batch_size=cfg.BATCH_SIZE,
                              shuffle=True)
valid_dataloader = Dataloader(valid_dataset,
                              batch_size=cfg.BATCH_SIZE,
                              shuffle=False)

loss = cfg.loss
optim = tf.keras.optimizers.SGD(lr=cfg.LR, momentum=0.9, nesterov=True)
metrics = [
    IOUScore(threshold=cfg.metric_threshold, per_image=cfg.metric_per_image),
    FScore(threshold=cfg.metric_threshold, per_image=cfg.metric_per_image),
    Precision(threshold=cfg.metric_threshold, per_image=cfg.metric_per_image),
    Recall(threshold=cfg.metric_threshold, per_image=cfg.metric_per_image),
    TruePositives(thresholds=cfg.metric_threshold),
    TrueNegatives(thresholds=cfg.metric_threshold),
    FalsePositives(thresholds=cfg.metric_threshold),
    FalseNegatives(thresholds=cfg.metric_threshold)
]

# with mirrored_strategy.scope():
model = sm.Unet(backbone_name=cfg.backbone_name,
                input_shape=cfg.input_shape,
                classes=n_classes,
                activation='sigmoid' if n_classes == 1 else 'softmax',
                weights=None,
Exemple #3
0
#assert np.max(X_val) == 1
#assert np.min(X_val) == -1

X_tst = preprocess_input(X_tst)
#assert np.max(X_tst) == 1
#assert np.min(X_tst) == -1

print(len(X_trn), len(X_val), len(X_tst))

# training
print(':: training')
print("dataset: ", dataset, "model_name: ", model_name, "seed: ", seed_id,
      " is training")
model.compile(optimizer=Adam(lr=1e-4),
              loss='binary_crossentropy',
              metrics=[IOUScore(threshold=0.5,
                                per_image=True)])  # model.summary()

earlystopper = EarlyStopping(monitor='val_loss',
                             patience=10,
                             restore_best_weights=True,
                             verbose=1)
# mcp_save = ModelCheckpoint('mnnet_seg_'+str(data_id)+'.h5', save_best_only=True, monitor='val_loss', mode='min')#monitor='val_iou_score', mode='max')
mcp_save = ModelCheckpoint(dataset + '/models/' + dataset + '_' + model_name +
                           '_' + "seed_" + str(seed_id) + '.h5',
                           save_best_only=True,
                           monitor='val_loss',
                           mode='min')  # monitor='val_iou_score', mode='max'))
model.fit_generator(
    data_flow,
    steps_per_epoch=np.ceil(len(X_trn) / batch_size),
    epochs=100,
Exemple #4
0
from segmentation_models.metrics import FScore, IOUScore
from segmentation_models.losses import DiceLoss

import numpy as np
'''
This file is used to train  and evaluate model
'''

IMG_WIDTH, IMG_HEIGHT = 256, 256
BATCH_SIZE = 4
EPOCHS = 15
IMG_CHANNELS = 1

dice_loss = DiceLoss(beta=1)
dice_metric = FScore(beta=1)
iou_metric = IOUScore()
'''
Training kidney
'''


def train_network(X_train, Y_train, X_test, Y_test, type_):
    '''
    Function trains model in regard to type_ (kidney model or tumour model)

    Takes:
        X_train -> arrays with training images
        Y_train -> array with training masks
        X_test -> array with test images
        Y_test -> array with test masks
        type_ -> string, 'kidney' or 'tumour'
#assert np.min(X_trn) == -1

X_val = preprocess_input(X_val)
#assert np.max(X_val) == 1
#assert np.min(X_val) == -1

X_tst = preprocess_input(X_tst)
#assert np.max(X_tst) == 1
#assert np.min(X_tst) == -1

print(len(X_trn), len(X_val), len(X_tst))

# prediction
print(':: prediction')
model = load_model(dataset + '/models/' + dataset + '_' + model_name + '_' + "seed_" + str(seed_id) + '.h5',
                   custom_objects={'iou_score': IOUScore(threshold=0.5, per_image=True)})

decay = decay_list[decay_id]
lam = lam_list[lam_id]
combine = combine_list[combine_id]
T = 5

a = 0
b = 0

single_result_iou = []
ensemble_result_iou = []
single_result_auroc = []
ensemble_result_auroc = []

for it in range(T + 1):
X_val = preprocess_input(X_val)
assert np.max(X_val) == 1
assert np.min(X_val) == -1

X_tst = preprocess_input(X_tst)
assert np.max(X_tst) == 1
assert np.min(X_tst) == -1

print(tmpbatchx.shape, tmpbatchy.shape)
print(X_tst.shape, Y_tst.shape)

# training
print(':: training')
model.compile(optimizer=Adam(lr=1e-4),
              loss=bce_jaccard_loss,
              metrics=[IOUScore(threshold=0.5,
                                per_image=True)])  #loss='binary_crossentropy'
#model.summary()

earlystopper = EarlyStopping(monitor='val_loss',
                             patience=20,
                             restore_best_weights=True,
                             verbose=1)
reduce_lr = ReduceLROnPlateau(monitor='val_loss',
                              factor=0.1,
                              patience=10,
                              min_lr=1e-7,
                              verbose=1)
mcp_save = ModelCheckpoint('mnnet_seg_' + str(model_id) + '.h5',
                           save_best_only=True,
                           monitor='val_iou_score',
                           mode='max')