Exemplo n.º 1
0
def deepMedicNewModelMain(modelConfigFilepath):
    print "Given Model-Configuration File: ", modelConfigFilepath
    #Parse the config file in this naive fashion...
    modelConfig = ModelConfig()
    execfile(modelConfigFilepath, modelConfig.configStruct)
    configGet = modelConfig.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(modelConfig.FOLDER_FOR_OUTPUT), modelConfigFilepath)
    modelName = configGet(modelConfig.MODEL_NAME) if configGet(
        modelConfig.MODEL_NAME
    ) else CreateModelSessionParameters.getDefaultModelName()
    [folderForCnnModels, folderForLogs
     ] = makeFoldersNeededForCreateModelSession(mainOutputAbsFolder, modelName)
    loggerFileName = folderForLogs + "/" + modelName + ".txt"
    sessionLogger = myLoggerModule.MyLogger(loggerFileName)

    sessionLogger.print3(
        "CONFIG: The configuration file for the model-creation session was loaded from: "
        + str(modelConfigFilepath))

    #Fill in the session's parameters.
    createModelSessionParameters = CreateModelSessionParameters(
        cnnModelName=modelName,
        sessionLogger=sessionLogger,
        mainOutputAbsFolder=mainOutputAbsFolder,
        folderForSessionCnnModels=folderForCnnModels,
        #===MODEL PARAMETERS===
        numberClasses=configGet(modelConfig.NUMB_CLASSES),
        numberOfInputChannelsNormal=configGet(
            modelConfig.NUMB_INPUT_CHANNELS_NORMAL),
        #===Normal pathway===
        numFMsNormal=configGet(modelConfig.N_FMS_NORM),
        kernDimNormal=configGet(modelConfig.KERN_DIM_NORM),
        residConnAtLayersNormal=configGet(ModelConfig.RESID_CONN_LAYERS_NORM),
        lowerRankLayersNormal=configGet(ModelConfig.LOWER_RANK_LAYERS_NORM),
        #==Subsampled pathway==
        useSubsampledBool=configGet(modelConfig.USE_SUBSAMPLED),
        numFMsSubsampled=configGet(modelConfig.N_FMS_SUBS),
        kernDimSubsampled=configGet(modelConfig.KERN_DIM_SUBS),
        subsampleFactor=configGet(modelConfig.SUBS_FACTOR),
        residConnAtLayersSubsampled=configGet(
            ModelConfig.RESID_CONN_LAYERS_SUBS),
        lowerRankLayersSubsampled=configGet(
            ModelConfig.LOWER_RANK_LAYERS_SUBS),
        #==FC Layers====
        numFMsFc=configGet(modelConfig.N_FMS_FC),
        kernelDimensionsFirstFcLayer=configGet(modelConfig.KERN_DIM_1ST_FC),
        residConnAtLayersFc=configGet(ModelConfig.RESID_CONN_LAYERS_FC),
        #==Size of Image Segments ==
        segmDimTrain=configGet(modelConfig.SEG_DIM_TRAIN),
        segmDimVal=configGet(modelConfig.SEG_DIM_VAL),
        segmDimInfer=configGet(modelConfig.SEG_DIM_INFERENCE),
        #== Batch Sizes ==
        batchSizeTrain=configGet(modelConfig.BATCH_SIZE_TR),
        batchSizeVal=configGet(modelConfig.BATCH_SIZE_VAL),
        batchSizeInfer=configGet(modelConfig.BATCH_SIZE_INFER),
        #===Other Architectural Parameters ===
        activationFunction=configGet(modelConfig.ACTIV_FUNCTION),
        #==Dropout Rates==
        dropNormal=configGet(modelConfig.DROP_R_NORM),
        dropSubsampled=configGet(modelConfig.DROP_R_SUBS),
        dropFc=configGet(modelConfig.DROP_R_FC),
        #==Regularization==
        l1Reg=configGet(modelConfig.L1_REG),
        l2Reg=configGet(modelConfig.L2_REG),
        #== Weight Initialization==
        initialMethod=configGet(modelConfig.INITIAL_METHOD),
        #== Batch Normalization ==
        bnRollingAverOverThatManyBatches=configGet(
            modelConfig.BN_ROLL_AV_BATCHES),
        #====Optimization=====
        learningRate=configGet(modelConfig.LRATE),
        optimizerSgd0Adam1Rms2=configGet(modelConfig.OPTIMIZER),
        classicMom0Nesterov1=configGet(modelConfig.MOM_TYPE),
        momentumValue=configGet(modelConfig.MOM),
        momNonNormalized0Normalized1=configGet(modelConfig.MOM_NORM_NONNORM),
        #Adam
        b1Adam=configGet(modelConfig.B1_ADAM),
        b2Adam=configGet(modelConfig.B2_ADAM),
        eAdam=configGet(modelConfig.EPS_ADAM),
        #Rms
        rhoRms=configGet(modelConfig.RHO_RMS),
        eRms=configGet(modelConfig.EPS_RMS))

    createModelSessionParameters.sessionLogger.print3(
        "===========    NEW CREATE-MODEL SESSION    ============")
    createModelSessionParameters.printParametersOfThisSession()

    createModelSessionParameters.sessionLogger.print3(
        "=========== Creating the CNN model ===============")
    cnn3dInstance = Cnn3d()
    cnn3dInstance.make_cnn_model(
        *createModelSessionParameters.getTupleForCnnCreation())

    cnn3dInstance.initializeTrainingState(
        *createModelSessionParameters.getTupleForInitializingTrainingState())
    cnn3dInstance.compileTrainFunction(
        *createModelSessionParameters.getTupleForCompilationOfTrainFunc())
    cnn3dInstance.compileValidationFunction(
        *createModelSessionParameters.getTupleForCompilationOfValFunc())
    cnn3dInstance.compileTestAndVisualisationFunction(
        *createModelSessionParameters.getTupleForCompilationOfTestFunc())

    filenameAndPathToSaveModel = createModelSessionParameters.getPathAndFilenameToSaveModel(
    ) + ".initial." + datetimeNowAsStr()
    filenameAndPathWhereModelWasSaved = dump_cnn_to_gzip_file_dotSave(
        cnn3dInstance, filenameAndPathToSaveModel, sessionLogger)

    createModelSessionParameters.sessionLogger.print3(
        "=========== Creation of the model: \"" +
        str(createModelSessionParameters.cnnModelName) +
        "\" finished =================")
    return (cnn3dInstance, filenameAndPathWhereModelWasSaved)
