Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

densenet dilation for segmentation #46

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/cifar10_densenet.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'''
"""
Trains a DenseNet-40-12 model on the CIFAR-10 Dataset.

Gets a 94.84% accuracy score after 100 epochs.
'''
"""
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
Expand Down Expand Up @@ -33,8 +33,8 @@
dropout_rate = 0.0 # 0.0 for data augmentation

# Create the model (without loading weights)
model = DenseNet(depth, nb_dense_block, growth_rate, nb_filter, dropout_rate=dropout_rate,
input_shape=img_dim, weights=None)
model = DenseNet(img_dim, depth, nb_dense_block, growth_rate, nb_filter,
dropout_rate=dropout_rate, weights=None)
print('Model created')

model.summary()
Expand Down
4 changes: 2 additions & 2 deletions examples/cifar10_ror.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'''
"""
Trains a Residual-of-Residual Network (WRN-40-2) model on the CIFAR-10 Dataset.

Gets a 94.53% accuracy score after 150 epochs.
'''
"""

import keras.callbacks as callbacks
import keras.utils.np_utils as kutils
Expand Down
4 changes: 2 additions & 2 deletions examples/cifar10_wide_resnet.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'''
"""
Trains a WRN-28-8 model on the CIFAR-10 Dataset.

Performance is slightly less than the paper, since
they use WRN-28-10 model (95.83%).

Gets a 95.54% accuracy score after 300 epochs.
'''
"""
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
Expand Down
525 changes: 371 additions & 154 deletions keras_contrib/applications/densenet.py

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions keras_contrib/applications/ror.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def __conv_block(input, nb_filters=32, k=1, dropout=0.0):


def __create_pre_residual_of_residual(nb_classes, img_input, include_top, depth=28, width=1, dropout=0.0):
'''
"""
Creates a Residual Network of Residual Network with specified parameters

Example : To create a Pre-RoR model, use k = 1
Expand All @@ -230,7 +230,7 @@ def __create_pre_residual_of_residual(nb_classes, img_input, include_top, depth=
Note : Generally not used in RoR

Returns: a Keras Model
'''
"""

N = (depth - 4) // 6

Expand Down
4 changes: 2 additions & 2 deletions keras_contrib/applications/wide_resnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def ___conv4_block(input, k=1, dropout=0.0):


def __create_wide_residual_network(nb_classes, img_input, include_top, depth=28, width=8, dropout=0.0):
''' Creates a Wide Residual Network with specified parameters
""" Creates a Wide Residual Network with specified parameters

