Esempio n. 1
0
 def test_MODEL_loading(self):
     nn = Neural_Network(preprocessor=self.pp3D)
     model_path = os.path.join(self.tmp_dir3D.name, "my_model.hdf5")
     nn.dump(model_path)
     nn_new = Neural_Network(preprocessor=self.pp3D)
     nn_new.load(model_path)
Esempio n. 2
0
 def test_MODEL_resetWeights(self):
     nn = Neural_Network(preprocessor=self.pp3D)
     nn.reset_weights()
Esempio n. 3
0
 def test_MODEL_validation3D(self):
     nn = Neural_Network(preprocessor=self.pp3D)
     history = nn.evaluate(self.sample_list3D[0:4], self.sample_list3D[4:6],
                           epochs=3)
     self.assertIsNotNone(history)
Esempio n. 4
0
 def test_MODEL_storage(self):
     nn = Neural_Network(preprocessor=self.pp3D)
     model_path = os.path.join(self.tmp_dir3D.name, "my_model.hdf5")
     nn.dump(model_path)
     self.assertTrue(os.path.exists(model_path))
Esempio n. 5
0
 def test_MODEL_training3D(self):
     nn = Neural_Network(preprocessor=self.pp3D)
     nn.train(self.sample_list3D, epochs=3)
Esempio n. 6
0
 def test_MODEL_prediction_returnOutput(self):
     nn = Neural_Network(preprocessor=self.pp2D)
     pred_list = nn.predict(self.sample_list2D, return_output=True)
     for pred in pred_list:
         self.assertIsNotNone(pred)
         self.assertEqual(pred.shape, (16,16))
Esempio n. 7
0
 def test_MODEL_multiGPU(self):
     nn = Neural_Network(preprocessor=self.pp2D,
                         multi_gpu=True)
     nn.train(self.sample_list2D, epochs=3)
## want a specific shape (e.g. DenseNet for classification)
sf = [Resize(new_shape=(224, 224))]

# Initialize the Preprocessor class
pp = Preprocessor(data_io,
                  data_aug=None,
                  batch_size=1,
                  subfunctions=sf,
                  prepare_subfunctions=True,
                  prepare_batches=False,
                  analysis="fullimage")
## We are using fullimage analysis due to a 2D image can easily fit completely
## in our GPU

# Initialize the neural network model
model = Neural_Network(preprocessor=pp)

# Start the fitting on some slices
model.train(samples_list[30:50], epochs=3, iterations=10, callbacks=[])

# Predict a generic slice with direct output
pred = model.predict(["case_00002:#:42"], direct_output=True)
print(np.asarray(pred).shape)
## Be aware that the direct prediction output, has a additional batch axis

# Predict a generic slice and save it as a NumPy pickle on disk
model.predict(["case_00002:#:42"], direct_output=False)

