Skip to content

Commit

Permalink
"config dict ready"
Browse files Browse the repository at this point in the history
  • Loading branch information
chezhia committed Oct 16, 2019
1 parent 6510d39 commit 3fd3e92
Show file tree
Hide file tree
Showing 20 changed files with 1,116 additions and 335 deletions.
70 changes: 70 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${workspaceRoot}/launcher.py",
"console": "integratedTerminal"
},
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
]
},
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "enter-your-module-name-here",
"console": "integratedTerminal"
},
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"console": "integratedTerminal",
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"django": true
},
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "app.py"
},
"args": [
"run",
"--no-debugger",
"--no-reload"
],
"jinja": true
},
{
"name": "Python: Current File (External Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "externalTerminal"
}
]
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.pythonPath": "C:\\Users\\somd7w\\Desktop\\DL_Projects\\tensorflow\\python.exe"
}
2 changes: 1 addition & 1 deletion T2_Masks/train_3DUnet_isense.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def main(overwrite=False):
base_model = load_old_model(config["model_file"])
model = get_multiGPUmodel(base_model=base_model,initial_learning_rate=config["initial_learning_rate"],GPU=config["GPU"])
else:
base_model,model = isensee2017_model_multiGPU(input_shape=config["input_shape"],
base_model, model = isensee2017_model_multiGPU(input_shape=config["input_shape"],
n_labels=config["n_labels"],
initial_learning_rate=config["initial_learning_rate"],
n_base_filters=config["n_base_filters"],GPU=config["GPU"])
Expand Down
2 changes: 1 addition & 1 deletion T2_Masks/train_isensee2017.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def main(overwrite=False):

with open('isensemodel_original.txt','w') as fh:
# Pass the file handle in as a lambda function to make it callable
model.summary(line_length=150,print_fn=lambda x: fh.write(x + '\n'))
model.summary(line_length=150, print_fn=lambda x: fh.write(x + '\n'))

# Save Model
plot_model(model,to_file="isensemodel_original.png",show_shapes=True)
Expand Down
Empty file added dummy.py
Empty file.
7 changes: 3 additions & 4 deletions launcher.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from src.run_preprocess import *

file = '@preproc.args'
main(file)
from src.run_training import *

file = '@training.args'
main(file)
Binary file added src/__pycache__/run_training.cpython-36.pyc
Binary file not shown.
Binary file not shown.
3 changes: 1 addition & 2 deletions src/pyimagesearch/callbacks/datagenerator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import keras
from keras.utils import np_utils
from pyimagesearch.utils.generator_utils_Elan import convert_data, add_data_classify,add_data_segment_2D,add_data_segment
from src.pyimagesearch.utils.generator_utils_Elan import convert_data, add_data_classify, add_data_segment_2D, add_data_segment
from sklearn import preprocessing
from keras.preprocessing.image import ImageDataGenerator

Expand All @@ -20,7 +20,6 @@ def __init__(self, data_file, index_list, n_labels=1, labels = None,
self.shuffle = shuffle_index_list
self.skip_blank = skip_blank
self.data_file = data_file
self.patch_shape = patch_shape
self.patch_overlap = patch_overlap
self.patch_offset = patch_start_offset
self.permute = permute
Expand Down
104 changes: 104 additions & 0 deletions src/pyimagesearch/nn/conv/isensee2017.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
from functools import partial

from keras.layers import Input, LeakyReLU, Add, UpSampling3D, Activation, SpatialDropout3D
from keras.engine import Model
from keras.optimizers import Adam

from src.model.unet import create_convolution_block, concatenate



create_convolution_block = partial(create_convolution_block, activation=LeakyReLU, instance_normalization=True)


def create_localization_module(input_layer, n_filters):
convolution1 = create_convolution_block(input_layer, n_filters)
convolution2 = create_convolution_block(convolution1, n_filters, kernel=(1, 1, 1))
return convolution2


def create_up_sampling_module(input_layer, n_filters, size=(2, 2, 2)):
up_sample = UpSampling3D(size=size)(input_layer)
convolution = create_convolution_block(up_sample, n_filters)
return convolution


def create_context_module(input_layer, n_level_filters, dropout_rate=0.3, data_format="channels_first"):
convolution1 = create_convolution_block(input_layer=input_layer, n_filters=n_level_filters)
dropout = SpatialDropout3D(rate=dropout_rate, data_format=data_format)(convolution1)
convolution2 = create_convolution_block(input_layer=dropout, n_filters=n_level_filters)
return convolution2



class isensee2017:
"""isensee2017."""

@staticmethod
def build(input_shape=(4, 128, 128, 128), n_base_filters=16, depth=5, dropout_rate=0.3,
n_segmentation_levels=3, n_labels=4, optimizer=Adam, initial_learning_rate=5e-4,
loss_function=weighted_dice_coefficient_loss, activation_name="sigmoid"):
"""
This function builds a model proposed by Isensee et al. for the BRATS 2017 competition:
https://www.cbica.upenn.edu/sbia/Spyridon.Bakas/MICCAI_BraTS/MICCAI_BraTS_2017_proceedings_shortPapers.pdf
This network is highly similar to the model proposed by Kayalibay et al. "CNN-based Segmentation of Medical
Imaging Data", 2017: https://arxiv.org/pdf/1701.03056.pdf
:param input_shape:
:param n_base_filters:
:param depth:
:param dropout_rate:
:param n_segmentation_levels:
:param n_labels:
:param optimizer:
:param initial_learning_rate:
:param loss_function:
:param activation_name:
:return:
"""
inputs = Input(input_shape)

current_layer = inputs
level_output_layers = list()
level_filters = list()
for level_number in range(depth):
n_level_filters = (2**level_number) * n_base_filters
level_filters.append(n_level_filters)

if current_layer is inputs:
in_conv = create_convolution_block(current_layer, n_level_filters)
else:
in_conv = create_convolution_block(current_layer, n_level_filters, strides=(2, 2, 2))

context_output_layer = create_context_module(in_conv, n_level_filters, dropout_rate=dropout_rate)

summation_layer = Add()([in_conv, context_output_layer])
level_output_layers.append(summation_layer)
current_layer = summation_layer

segmentation_layers = list()
for level_number in range(depth - 2, -1, -1):
up_sampling = create_up_sampling_module(current_layer, level_filters[level_number])
concatenation_layer = concatenate([level_output_layers[level_number], up_sampling], axis=1)
localization_output = create_localization_module(concatenation_layer, level_filters[level_number])
current_layer = localization_output
if level_number < n_segmentation_levels:
segmentation_layers.insert(0, create_convolution_block(current_layer, n_filters=n_labels, kernel=(1, 1, 1)))

output_layer = None
for level_number in reversed(range(n_segmentation_levels)):
segmentation_layer = segmentation_layers[level_number]
if output_layer is None:
output_layer = segmentation_layer
else:
output_layer = Add()([output_layer, segmentation_layer])

if level_number > 0:
output_layer = UpSampling3D(size=(2, 2, 2))(output_layer)

activation_block = Activation(activation_name)(output_layer)

model = Model(inputs=inputs, outputs=activation_block)
return model
134 changes: 134 additions & 0 deletions src/pyimagesearch/nn/conv/unet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import numpy as np
from keras import backend as K
from keras.engine import Input, Model
from keras.layers import Conv3D, MaxPooling3D, UpSampling3D, Activation, BatchNormalization, PReLU, Deconvolution3D
from keras.optimizers import Adam

from unet3d.metrics import dice_coefficient_loss, get_label_dice_coefficient_function, dice_coefficient

K.set_image_data_format("channels_first")

try:
from keras.engine import merge
except ImportError:
from keras.layers.merge import concatenate


def unet_model_3d(input_shape, pool_size=(2, 2, 2), n_labels=1, initial_learning_rate=0.00001, deconvolution=False,
depth=4, n_base_filters=32, include_label_wise_dice_coefficients=False, metrics=dice_coefficient,
batch_normalization=False, activation_name="sigmoid"):
"""
Builds the 3D UNet Keras model.f
:param metrics: List metrics to be calculated during model training (default is dice coefficient).
:param include_label_wise_dice_coefficients: If True and n_labels is greater than 1, model will report the dice
coefficient for each label as metric.
:param n_base_filters: The number of filters that the first layer in the convolution network will have. Following
layers will contain a multiple of this number. Lowering this number will likely reduce the amount of memory required
to train the model.
:param depth: indicates the depth of the U-shape for the model. The greater the depth, the more max pooling
layers will be added to the model. Lowering the depth may reduce the amount of memory required for training.
:param input_shape: Shape of the input data (n_chanels, x_size, y_size, z_size). The x, y, and z sizes must be
divisible by the pool size to the power of the depth of the UNet, that is pool_size^depth.
:param pool_size: Pool size for the max pooling operations.
:param n_labels: Number of binary labels that the model is learning.
:param initial_learning_rate: Initial learning rate for the model. This will be decayed during training.
:param deconvolution: If set to True, will use transpose convolution(deconvolution) instead of up-sampling. This
increases the amount memory required during training.
:return: Untrained 3D UNet Model
"""
inputs = Input(input_shape)
current_layer = inputs
levels = list()

# add levels with max pooling
for layer_depth in range(depth):
layer1 = create_convolution_block(input_layer=current_layer, n_filters=n_base_filters*(2**layer_depth),
batch_normalization=batch_normalization)
layer2 = create_convolution_block(input_layer=layer1, n_filters=n_base_filters*(2**layer_depth)*2,
batch_normalization=batch_normalization)
if layer_depth < depth - 1:
current_layer = MaxPooling3D(pool_size=pool_size)(layer2)
levels.append([layer1, layer2, current_layer])
else:
current_layer = layer2
levels.append([layer1, layer2])

# add levels with up-convolution or up-sampling
for layer_depth in range(depth-2, -1, -1):
up_convolution = get_up_convolution(pool_size=pool_size, deconvolution=deconvolution,
n_filters=current_layer._keras_shape[1])(current_layer)
concat = concatenate([up_convolution, levels[layer_depth][1]], axis=1)
current_layer = create_convolution_block(n_filters=levels[layer_depth][1]._keras_shape[1],
input_layer=concat, batch_normalization=batch_normalization)
current_layer = create_convolution_block(n_filters=levels[layer_depth][1]._keras_shape[1],
input_layer=current_layer,
batch_normalization=batch_normalization)

final_convolution = Conv3D(n_labels, (1, 1, 1))(current_layer)
act = Activation(activation_name)(final_convolution)
model = Model(inputs=inputs, outputs=act)

if not isinstance(metrics, list):
metrics = [metrics]

if include_label_wise_dice_coefficients and n_labels > 1:
label_wise_dice_metrics = [get_label_dice_coefficient_function(index) for index in range(n_labels)]
if metrics:
metrics = metrics + label_wise_dice_metrics
else:
metrics = label_wise_dice_metrics

model.compile(optimizer=Adam(lr=initial_learning_rate), loss=dice_coefficient_loss, metrics=metrics)
return model


def create_convolution_block(input_layer, n_filters, batch_normalization=False, kernel=(3, 3, 3), activation=None,
padding='same', strides=(1, 1, 1), instance_normalization=False):
"""
:param strides:
:param input_layer:
:param n_filters:
:param batch_normalization:
:param kernel:
:param activation: Keras activation layer to use. (default is 'relu')
:param padding:
:return:
"""
layer = Conv3D(n_filters, kernel, padding=padding, strides=strides)(input_layer)
if batch_normalization:
layer = BatchNormalization(axis=1)(layer)
elif instance_normalization:
try:
from keras_contrib.layers.normalization import InstanceNormalization
except ImportError:
raise ImportError("Install keras_contrib in order to use instance normalization."
"\nTry: pip install git+https://www.github.com/farizrahman4u/keras-contrib.git")
layer = InstanceNormalization(axis=1)(layer)
if activation is None:
return Activation('relu')(layer)
else:
return activation()(layer)


def compute_level_output_shape(n_filters, depth, pool_size, image_shape):
"""
Each level has a particular output shape based on the number of filters used in that level and the depth or number
of max pooling operations that have been done on the data at that point.
:param image_shape: shape of the 3d image.
:param pool_size: the pool_size parameter used in the max pooling operation.
:param n_filters: Number of filters used by the last node in a given level.
:param depth: The number of levels down in the U-shaped model a given node is.
:return: 5D vector of the shape of the output node
"""
output_image_shape = np.asarray(np.divide(image_shape, np.power(pool_size, depth)), dtype=np.int32).tolist()
return tuple([None, n_filters] + output_image_shape)


def get_up_convolution(n_filters, pool_size, kernel_size=(2, 2, 2), strides=(2, 2, 2),
deconvolution=False):
if deconvolution:
return Deconvolution3D(filters=n_filters, kernel_size=kernel_size,
strides=strides)
else:
return UpSampling3D(size=pool_size)
Loading

0 comments on commit 3fd3e92

Please sign in to comment.