Args:
nb_classes: Number of output classes
Expand All @@ -265,7 +265,7 @@ def __create_wide_residual_network(nb_classes, img_input, include_top, depth=28,
dropout: Adds dropout if value is greater than 0.0

Returns:a Keras Model
'''
"""

N = (depth - 4) // 6

Expand Down
16 changes: 8 additions & 8 deletions keras_contrib/backend/tensorflow_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ def _preprocess_deconv_output_shape(x, shape, data_format):

def conv2d(x, kernel, strides=(1, 1), padding='valid', data_format='channels_first',
image_shape=None, filter_shape=None):
'''2D convolution.
"""2D convolution.
# Arguments
kernel: kernel tensor.
strides: strides tuple.
padding: string, "same" or "valid".
data_format: "tf" or "th". Whether to use Theano or TensorFlow dimension ordering
in inputs/kernels/ouputs.
'''
"""
if padding == 'same':
padding = 'SAME'
elif padding == 'valid':
Expand Down Expand Up @@ -74,7 +74,7 @@ def deconv3d(x, kernel, output_shape, strides=(1, 1, 1),
padding='valid',
data_format='default',
image_shape=None, filter_shape=None):
'''3D deconvolution (i.e. transposed convolution).
"""3D deconvolution (i.e. transposed convolution).

# Arguments
x: input tensor.
Expand All @@ -91,7 +91,7 @@ def deconv3d(x, kernel, output_shape, strides=(1, 1, 1),

# Raises
ValueError: if `data_format` is neither `tf` or `th`.
'''
"""
if data_format == 'default':
data_format = image_data_format()
if data_format not in {'channels_first', 'channels_last'}:
Expand All @@ -111,7 +111,7 @@ def deconv3d(x, kernel, output_shape, strides=(1, 1, 1),

def extract_image_patches(x, ksizes, ssizes, padding='same',
data_format='tf'):
'''
"""
Extract the patches from an image
# Parameters

Expand All @@ -125,7 +125,7 @@ def extract_image_patches(x, ksizes, ssizes, padding='same',
The (k_w,k_h) patches extracted
TF ==> (batch_size,w,h,k_w,k_h,c)
TH ==> (batch_size,w,h,c,k_w,k_h)
'''
"""
kernel = [1, ksizes[0], ksizes[1], 1]
strides = [1, ssizes[0], ssizes[1], 1]
padding = _preprocess_padding(padding)
Expand All @@ -144,7 +144,7 @@ def extract_image_patches(x, ksizes, ssizes, padding='same',


def depth_to_space(input, scale, data_format=None):
''' Uses phase shift algorithm to convert channels/depth for spatial resolution '''
""" Uses phase shift algorithm to convert channels/depth for spatial resolution """
if data_format is None:
data_format = image_data_format()
data_format = data_format.lower()
Expand All @@ -155,6 +155,6 @@ def depth_to_space(input, scale, data_format=None):


def moments(x, axes, shift=None, keep_dims=False):
''' Wrapper over tensorflow backend call '''
""" Wrapper over tensorflow backend call """

return tf.nn.moments(x, axes, shift=shift, keep_dims=keep_dims)
16 changes: 8 additions & 8 deletions keras_contrib/backend/theano_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@

def conv2d(x, kernel, strides=(1, 1), padding='valid', data_format='channels_first',
image_shape=None, filter_shape=None):
'''
"""
padding: string, "same" or "valid".
'''
"""
if data_format not in {'channels_first', 'channels_last'}:
raise Exception('Unknown data_format ' + str(data_format))

Expand Down Expand Up @@ -88,7 +88,7 @@ def int_or_none(value):
def deconv3d(x, kernel, output_shape, strides=(1, 1, 1),
padding='valid',
data_format=None, filter_shape=None):
'''3D deconvolution (transposed convolution).
"""3D deconvolution (transposed convolution).

# Arguments
kernel: kernel tensor.
Expand All @@ -98,7 +98,7 @@ def deconv3d(x, kernel, output_shape, strides=(1, 1, 1),
data_format: "channels_last" or "channels_first".
Whether to use Theano or TensorFlow dimension ordering
in inputs/kernels/ouputs.
'''
"""
flip_filters = False
if data_format is None:
data_format = image_data_format()
Expand Down Expand Up @@ -136,7 +136,7 @@ def deconv3d(x, kernel, output_shape, strides=(1, 1, 1),


def extract_image_patches(X, ksizes, strides, padding='valid', data_format='channels_first'):
'''
"""
Extract the patches from an image
Parameters
----------
Expand All @@ -150,7 +150,7 @@ def extract_image_patches(X, ksizes, strides, padding='valid', data_format='chan
The (k_w,k_h) patches extracted
TF ==> (batch_size,w,h,k_w,k_h,c)
TH ==> (batch_size,w,h,c,k_w,k_h)
'''
"""
patch_size = ksizes[1]
if padding == 'same':
padding = 'ignore_borders'
Expand All @@ -174,7 +174,7 @@ def extract_image_patches(X, ksizes, strides, padding='valid', data_format='chan


def depth_to_space(input, scale, data_format=None):
''' Uses phase shift algorithm to convert channels/depth for spatial resolution '''
""" Uses phase shift algorithm to convert channels/depth for spatial resolution """
if data_format is None:
data_format = image_data_format()
data_format = data_format.lower()
Expand All @@ -191,7 +191,7 @@ def depth_to_space(input, scale, data_format=None):


def moments(x, axes, shift=None, keep_dims=False):
''' Calculates and returns the mean and variance of the input '''
""" Calculates and returns the mean and variance of the input """

mean_batch = KTH.mean(x, axis=axes, keepdims=keep_dims)
var_batch = KTH.var(x, axis=axes, keepdims=keep_dims)
Expand Down
35 changes: 35 additions & 0 deletions tests/keras_contrib/applications/densenet_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pytest
import numpy as np
from numpy.testing import assert_allclose

from keras import backend as K
from keras.optimizers import Adam
from keras_contrib.applications.densenet import DenseNet
from keras_contrib.applications.densenet import DenseNetFCN


def test_densenet():
'''Tests if DenseNet Models can be created correctly
'''
densenet = DenseNet()
optimizer = Adam(lr=1e-3)
densenet.compile(loss='categorical_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])

fcn_densenet = DenseNetFCN((32, 32, 3))
fcn_densenet.compile(loss='categorical_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])

atrous_densenet = DenseNet(depth=None, nb_dense_block=4, growth_rate=12,
nb_filter=16, nb_layers_per_block=4,
weights=None, dilation_rate=2)

atrous_densenet.compile(loss='categorical_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])


if __name__ == '__main__':
pytest.main([__file__])