def segmentVolume(
        myNetworkModel,
        i_d,
        imageNames_Test,
        names_Test,
        groundTruthNames_Test,
        roiNames_Test,
        imageType,
        padInputImagesBool,
        receptiveField,
        sampleSize_Test,
        strideVal,
        batch_Size,
        task  # Validation (0) or testing (1)
):
    # Get info from the network model
    networkName = myNetworkModel.networkName
    folderName = myNetworkModel.folderName
    n_classes = myNetworkModel.n_classes
    sampleSize_Test = myNetworkModel.sampleSize_Test
    receptiveField = myNetworkModel.receptiveField
    outputShape = myNetworkModel.lastLayer.outputShapeTest[2:]
    batch_Size = myNetworkModel.batch_Size
    padInputImagesBool = True

    # Get half sample size
    sampleHalf = []
    for h_i in range(3):
        sampleHalf.append((receptiveField[h_i] - 1) / 2)

    # Load the images to segment
    [imgSubject, gtLabelsImage, roi, paddingValues] = load_imagesSinglePatient(
        i_d,
        imageNames_Test,
        groundTruthNames_Test,
        roiNames_Test,
        padInputImagesBool,
        receptiveField,
        sampleSize_Test,
        imageType,
    )

    # Get image dimensions
    imgDims = list(imgSubject.shape)

    [sampleCoords] = sampleWholeImage(imgSubject, roi, sampleSize_Test,
                                      strideVal, batch_Size)

    numberOfSamples = len(sampleCoords)
    sampleID = 0
    numberOfBatches = numberOfSamples / batch_Size

    #The probability-map that will be constructed by the predictions.
    probMaps = np.zeros([n_classes] + imgDims, dtype="float32")

    # Run over all the batches
    for b_i in xrange(numberOfBatches):

        # Get samples for batch b_i

        sampleCoords_b = sampleCoords[b_i * batch_Size:(b_i + 1) * batch_Size]

        [imgSamples] = extractSamples(imgSubject, sampleCoords_b,
                                      sampleSize_Test, receptiveField)

        # Load the data of the batch on the GPU
        myNetworkModel.testingData_x.set_value(imgSamples, borrow=True)

        # Call the testing Theano function
        predictions = myNetworkModel.networkModel_Test(0)

        predOutput = predictions[-1]

        # --- Now we can generate the probability maps from the predictions ----
        # Run over all the regions
        for r_i in xrange(batch_Size):

            sampleCoords_i = sampleCoords[sampleID]
            coords = [
                sampleCoords_i[0][0], sampleCoords_i[1][0],
                sampleCoords_i[2][0]
            ]

            # Get the min and max coords
            xMin = coords[0] + sampleHalf[0]
            xMax = coords[0] + sampleHalf[0] + strideVal[0]

            yMin = coords[1] + sampleHalf[1]
            yMax = coords[1] + sampleHalf[1] + strideVal[1]

            zMin = coords[2] + sampleHalf[2]
            zMax = coords[2] + sampleHalf[2] + strideVal[2]

            probMaps[:, xMin:xMax, yMin:yMax, zMin:zMax] = predOutput[r_i]

            sampleID += 1

    # Release data
    myNetworkModel.testingData_x.set_value(
        np.zeros([1, 1, 1, 1, 1], dtype="float32"))

    # Segmentation has been done in this point.

    # Now: Save the data
    # Get the segmentation from the probability maps ---
    segmentationImage = np.argmax(probMaps, axis=0)

    #Save Result:
    npDtypeForPredictedImage = np.dtype(np.int16)
    suffixToAdd = "_Segm"

    # Apply unpadding if specified
    if padInputImagesBool == True:
        segmentationRes = applyUnpadding(segmentationImage, paddingValues)
    else:
        segmentationRes = segmentationImage

    # Generate folders to store the model
    BASE_DIR = os.getcwd()
    path_Temp = os.path.join(BASE_DIR, 'outputFiles')

    # For the predictions
    predlFolderName = os.path.join(path_Temp, myNetworkModel.folderName)
    predlFolderName = os.path.join(predlFolderName, 'Pred')
    if task == 0:
        predTestFolderName = os.path.join(predlFolderName, 'Validation')
    else:
        predTestFolderName = os.path.join(predlFolderName, 'Testing')

    nameToSave = predTestFolderName + '/Segmentation_' + names_Test[i_d]

    # Save Segmentation image

    print(" ... Saving segmentation result..."),
    if imageType == 0:  # nifti
        imageTypeToSave = np.dtype(np.int16)
        saveImageAsNifti(segmentationRes, nameToSave, imageNames_Test[i_d],
                         imageTypeToSave)
    else:  # Matlab
        # Cast to int8 for saving purposes
        saveImageAsMatlab(segmentationRes.astype('int8'), nameToSave)

    # Save the prob maps for each class (except background)
    for c_i in xrange(1, n_classes):

        nameToSave = predTestFolderName + '/ProbMap_class_' + str(
            c_i) + '_' + names_Test[i_d]

        probMapClass = probMaps[c_i, :, :, :]

        # Apply unpadding if specified
        if padInputImagesBool == True:
            probMapClassRes = applyUnpadding(probMapClass, paddingValues)
        else:
            probMapClassRes = probMapClass

        print(" ... Saving prob map for class {}...".format(str(c_i))),
        if imageType == 0:  # nifti
            imageTypeToSave = np.dtype(np.float32)
            saveImageAsNifti(probMapClassRes, nameToSave, imageNames_Test[i_d],
                             imageTypeToSave)
        else:
            # Cast to float32 for saving purposes
            saveImageAsMatlab(probMapClassRes.astype('float32'), nameToSave)

    # If segmentation done during evaluation, get dice
    if task == 0:
        print(" ... Computing Dice scores: ")
        DiceArray = computeDice(segmentationImage, gtLabelsImage)
        for d_i in xrange(len(DiceArray)):
            print(" -------------- DSC (Class {}) : {}".format(
                str(d_i + 1), DiceArray[d_i]))
