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
cf2012dd
Commit
cf2012dd
authored
Jul 27, 2017
by
Yuxin Wu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
better EMA scope under existing name_scope.
parent
7c6d9b4b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
33 deletions
+37
-33
examples/GAN/CycleGAN.py
examples/GAN/CycleGAN.py
+33
-31
tensorpack/tfutils/summary.py
tensorpack/tfutils/summary.py
+4
-2
No files found.
examples/GAN/CycleGAN.py
View file @
cf2012dd
...
...
@@ -86,15 +86,17 @@ class Model(GANModelDesc):
def
_build_graph
(
self
,
inputs
):
A
,
B
=
inputs
A
=
tf
.
transpose
(
A
/
128.0
-
1.0
,
[
0
,
3
,
1
,
2
])
B
=
tf
.
transpose
(
B
/
128.0
-
1.0
,
[
0
,
3
,
1
,
2
])
with
tf
.
name_scope
(
'preprocess'
):
A
=
tf
.
transpose
(
A
/
128.0
-
1.0
,
[
0
,
3
,
1
,
2
])
B
=
tf
.
transpose
(
B
/
128.0
-
1.0
,
[
0
,
3
,
1
,
2
])
def
viz3
(
name
,
a
,
b
,
c
):
im
=
tf
.
concat
([
a
,
b
,
c
],
axis
=
3
)
im
=
tf
.
transpose
(
im
,
[
0
,
2
,
3
,
1
])
im
=
(
im
+
1.0
)
*
128
im
=
tf
.
clip_by_value
(
im
,
0
,
255
)
im
=
tf
.
cast
(
im
,
tf
.
uint8
,
name
=
'viz_'
+
name
)
with
tf
.
name_scope
(
name
):
im
=
tf
.
concat
([
a
,
b
,
c
],
axis
=
3
)
im
=
tf
.
transpose
(
im
,
[
0
,
2
,
3
,
1
])
im
=
(
im
+
1.0
)
*
128
im
=
tf
.
clip_by_value
(
im
,
0
,
255
)
im
=
tf
.
cast
(
im
,
tf
.
uint8
,
name
=
'viz'
)
tf
.
summary
.
image
(
name
,
im
,
max_outputs
=
50
)
# use the initializers from torch
...
...
@@ -124,29 +126,29 @@ class Model(GANModelDesc):
B_dis_fake
=
self
.
discriminator
(
AB
)
def
LSGAN_losses
(
real
,
fake
):
with
tf
.
name_scope
(
'LSGAN_losses'
):
d_real
=
tf
.
reduce_mean
(
tf
.
squared_difference
(
real
,
0.9
),
name
=
'd_real
'
)
d_fake
=
tf
.
reduce_mean
(
tf
.
square
(
fake
),
name
=
'd_fake
'
)
d_loss
=
tf
.
multiply
(
d_real
+
d_fake
,
0.5
,
name
=
'd_loss'
)
g_loss
=
tf
.
reduce_mean
(
tf
.
squared_difference
(
fake
,
0.9
),
name
=
'g_loss'
)
add_moving_summary
(
g_loss
,
d_loss
)
return
g_loss
,
d_loss
with
tf
.
name_scope
(
'LossA'
):
# reconstruction loss
recon_loss_A
=
tf
.
reduce_mean
(
tf
.
abs
(
A
-
ABA
),
name
=
'recon_loss'
)
# gan loss
G_loss_A
,
D_loss_A
=
LSGAN_losses
(
A_dis_real
,
A_dis_fake
)
with
tf
.
name_scope
(
'LossB'
):
recon_loss_B
=
tf
.
reduce_mean
(
tf
.
abs
(
B
-
BAB
),
name
=
'recon_loss'
)
G_loss_B
,
D_loss_B
=
LSGAN_losses
(
B_dis_real
,
B_dis_fake
)
LAMBDA
=
10.0
self
.
g_loss
=
tf
.
add
((
G_loss_A
+
G_loss_B
),
(
recon_loss_A
+
recon_loss_B
)
*
LAMBDA
,
name
=
'G_loss_total'
)
self
.
d_loss
=
tf
.
add
(
D_loss_A
,
D_loss_B
,
name
=
'D_loss_total'
)
d_real
=
tf
.
reduce_mean
(
tf
.
squared_difference
(
real
,
0.9
),
name
=
'd_real'
)
d_fake
=
tf
.
reduce_mean
(
tf
.
square
(
fake
),
name
=
'd_fake
'
)
d_loss
=
tf
.
multiply
(
d_real
+
d_fake
,
0.5
,
name
=
'd_loss
'
)
g_loss
=
tf
.
reduce_mean
(
tf
.
squared_difference
(
fake
,
0.9
),
name
=
'g_loss'
)
add_moving_summary
(
g_loss
,
d_loss
)
return
g_loss
,
d_loss
with
tf
.
name_scope
(
'losses'
):
with
tf
.
name_scope
(
'LossA'
):
# reconstruction loss
recon_loss_A
=
tf
.
reduce_mean
(
tf
.
abs
(
A
-
ABA
),
name
=
'recon_loss'
)
# gan loss
G_loss_A
,
D_loss_A
=
LSGAN_losses
(
A_dis_real
,
A_dis_fake
)
with
tf
.
name_scope
(
'LossB'
):
recon_loss_B
=
tf
.
reduce_mean
(
tf
.
abs
(
B
-
BAB
),
name
=
'recon_loss'
)
G_loss_B
,
D_loss_B
=
LSGAN_losses
(
B_dis_real
,
B_dis_fake
)
LAMBDA
=
10.0
self
.
g_loss
=
tf
.
add
((
G_loss_A
+
G_loss_B
),
(
recon_loss_A
+
recon_loss_B
)
*
LAMBDA
,
name
=
'G_loss_total'
)
self
.
d_loss
=
tf
.
add
(
D_loss_A
,
D_loss_B
,
name
=
'D_loss_total'
)
self
.
collect_variables
(
'gen'
,
'discrim'
)
add_moving_summary
(
recon_loss_A
,
recon_loss_B
,
self
.
g_loss
,
self
.
d_loss
)
...
...
@@ -183,7 +185,7 @@ def get_data(datadir, isTrain=True):
class
VisualizeTestSet
(
Callback
):
def
_setup_graph
(
self
):
self
.
pred
=
self
.
trainer
.
get_predictor
(
[
'inputA'
,
'inputB'
],
[
'
viz_A_recon'
,
'viz_B_recon
'
])
[
'inputA'
,
'inputB'
],
[
'
A_recon/viz'
,
'B_recon/viz
'
])
def
_before_train
(
self
):
global
args
...
...
tensorpack/tfutils/summary.py
View file @
cf2012dd
...
...
@@ -179,11 +179,13 @@ def add_moving_summary(v, *args, **kwargs):
ema_var
=
tf
.
get_variable
(
name
,
shape
=
c
.
shape
,
dtype
=
c
.
dtype
,
initializer
=
tf
.
constant_initializer
(),
trainable
=
False
)
ns
=
vs
.
original_name_scope
with
tf
.
name_scope
(
ns
):
# first clear NS to avoid duplicated name in variables
with
tf
.
name_scope
(
None
),
tf
.
name_scope
(
ns
):
ema_op
=
moving_averages
.
assign_moving_average
(
ema_var
,
c
,
decay
,
zero_debias
=
True
,
name
=
name
+
'_EMA_apply'
)
tf
.
summary
.
scalar
(
name
+
'-summary'
,
ema_op
)
with
tf
.
name_scope
(
None
):
tf
.
summary
.
scalar
(
name
+
'-summary'
,
ema_op
)
tf
.
add_to_collection
(
coll
,
ema_op
)
# TODO a new collection to summary every step?
...
...
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