예제 #1
0
def deepMedicTestMain(testConfigFilepath, absPathToSavedModelFromCmdLine):
    print("Given Test-Configuration File: ", testConfigFilepath)
    #Parse the config file in this naive fashion...
    testConfig = TestConfig()
    #configStruct = testConfig.configStruct
    exec(open(testConfigFilepath).read(), testConfig.configStruct)
    configGet = testConfig.get  #Main interface

    #Do checks.
    #checkIfMainTestConfigIsCorrect(testConfig, testConfigFilepath, absPathToSavedModelFromCmdLine) #Checks REQUIRED fields are complete.
    #checkIfFilesThatListFilesPerCaseAreCorrect(testConfig, testConfigFilepath) #Checks listing-files (whatever given).
    #checkIfOptionalParametersAreGivenCorrectly(testConfig, testConfigFilepath)

    #At this point it was checked that all parameters (that could be checked) and filepaths are correct, pointing to files/dirs and all files/dirs exist.

    #Create Folders and Logger
    mainOutputAbsFolder = getAbsPathEvenIfRelativeIsGiven(
        configGet(testConfig.FOLDER_FOR_OUTPUT), testConfigFilepath)
    sessionName = configGet(testConfig.SESSION_NAME) if configGet(
        testConfig.SESSION_NAME
    ) else TestSessionParameters.getDefaultSessionName()
    [folderForLogs, folderForPredictions, folderForFeatures
     ] = makeFoldersNeededForTestingSession(mainOutputAbsFolder, sessionName)
    loggerFileName = folderForLogs + "/" + sessionName + ".txt"
    sessionLogger = myLoggerModule.MyLogger(loggerFileName)

    sessionLogger.print3(
        "CONFIG: The configuration file for the testing session was loaded from: "
        + str(testConfigFilepath))

    #Load the CNN Model!
    sessionLogger.print3(
        "=========== Loading the CNN model for testing... ===============")
    #If CNN-Model was specified in command line, completely override the one in the config file.
    filepathToCnnModelToLoad = None
    if absPathToSavedModelFromCmdLine and configGet(
            testConfig.CNN_MODEL_FILEPATH):
        sessionLogger.print3(
            "WARN: A CNN-Model to use was specified both in the command line input and in the test-config-file! The input by the command line will be used: "
            + str(absPathToSavedModelFromCmdLine))
        filepathToCnnModelToLoad = absPathToSavedModelFromCmdLine
    elif absPathToSavedModelFromCmdLine:
        filepathToCnnModelToLoad = absPathToSavedModelFromCmdLine
    else:
        filepathToCnnModelToLoad = getAbsPathEvenIfRelativeIsGiven(
            configGet(testConfig.CNN_MODEL_FILEPATH), testConfigFilepath)
    sessionLogger.print3(
        "...Loading the network can take a few minutes if the model is big...")
    cnn3dInstance = load_object_from_gzip_file(filepathToCnnModelToLoad)
    sessionLogger.print3("The CNN model was loaded successfully from: " +
                         str(filepathToCnnModelToLoad))
    #Do final checks of the parameters. Check the ones that need check in comparison to the model's parameters! Such as: SAVE_PROBMAPS_PER_CLASS, INDICES_OF_FMS_TO_SAVE, Number of Channels!
    #testConfig.checkIfConfigIsCorrectForParticularCnnModel(cnn3dInstance)

    #Fill in the session's parameters.
    #[[case1-ch1, ..., caseN-ch1], [case1-ch2,...,caseN-ch2]]
    listOfAListPerChannelWithFilepathsOfAllCases = [
        parseAbsFileLinesInList(
            getAbsPathEvenIfRelativeIsGiven(channelConfPath,
                                            testConfigFilepath))
        for channelConfPath in configGet(testConfig.CHANNELS)
    ]
    #[[case1-ch1, case1-ch2], ..., [caseN-ch1, caseN-ch2]]
    listWithAListPerCaseWithFilepathPerChannel = [
        list(item)
        for item in zip(*tuple(listOfAListPerChannelWithFilepathsOfAllCases))
    ]
    gtLabelsFilepaths = parseAbsFileLinesInList(
        getAbsPathEvenIfRelativeIsGiven(configGet(testConfig.GT_LABELS),
                                        testConfigFilepath)) if configGet(
                                            testConfig.GT_LABELS) else None
    roiMasksFilepaths = parseAbsFileLinesInList(
        getAbsPathEvenIfRelativeIsGiven(configGet(testConfig.ROI_MASKS),
                                        testConfigFilepath)) if configGet(
                                            testConfig.ROI_MASKS) else None
    namesToSavePredsAndFeats = parseFileLinesInList(
        getAbsPathEvenIfRelativeIsGiven(
            configGet(testConfig.NAMES_FOR_PRED_PER_CASE),
            testConfigFilepath)) if configGet(
                testConfig.NAMES_FOR_PRED_PER_CASE
            ) else None  #CAREFUL: Here we use a different parsing function!

    testSessionParameters = TestSessionParameters(
                    sessionName = sessionName,
                    sessionLogger = sessionLogger,
                    mainOutputAbsFolder = mainOutputAbsFolder,
                    cnn3dInstance = cnn3dInstance,
                    cnnModelFilepath = filepathToCnnModelToLoad,

                    #Input:
                    listWithAListPerCaseWithFilepathPerChannel = listWithAListPerCaseWithFilepathPerChannel,
                    gtLabelsFilepaths = gtLabelsFilepaths,
                    roiMasksFilepaths = roiMasksFilepaths,

                    #Output
                    namesToSavePredictionsAndFeatures = namesToSavePredsAndFeats,
                    #predictions
                    saveSegmentation = configGet(testConfig.SAVE_SEGM),
                    saveProbMapsBoolPerClass = configGet(testConfig.SAVE_PROBMAPS_PER_CLASS),
                    folderForPredictions = folderForPredictions,

                    #features:
                    saveIndividualFmImages = configGet(testConfig.SAVE_INDIV_FMS),
                    saveMultidimensionalImageWithAllFms = configGet(testConfig.SAVE_4DIM_FMS),
                    indicesOfFmsToVisualisePerPathwayAndLayer = [configGet(testConfig.INDICES_OF_FMS_TO_SAVE_NORMAL)] +\
                                                                [configGet(testConfig.INDICES_OF_FMS_TO_SAVE_SUBSAMPLED)] +\
                                                                [configGet(testConfig.INDICES_OF_FMS_TO_SAVE_FC)
                                                                ],
                    folderForFeatures = folderForFeatures,

                    padInputImagesBool = configGet(testConfig.PAD_INPUT),
                    )

    testSessionParameters.sessionLogger.print3(
        "\n===========       NEW TESTING SESSION         ===============")
    testSessionParameters.printParametersOfThisSession()

    testSessionParameters.sessionLogger.print3(
        "\n=======================================================")
    testSessionParameters.sessionLogger.print3(
        "=========== Compiling the Testing Function ============")
    testSessionParameters.sessionLogger.print3(
        "=======================================================")
    cnn3dInstance.compileTestAndVisualisationFunction(
        *testSessionParameters.getTupleForCompilationOfTestFunc())

    testSessionParameters.sessionLogger.print3(
        "\n======================================================")
    testSessionParameters.sessionLogger.print3(
        "=========== Testing with the CNN model ===============")
    testSessionParameters.sessionLogger.print3(
        "======================================================")
    performInferenceForTestingOnWholeVolumes(
        *testSessionParameters.getTupleForCnnTesting())
    testSessionParameters.sessionLogger.print3(
        "\n======================================================")
    testSessionParameters.sessionLogger.print3(
        "=========== Testing session finished =================")
    testSessionParameters.sessionLogger.print3(
        "======================================================")