def segmentVolume(myNetworkModel,
                  i_d,
                  imageNames_Test,
                  imageNames_Test_Bottom,
                  names_Test,
                  groundTruthNames_Test,
                  roiNames_Test,
                  imageType,
                  padInputImagesBool,
                  receptiveField, 
                  sampleSize_Test,
                  strideVal,
                  batch_Size,
                  task # Validation (0) or testing (1)
                  ):
        # Get info from the network model        
        networkName        = myNetworkModel.networkName
        folderName         = myNetworkModel.folderName
        n_classes          = myNetworkModel.n_classes
        sampleSize_Test    = myNetworkModel.sampleSize_Test
        receptiveField     = myNetworkModel.receptiveField  
        outputShape        = myNetworkModel.lastLayer.outputShapeTest[2:] 
        batch_Size         = myNetworkModel.batch_Size
        padInputImagesBool = True
    
        # Get half sample size
        sampleHalf = []
        for h_i in range(3):
            sampleHalf.append((receptiveField[h_i]-1)/2)
        
        # Load the images to segment
        [imgSubject, 
        imgSubject_Bottom, 
        gtLabelsImage, 
        roi, 
        paddingValues] = load_imagesSinglePatient(i_d,
                                                  imageNames_Test,
                                                  imageNames_Test_Bottom,
                                                  groundTruthNames_Test,
                                                  roiNames_Test,
                                                  padInputImagesBool,
                                                  receptiveField, 
                                                  sampleSize_Test,
                                                  imageType, 
                                                  )
                                                  
                                  
        # Get image dimensions                                                    
        imgDims = list(imgSubject.shape)
    
        [ sampleCoords ] = sampleWholeImage(imgSubject,
                                            roi,
                                            sampleSize_Test,
                                            strideVal,
                                            batch_Size
                                            )
        
        numberOfSamples = len(sampleCoords)
        sampleID = 0
        numberOfBatches = numberOfSamples/batch_Size

        #The probability-map that will be constructed by the predictions.
        probMaps = np.zeros([n_classes]+imgDims, dtype = "float32")
        
        # Run over all the batches 
        for b_i in xrange(numberOfBatches) :
                 
            # Get samples for batch b_i
            
            sampleCoords_b = sampleCoords[ b_i*batch_Size : (b_i+1)*batch_Size ]
            
            [imgSamples,
            imgSample_Bottom] = extractSamples(imgSubject,
                                          imgSubject_Bottom,
                                          sampleCoords_b,
                                          sampleSize_Test,
                                          receptiveField)

            # Load the data of the batch on the GPU
            myNetworkModel.testingData_x.set_value(imgSamples, borrow=True)
            myNetworkModel.testingData_x_Bottom.set_value(imgSample_Bottom, borrow=True)
           
            # Call the testing Theano function            
            predictions = myNetworkModel.networkModel_Test(0)
            
            predOutput = predictions[-1]
            
            # --- Now we can generate the probability maps from the predictions ----
            # Run over all the regions
            for r_i in xrange(batch_Size) :
 
                sampleCoords_i = sampleCoords[sampleID]
                coords = [ sampleCoords_i[0][0], sampleCoords_i[1][0], sampleCoords_i[2][0] ]

                # Get the min and max coords
                xMin = coords[0] + sampleHalf[0]
                xMax = coords[0] + sampleHalf[0] + strideVal[0]

                yMin = coords[1] + sampleHalf[1]
                yMax = coords[1] + sampleHalf[1] + strideVal[1]

                zMin = coords[2] + sampleHalf[2]
                zMax = coords[2] + sampleHalf[2] + strideVal[2]
                
                probMaps[:,xMin:xMax, yMin:yMax, zMin:zMax] = predOutput[r_i]

                sampleID += 1
            
        # Release data
        myNetworkModel.testingData_x.set_value(np.zeros([1,1,1,1,1], dtype="float32"))
        myNetworkModel.testingData_x_Bottom.set_value(np.zeros([1,1,1,1,1], dtype="float32"))

        # Segmentation has been done in this point.
        
        # Now: Save the data
        # Get the segmentation from the probability maps ---
        segmentationImage = np.argmax(probMaps, axis=0) 
        
        #Save Result:
        npDtypeForPredictedImage = np.dtype(np.int16)
        suffixToAdd = "_Segm"
 
        # Apply unpadding if specified
        if padInputImagesBool == True:
            segmentationRes = applyUnpadding(segmentationImage, paddingValues)
        else:
            segmentationRes = segmentationImage

        # Generate folders to store the model
        BASE_DIR = os.getcwd()
        path_Temp = os.path.join(BASE_DIR,'outputFiles')

        # For the predictions
        predlFolderName = os.path.join(path_Temp,myNetworkModel.folderName)
        predlFolderName = os.path.join(predlFolderName,'Pred')
        if task == 0:
            predTestFolderName = os.path.join(predlFolderName,'Validation')
        else:
            predTestFolderName = os.path.join(predlFolderName,'Testing')
        
        nameToSave = predTestFolderName + '/Segmentation_'+ names_Test[i_d]
        
        # Save Segmentation image
        
        print(" ... Saving segmentation result..."),
        if imageType == 0: # nifti
            imageTypeToSave = np.dtype(np.int16)
            saveImageAsNifti(segmentationRes,
                             nameToSave,
                             imageNames_Test[i_d],
                             imageTypeToSave)
        else: # Matlab
            # Cast to int8 for saving purposes
            saveImageAsMatlab(segmentationRes.astype('int8'),
                              nameToSave)


        # Save the prob maps for each class (except background)
        for c_i in xrange(1, n_classes) :
            
            
            nameToSave = predTestFolderName + '/ProbMap_class_'+ str(c_i) + '_' + names_Test[i_d] 

            probMapClass = probMaps[c_i,:,:,:]

            # Apply unpadding if specified
            if padInputImagesBool == True:
                probMapClassRes = applyUnpadding(probMapClass, paddingValues)
            else:
                probMapClassRes = probMapClass

            print(" ... Saving prob map for class {}...".format(str(c_i))),
            if imageType == 0: # nifti
                imageTypeToSave = np.dtype(np.float32)
                saveImageAsNifti(probMapClassRes,
                                 nameToSave,
                                 imageNames_Test[i_d],
                                 imageTypeToSave)
            else:
                # Cast to float32 for saving purposes
                saveImageAsMatlab(probMapClassRes.astype('float32'),
                                  nameToSave)

        # If segmentation done during evaluation, get dice
        if roiNames_Test == 1:
            segmentationImage = segmentationImage*roi

        if task == 0:
            print(" ... Computing Dice scores: ")
            DiceArray = computeDice(segmentationImage,gtLabelsImage)
            for d_i in xrange(len(DiceArray)):
                print(" -------------- DSC (Class {}) : {}".format(str(d_i+1),DiceArray[d_i]))
