Exemple #1
0
def evaluate(model_path, idx):
    '''
        Evaluate performance of single CT slice

        :model_path: path of evaluated model
        :idx: index of evaluated patient
    '''
    model = UNet(input_shape=(512, 512, 1))
    model.compile(optimizer=Adam(lr=1e-4),
                  loss=focal_tversky_loss,
                  metrics=[dice_coef])
    model.load_weights(model_path)

    # reading original images
    reader = sitk.ImageSeriesReader()
    dicom_names = reader.GetGDCMSeriesFileNames(data_path + 'volume/' +
                                                str(idx) + '/')
    reader.SetFileNames(dicom_names)
    images = reader.Execute()
    images = sitk.GetArrayFromImage(images)  # images.shape: (slices, 512, 512)

    # HU values are restricted to lung window settings [-1100, 300]
    # namely: window width: 1400, window level: -400
    images[images < -1100] = -1100
    images[images > 300] = 300

    # normalization
    max_value, min_value = images.max(), images.min()
    images = images / (max_value - min_value)

    # reading ground truth labels
    labels = sitk.ReadImage(data_path + 'segmentation/' + str(idx) + '.nii')
    labels = sitk.GetArrayFromImage(
        labels)  # eval_labels.shape: (slices, 512, 512)

    # (slices, 512, 512) => (slices, 512, 512, 1)
    images = np.expand_dims(images, axis=-1)
    labels = np.expand_dims(labels, axis=-1)

    results = model.evaluate(images, labels, batch_size=1)
    for i, metric in enumerate(model.metrics_names):
        print(metric, ':', results[i])
    architecture = sys.argv[2]

    if architecture == '--UNet':
        UNet = UNet()
        UNet.summary()
    elif architecture == '--UNet++':
        UNetPP = UNetPP()
        UNetPP.summary()
    else:
        raise Exception(
            "You have passed an invalid argument.Check the documentation for the allowed arguments."
        )

elif command == "--Evaluate":
    architecture = sys.argv[2]

    if architecture == '--UNet':
        UNet = UNet()
        UNet.evaluate()
    elif architecture == '--UNet++':
        UNetPP = UNetPP()
        UNetPP.evaluate()
    else:
        raise Exception(
            "You have passed an invalid argument.Check the documentation for the allowed arguments."
        )

else:
    raise Exception(
        "You have passed an invalid argument.Check the documentation for the allowed arguments."
    )