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
fc81be3f
Commit
fc81be3f
authored
Oct 23, 2016
by
Yuxin Wu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
some small cleanups
parent
e6595ef9
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
39 additions
and
43 deletions
+39
-43
examples/ResNet/load-resnet.py
examples/ResNet/load-resnet.py
+27
-33
examples/load-alexnet.py
examples/load-alexnet.py
+2
-4
examples/load-vgg16.py
examples/load-vgg16.py
+3
-4
scripts/plot-point.py
scripts/plot-point.py
+1
-1
tensorpack/RL/gymenv.py
tensorpack/RL/gymenv.py
+5
-1
tensorpack/models/image_sample.py
tensorpack/models/image_sample.py
+1
-0
No files found.
examples/ResNet/load-resnet.py
View file @
fc81be3f
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# File: load-
ResN
et.py
# File: load-
resn
et.py
# Author: Eric Yujia Huang yujiah1@andrew.cmu.edu
# Yuxin Wu <ppwwyyxx@gmail.com>
#
#
import
cv2
import
tensorflow
as
tf
import
argparse
import
os
,
re
import
numpy
as
np
from
six.moves
import
zip
from
tensorflow.contrib.layers
import
variance_scaling_initializer
...
...
@@ -19,19 +20,17 @@ from tensorpack.dataflow.dataset import ILSVRCMeta
"""
Usage:
python
2
-m tensorpack.utils.loadcaffe PATH/TO/CAFFE/{ResNet-101-deploy.prototxt,ResNet-101-model.caffemodel} ResNet101.npy
./load-
alex
net.py --load ResNet-101.npy --input cat.png --depth 101
python -m tensorpack.utils.loadcaffe PATH/TO/CAFFE/{ResNet-101-deploy.prototxt,ResNet-101-model.caffemodel} ResNet101.npy
./load-
res
net.py --load ResNet-101.npy --input cat.png --depth 101
"""
MODEL_DEPTH
=
None
class
Model
(
ModelDesc
):
def
_get_input_vars
(
self
):
return
[
InputVar
(
tf
.
float32
,
[
None
,
224
,
224
,
3
],
'input'
),
InputVar
(
tf
.
int32
,
[
None
],
'label'
)]
return
[
InputVar
(
tf
.
float32
,
[
None
,
224
,
224
,
3
],
'input'
)]
def
_build_graph
(
self
,
input_vars
):
image
,
label
=
input_vars
image
=
input_vars
[
0
]
def
caffe_shortcut
(
l
,
n_in
,
n_out
,
stride
):
if
n_in
!=
n_out
:
...
...
@@ -77,7 +76,7 @@ class Model(ModelDesc):
with
argscope
(
Conv2D
,
nl
=
tf
.
identity
,
use_bias
=
False
,
W_init
=
variance_scaling_initializer
(
mode
=
'FAN_OUT'
)):
fc1000l
=
(
LinearWrap
(
image
)
.
Conv2D
(
'conv0'
,
64
,
7
,
stride
=
2
,
nl
=
BNReLU
)
.
Conv2D
(
'conv0'
,
64
,
7
,
stride
=
2
,
nl
=
BNReLU
)
.
MaxPooling
(
'pool0'
,
shape
=
3
,
stride
=
2
,
padding
=
'SAME'
)
.
apply
(
layer
,
'group0'
,
block_func
,
64
,
defs
[
0
],
1
,
first
=
True
)
.
apply
(
layer
,
'group1'
,
block_func
,
128
,
defs
[
1
],
2
)
...
...
@@ -88,8 +87,6 @@ class Model(ModelDesc):
.
FullyConnected
(
'fc1000'
,
1000
,
nl
=
tf
.
identity
)())
prob
=
tf
.
nn
.
softmax
(
fc1000l
,
name
=
'prob_output'
)
def
run_test
(
path
,
input
):
image_mean
=
np
.
array
([
0.485
,
0.456
,
0.406
],
dtype
=
'float32'
)
...
...
@@ -100,8 +97,7 @@ def run_test(path, input):
model
=
Model
(),
input_var_names
=
[
'input'
],
session_init
=
ParamRestore
(
resNet_param
),
session_config
=
get_default_sess_config
(
0.9
),
output_var_names
=
[
'prob_output'
]
# output:0 is the probability distribution
output_var_names
=
[
'prob_output'
]
)
predict_func
=
get_predict_func
(
pred_config
)
...
...
@@ -120,20 +116,18 @@ def run_test(path, input):
def
caffeResNet2tensorpackResNet
(
caffe_layer_name
):
import
re
map
=
dict
()
#begining & ending stage
map
[
'conv1/W'
]
=
'conv0/W'
map
[
'conv1/b'
]
=
'conv0/b'
map
[
'bn_conv1/beta'
]
=
'conv0/bn/beta'
map
[
'bn_conv1/gamma'
]
=
'conv0/bn/gamma'
map
[
'bn_conv1/mean/EMA'
]
=
'conv0/bn/mean/EMA'
map
[
'bn_conv1/variance/EMA'
]
=
'conv0/bn/variance/EMA'
map
[
'fc1000/W'
]
=
'fc1000/W'
map
[
'fc1000/b'
]
=
'fc1000/b'
if
map
.
get
(
caffe_layer_name
)
!=
None
:
print
(
caffe_layer_name
+
' --> '
+
map
[
caffe_layer_name
])
return
map
[
caffe_layer_name
]
# begining & ending stage
name_map
=
{
'bn_conv1/beta'
:
'conv0/bn/beta'
,
'bn_conv1/gamma'
:
'conv0/bn/gamma'
,
'bn_conv1/mean/EMA'
:
'conv0/bn/mean/EMA'
,
'bn_conv1/variance/EMA'
:
'conv0/bn/variance/EMA'
,
'conv1/W'
:
'conv0/W'
,
'conv1/b'
:
'conv0/b'
,
'fc1000/W'
:
'fc1000/W'
,
'fc1000/b'
:
'fc1000/b'
}
if
caffe_layer_
in
name_map
:
print
(
caffe_layer_name
+
' --> '
+
name_map
[
caffe_layer_name
])
return
name_map
[
caffe_layer_name
]
print
(
caffe_layer_name
)
...
...
@@ -151,7 +145,7 @@ def caffeResNet2tensorpackResNet(caffe_layer_name):
if
s
.
group
(
0
)
==
caffe_layer_name
[
0
:
caffe_layer_name
.
index
(
'/'
)]:
layer_type
=
s
.
group
(
1
)
layer_group
=
s
.
group
(
2
)
layer_block
=
ord
(
s
.
group
(
3
))
-
ord
(
'a'
)
layer_block
=
ord
(
s
.
group
(
3
))
-
ord
(
'a'
)
layer_branch
=
s
.
group
(
4
)
else
:
# print('s group ' + s.group(0))
...
...
@@ -172,13 +166,13 @@ def caffeResNet2tensorpackResNet(caffe_layer_name):
else
:
print
(
'model block error!'
)
layer_branch
=
s
.
group
(
5
)
layer_branch
=
s
.
group
(
5
)
if
s
.
group
(
0
)
!=
caffe_layer_name
[
0
:
caffe_layer_name
.
index
(
'/'
)]:
print
(
'model depth error!'
)
# error handling
# TODO error handling
type_dict
=
{
'res'
:
'/conv'
,
'bn'
:
'/bn'
,
'scale'
:
'/bn'
}
shortcut_dict
=
{
'res'
:
'/convshortcut'
,
'bn'
:
'/bnshortcut'
,
'scale'
:
'/bnshortcut'
}
...
...
@@ -194,7 +188,7 @@ def caffeResNet2tensorpackResNet(caffe_layer_name):
shortcut_dict
[
layer_type
]
+
tf_name
else
:
print
(
'renaming error!'
)
# error handling
#
TODO
error handling
print
(
caffe_layer_name
+
' --> '
+
tf_name
)
return
tf_name
...
...
@@ -212,4 +206,4 @@ if __name__ == '__main__':
os
.
environ
[
'CUDA_VISIBLE_DEVICES'
]
=
args
.
gpu
# run resNet with given model (in npy format)
MODEL_DEPTH
=
args
.
depth
run_test
(
args
.
load
,
args
.
input
)
\ No newline at end of file
run_test
(
args
.
load
,
args
.
input
)
examples/load-alexnet.py
View file @
fc81be3f
...
...
@@ -21,13 +21,11 @@ Usage:
class
Model
(
ModelDesc
):
def
_get_input_vars
(
self
):
return
[
InputVar
(
tf
.
float32
,
(
None
,
227
,
227
,
3
),
'input'
),
InputVar
(
tf
.
int32
,
(
None
,),
'label'
)
]
return
[
InputVar
(
tf
.
float32
,
(
None
,
227
,
227
,
3
),
'input'
)
]
def
_build_graph
(
self
,
inputs
):
# img: 227x227x3
image
,
label
=
inputs
image
=
inputs
[
0
]
with
argscope
([
Conv2D
,
FullyConnected
],
nl
=
tf
.
nn
.
relu
):
l
=
Conv2D
(
'conv1'
,
image
,
out_channel
=
96
,
kernel_shape
=
11
,
stride
=
4
,
padding
=
'VALID'
)
...
...
examples/load-vgg16.py
View file @
fc81be3f
...
...
@@ -29,12 +29,11 @@ Usage:
class
Model
(
ModelDesc
):
def
_get_input_vars
(
self
):
return
[
InputVar
(
tf
.
float32
,
(
None
,
224
,
224
,
3
),
'input'
),
InputVar
(
tf
.
int32
,
(
None
,),
'label'
)
]
return
[
InputVar
(
tf
.
float32
,
(
None
,
224
,
224
,
3
),
'input'
)
]
def
_build_graph
(
self
,
inputs
,
is_training
):
def
_build_graph
(
self
,
inputs
):
image
,
label
=
inputs
image
=
inputs
[
0
]
with
argscope
(
Conv2D
,
kernel_shape
=
3
,
nl
=
tf
.
nn
.
relu
):
# 224
...
...
scripts/plot-point.py
View file @
fc81be3f
...
...
@@ -287,7 +287,7 @@ Line: {}""".format(repr(args.delimeter), line)
data_ys
=
[
data
[
k
]
for
k
in
args
.
y_column_idx
]
max_ysize
=
max
([
len
(
t
)
for
t
in
data_ys
])
print
"Size of the longest y column: "
,
max_ysize
print
(
"Size of the longest y column: "
,
max_ysize
)
if
nr_x_column
:
data_xs
=
[
data
[
k
]
for
k
in
args
.
x_column_idx
]
...
...
tensorpack/RL/gymenv.py
View file @
fc81be3f
...
...
@@ -8,6 +8,9 @@ import time
from
..utils
import
logger
try
:
import
gym
gym
.
undo_logger_setup
()
# https://github.com/openai/gym/pull/199
# not sure does it cause other problems
except
ImportError
:
logger
.
warn
(
"Cannot import gym. GymEnv won't be available."
)
...
...
@@ -23,7 +26,7 @@ _ALE_LOCK = threading.Lock()
class
GymEnv
(
RLEnvironment
):
"""
An OpenAI/gym wrapper.
Will
auto restart.
An OpenAI/gym wrapper.
Can optionally
auto restart.
"""
def
__init__
(
self
,
name
,
dumpdir
=
None
,
viz
=
False
,
auto_restart
=
True
):
with
_ALE_LOCK
:
...
...
@@ -43,6 +46,7 @@ class GymEnv(RLEnvironment):
self
.
_ob
=
self
.
gymenv
.
reset
()
def
finish_episode
(
self
):
self
.
gymenv
.
monitor
.
flush
()
self
.
stats
[
'score'
]
.
append
(
self
.
rwd_counter
.
sum
)
def
current_state
(
self
):
...
...
tensorpack/models/image_sample.py
View file @
fc81be3f
...
...
@@ -54,6 +54,7 @@ def ImageSample(inputs, borderMode='repeat'):
:param borderMode: either 'repeat' or 'constant' (0)
:returns: a NHWC output tensor.
"""
# TODO borderValue
template
,
mapping
=
inputs
assert
template
.
get_shape
()
.
ndims
==
4
and
mapping
.
get_shape
()
.
ndims
==
4
input_shape
=
template
.
get_shape
()
.
as_list
()[
1
:]
...
...
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