Beispiel #3
0
def segmentVolume(myNetworkModel,
                  folderName,
                  i_d,
                  imageNames_Test,
                  names_Test,
                  groundTruthNames_Test,
                  imageType,
                  sampleSize_Test,
                  strideVal,
                  n_classes,
                  batch_Size,
                  task, # Validation (0) or testing (1)
                  ):
        
        # Load the images to segment
        [imgSubject,  
        gtLabelsImage] = load_imagesSinglePatient(i_d,
                                                  imageNames_Test,
                                                  groundTruthNames_Test,
                                                  sampleSize_Test,
                                                  imageType
                                                  )
                                                  
                                  
        # Get image dimensions                                                    
        imgDims = list(imgSubject.shape)
                   
        [ sampleCoords ] = sampleWholeImage(imgSubject,
                                            sampleSize_Test,
                                            strideVal,
                                            batch_Size
                                            )        
        numberOfSamples = len(sampleCoords)
        #print('numberOfSamples:',numberOfSamples)
        sampleID = 0
        numberOfBatches = int(numberOfSamples/batch_Size)
        #print(numberOfSamples,batch_Size)
        #The probability-map that will be constructed by the predictions.
        probMaps = np.zeros([n_classes]+imgDims, dtype = "float32")
        countMaps = np.zeros([n_classes]+imgDims, dtype = "float32")
        # Run over all the batches 
        
        for b_i in range(numberOfBatches) :
                 
            # Get samples for batch b_i
            
            sampleCoords_b = sampleCoords[ b_i*batch_Size : (b_i+1)*batch_Size ]
            

            [imgSamples] = extractSamples(imgSubject,
                                          sampleCoords_b,
                                          sampleSize_Test)

            # Load the data of the batch on the GPU
            imgSamples = np.array(imgSamples)
            test_data = MyDatasetTest(imgSamples,transform=transforms.ToTensor())
            test_loader = DataLoader(test_data, batch_Size, shuffle=False, num_workers=8)
            predictions = test(myNetworkModel, test_loader)

            # --- Now we can generate the probability maps from the predictions ----
            # Run over all the regions
            
            tmpPredMap = np.zeros([n_classes]+imgDims, dtype = "float32")
            tmpCountMap = np.zeros([n_classes]+imgDims, dtype = "float32")
            for r_i in range(batch_Size) :
                sampleCoords_i = sampleCoords[sampleID]
                coords = [ sampleCoords_i[0][0], sampleCoords_i[1][0], sampleCoords_i[2][0] ]

                # Get the min and max coords
                xMin = coords[0]
                xMax = coords[0] + sampleSize_Test[0]

                yMin = coords[1]
                yMax = coords[1] + sampleSize_Test[1]

                zMin = coords[2] 
                zMax = coords[2] + sampleSize_Test[2]
                
                tmpPredMap[:,xMin:xMax, yMin:yMax, zMin:zMax] = predictions[r_i]
                tmpCountMap[:,xMin:xMax, yMin:yMax, zMin:zMax] = 1
                probMaps += tmpPredMap 
                countMaps += tmpCountMap
                #tmpPredMap[:,xMin:xMax, yMin:yMax, zMin:zMax] = predictions[r_i]
                #probMaps += tmpPredMap 
        
                sampleID += 1
            
        # Now: Save the data
        # Get the segmentation from the probability maps ---
        #probMaps /= sampleID
        #segmentationImage = np.argmax(probMaps, axis=0) 
        addition = np.double(np.logical_not(countMaps!=0))
        countMaps = countMaps + addition
        probMaps /= countMaps
        segmentationImage = np.argmax(probMaps, axis=0) 
        #Save Result:
        npDtypeForPredictedImage = np.dtype(np.int16)
        suffixToAdd = "_Segm"
 
        segmentationRes = segmentationImage

        # Generate folders to store the model
        BASE_DIR = os.getcwd()
        path_Temp = os.path.join(BASE_DIR,'outputFiles')

        # For the predictions
        predlFolderName = os.path.join(path_Temp,folderName)
        predlFolderName = os.path.join(predlFolderName,'Pred')
        if task == 0:
            predTestFolderName = os.path.join(predlFolderName,'Validation')
        else:
            predTestFolderName = os.path.join(predlFolderName,'Testing')
        
        dirMake(predTestFolderName)
        nameToSave = predTestFolderName + '/Segmentation_'+ names_Test[i_d]
        
        # Save Segmentation image
        
        print(" ... Saving segmentation result..."),
        if imageType == 0: # nifti
            imageTypeToSave = np.dtype(np.int16)
            saveImageAsNifti(segmentationRes,
                             nameToSave,
                             imageNames_Test[i_d],
                             imageTypeToSave)
        else: # Matlab
            # Cast to int8 for saving purposes
            saveImageAsMatlab(segmentationRes.astype('int8'),
                              nameToSave)


        # Save the prob maps for each class (except background)
        for c_i in range(1, n_classes) :
            
            
            nameToSave = predTestFolderName + '/ProbMap_class_'+ str(c_i) + '_' + names_Test[i_d] 

            probMapClass = probMaps[c_i,:,:,:]

            probMapClassRes = probMapClass

            print(" ... Saving prob map for class {}...".format(str(c_i))),
            if imageType == 0: # nifti
                imageTypeToSave = np.dtype(np.float32)
                saveImageAsNifti(probMapClassRes,
                                 nameToSave,
                                 imageNames_Test[i_d],
                                 imageTypeToSave)
            else:
                # Cast to float32 for saving purposes
                saveImageAsMatlab(probMapClassRes.astype('float32'),
                                  nameToSave)

        # If segmentation done during evaluation, get dice
        if task == 0:
            print(" ... Computing Dice scores: ")
            [DiceArray, DiceAll] = computeDice(segmentationImage,gtLabelsImage)
            for d_i in range(len(DiceArray)):
                print(" ------DSC (Class {}) : {}".format(str(d_i+1),DiceArray[d_i]))
            print("------DSC (All ROIs) : {}".format(DiceAll))
        
        if task == 1:
            print(" ... Computing Dice scores: ")
            [DiceArray, DiceAll] = computeDice(segmentationImage,gtLabelsImage)
            for d_i in range(len(DiceArray)):
                print(" -------------- DSC (Class {}) : {}".format(str(d_i+1),DiceArray[d_i]))
            print("------DSC (All ROIs) : {}".format(DiceAll))

        return DiceArray, DiceAll
