Skip to content

[WIP] Fix Issue #561 - Convolution Support NCHW #640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 17 additions & 3 deletions tensorlayer/layers/convolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,19 @@ def __init__(
act=None,
shape=(5, 1, 5),
stride=1,
dilation_rate=1,
padding='SAME',
data_format='NWC',
dilation_rate=1,
data_format=None,
W_init=tf.truncated_normal_initializer(stddev=0.02),
b_init=tf.constant_initializer(value=0.0),
W_init_args=None,
b_init_args=None,
name='cnn1d',
):

if data_format not in [None, 'NWC', 'channels_last', 'NCW', 'channels_first']:
raise ValueError("`data_format` should be among 'NWC', 'channels_last', 'NCW', 'channels_first'")

super(Conv1dLayer, self
).__init__(prev_layer=prev_layer, act=act, W_init_args=W_init_args, b_init_args=b_init_args, name=name)

Expand Down Expand Up @@ -207,6 +211,11 @@ def __init__(
data_format=None,
name='cnn_layer',
):

# dic = {'channels_last': 'NHWC', 'channels_first': 'NCHW'}
if data_format not in [None, 'NHWC', 'channels_last', 'NCHW', 'channels_first']:
raise ValueError("'data_format' must be among 'NHWC', 'channels_last', 'NCHW', 'channels_first'.")

super(Conv2dLayer, self
).__init__(prev_layer=prev_layer, act=act, W_init_args=W_init_args, b_init_args=b_init_args, name=name)

Expand Down Expand Up @@ -418,12 +427,16 @@ def __init__(
shape=(2, 2, 2, 3, 32),
strides=(1, 2, 2, 2, 1),
padding='SAME',
data_format=None,
W_init=tf.truncated_normal_initializer(stddev=0.02),
b_init=tf.constant_initializer(value=0.0),
W_init_args=None,
b_init_args=None,
name='cnn3d_layer',
):
if data_format not in [None, 'NDHWC', 'channels_last', 'NCDHW', 'channels_first']:
raise ValueError("'data_format' must be one of 'channels_last', 'channels_first'.")

super(Conv3dLayer, self
).__init__(prev_layer=prev_layer, act=act, W_init_args=W_init_args, b_init_args=b_init_args, name=name)

Expand All @@ -440,7 +453,7 @@ def __init__(
name='W_conv3d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **self.W_init_args
)

self.outputs = tf.nn.conv3d(self.inputs, W, strides=strides, padding=padding, name=None)
self.outputs = tf.nn.conv3d(self.inputs, W, strides=strides, padding=padding, data_format=data_format, name=None)

if b_init:
b = tf.get_variable(
Expand Down Expand Up @@ -1448,6 +1461,7 @@ def __init__(
if self.act is not None else '- No Activation'
)
)

# with tf.variable_scope(name) as vs:
conv2d = tf.layers.Conv2D(
# inputs=self.inputs,
Expand Down
63 changes: 63 additions & 0 deletions tests/test_layers_convolution_channel_first.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import unittest

try:
from tests.unittests_helper import CustomTestCase
except ImportError:
from unittests_helper import CustomTestCase

# To be removed later
import sys
sys.path.insert(0, './')

import tensorflow as tf
import tensorlayer as tl


class Layer_Data_Format_Test(CustomTestCase):

@classmethod
def setUpClass(cls):
x_1d = tf.placeholder(tf.float32, [None, 1, 5])
cls.input_1d = tl.layers.InputLayer(x_1d)

x_2d = tf.placeholder(tf.float32, [None, 1, 5, 5])
cls.input_2d = tl.layers.InputLayer(x_2d)

x_3d = tf.placeholder(tf.float32, [None, 3, 5, 6, 7])
cls.input_3d = tl.layers.InputLayer(x_3d)

@classmethod
def tearDownClass(cls):
tf.reset_default_graph()

def test_Conv1dLayer_NCW(self):
with self.assertRaises(Exception):
with tf.variable_scope('test_Conv1dLayer_NCW', reuse=False):
tl.layers.Conv1dLayer(self.input_1d, data_format="channels_first")

def test_Conv1d_NCW(self):
with self.assertNotRaises(Exception):
with tf.variable_scope('test_Conv1d_NCW', reuse=False):
tl.layers.Conv1d(self.input_1d, data_format="channels_first")

def test_Conv2dLayer_NCHW(self):
with self.assertRaises(Exception):
with tf.variable_scope('test_Conv2dLayer_NCHW', reuse=False):
tl.layers.Conv2dLayer(self.input_2d, data_format="NCHW")

def test_Conv2d_NCHW(self):
with self.assertNotRaises(Exception):
with tf.variable_scope('test_Conv2d_NCHW', reuse=False):
tl.layers.Conv2d(self.input_2d, data_format="NCHW")

def test_Conv3dLayer_NCDHW(self):
with self.assertRaises(Exception):
with tf.variable_scope('test_Conv3dLayer_NCDHW', reuse=False):
tl.layers.Conv3dLayer(self.input_3d, data_format="NCDHW")


if __name__ == '__main__':
# tf.logging.set_verbosity(tf.logging.INFO)
tf.logging.set_verbosity(tf.logging.DEBUG)

unittest.main()
63 changes: 63 additions & 0 deletions tests/test_layers_convolution_channel_last.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import unittest

try:
from tests.unittests_helper import CustomTestCase
except ImportError:
from unittests_helper import CustomTestCase

# To be removed later
import sys
sys.path.insert(0, './')

import tensorflow as tf
import tensorlayer as tl


class Layer_Data_Format_Test(CustomTestCase):

@classmethod
def setUpClass(cls):
x_1d = tf.placeholder(tf.float32, [None, 5, 1])
cls.input_1d = tl.layers.InputLayer(x_1d)

x_2d = tf.placeholder(tf.float32, [None, 5, 5, 1])
cls.input_2d = tl.layers.InputLayer(x_2d)

x_3d = tf.placeholder(tf.float32, [None, 5, 6, 7, 3])
cls.input_3d = tl.layers.InputLayer(x_3d)

@classmethod
def tearDownClass(cls):
tf.reset_default_graph()

def test_Conv1dLayer_NWC(self):
with self.assertNotRaises(Exception):
with tf.variable_scope('test_Conv1dLayer_NWC', reuse=False):
tl.layers.Conv1dLayer(self.input_1d, data_format="channels_last")

def test_Conv1d_NWC(self):
with self.assertNotRaises(Exception):
with tf.variable_scope('test_Conv1d_NWC', reuse=False):
tl.layers.Conv1d(self.input_1d, data_format="channels_last")

def test_Conv2dLayer_NHWC(self):
with self.assertNotRaises(Exception):
with tf.variable_scope('test_Conv2dLayer_NHWC', reuse=False):
tl.layers.Conv2dLayer(self.input_2d, data_format="NHWC")

def test_Conv2d_NHWC(self):
with self.assertNotRaises(Exception):
with tf.variable_scope('test_Conv2d_NHWC', reuse=False):
tl.layers.Conv2d(self.input_2d, data_format="channels_last")

def test_Conv3dLayer_NDHWC(self):
with self.assertNotRaises(Exception):
with tf.variable_scope('test_Conv3dLayer_1', reuse=False):
tl.layers.Conv3dLayer(self.input_3d, data_format="NDHWC")


if __name__ == '__main__':
# tf.logging.set_verbosity(tf.logging.INFO)
tf.logging.set_verbosity(tf.logging.DEBUG)

unittest.main()