예제 #2
0
def deepMedicTestMain(testConfigFilepath, absPathToSavedModelFromCmdLine) :


	print "Given Test-Configuration File: ", testConfigFilepath
	#Parse the config file in this naive fashion...
	testConfig = TestConfig()
	#configStruct = testConfig.configStruct
	execfile(testConfigFilepath, testConfig.configStruct)
	configGet = testConfig.get #Main interface
	
	#Do checks.
	#checkIfMainTestConfigIsCorrect(testConfig, testConfigFilepath, absPathToSavedModelFromCmdLine) #Checks REQUIRED fields are complete.
	#checkIfFilesThatListFilesPerCaseAreCorrect(testConfig, testConfigFilepath) #Checks listing-files (whatever given).
	#checkIfOptionalParametersAreGivenCorrectly(testConfig, testConfigFilepath)

	#At this point it was checked that all parameters (that could be checked) and filepaths are correct, pointing to files/dirs and all files/dirs exist.

	
	#Create Folders and Logger
	mainOutputAbsFolder = getAbsPathEvenIfRelativeIsGiven(configGet(testConfig.FOLDER_FOR_OUTPUT), testConfigFilepath)
	sessionName = configGet(testConfig.SESSION_NAME) if configGet(testConfig.SESSION_NAME) else TestSessionParameters.getDefaultSessionName()
	[folderForLogs,
	folderForPredictions,
	folderForFeatures] = makeFoldersNeededForTestingSession(mainOutputAbsFolder, sessionName)
	loggerFileName = folderForLogs + "/" + sessionName + ".txt"
	sessionLogger = myLoggerModule.MyLogger(loggerFileName)

	sessionLogger.print3("CONFIG: The configuration file for the testing session was loaded from: " + str(testConfigFilepath))

	#Load the CNN Model!
	sessionLogger.print3("=========== Loading the CNN model for testing... ===============")
	#If CNN-Model was specified in command line, completely override the one in the config file.
	filepathToCnnModelToLoad = None
	if absPathToSavedModelFromCmdLine and configGet(testConfig.CNN_MODEL_FILEPATH) :
		sessionLogger.print3("WARN: A CNN-Model to use was specified both in the command line input and in the test-config-file! The input by the command line will be used: " + str(absPathToSavedModelFromCmdLine) )
		filepathToCnnModelToLoad = absPathToSavedModelFromCmdLine
	elif absPathToSavedModelFromCmdLine :
		filepathToCnnModelToLoad = absPathToSavedModelFromCmdLine
	else :
		filepathToCnnModelToLoad = getAbsPathEvenIfRelativeIsGiven(configGet(testConfig.CNN_MODEL_FILEPATH), testConfigFilepath)
	sessionLogger.print3("...Loading the network can take a few minutes if the model is big...")
	cnn3dInstance = load_object_from_gzip_file(filepathToCnnModelToLoad)
	sessionLogger.print3("The CNN model was loaded successfully from: " + str(filepathToCnnModelToLoad))
	#Do final checks of the parameters. Check the ones that need check in comparison to the model's parameters! Such as: SAVE_PROBMAPS_PER_CLASS, INDICES_OF_FMS_TO_SAVE, Number of Channels!
	#testConfig.checkIfConfigIsCorrectForParticularCnnModel(cnn3dInstance)


	#Fill in the session's parameters.
	#[[case1-ch1, ..., caseN-ch1], [case1-ch2,...,caseN-ch2]]
	listOfAListPerChannelWithFilepathsOfAllCases = [parseAbsFileLinesInList(getAbsPathEvenIfRelativeIsGiven(channelConfPath, testConfigFilepath)) for channelConfPath in configGet(testConfig.CHANNELS)]
	#[[case1-ch1, case1-ch2], ..., [caseN-ch1, caseN-ch2]]
	listWithAListPerCaseWithFilepathPerChannel = [ list(item) for item in zip(*tuple(listOfAListPerChannelWithFilepathsOfAllCases)) ]
	gtLabelsFilepaths = parseAbsFileLinesInList( getAbsPathEvenIfRelativeIsGiven(configGet(testConfig.GT_LABELS), testConfigFilepath) ) if configGet(testConfig.GT_LABELS) else None
	roiMasksFilepaths = parseAbsFileLinesInList( getAbsPathEvenIfRelativeIsGiven(configGet(testConfig.ROI_MASKS), testConfigFilepath) ) if configGet(testConfig.ROI_MASKS) else None
	namesToSavePredsAndFeats = parseFileLinesInList( getAbsPathEvenIfRelativeIsGiven(configGet(testConfig.NAMES_FOR_PRED_PER_CASE), testConfigFilepath) ) if configGet(testConfig.NAMES_FOR_PRED_PER_CASE) else None #CAREFUL: Here we use a different parsing function!

	testSessionParameters = TestSessionParameters(
			sessionName = sessionName,
			sessionLogger = sessionLogger,
			mainOutputAbsFolder = mainOutputAbsFolder,
			cnn3dInstance = cnn3dInstance,
			cnnModelFilepath = filepathToCnnModelToLoad,


			#Input:
			listWithAListPerCaseWithFilepathPerChannel = listWithAListPerCaseWithFilepathPerChannel,
			gtLabelsFilepaths = gtLabelsFilepaths,
			roiMasksFilepaths = roiMasksFilepaths,

			#Output				

			
			namesToSavePredictionsAndFeatures = namesToSavePredsAndFeats,
			#predictions
			saveSegmentation = configGet(testConfig.SAVE_SEGM),
			saveProbMapsBoolPerClass = configGet(testConfig.SAVE_PROBMAPS_PER_CLASS),
			folderForPredictions = folderForPredictions,

			#features:
			saveIndividualFmImages = configGet(testConfig.SAVE_INDIV_FMS),
			saveMultidimensionalImageWithAllFms = configGet(testConfig.SAVE_4DIM_FMS),
			indicesOfFmsToVisualisePerPathwayAndLayer = [	configGet(testConfig.INDICES_OF_FMS_TO_SAVE_NORMAL),
									configGet(testConfig.INDICES_OF_FMS_TO_SAVE_SUBSAMPLED),
									configGet(testConfig.INDICES_OF_FMS_TO_SAVE_FC)
									],
			folderForFeatures = folderForFeatures,

			padInputImagesBool = configGet(testConfig.PAD_INPUT),

			)

	testSessionParameters.sessionLogger.print3("===========       NEW TESTING SESSION         ===============")
	testSessionParameters.printParametersOfThisSession()

	testSessionParameters.sessionLogger.print3("======================================================")
	testSessionParameters.sessionLogger.print3("=========== Testing with the CNN model ===============")
	testSessionParameters.sessionLogger.print3("======================================================")
	performInferenceForTestingOnWholeVolumes(*testSessionParameters.getTupleForCnnTesting())
	testSessionParameters.sessionLogger.print3("======================================================")
	testSessionParameters.sessionLogger.print3("=========== Testing session finished =================")
	testSessionParameters.sessionLogger.print3("======================================================")