Skip to content

Commit

Permalink
Replaced whole deepmedic source from Theano to Tensorflow version.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamnitsask committed Jun 17, 2018
1 parent 5388eda commit 70cf20f
Show file tree
Hide file tree
Showing 38 changed files with 2,820 additions and 3,395 deletions.
222 changes: 105 additions & 117 deletions deepMedicRun

Large diffs are not rendered by default.

326 changes: 150 additions & 176 deletions deepmedic/dataManagement/sampling.py

Large diffs are not rendered by default.

29 changes: 14 additions & 15 deletions deepmedic/dataManagement/samplingType.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
# or read the terms at https://opensource.org/licenses/BSD-3-Clause.

from __future__ import absolute_import, print_function, division
from six.moves import xrange
import numpy as np

class SamplingType(object) :
def __init__(self, myLogger, samplingType, numberOfClassesInclBackgr):
self.logger = myLogger
def __init__(self, log, samplingType, numberOfClassesInclBackgr):
self.log = log
# foreBackgr=0 , uniform=1, fullImage=2, targettedPerClass=3
self.samplingType = samplingType

Expand All @@ -26,13 +25,13 @@ def __init__(self, myLogger, samplingType, numberOfClassesInclBackgr):
self.arrayWithStringPerCategoryToSample = ["WholeImage"]
elif self.samplingType == 3 :
self.stringOfSamplingType = "Per-Class"
self.arrayWithStringPerCategoryToSample = ["Class-" + str(i) for i in xrange(numberOfClassesInclBackgr) ]
self.arrayWithStringPerCategoryToSample = ["Class-" + str(i) for i in range(numberOfClassesInclBackgr) ]
else :
self.logger.print3("ERROR: Tried to create a SamplingType instance, but the samplingType passed was invalid. Should be [0,1,2,3]. Exiting!"); exit(1)
self.log.print3("ERROR: Tried to create a SamplingType instance, but the samplingType passed was invalid. Should be [0,1,2,3]. Exiting!"); exit(1)

def setPercentOfSamplesPerCategoryToSample(self, percentageOfSamplesPerCategoryOfSampling) :
if self.samplingType in [0,3] and len(percentageOfSamplesPerCategoryOfSampling) != self.getNumberOfCategoriesToSample() :
self.logger.print3("ERROR: In class [SamplingType], the list percentageOfSamplesPerCategoryOfSampling had [" + str(len(percentageOfSamplesPerCategoryOfSampling)) + "] elements. In this case of [" + self.stringOfSamplingType + "], it should have [" + str(self.getNumberOfCategoriesToSample()) + "]! Exiting!"); exit(1)
self.log.print3("ERROR: In class [SamplingType], the list percentageOfSamplesPerCategoryOfSampling had [" + str(len(percentageOfSamplesPerCategoryOfSampling)) + "] elements. In this case of [" + self.stringOfSamplingType + "], it should have [" + str(self.getNumberOfCategoriesToSample()) + "]! Exiting!"); exit(1)

