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
eedec322
You need to sign in or sign up before continuing.
Commit
eedec322
authored
Jan 27, 2016
by
Yuxin Wu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
sampling
parent
efabaaa4
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
68 additions
and
11 deletions
+68
-11
tensorpack/dataflow/base.py
tensorpack/dataflow/base.py
+1
-1
tensorpack/models/_test.py
tensorpack/models/_test.py
+0
-1
tensorpack/models/image_sample.py
tensorpack/models/image_sample.py
+62
-9
tensorpack/train.py
tensorpack/train.py
+5
-0
No files found.
tensorpack/dataflow/base.py
View file @
eedec322
...
...
@@ -17,10 +17,10 @@ class DataFlow(object):
A generator to generate data as tuple.
"""
@
abstractmethod
def
size
(
self
):
"""
Size of this data flow.
"""
raise
NotImplementedError
()
tensorpack/models/_test.py
View file @
eedec322
...
...
@@ -30,7 +30,6 @@ def run_test_case(case):
if
__name__
==
'__main__'
:
import
tensorpack
subs
=
tensorpack
.
models
.
_test
.
TestModel
.
__subclasses__
()
print
subs
for
cls
in
subs
:
run_test_case
(
cls
)
...
...
tensorpack/models/image_sample.py
View file @
eedec322
...
...
@@ -39,20 +39,41 @@ def sample(img, coords):
return
sampled
@
layer_register
()
def
ImageSample
(
template
,
mapping
,
interpolate
):
def
ImageSample
(
template
,
mapping
):
"""
Sample
an image from template, using the given coordinate
Sample
the template image, using the given coordinate, by bilinear interpolation.
template: bxhxwxc
mapping: bxh2xw2x2 (y, x) real-value coordinates
interpolate: 'nearest'
Return: bxh2xw2xc
"""
mapping
=
tf
.
maximum
(
mapping
,
0.0
)
tf
.
check_numerics
(
mapping
,
"mapping"
)
lcoor
=
tf
.
cast
(
mapping
,
tf
.
int32
)
# floor
ucoor
=
lcoor
+
1
if
interpolate
==
'nearest'
:
mapping
=
tf
.
cast
(
tf
.
floor
(
mapping
+
0.5
),
tf
.
int32
)
return
sample
(
template
,
mapping
)
else
:
raise
NotImplementedError
()
# has to cast to int32 and then cast back
# XXX tf.floor have gradient 1 w.r.t input, bug or feature?
diff
=
mapping
-
tf
.
cast
(
lcoor
,
tf
.
float32
)
neg_diff
=
1.0
-
diff
#bxh2xw2x2
lcoory
,
lcoorx
=
tf
.
split
(
3
,
2
,
lcoor
)
ucoory
,
ucoorx
=
tf
.
split
(
3
,
2
,
ucoor
)
lyux
=
tf
.
concat
(
3
,
[
lcoory
,
ucoorx
])
uylx
=
tf
.
concat
(
3
,
[
ucoory
,
lcoorx
])
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
)
return
sample
(
template
,
lcoor
)
*
neg_diffx
*
neg_diffy
+
\
sample
(
template
,
ucoor
)
*
diffx
*
diffy
+
\
sample
(
template
,
lyux
)
*
neg_diffy
*
diffx
+
\
sample
(
template
,
uylx
)
*
diffy
*
neg_diffx
from
_test
import
TestModel
class
TestSample
(
TestModel
):
...
...
@@ -85,7 +106,39 @@ class TestSample(TestModel):
true_res
=
np_sample
(
bimg
,
np
.
floor
(
mat
+
0.5
)
.
astype
(
'int32'
))
inp
,
mapping
=
self
.
make_variable
(
bimg
,
mat
)
output
=
ImageSample
(
'sample'
,
inp
,
mapping
,
'nearest'
)
output
=
sample
(
inp
,
tf
.
cast
(
tf
.
floor
(
mapping
+
0.5
),
tf
.
int32
)
)
res
=
self
.
run_variable
(
output
)
self
.
assertTrue
((
res
==
true_res
)
.
all
())
if
__name__
==
'__main__'
:
import
cv2
import
numpy
as
np
import
sys
im
=
cv2
.
imread
(
'cat.jpg'
)
im
=
im
.
reshape
((
1
,)
+
im
.
shape
)
.
astype
(
'float32'
)
imv
=
tf
.
Variable
(
im
)
h
,
w
=
300
,
400
mapping
=
np
.
zeros
((
1
,
h
,
w
,
2
),
dtype
=
'float32'
)
diff
=
2000
for
x
in
range
(
w
):
for
y
in
range
(
h
):
mapping
[
0
,
y
,
x
,:]
=
np
.
array
([
y
-
diff
+
0.4
,
x
-
diff
+
0.5
])
mapv
=
tf
.
Variable
(
mapping
)
output
=
ImageSample
(
'sample'
,
imv
,
mapv
)
sess
=
tf
.
Session
()
sess
.
run
(
tf
.
initialize_all_variables
())
out
=
sess
.
run
(
tf
.
gradients
(
tf
.
reduce_sum
(
output
),
mapv
))
#out = sess.run(output)
print
out
[
0
]
.
min
()
print
out
[
0
]
.
max
()
print
out
[
0
]
.
sum
()
out
=
sess
.
run
([
output
])[
0
]
im
=
out
[
0
]
cv2
.
imwrite
(
'sampled.jpg'
,
im
)
tensorpack/train.py
View file @
eedec322
...
...
@@ -87,6 +87,10 @@ def summary_grads(grads):
if
grad
:
tf
.
histogram_summary
(
var
.
op
.
name
+
'/gradients'
,
grad
)
def
check_grads
(
grads
):
for
grad
,
var
in
grads
:
tf
.
Assert
(
tf
.
reduce_all
(
tf
.
is_finite
(
var
)),
[
var
])
def
start_train
(
config
):
"""
Start training with the given config
...
...
@@ -144,6 +148,7 @@ def start_train(config):
cost_var
=
config
.
get_model_func
(
model_inputs
,
is_training
=
True
)
grads
=
config
.
optimizer
.
compute_gradients
(
cost_var
)
summary_grads
(
grads
)
check_grads
(
grads
)
avg_maintain_op
=
summary_moving_average
(
cost_var
)
train_op
=
tf
.
group
(
...
...
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