Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
seminar-breakout
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Shashank Suhas
seminar-breakout
Commits
152c4c2c
Commit
152c4c2c
authored
Feb 20, 2019
by
Yuxin Wu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reimplement Conv2DTranspose to avoid Keras bugs.
parent
d0e410ad
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
77 additions
and
33 deletions
+77
-33
tensorpack/models/_old_batch_norm.py
tensorpack/models/_old_batch_norm.py
+1
-1
tensorpack/models/batch_norm.py
tensorpack/models/batch_norm.py
+1
-1
tensorpack/models/conv2d.py
tensorpack/models/conv2d.py
+66
-22
tensorpack/models/layer_norm.py
tensorpack/models/layer_norm.py
+2
-2
tensorpack/models/pool.py
tensorpack/models/pool.py
+1
-1
tensorpack/models/tflayer.py
tensorpack/models/tflayer.py
+1
-1
tensorpack/train/trainers.py
tensorpack/train/trainers.py
+1
-1
tensorpack/utils/argtools.py
tensorpack/utils/argtools.py
+4
-4
No files found.
tensorpack/models/_old_batch_norm.py
View file @
152c4c2c
...
@@ -98,7 +98,7 @@ def BatchNorm(inputs, training=None, momentum=0.9, epsilon=1e-5,
...
@@ -98,7 +98,7 @@ def BatchNorm(inputs, training=None, momentum=0.9, epsilon=1e-5,
don't want to fine tune the EMA. EMA will not be updated in
don't want to fine tune the EMA. EMA will not be updated in
this case.
this case.
"""
"""
data_format
=
get_data_format
(
data_format
,
tf
mode
=
False
)
data_format
=
get_data_format
(
data_format
,
keras_
mode
=
False
)
shape
=
inputs
.
get_shape
()
.
as_list
()
shape
=
inputs
.
get_shape
()
.
as_list
()
ndims
=
len
(
shape
)
ndims
=
len
(
shape
)
assert
ndims
in
[
2
,
4
]
assert
ndims
in
[
2
,
4
]
...
...
tensorpack/models/batch_norm.py
View file @
152c4c2c
...
@@ -147,7 +147,7 @@ def BatchNorm(inputs, axis=None, training=None, momentum=0.9, epsilon=1e-5,
...
@@ -147,7 +147,7 @@ def BatchNorm(inputs, axis=None, training=None, momentum=0.9, epsilon=1e-5,
this case.
this case.
"""
"""
# parse shapes
# parse shapes
data_format
=
get_data_format
(
data_format
,
tf
mode
=
False
)
data_format
=
get_data_format
(
data_format
,
keras_
mode
=
False
)
shape
=
inputs
.
get_shape
()
.
as_list
()
shape
=
inputs
.
get_shape
()
.
as_list
()
ndims
=
len
(
shape
)
ndims
=
len
(
shape
)
assert
ndims
in
[
2
,
4
],
ndims
assert
ndims
in
[
2
,
4
],
ndims
...
...
tensorpack/models/conv2d.py
View file @
152c4c2c
...
@@ -80,7 +80,7 @@ def Conv2D(
...
@@ -80,7 +80,7 @@ def Conv2D(
else
:
else
:
# group conv implementation
# group conv implementation
data_format
=
get_data_format
(
data_format
,
tf
mode
=
False
)
data_format
=
get_data_format
(
data_format
,
keras_
mode
=
False
)
in_shape
=
inputs
.
get_shape
()
.
as_list
()
in_shape
=
inputs
.
get_shape
()
.
as_list
()
channel_axis
=
3
if
data_format
==
'NHWC'
else
1
channel_axis
=
3
if
data_format
==
'NHWC'
else
1
in_channel
=
in_shape
[
channel_axis
]
in_channel
=
in_shape
[
channel_axis
]
...
@@ -163,27 +163,71 @@ def Conv2DTranspose(
...
@@ -163,27 +163,71 @@ def Conv2DTranspose(
else
:
else
:
kernel_initializer
=
tf
.
keras
.
initializers
.
VarianceScaling
(
2.0
,
distribution
=
'untruncated_normal'
)
kernel_initializer
=
tf
.
keras
.
initializers
.
VarianceScaling
(
2.0
,
distribution
=
'untruncated_normal'
)
with
rename_get_variable
({
'kernel'
:
'W'
,
'bias'
:
'b'
}):
if
get_tf_version_tuple
()
<=
(
1
,
12
):
layer
=
tf
.
layers
.
Conv2DTranspose
(
with
rename_get_variable
({
'kernel'
:
'W'
,
'bias'
:
'b'
}):
filters
,
layer
=
tf
.
layers
.
Conv2DTranspose
(
kernel_size
,
filters
,
strides
=
strides
,
kernel_size
,
padding
=
padding
,
strides
=
strides
,
data_format
=
data_format
,
padding
=
padding
,
activation
=
activation
,
data_format
=
data_format
,
use_bias
=
use_bias
,
activation
=
activation
,
kernel_initializer
=
kernel_initializer
,
use_bias
=
use_bias
,
bias_initializer
=
bias_initializer
,
kernel_initializer
=
kernel_initializer
,
kernel_regularizer
=
kernel_regularizer
,
bias_initializer
=
bias_initializer
,
bias_regularizer
=
bias_regularizer
,
kernel_regularizer
=
kernel_regularizer
,
activity_regularizer
=
activity_regularizer
,
bias_regularizer
=
bias_regularizer
,
_reuse
=
tf
.
get_variable_scope
()
.
reuse
)
activity_regularizer
=
activity_regularizer
,
ret
=
layer
.
apply
(
inputs
,
scope
=
tf
.
get_variable_scope
())
_reuse
=
tf
.
get_variable_scope
()
.
reuse
)
ret
=
tf
.
identity
(
ret
,
name
=
'output'
)
ret
=
layer
.
apply
(
inputs
,
scope
=
tf
.
get_variable_scope
())
ret
=
tf
.
identity
(
ret
,
name
=
'output'
)
ret
.
variables
=
VariableHolder
(
W
=
layer
.
kernel
)
ret
.
variables
=
VariableHolder
(
W
=
layer
.
kernel
)
if
use_bias
:
if
use_bias
:
ret
.
variables
.
b
=
layer
.
bias
ret
.
variables
.
b
=
layer
.
bias
else
:
# Our own implementation, to avoid Keras bugs. https://github.com/tensorflow/tensorflow/issues/25946
assert
kernel_regularizer
is
None
and
bias_regularizer
is
None
and
activity_regularizer
is
None
,
\
"Unsupported arguments due to bug in TensorFlow 1.13"
data_format
=
get_data_format
(
data_format
,
keras_mode
=
False
)
shape_dyn
=
tf
.
shape
(
inputs
)
strides2d
=
shape2d
(
strides
)
channels_in
=
inputs
.
shape
[
1
if
data_format
==
'NCHW'
else
3
]
if
data_format
==
'NCHW'
:
channels_in
=
inputs
.
shape
[
1
]
out_shape_dyn
=
tf
.
stack
(
[
shape_dyn
[
0
],
filters
,
shape_dyn
[
2
]
*
strides2d
[
0
],
shape_dyn
[
3
]
*
strides2d
[
1
]])
out_shape3_sta
=
[
filters
,
None
if
inputs
.
shape
[
2
]
is
None
else
inputs
.
shape
[
2
]
*
strides2d
[
0
],
None
if
inputs
.
shape
[
3
]
is
None
else
inputs
.
shape
[
3
]
*
strides2d
[
1
]]
else
:
channels_in
=
inputs
.
shape
[
-
1
]
out_shape_dyn
=
tf
.
stack
(
[
shape_dyn
[
0
],
shape_dyn
[
1
]
*
strides2d
[
0
],
shape_dyn
[
2
]
*
strides2d
[
1
],
channels_in
])
out_shape3_sta
=
[
None
if
inputs
.
shape
[
1
]
is
None
else
inputs
.
shape
[
1
]
*
strides2d
[
0
],
None
if
inputs
.
shape
[
2
]
is
None
else
inputs
.
shape
[
2
]
*
strides2d
[
1
],
filters
]
kernel_shape
=
shape2d
(
kernel_size
)
W
=
tf
.
get_variable
(
'W'
,
kernel_shape
+
[
filters
,
channels_in
],
initializer
=
kernel_initializer
)
if
use_bias
:
b
=
tf
.
get_variable
(
'b'
,
[
filters
],
initializer
=
bias_initializer
)
conv
=
tf
.
nn
.
conv2d_transpose
(
inputs
,
W
,
out_shape_dyn
,
shape4d
(
strides
,
data_format
=
data_format
),
padding
=
padding
.
upper
(),
data_format
=
data_format
)
conv
.
set_shape
(
tf
.
TensorShape
([
None
]
+
out_shape3_sta
))
ret
=
activation
(
tf
.
nn
.
bias_add
(
conv
,
b
,
data_format
=
data_format
)
if
use_bias
else
conv
,
name
=
'output'
)
ret
.
variables
=
VariableHolder
(
W
=
W
)
if
use_bias
:
ret
.
variables
.
b
=
b
return
ret
return
ret
...
...
tensorpack/models/layer_norm.py
View file @
152c4c2c
...
@@ -24,7 +24,7 @@ def LayerNorm(
...
@@ -24,7 +24,7 @@ def LayerNorm(
epsilon (float): epsilon to avoid divide-by-zero.
epsilon (float): epsilon to avoid divide-by-zero.
use_scale, use_bias (bool): whether to use the extra affine transformation or not.
use_scale, use_bias (bool): whether to use the extra affine transformation or not.
"""
"""
data_format
=
get_data_format
(
data_format
,
tf
mode
=
False
)
data_format
=
get_data_format
(
data_format
,
keras_
mode
=
False
)
shape
=
x
.
get_shape
()
.
as_list
()
shape
=
x
.
get_shape
()
.
as_list
()
ndims
=
len
(
shape
)
ndims
=
len
(
shape
)
assert
ndims
in
[
2
,
4
]
assert
ndims
in
[
2
,
4
]
...
@@ -75,7 +75,7 @@ def InstanceNorm(x, epsilon=1e-5, use_affine=True, gamma_init=None, data_format=
...
@@ -75,7 +75,7 @@ def InstanceNorm(x, epsilon=1e-5, use_affine=True, gamma_init=None, data_format=
epsilon (float): avoid divide-by-zero
epsilon (float): avoid divide-by-zero
use_affine (bool): whether to apply learnable affine transformation
use_affine (bool): whether to apply learnable affine transformation
"""
"""
data_format
=
get_data_format
(
data_format
,
tf
mode
=
False
)
data_format
=
get_data_format
(
data_format
,
keras_
mode
=
False
)
shape
=
x
.
get_shape
()
.
as_list
()
shape
=
x
.
get_shape
()
.
as_list
()
assert
len
(
shape
)
==
4
,
"Input of InstanceNorm has to be 4D!"
assert
len
(
shape
)
==
4
,
"Input of InstanceNorm has to be 4D!"
...
...
tensorpack/models/pool.py
View file @
152c4c2c
...
@@ -102,7 +102,7 @@ def FixedUnPooling(x, shape, unpool_mat=None, data_format='channels_last'):
...
@@ -102,7 +102,7 @@ def FixedUnPooling(x, shape, unpool_mat=None, data_format='channels_last'):
Returns:
Returns:
tf.Tensor: a 4D image tensor.
tf.Tensor: a 4D image tensor.
"""
"""
data_format
=
get_data_format
(
data_format
,
tf
mode
=
False
)
data_format
=
get_data_format
(
data_format
,
keras_
mode
=
False
)
shape
=
shape2d
(
shape
)
shape
=
shape2d
(
shape
)
output_shape
=
StaticDynamicShape
(
x
)
output_shape
=
StaticDynamicShape
(
x
)
...
...
tensorpack/models/tflayer.py
View file @
152c4c2c
...
@@ -15,7 +15,7 @@ __all__ = []
...
@@ -15,7 +15,7 @@ __all__ = []
def
map_common_tfargs
(
kwargs
):
def
map_common_tfargs
(
kwargs
):
df
=
kwargs
.
pop
(
'data_format'
,
None
)
df
=
kwargs
.
pop
(
'data_format'
,
None
)
if
df
is
not
None
:
if
df
is
not
None
:
df
=
get_data_format
(
df
,
tf
mode
=
True
)
df
=
get_data_format
(
df
,
keras_
mode
=
True
)
kwargs
[
'data_format'
]
=
df
kwargs
[
'data_format'
]
=
df
old_nl
=
kwargs
.
pop
(
'nl'
,
None
)
old_nl
=
kwargs
.
pop
(
'nl'
,
None
)
...
...
tensorpack/train/trainers.py
View file @
152c4c2c
...
@@ -340,7 +340,7 @@ class HorovodTrainer(SingleCostTrainer):
...
@@ -340,7 +340,7 @@ class HorovodTrainer(SingleCostTrainer):
for Horovod installation and in the MPI command line.
for Horovod installation and in the MPI command line.
See Horovod docs for details.
See Horovod docs for details.
2. Due to a TF bug, you must not initialize CUDA context before the trainer starts training.
2. Due to a TF bug
(#8136)
, you must not initialize CUDA context before the trainer starts training.
Therefore TF functions like `is_gpu_available()` or `list_local_devices()`
Therefore TF functions like `is_gpu_available()` or `list_local_devices()`
must be avoided.
must be avoided.
...
...
tensorpack/utils/argtools.py
View file @
152c4c2c
...
@@ -104,8 +104,8 @@ def shape2d(a):
...
@@ -104,8 +104,8 @@ def shape2d(a):
raise
RuntimeError
(
"Illegal shape: {}"
.
format
(
a
))
raise
RuntimeError
(
"Illegal shape: {}"
.
format
(
a
))
def
get_data_format
(
data_format
,
tf
mode
=
True
):
def
get_data_format
(
data_format
,
keras_
mode
=
True
):
if
tf
mode
:
if
keras_
mode
:
dic
=
{
'NCHW'
:
'channels_first'
,
'NHWC'
:
'channels_last'
}
dic
=
{
'NCHW'
:
'channels_first'
,
'NHWC'
:
'channels_last'
}
else
:
else
:
dic
=
{
'channels_first'
:
'NCHW'
,
'channels_last'
:
'NHWC'
}
dic
=
{
'channels_first'
:
'NCHW'
,
'channels_last'
:
'NHWC'
}
...
@@ -115,7 +115,7 @@ def get_data_format(data_format, tfmode=True):
...
@@ -115,7 +115,7 @@ def get_data_format(data_format, tfmode=True):
return
ret
return
ret
def
shape4d
(
a
,
data_format
=
'
channels_last
'
):
def
shape4d
(
a
,
data_format
=
'
NHWC
'
):
"""
"""
Ensuer a 4D shape, to use with 4D symbolic functions.
Ensuer a 4D shape, to use with 4D symbolic functions.
...
@@ -127,7 +127,7 @@ def shape4d(a, data_format='channels_last'):
...
@@ -127,7 +127,7 @@ def shape4d(a, data_format='channels_last'):
or ``[1, 1, a, a]`` depending on data_format.
or ``[1, 1, a, a]`` depending on data_format.
"""
"""
s2d
=
shape2d
(
a
)
s2d
=
shape2d
(
a
)
if
get_data_format
(
data_format
)
==
'channels_last
'
:
if
get_data_format
(
data_format
,
False
)
==
'NHWC
'
:
return
[
1
]
+
s2d
+
[
1
]
return
[
1
]
+
s2d
+
[
1
]
else
:
else
:
return
[
1
,
1
]
+
s2d
return
[
1
,
1
]
+
s2d
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment