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
f7af025e
Commit
f7af025e
authored
Feb 12, 2016
by
Yuxin Wu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
batch normalization
parent
fa2f223a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
50 additions
and
4 deletions
+50
-4
example_cifar10.py
example_cifar10.py
+2
-0
tensorpack/models/batch_norm.py
tensorpack/models/batch_norm.py
+44
-0
tensorpack/models/image_sample.py
tensorpack/models/image_sample.py
+4
-4
No files found.
example_cifar10.py
View file @
f7af025e
...
...
@@ -47,12 +47,14 @@ class Model(ModelDesc):
l
=
Conv2D
(
'conv1'
,
image
,
out_channel
=
64
,
kernel_shape
=
5
,
padding
=
'SAME'
,
W_init
=
tf
.
truncated_normal_initializer
(
stddev
=
1e-4
))
#l = BatchNorm('bn0', l, is_training)
l
=
MaxPooling
(
'pool1'
,
l
,
3
,
stride
=
2
,
padding
=
'SAME'
)
l
=
tf
.
nn
.
lrn
(
l
,
4
,
bias
=
1.0
,
alpha
=
0.001
/
9.0
,
beta
=
0.75
,
name
=
'norm1'
)
l
=
Conv2D
(
'conv2'
,
l
,
out_channel
=
64
,
kernel_shape
=
5
,
padding
=
'SAME'
,
W_init
=
tf
.
truncated_normal_initializer
(
stddev
=
1e-4
),
b_init
=
tf
.
constant_initializer
(
0.1
))
#l = BatchNorm('bn1', l, is_training)
l
=
tf
.
nn
.
lrn
(
l
,
4
,
bias
=
1.0
,
alpha
=
0.001
/
9.0
,
beta
=
0.75
,
name
=
'norm2'
)
l
=
MaxPooling
(
'pool2'
,
l
,
3
,
stride
=
2
,
padding
=
'SAME'
)
...
...
tensorpack/models/batch_norm.py
0 → 100644
View file @
f7af025e
#!/usr/bin/env python2
# -*- coding: UTF-8 -*-
# File: batch_norm.py
# Author: Yuxin Wu <ppwwyyxx@gmail.com>
import
tensorflow
as
tf
from
._common
import
layer_register
__all__
=
[
'BatchNorm'
]
# http://stackoverflow.com/questions/33949786/how-could-i-use-batch-normalization-in-tensorflow
@
layer_register
()
def
BatchNorm
(
x
,
is_training
):
"""
x: has to be BHWC for now
is_training: bool
"""
is_training
=
bool
(
is_training
)
shape
=
x
.
get_shape
()
.
as_list
()
assert
len
(
shape
)
==
4
n_out
=
shape
[
-
1
]
# channel
beta
=
tf
.
get_variable
(
'beta'
,
[
n_out
])
gamma
=
tf
.
get_variable
(
'gamma'
,
[
n_out
],
initializer
=
tf
.
constant_initializer
(
1.0
))
batch_mean
,
batch_var
=
tf
.
nn
.
moments
(
x
,
[
0
,
1
,
2
],
name
=
'moments'
)
ema
=
tf
.
train
.
ExponentialMovingAverage
(
decay
=
0.9
)
ema_apply_op
=
ema
.
apply
([
batch_mean
,
batch_var
])
ema_mean
,
ema_var
=
ema
.
average
(
batch_mean
),
ema
.
average
(
batch_var
)
if
is_training
:
def
mean_var_with_update
():
with
tf
.
control_dependencies
([
ema_apply_op
]):
return
tf
.
identity
(
batch_mean
),
tf
.
identity
(
batch_var
)
mean
,
var
=
mean_var_with_update
()
else
:
mean
,
var
=
ema_mean
,
ema_var
normed
=
tf
.
nn
.
batch_norm_with_global_normalization
(
x
,
mean
,
var
,
beta
,
gamma
,
1e-4
,
True
)
return
normed
tensorpack/models/image_sample.py
View file @
f7af025e
...
...
@@ -69,10 +69,10 @@ def ImageSample(inputs):
diffy
,
diffx
=
tf
.
split
(
3
,
2
,
diff
)
neg_diffy
,
neg_diffx
=
tf
.
split
(
3
,
2
,
neg_diff
)
prod
=
tf
.
reduce_prod
(
diff
,
3
,
keep_dims
=
True
)
diff
=
tf
.
Print
(
diff
,
[
tf
.
is_finite
(
tf
.
reduce_sum
(
diff
)),
tf
.
shape
(
prod
),
tf
.
reduce_max
(
diff
),
diff
],
summarize
=
50
)
#
prod = tf.reduce_prod(diff, 3, keep_dims=True)
#
diff = tf.Print(diff, [tf.is_finite(tf.reduce_sum(diff)), tf.shape(prod),
#
tf.reduce_max(diff), diff],
#
summarize=50)
return
tf
.
add_n
([
sample
(
template
,
lcoor
)
*
neg_diffx
*
neg_diffy
,
sample
(
template
,
ucoor
)
*
diffx
*
diffy
,
...
...
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