# Load the slice via sample-loader and output also the new prediction, now
sample = data_io.sample_loader("case_00000:#:89",
                               load_seg=True,
Esempio n. 9
0
sf = [sf_clipping, sf_normalize, sf_resample, sf_zscore]

# Create and configure the Preprocessor class
pp = Preprocessor(data_io, data_aug=data_aug, batch_size=2, subfunctions=sf,
                  prepare_subfunctions=True, prepare_batches=False,
                  analysis="patchwise-crop", patch_shape=(160, 160, 80))
# Adjust the patch overlap for predictions
pp.patchwise_overlap = (80, 80, 30)

# Initialize the Architecture
unet_standard = Architecture(depth=4, activation="softmax",
                             batch_normalization=True)

# Create the Neural Network model
model = Neural_Network(preprocessor=pp, architecture=unet_standard,
                       loss=tversky_crossentropy,
                       metrics=[tversky_loss, dice_soft, dice_crossentropy],
                       batch_queue_size=3, workers=3, learninig_rate=0.001)

# Define Callbacks
cb_lr = ReduceLROnPlateau(monitor='loss', factor=0.1, patience=15,
                          verbose=1, mode='min', min_delta=0.0001, cooldown=1,
                          min_lr=0.00001)
cb_es = EarlyStopping(monitor="loss", patience=100)
cb_tb = TensorBoard(log_dir=os.path.join(fold_subdir, "tensorboard"),
                    histogram_freq=0, write_graph=True, write_images=True)
cb_cl = CSVLogger(os.path.join(fold_subdir, "logs.csv"), separator=',',
                  append=True)
cb_mc = ModelCheckpoint(os.path.join(fold_subdir, "model.best.hdf5"),
                        monitor="loss", verbose=1,
                        save_best_only=True, mode="min")
Esempio n. 10
0
class metricTEST(unittest.TestCase):
    # Create random imaging and segmentation data
    @classmethod
    def setUpClass(self):
        np.random.seed(1234)
        # Create 2D imgaging and segmentation data set
        self.dataset = dict()
        for i in range(0, 2):
            img = np.random.rand(16, 16) * 255
            self.img = img.astype(int)
            seg = np.random.rand(16, 16) * 2
            self.seg = seg.astype(int)
            self.dataset["TEST.sample_" + str(i)] = (self.img, self.seg)
        # Initialize Dictionary IO Interface
        io_interface = Dictionary_interface(self.dataset, classes=3,
                                              three_dim=False)
        # Initialize temporary directory
        self.tmp_dir = tempfile.TemporaryDirectory(prefix="tmp.miscnn.")
        tmp_batches = os.path.join(self.tmp_dir.name, "batches")
        # Initialize Data IO
        self.data_io = Data_IO(io_interface,
                               input_path=os.path.join(self.tmp_dir.name),
                               output_path=os.path.join(self.tmp_dir.name),
                               batch_path=tmp_batches, delete_batchDir=False)
        # Initialize Preprocessor
        self.pp = Preprocessor(self.data_io, batch_size=1,
                               data_aug=None, analysis="fullimage")
        # Initialize Neural Network
        self.model = Neural_Network(self.pp)
        # Get sample list
        self.sample_list = self.data_io.get_indiceslist()

    # Delete all temporary files
    @classmethod
    def tearDownClass(self):
        self.tmp_dir.cleanup()

    #-------------------------------------------------#
    #               Standard DSC Metric               #
    #-------------------------------------------------#
    def test_METRICS_DSC_standard(self):
        self.model.loss = dice_coefficient
        self.model.metrics = [dice_coefficient]
        self.model.train(self.sample_list, epochs=1)

    #-------------------------------------------------#
    #                Standard DSC Loss               #
    #-------------------------------------------------#
    def test_METRICS_DSC_standardLOSS(self):
        self.model.loss = dice_coefficient_loss
        self.model.metrics = [dice_coefficient_loss]
        self.model.train(self.sample_list, epochs=1)

    #-------------------------------------------------#
    #                 Soft DSC Metric                 #
    #-------------------------------------------------#
    def test_METRICS_DSC_soft(self):
        self.model.loss = dice_soft
        self.model.metrics = [dice_soft]
        self.model.train(self.sample_list, epochs=1)

    #-------------------------------------------------#
    #                  Soft DSC Loss                  #
    #-------------------------------------------------#
    def test_METRICS_DSC_softLOSS(self):
        self.model.loss = dice_soft_loss
        self.model.metrics = [dice_soft_loss]
        self.model.train(self.sample_list, epochs=1)

    #-------------------------------------------------#
    #                   Weighted DSC                  #
    #-------------------------------------------------#
    def test_METRICS_DSC_weighted(self):
        self.model.loss = dice_weighted([1,1,4])
        self.model.metrics = [dice_weighted([1,1,4])]
        self.model.train(self.sample_list, epochs=1)

    #-------------------------------------------------#
    #             Dice & Crossentropy loss            #
    #-------------------------------------------------#
    def test_METRICS_DSC_CrossEntropy(self):
        self.model.loss = dice_crossentropy
        self.model.metrics = [dice_crossentropy]
        self.model.train(self.sample_list, epochs=1)

    #-------------------------------------------------#
    #                   Tversky loss                  #
    #-------------------------------------------------#
    def test_METRICS_Tversky(self):
        self.model.loss = tversky_loss
        self.model.metrics = [tversky_loss]
        self.model.train(self.sample_list, epochs=1)

    #-------------------------------------------------#
    #           Tversky & Crossentropy loss           #
    #-------------------------------------------------#
    def test_METRICS_Tversky_CrossEntropy(self):
        self.model.loss = tversky_crossentropy
        self.model.metrics = [tversky_crossentropy]
        self.model.train(self.sample_list, epochs=1)

    #-------------------------------------------------#
    #            Focal Loss - Categorical             #
    #-------------------------------------------------#
    def test_METRICS_FocalCategorical(self):
        self.model.loss = categorical_focal_loss([0.9, 0.1])
        self.model.metrics = [categorical_focal_loss([0.1, 0.9])]
        self.model.train(self.sample_list, epochs=1)
Esempio n. 11
0
# Create a pixel value normalization Subfunction for z-score scaling
sf_zscore = Normalization(mode="z-score")

# Assemble Subfunction classes into a list
sf = [sf_clipping, sf_normalize, sf_resample, sf_zscore]

# Create and configure the Preprocessor class
pp = Preprocessor(data_io, data_aug=None, batch_size=2, subfunctions=sf,
                  prepare_subfunctions=True, prepare_batches=False,
                  analysis="patchwise-crop", patch_shape=(160, 160, 80),
                  use_multiprocessing=True)
# Adjust the patch overlap for predictions
pp.patchwise_overlap = (80, 80, 30)
pp.mp_threads = 16

# Initialize the Architecture
unet_standard = Architecture(depth=4, activation="softmax",
                             batch_normalization=True)

# Create the Neural Network model
model = Neural_Network(preprocessor=pp, architecture=unet_standard,
                       loss=tversky_crossentropy,
                       metrics=[tversky_loss, dice_soft, dice_crossentropy],
                       batch_queue_size=3, workers=3, learninig_rate=0.001)

# Load best model weights during fitting
model.load(path_model)

# Compute predictions
model.predict(sample_list, return_output=False)