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
e04bb2d5
Commit
e04bb2d5
authored
Jan 03, 2017
by
Yuxin Wu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix LinearWrap imports. use varreplace in DoReFa.
parent
d2d3e3c0
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
39 additions
and
9 deletions
+39
-9
.travis.yml
.travis.yml
+5
-1
examples/DoReFa-Net/alexnet-dorefa.py
examples/DoReFa-Net/alexnet-dorefa.py
+5
-4
examples/DoReFa-Net/svhn-digit-dorefa.py
examples/DoReFa-Net/svhn-digit-dorefa.py
+5
-4
tensorpack/models/__init__.py
tensorpack/models/__init__.py
+2
-0
tensorpack/tfutils/varreplace.py
tensorpack/tfutils/varreplace.py
+22
-0
No files found.
.travis.yml
View file @
e04bb2d5
language
:
"
python"
python
:
-
"
2.7"
-
"
3.5"
sudo
:
false
cache
:
pip
before_script
:
install
:
-
pip install flake8
before_script
:
-
flake8 --version
script
:
-
flake8 .
-
cd examples && flake8 .
...
...
examples/DoReFa-Net/alexnet-dorefa.py
View file @
e04bb2d5
...
...
@@ -15,6 +15,7 @@ import sys
from
tensorpack
import
*
from
tensorpack.tfutils.symbolic_functions
import
*
from
tensorpack.tfutils.summary
import
*
from
tensorpack.tfutils.varreplace
import
replace_get_variable
from
dorefa
import
get_dorefa
"""
...
...
@@ -82,9 +83,10 @@ class Model(ModelDesc):
image
=
image
/
255.0
fw
,
fa
,
fg
=
get_dorefa
(
BITW
,
BITA
,
BITG
)
# monkey-patch tf.get_variable to apply fw
old_get_variable
=
tf
.
get_variable
# monkey-patch tf.get_variable to apply fw
def
new_get_variable
(
name
,
shape
=
None
,
**
kwargs
):
v
=
old_get_variable
(
name
,
shape
,
**
kwargs
)
# don't binarize first and last layer
...
...
@@ -93,7 +95,6 @@ class Model(ModelDesc):
else
:
logger
.
info
(
"Binarizing weight {}"
.
format
(
v
.
op
.
name
))
return
fw
(
v
)
tf
.
get_variable
=
new_get_variable
def
nonlin
(
x
):
if
BITA
==
32
:
...
...
@@ -103,7 +104,8 @@ class Model(ModelDesc):
def
activate
(
x
):
return
fa
(
nonlin
(
x
))
with
argscope
(
BatchNorm
,
decay
=
0.9
,
epsilon
=
1e-4
),
\
with
replace_get_variable
(
new_get_variable
),
\
argscope
(
BatchNorm
,
decay
=
0.9
,
epsilon
=
1e-4
),
\
argscope
([
Conv2D
,
FullyConnected
],
use_bias
=
False
,
nl
=
tf
.
identity
):
logits
=
(
LinearWrap
(
image
)
.
Conv2D
(
'conv0'
,
96
,
12
,
stride
=
4
,
padding
=
'VALID'
)
...
...
@@ -141,7 +143,6 @@ class Model(ModelDesc):
.
BatchNorm
(
'bnfc1'
)
.
apply
(
nonlin
)
.
FullyConnected
(
'fct'
,
1000
,
use_bias
=
True
)())
tf
.
get_variable
=
old_get_variable
prob
=
tf
.
nn
.
softmax
(
logits
,
name
=
'output'
)
...
...
examples/DoReFa-Net/svhn-digit-dorefa.py
View file @
e04bb2d5
...
...
@@ -11,6 +11,7 @@ import os
from
tensorpack
import
*
from
tensorpack.tfutils.symbolic_functions
import
*
from
tensorpack.tfutils.summary
import
*
from
tensorpack.tfutils.varreplace
import
replace_get_variable
from
dorefa
import
get_dorefa
"""
...
...
@@ -52,9 +53,10 @@ class Model(ModelDesc):
is_training
=
get_current_tower_context
()
.
is_training
fw
,
fa
,
fg
=
get_dorefa
(
BITW
,
BITA
,
BITG
)
# monkey-patch tf.get_variable to apply fw
old_get_variable
=
tf
.
get_variable
# monkey-patch tf.get_variable to apply fw
def
new_get_variable
(
name
,
shape
=
None
,
**
kwargs
):
v
=
old_get_variable
(
name
,
shape
,
**
kwargs
)
# don't binarize first and last layer
...
...
@@ -63,7 +65,6 @@ class Model(ModelDesc):
else
:
logger
.
info
(
"Binarizing weight {}"
.
format
(
v
.
op
.
name
))
return
fw
(
v
)
tf
.
get_variable
=
new_get_variable
def
cabs
(
x
):
return
tf
.
minimum
(
1.0
,
tf
.
abs
(
x
),
name
=
'cabs'
)
...
...
@@ -73,7 +74,8 @@ class Model(ModelDesc):
image
=
image
/
256.0
with
argscope
(
BatchNorm
,
decay
=
0.9
,
epsilon
=
1e-4
),
\
with
replace_get_variable
(
new_get_variable
),
\
argscope
(
BatchNorm
,
decay
=
0.9
,
epsilon
=
1e-4
),
\
argscope
(
Conv2D
,
use_bias
=
False
,
nl
=
tf
.
identity
):
logits
=
(
LinearWrap
(
image
)
.
Conv2D
(
'conv0'
,
48
,
5
,
padding
=
'VALID'
,
use_bias
=
True
)
...
...
@@ -108,7 +110,6 @@ class Model(ModelDesc):
.
apply
(
fg
)
.
BatchNorm
(
'bn6'
)
.
apply
(
cabs
)
.
FullyConnected
(
'fc1'
,
10
,
nl
=
tf
.
identity
)())
tf
.
get_variable
=
old_get_variable
prob
=
tf
.
nn
.
softmax
(
logits
,
name
=
'output'
)
# compute the number of failed samples
...
...
tensorpack/models/__init__.py
View file @
e04bb2d5
...
...
@@ -7,6 +7,8 @@ from types import ModuleType
import
six
import
os
import
os.path
# this line is necessary for TFModuleFunc to work
import
tensorflow
as
tf
# noqa: F401
from
..utils
import
logger
__all__
=
[
'LinearWrap'
]
...
...
tensorpack/tfutils/varreplace.py
0 → 100644
View file @
e04bb2d5
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File: varreplace.py
# Credit: Qinyao He
import
tensorflow
as
tf
from
tensorflow.python.ops
import
variable_scope
from
contextlib
import
contextmanager
__all__
=
[
'replace_get_variable'
]
@
contextmanager
def
replace_get_variable
(
fn
):
old_getv
=
tf
.
get_variable
old_vars_getv
=
variable_scope
.
get_variable
tf
.
get_variable
=
fn
variable_scope
.
get_variable
=
fn
yield
tf
.
get_variable
=
old_getv
variable_scope
.
get_variable
=
old_vars_getv
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