Exemplo n.º 2
0
def deepMedicNewModelMain(modelConfigFilepath,
                          absPathToPreTrainedModelGivenInCmdLine,
                          listOfLayersToTransfer):
    print("Given Model-Configuration File: ", modelConfigFilepath)
    #Parse the config file in this naive fashion...
    modelConfig = ModelConfig()
    exec(open(modelConfigFilepath).read(), modelConfig.configStruct)
    configGet = modelConfig.get  #Main interface

    #Create Folders and Logger
    mainOutputAbsFolder = getAbsPathEvenIfRelativeIsGiven(
        configGet(modelConfig.FOLDER_FOR_OUTPUT), modelConfigFilepath)
    modelName = configGet(modelConfig.MODEL_NAME) if configGet(
        modelConfig.MODEL_NAME
    ) else CreateModelSessionParameters.getDefaultModelName()
    [folderForCnnModels, folderForLogs
     ] = makeFoldersNeededForCreateModelSession(mainOutputAbsFolder, modelName)
    loggerFileName = folderForLogs + "/" + modelName + ".txt"
    sessionLogger = myLoggerModule.MyLogger(loggerFileName)

    sessionLogger.print3(
        "CONFIG: The configuration file for the model-creation session was loaded from: "
        + str(modelConfigFilepath))

    #Fill in the session's parameters.
    createModelSessionParameters = CreateModelSessionParameters(
        cnnModelName=modelName,
        sessionLogger=sessionLogger,
        mainOutputAbsFolder=mainOutputAbsFolder,
        folderForSessionCnnModels=folderForCnnModels,
        #===MODEL PARAMETERS===
        numberClasses=configGet(modelConfig.NUMB_CLASSES),
        numberOfInputChannelsNormal=configGet(
            modelConfig.NUMB_INPUT_CHANNELS_NORMAL),
        #===Normal pathway===
        numFMsNormal=configGet(modelConfig.N_FMS_NORM),
        kernDimNormal=configGet(modelConfig.KERN_DIM_NORM),
        residConnAtLayersNormal=configGet(ModelConfig.RESID_CONN_LAYERS_NORM),
        lowerRankLayersNormal=configGet(ModelConfig.LOWER_RANK_LAYERS_NORM),
        #==Subsampled pathway==
        useSubsampledBool=configGet(modelConfig.USE_SUBSAMPLED),
        numFMsSubsampled=configGet(modelConfig.N_FMS_SUBS),
        kernDimSubsampled=configGet(modelConfig.KERN_DIM_SUBS),
        subsampleFactor=configGet(modelConfig.SUBS_FACTOR),
        residConnAtLayersSubsampled=configGet(
            ModelConfig.RESID_CONN_LAYERS_SUBS),
        lowerRankLayersSubsampled=configGet(
            ModelConfig.LOWER_RANK_LAYERS_SUBS),
        #==FC Layers====
        numFMsFc=configGet(modelConfig.N_FMS_FC),
        kernelDimensionsFirstFcLayer=configGet(modelConfig.KERN_DIM_1ST_FC),
        residConnAtLayersFc=configGet(ModelConfig.RESID_CONN_LAYERS_FC),
        #==Size of Image Segments ==
        segmDimTrain=configGet(modelConfig.SEG_DIM_TRAIN),
        segmDimVal=configGet(modelConfig.SEG_DIM_VAL),
        segmDimInfer=configGet(modelConfig.SEG_DIM_INFERENCE),
        #== Batch Sizes ==
        batchSizeTrain=configGet(modelConfig.BATCH_SIZE_TR),
        batchSizeVal=configGet(modelConfig.BATCH_SIZE_VAL),
        batchSizeInfer=configGet(modelConfig.BATCH_SIZE_INFER),
        #===Other Architectural Parameters ===
        activationFunction=configGet(modelConfig.ACTIV_FUNCTION),
        #==Dropout Rates==
        dropNormal=configGet(modelConfig.DROP_R_NORM),
        dropSubsampled=configGet(modelConfig.DROP_R_SUBS),
        dropFc=configGet(modelConfig.DROP_R_FC),
        #== Weight Initialization==
        initialMethod=configGet(modelConfig.INITIAL_METHOD),
        #== Batch Normalization ==
        bnRollingAverOverThatManyBatches=configGet(
            modelConfig.BN_ROLL_AV_BATCHES),
    )

    createModelSessionParameters.sessionLogger.print3(
        "\n===========    NEW CREATE-MODEL SESSION    ============")
    createModelSessionParameters.printParametersOfThisSession()

    createModelSessionParameters.sessionLogger.print3(
        "\n=========== Creating the CNN model ===============")
    cnn3dInstance = Cnn3d()
    cnn3dInstance.make_cnn_model(
        *createModelSessionParameters.getTupleForCnnCreation())

    if absPathToPreTrainedModelGivenInCmdLine != None:  # Transfer parameters from a previously trained model to the new one.
        createModelSessionParameters.sessionLogger.print3(
            "\n=========== Pre-training the new model ===============")
        sessionLogger.print3(
            "...Loading the pre-trained network. This can take a few minutes if the model is big..."
        )
        cnnPretrainedInstance = load_object_from_gzip_file(
            absPathToPreTrainedModelGivenInCmdLine)
        sessionLogger.print3(
            "The pre-trained model was loaded successfully from: " +
            str(absPathToPreTrainedModelGivenInCmdLine))
        from deepmedic import cnnTransferParameters
        cnn3dInstance = cnnTransferParameters.transferParametersBetweenModels(
            sessionLogger, cnn3dInstance, cnnPretrainedInstance,
            listOfLayersToTransfer)

    createModelSessionParameters.sessionLogger.print3(
        "\n=========== Saving the model ===============")
    if absPathToPreTrainedModelGivenInCmdLine != None:
        filenameAndPathToSaveModel = createModelSessionParameters.getPathAndFilenameToSaveModel(
        ) + ".initial.pretrained." + datetimeNowAsStr()
    else:
        filenameAndPathToSaveModel = createModelSessionParameters.getPathAndFilenameToSaveModel(
        ) + ".initial." + datetimeNowAsStr()
    filenameAndPathWhereModelWasSaved = dump_cnn_to_gzip_file_dotSave(
        cnn3dInstance, filenameAndPathToSaveModel, sessionLogger)
    createModelSessionParameters.sessionLogger.print3(
        "=========== Creation of the model: \"" +
        str(createModelSessionParameters.cnnModelName) +
        "\" finished =================")

    return (cnn3dInstance, filenameAndPathWhereModelWasSaved)