if self.samplingType == 0 :
self.percentOfSamplesPerCategoryToSample = self.normalizePercentages(percentageOfSamplesPerCategoryOfSampling)
Expand All @@ -43,7 +42,7 @@ def setPercentOfSamplesPerCategoryToSample(self, percentageOfSamplesPerCategoryO
elif self.samplingType == 3 :
self.percentOfSamplesPerCategoryToSample = self.normalizePercentages(percentageOfSamplesPerCategoryOfSampling)
else :
self.logger.print3("ERROR: in [SamplingType]. self.samplingType was invalid. Should be [0,1,2,3]. Exiting!"); exit(1)
self.log.print3("ERROR: in [SamplingType]. self.samplingType was invalid. Should be [0,1,2,3]. Exiting!"); exit(1)

def normalizePercentages(self, listOfWeights) :
arrayOfWeights = np.asarray(listOfWeights, dtype="float32")
Expand Down Expand Up @@ -84,10 +83,10 @@ def logicDecidingAndGivingFinalSamplingMapsForEachCategory( self,
if providedWeightMapsToSampleForEachCategory : #Both weight maps should be provided currently.
numOfProvidedWeightMaps = len(arrayWithWeightMapsWhereToSampleForEachCategory)
if numOfProvidedWeightMaps != self.getNumberOfCategoriesToSample() :
self.logger.print3("ERROR: For SamplingType = Fore/Background(0), [" + str(numOfProvidedWeightMaps) + "] weight maps were provided! Two (fore/back) were expected! Exiting!"); exit(1)
self.log.print3("ERROR: For SamplingType = Fore/Background(0), [" + str(numOfProvidedWeightMaps) + "] weight maps were provided! Two (fore/back) were expected! Exiting!"); exit(1)
finalWeightMapsToSampleFromPerCategoryForSubject = arrayWithWeightMapsWhereToSampleForEachCategory
elif not providedGtLabelsBool:
self.logger.print3("ERROR: For SamplingType=[" + self.stringOfSamplingType + "], if weighted-maps are not provided, at least Ground Truth labels should be given to extract foreground! Exiting!"); exit(1)
self.log.print3("ERROR: For SamplingType=[" + self.stringOfSamplingType + "], if weighted-maps are not provided, at least Ground Truth labels should be given to extract foreground! Exiting!"); exit(1)
elif providedRoiMaskBool : # and providedGtLabelsBool
maskForForegroundSampling = (gtLabelsImage>0).astype(int)
maskForBackgroundSampling_roiMinusGtLabels = (roiMask>0) * (maskForForegroundSampling==0)
Expand All @@ -100,7 +99,7 @@ def logicDecidingAndGivingFinalSamplingMapsForEachCategory( self,
if providedWeightMapsToSampleForEachCategory :
numOfProvidedWeightMaps = len(arrayWithWeightMapsWhereToSampleForEachCategory)
if numOfProvidedWeightMaps != 1 :
self.logger.print3("ERROR: For SamplingType=[" + self.stringOfSamplingType + "], [" + str(numOfProvidedWeightMaps) + "] weight maps were provided! One was expected! Exiting!"); exit(1)
self.log.print3("ERROR: For SamplingType=[" + self.stringOfSamplingType + "], [" + str(numOfProvidedWeightMaps) + "] weight maps were provided! One was expected! Exiting!"); exit(1)
finalWeightMapsToSampleFromPerCategoryForSubject = arrayWithWeightMapsWhereToSampleForEachCategory #Should be an array with dim1==1 already.
elif providedRoiMaskBool :
finalWeightMapsToSampleFromPerCategoryForSubject = [ roiMask ] #Be careful to not change either of the two arrays later or there'll be a problem.
Expand All @@ -110,7 +109,7 @@ def logicDecidingAndGivingFinalSamplingMapsForEachCategory( self,
if providedWeightMapsToSampleForEachCategory :
numOfProvidedWeightMaps = len(arrayWithWeightMapsWhereToSampleForEachCategory)
if numOfProvidedWeightMaps != 1 :
self.logger.print3("ERROR: For SamplingType=[" + self.stringOfSamplingType + "], [" + str(numOfProvidedWeightMaps) + "] weight maps were provided! One was expected! Exiting!"); exit(1)
self.log.print3("ERROR: For SamplingType=[" + self.stringOfSamplingType + "], [" + str(numOfProvidedWeightMaps) + "] weight maps were provided! One was expected! Exiting!"); exit(1)
finalWeightMapsToSampleFromPerCategoryForSubject = arrayWithWeightMapsWhereToSampleForEachCategory #Should be an array with dim1==1 already.
elif providedRoiMaskBool :
finalWeightMapsToSampleFromPerCategoryForSubject = [ roiMask ] #Be careful to not change either of the two arrays later or there'll be a problem.
Expand All @@ -120,16 +119,16 @@ def logicDecidingAndGivingFinalSamplingMapsForEachCategory( self,
if providedWeightMapsToSampleForEachCategory :
numOfProvidedWeightMaps = len(arrayWithWeightMapsWhereToSampleForEachCategory)
if numOfProvidedWeightMaps != self.getNumberOfCategoriesToSample() :
self.logger.print3("ERROR: For SamplingType=[" + self.stringOfSamplingType + "], [" + str(numOfProvidedWeightMaps) + "] weight maps were provided! As many as the classes [" + str(self.getNumberOfCategoriesToSample()) + "] (incl Background) were expected! Exiting!"); exit(1)
self.log.print3("ERROR: For SamplingType=[" + self.stringOfSamplingType + "], [" + str(numOfProvidedWeightMaps) + "] weight maps were provided! As many as the classes [" + str(self.getNumberOfCategoriesToSample()) + "] (incl Background) were expected! Exiting!"); exit(1)
finalWeightMapsToSampleFromPerCategoryForSubject = arrayWithWeightMapsWhereToSampleForEachCategory #Should have as many entries as classes (incl backgr).
elif providedGtLabelsBool :
finalWeightMapsToSampleFromPerCategoryForSubject = []
for cat_i in xrange( self.getNumberOfCategoriesToSample() ) : # Should be the same number as the number of actual classes, including background.
for cat_i in range( self.getNumberOfCategoriesToSample() ) : # Should be the same number as the number of actual classes, including background.
finalWeightMapsToSampleFromPerCategoryForSubject.append( (gtLabelsImage == cat_i).astype(int) )
else :
self.logger.print3("ERROR: For SamplingType=TargettedPerClass(3), either weightMaps for each class or GT labels should be given! Exiting!"); exit(1)
self.log.print3("ERROR: For SamplingType=TargettedPerClass(3), either weightMaps for each class or GT labels should be given! Exiting!"); exit(1)
else :
self.logger.print3("ERROR: Sampling-type-number passed in [logicDecidingAndGivingFinalSamplingMapsForEachCategory] was invalid. Should be [0,1,2,3]. Exiting!"); exit(1)
self.log.print3("ERROR: Sampling-type-number passed in [logicDecidingAndGivingFinalSamplingMapsForEachCategory] was invalid. Should be [0,1,2,3]. Exiting!"); exit(1)

return finalWeightMapsToSampleFromPerCategoryForSubject

File renamed without changes.
35 changes: 35 additions & 0 deletions deepmedic/frontEnd/configParsing/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright (c) 2016, Konstantinos Kamnitsas
# All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the BSD license. See the accompanying LICENSE file
# or read the terms at https://opensource.org/licenses/BSD-3-Clause.

from __future__ import absolute_import, print_function, division
import os

class Config(object):

def __init__(self, abs_path_to_cfg):
self._configStruct = {}
self._abs_path_to_cfg = abs_path_to_cfg # for printing later.
print("Given configuration file: ", self._abs_path_to_cfg)
exec(open(self._abs_path_to_cfg).read(), self._configStruct)
self._check_for_deprecated_cfg()

def __getitem__(self, key): # overriding the [] operator.
return self.get(key)

def get(self, string1) :
return self._configStruct[string1] if string1 in self._configStruct else None

def get_abs_path_to_cfg(self):
return self._abs_path_to_cfg

def _check_for_deprecated_cfg(self):
pass

def override_file_cfg_with_cmd_line_cfg(self, log, args):
pass


85 changes: 85 additions & 0 deletions deepmedic/frontEnd/configParsing/modelConfig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Copyright (c) 2016, Konstantinos Kamnitsas
# All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the BSD license. See the accompanying LICENSE file
# or read the terms at https://opensource.org/licenses/BSD-3-Clause.

from __future__ import absolute_import, print_function, division
import os

from deepmedic.frontEnd.configParsing.utils import *
from deepmedic.frontEnd.configParsing.config import Config


class ModelConfig(Config):

#Optional but highly suggested.
MODEL_NAME = "modelName"
#[REQUIRED] Output:
FOLDER_OUTP = "folderForOutput" #MUST BE GIVEN

#================ MODEL PARAMETERS =================
NUM_CLASSES = "numberOfOutputClasses"
NUM_INPUT_CHANS = "numberOfInputChannels"

#===Normal pathway===
N_FMS_NORM = "numberFMsPerLayerNormal"
KERN_DIM_NORM = "kernelDimPerLayerNormal"
RESID_CONN_LAYERS_NORM = "layersWithResidualConnNormal"
LOWER_RANK_LAYERS_NORM = "lowerRankLayersNormal"

#==Subsampled pathway==
USE_SUBSAMPLED = "useSubsampledPathway"
#The below should be mirroring the pathway, otherwise let them specify them but throw warning all around that receptive field should stay the same!
N_FMS_SUBS = "numberFMsPerLayerSubsampled"
KERN_DIM_SUBS = "kernelDimPerLayerSubsampled"
SUBS_FACTOR = "subsampleFactor"
RESID_CONN_LAYERS_SUBS = "layersWithResidualConnSubsampled"
LOWER_RANK_LAYERS_SUBS = "lowerRankLayersSubsampled"

#==Extra hidden FC Layers. Final Classification layer is not included in here.
N_FMS_FC = "numberFMsPerLayerFC"
KERN_DIM_1ST_FC = "kernelDimFor1stFcLayer"
RESID_CONN_LAYERS_FC = "layersWithResidualConnFC"

#Size of Image Segments
SEG_DIM_TRAIN = "segmentsDimTrain"
SEG_DIM_VAL = "segmentsDimVal"
SEG_DIM_INFER = "segmentsDimInference"

#==Batch Sizes===
#Required.
BATCH_SIZE_TR = "batchSizeTrain"
BATCH_SIZE_VAL = "batchSizeVal"
BATCH_SIZE_INFER = "batchSizeInfer"

#Dropout Rates:
DROP_NORM = "dropoutRatesNormal"
DROP_SUBS = "dropoutRatesSubsampled"
DROP_FC = "dropoutRatesFc"

#Initialization method of the kernel weights.
CONV_W_INIT = "convWeightsInit"
#Activation Function for all convolutional layers:
ACTIV_FUNC = "activationFunction"

#Batch Normalization
BN_ROLL_AV_BATCHES = "rollAverageForBNOverThatManyBatches"


def __init__(self, abs_path_to_cfg):
Config.__init__(self, abs_path_to_cfg)

# Called from parent constructor
def _check_for_deprecated_cfg(self):
msg_part1 = "ERROR: Deprecated input to the config: ["
msg_part2 = "]. Please update config and use the new corresponding variable "
msg_part3 = "]. Exiting."
if self.get("initializeClassic0orDelving1") is not None:
print(msg_part1 + "initializeClassic0orDelving1" + msg_part2 + "convWeightsInit" + msg_part3); exit(1)
if self.get("relu0orPrelu1") is not None:
print(msg_part1 + "relu0orPrelu1" + msg_part2 + "activationFunction" + msg_part3); exit(1)



0 comments on commit 70cf20f

Please sign in to comment.