def checkAnotatedLabels(argv):
    # Number of input arguments
    #    1: Folder containing label images
    #    2: Folder to save corrected label images
    #    3: Number of expected classes (including background)
    #    4: Image type
    #             0: nifti format
    #             1: matlab format
    # Do some sanity checks

    if len(argv) < 4:
        printUsage(1)
        sys.exit()

    imagesFolder = argv[0]
    imagesFolderdst = argv[1]
    numClasses = int(argv[2])
    imageType = int(argv[3])

    if len(argv) == 5:
        print("+++++++")
        specLabel = int(argv[4])
        print(specLabel)
    imageNames = getImageImageList(imagesFolder)
    printFileNames = False

    for i_d in range(0, len(imageNames)):
        #print(imageNames[i_d])
        if imageType == 0:
            imageFileName = imagesFolder + '/' + imageNames[i_d]
            [imageData, img_proxy] = load_nii(imageFileName, printFileNames)
        else:
            imageFileName = imagesFolder + '/' + imageNames[i_d]
            imageData = load_matlab(imageFileName, printFileNames)

        labelsOrig = np.unique(imageData)
        imageData = np.squeeze(imageData)

        if (len(labelsOrig) != numClasses):
            print(
                " WARNING!!!!! Number of expected clases ({}) is different to found labels ({}) "
                .format(numClasses, len(labelsOrig)))

        # Correct labelsi
        if len(argv) == 4:

            labelCorrectedImage = np.zeros(imageData.shape, dtype=np.int16)
            for i_l in range(0, len(labelsOrig)):
                idx = np.where(imageData == labelsOrig[i_l])
                labelCorrectedImage[idx] = i_l

            idx = np.where(labelCorrectedImage >= numClasses)
            labelCorrectedImage[idx] = 0

        if len(argv) == 5:
            labelCorrectedImage = np.zeros(imageData.shape, dtype=np.int16)
            idx = np.where(imageData != specLabel)
            labelCorrectedImage[idx] = 0
            SpecIdx = np.where(imageData == specLabel)
            labelCorrectedImage[SpecIdx] = 1

        print(" ... Saving labels...")
        nameToSave = imagesFolderdst + '/' + imageNames[i_d]
        print(imageNames[i_d], nameToSave)
        if imageType == 0:  # nifti
            imageTypeToSave = np.dtype(np.int16)
            saveImageAsNifti(labelCorrectedImage, nameToSave, imageFileName,
                             imageTypeToSave)
        else:  # Matlab
            # Cast to int8 for saving purposes
            saveImageAsMatlab(labelCorrectedImage.astype('int8'), nameToSave)

    print(
        " ******************************************  PROCESSING LABELS DONE  ******************************************"
    )