Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
bob.learn.tensorflow
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
11
Issues
11
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bob
bob.learn.tensorflow
Commits
1db6d460
Commit
1db6d460
authored
Oct 21, 2016
by
Tiago de Freitas Pereira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Debuging the triplet trainer
parent
4afda035
Changes
13
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
69 additions
and
42 deletions
+69
-42
bob/learn/tensorflow/datashuffler/__init__.py
bob/learn/tensorflow/datashuffler/__init__.py
+1
-0
bob/learn/tensorflow/initialization/Constant.py
bob/learn/tensorflow/initialization/Constant.py
+3
-3
bob/learn/tensorflow/initialization/Gaussian.py
bob/learn/tensorflow/initialization/Gaussian.py
+3
-3
bob/learn/tensorflow/initialization/Initialization.py
bob/learn/tensorflow/initialization/Initialization.py
+1
-1
bob/learn/tensorflow/initialization/SimplerXavier.py
bob/learn/tensorflow/initialization/SimplerXavier.py
+3
-3
bob/learn/tensorflow/initialization/Xavier.py
bob/learn/tensorflow/initialization/Xavier.py
+3
-3
bob/learn/tensorflow/layers/Conv2D.py
bob/learn/tensorflow/layers/Conv2D.py
+11
-4
bob/learn/tensorflow/layers/FullyConnected.py
bob/learn/tensorflow/layers/FullyConnected.py
+6
-2
bob/learn/tensorflow/network/__init__.py
bob/learn/tensorflow/network/__init__.py
+1
-0
bob/learn/tensorflow/script/train_mnist_triplet.py
bob/learn/tensorflow/script/train_mnist_triplet.py
+3
-2
bob/learn/tensorflow/script/train_mobio.py
bob/learn/tensorflow/script/train_mobio.py
+21
-14
bob/learn/tensorflow/script/train_siamese_casia_webface.py
bob/learn/tensorflow/script/train_siamese_casia_webface.py
+11
-6
bob/learn/tensorflow/trainers/Trainer.py
bob/learn/tensorflow/trainers/Trainer.py
+2
-1
No files found.
bob/learn/tensorflow/datashuffler/__init__.py
View file @
1db6d460
...
...
@@ -10,6 +10,7 @@ from .Disk import Disk
from
.SiameseMemory
import
SiameseMemory
from
.TripletMemory
import
TripletMemory
from
.TripletWithSelectionMemory
import
TripletWithSelectionMemory
from
.TripletWithFastSelectionDisk
import
TripletWithFastSelectionDisk
from
.SiameseDisk
import
SiameseDisk
from
.TripletDisk
import
TripletDisk
...
...
bob/learn/tensorflow/initialization/Constant.py
View file @
1db6d460
...
...
@@ -27,11 +27,11 @@ class Constant(Initialization):
self
.
constant_value
=
constant_value
super
(
Constant
,
self
).
__init__
(
seed
=
None
,
use_gpu
=
use_gpu
)
def
__call__
(
self
,
shape
,
name
):
def
__call__
(
self
,
shape
,
name
,
scope
):
initializer
=
tf
.
constant
(
self
.
constant_value
,
shape
=
shape
)
try
:
with
tf
.
variable_scope
(
nam
e
):
with
tf
.
variable_scope
(
scop
e
):
if
self
.
use_gpu
:
with
tf
.
device
(
"/gpu:0"
):
return
tf
.
get_variable
(
name
,
initializer
=
initializer
,
dtype
=
tf
.
float32
)
...
...
@@ -39,7 +39,7 @@ class Constant(Initialization):
with
tf
.
device
(
"/cpu"
):
return
tf
.
get_variable
(
name
,
initializer
=
initializer
,
dtype
=
tf
.
float32
)
except
ValueError
:
with
tf
.
variable_scope
(
nam
e
,
reuse
=
True
):
with
tf
.
variable_scope
(
scop
e
,
reuse
=
True
):
if
self
.
use_gpu
:
with
tf
.
device
(
"/gpu:0"
):
return
tf
.
get_variable
(
name
,
initializer
=
initializer
,
dtype
=
tf
.
float32
)
...
...
bob/learn/tensorflow/initialization/Gaussian.py
View file @
1db6d460
...
...
@@ -24,7 +24,7 @@ class Gaussian(Initialization):
self
.
std
=
std
super
(
Gaussian
,
self
).
__init__
(
seed
,
use_gpu
=
use_gpu
)
def
__call__
(
self
,
shape
,
name
):
def
__call__
(
self
,
shape
,
name
,
scope
):
if
len
(
shape
)
==
4
:
in_out
=
shape
[
0
]
*
shape
[
1
]
*
shape
[
2
]
+
shape
[
3
]
...
...
@@ -37,7 +37,7 @@ class Gaussian(Initialization):
seed
=
self
.
seed
)
try
:
with
tf
.
variable_scope
(
nam
e
):
with
tf
.
variable_scope
(
scop
e
):
if
self
.
use_gpu
:
with
tf
.
device
(
"/gpu:0"
):
return
tf
.
get_variable
(
name
,
initializer
=
initializer
,
dtype
=
tf
.
float32
)
...
...
@@ -46,7 +46,7 @@ class Gaussian(Initialization):
return
tf
.
get_variable
(
name
,
initializer
=
initializer
,
dtype
=
tf
.
float32
)
except
ValueError
:
with
tf
.
variable_scope
(
nam
e
,
reuse
=
True
):
with
tf
.
variable_scope
(
scop
e
,
reuse
=
True
):
if
self
.
use_gpu
:
with
tf
.
device
(
"/gpu:0"
):
return
tf
.
get_variable
(
name
,
initializer
=
initializer
,
dtype
=
tf
.
float32
)
...
...
bob/learn/tensorflow/initialization/Initialization.py
View file @
1db6d460
...
...
@@ -26,5 +26,5 @@ class Initialization(object):
self
.
seed
=
seed
self
.
use_gpu
=
use_gpu
def
__call__
(
self
,
shape
,
name
):
def
__call__
(
self
,
shape
,
name
,
scope
):
NotImplementedError
(
"Please implement this function in derived classes"
)
bob/learn/tensorflow/initialization/SimplerXavier.py
View file @
1db6d460
...
...
@@ -26,7 +26,7 @@ class SimplerXavier(Initialization):
def
__init__
(
self
,
seed
=
10.
,
use_gpu
=
False
):
super
(
SimplerXavier
,
self
).
__init__
(
seed
,
use_gpu
=
use_gpu
)
def
__call__
(
self
,
shape
,
name
):
def
__call__
(
self
,
shape
,
name
,
scope
):
if
len
(
shape
)
==
4
:
in_out
=
shape
[
0
]
*
shape
[
1
]
*
shape
[
2
]
...
...
@@ -39,7 +39,7 @@ class SimplerXavier(Initialization):
initializer
=
tf
.
truncated_normal
(
shape
,
stddev
=
stddev
,
seed
=
self
.
seed
)
try
:
with
tf
.
variable_scope
(
nam
e
):
with
tf
.
variable_scope
(
scop
e
):
if
self
.
use_gpu
:
with
tf
.
device
(
"/gpu:0"
):
return
tf
.
get_variable
(
name
,
initializer
=
initializer
,
dtype
=
tf
.
float32
)
...
...
@@ -47,7 +47,7 @@ class SimplerXavier(Initialization):
with
tf
.
device
(
"/cpu"
):
return
tf
.
get_variable
(
name
,
initializer
=
initializer
,
dtype
=
tf
.
float32
)
except
ValueError
:
with
tf
.
variable_scope
(
nam
e
,
reuse
=
True
):
with
tf
.
variable_scope
(
scop
e
,
reuse
=
True
):
if
self
.
use_gpu
:
with
tf
.
device
(
"/gpu:0"
):
return
tf
.
get_variable
(
name
,
initializer
=
initializer
,
dtype
=
tf
.
float32
)
...
...
bob/learn/tensorflow/initialization/Xavier.py
View file @
1db6d460
...
...
@@ -27,7 +27,7 @@ class Xavier(Initialization):
super
(
Xavier
,
self
).
__init__
(
seed
,
use_gpu
=
use_gpu
)
def
__call__
(
self
,
shape
,
name
):
def
__call__
(
self
,
shape
,
name
,
scope
):
if
len
(
shape
)
==
4
:
in_out
=
shape
[
0
]
*
shape
[
1
]
*
shape
[
2
]
+
shape
[
3
]
...
...
@@ -40,7 +40,7 @@ class Xavier(Initialization):
initializer
=
tf
.
truncated_normal
(
shape
,
stddev
=
stddev
,
seed
=
self
.
seed
)
try
:
with
tf
.
variable_scope
(
nam
e
):
with
tf
.
variable_scope
(
scop
e
):
if
self
.
use_gpu
:
with
tf
.
device
(
"/gpu:0"
):
return
tf
.
get_variable
(
name
,
initializer
=
initializer
,
dtype
=
tf
.
float32
)
...
...
@@ -48,7 +48,7 @@ class Xavier(Initialization):
with
tf
.
device
(
"/cpu"
):
return
tf
.
get_variable
(
name
,
initializer
=
initializer
,
dtype
=
tf
.
float32
)
except
ValueError
:
with
tf
.
variable_scope
(
nam
e
,
reuse
=
True
):
with
tf
.
variable_scope
(
scop
e
,
reuse
=
True
):
if
self
.
use_gpu
:
with
tf
.
device
(
"/gpu:0"
):
return
tf
.
get_variable
(
name
,
initializer
=
initializer
,
dtype
=
tf
.
float32
)
...
...
bob/learn/tensorflow/layers/Conv2D.py
View file @
1db6d460
...
...
@@ -18,6 +18,7 @@ class Conv2D(Layer):
def
__init__
(
self
,
name
,
activation
=
None
,
kernel_size
=
3
,
filters
=
8
,
stride
=
[
1
,
1
,
1
,
1
],
weights_initialization
=
Xavier
(),
bias_initialization
=
Constant
(),
use_gpu
=
False
...
...
@@ -38,12 +39,15 @@ class Conv2D(Layer):
activation
=
activation
,
weights_initialization
=
weights_initialization
,
bias_initialization
=
bias_initialization
,
use_gpu
=
use_gpu
use_gpu
=
use_gpu
,
)
self
.
kernel_size
=
kernel_size
self
.
filters
=
filters
self
.
W
=
None
self
.
b
=
None
self
.
stride
=
stride
def
create_variables
(
self
,
input_layer
):
self
.
input_layer
=
input_layer
...
...
@@ -56,16 +60,19 @@ class Conv2D(Layer):
if
self
.
W
is
None
:
self
.
W
=
self
.
weights_initialization
(
shape
=
[
self
.
kernel_size
,
self
.
kernel_size
,
n_channels
,
self
.
filters
],
name
=
"w_"
+
str
(
self
.
name
)
name
=
"w_"
+
str
(
self
.
name
),
scope
=
"w_"
+
str
(
self
.
name
)
)
self
.
b
=
self
.
bias_initialization
(
shape
=
[
self
.
filters
],
name
=
"b_"
+
str
(
self
.
name
)
+
"bias"
)
name
=
"b_"
+
str
(
self
.
name
)
+
"bias"
,
scope
=
"b_"
+
str
(
self
.
name
)
)
def
get_graph
(
self
):
with
tf
.
name_scope
(
str
(
self
.
name
)):
conv2d
=
tf
.
nn
.
conv2d
(
self
.
input_layer
,
self
.
W
,
strides
=
[
1
,
1
,
1
,
1
]
,
padding
=
'SAME'
)
conv2d
=
tf
.
nn
.
conv2d
(
self
.
input_layer
,
self
.
W
,
strides
=
self
.
stride
,
padding
=
'SAME'
)
if
self
.
activation
is
not
None
:
output
=
self
.
activation
(
tf
.
nn
.
bias_add
(
conv2d
,
self
.
b
))
...
...
bob/learn/tensorflow/layers/FullyConnected.py
View file @
1db6d460
...
...
@@ -50,10 +50,14 @@ class FullyConnected(Layer):
if
self
.
W
is
None
:
input_dim
=
reduce
(
mul
,
self
.
input_layer
.
get_shape
().
as_list
()[
1
:])
self
.
W
=
self
.
weights_initialization
(
shape
=
[
input_dim
,
self
.
output_dim
],
name
=
"W_"
+
str
(
self
.
name
))
name
=
"W_"
+
str
(
self
.
name
),
scope
=
"W_"
+
str
(
self
.
name
)
)
# if self.activation is not None:
self
.
b
=
self
.
bias_initialization
(
shape
=
[
self
.
output_dim
],
name
=
"b_"
+
str
(
self
.
name
))
name
=
"b_"
+
str
(
self
.
name
),
scope
=
"b_"
+
str
(
self
.
name
)
)
def
get_graph
(
self
):
...
...
bob/learn/tensorflow/network/__init__.py
View file @
1db6d460
...
...
@@ -9,6 +9,7 @@ from .Dummy import Dummy
from
.VGG
import
VGG
from
.LenetDropout
import
LenetDropout
from
.MLP
import
MLP
from
.FaceNet
import
FaceNet
# gets sphinx autodoc done right - don't remove it
__all__
=
[
_
for
_
in
dir
()
if
not
_
.
startswith
(
'_'
)]
bob/learn/tensorflow/script/train_mnist_triplet.py
View file @
1db6d460
...
...
@@ -23,7 +23,7 @@ import tensorflow as tf
from
..
import
util
SEED
=
10
from
bob.learn.tensorflow.datashuffler
import
TripletMemory
,
TripletWithSelectionMemory
from
bob.learn.tensorflow.network
import
Lenet
,
MLP
,
LenetDropout
,
VGG
,
Chopra
,
Dummy
from
bob.learn.tensorflow.network
import
Lenet
,
MLP
,
LenetDropout
,
VGG
,
Chopra
,
Dummy
,
FaceNet
from
bob.learn.tensorflow.trainers
import
TripletTrainer
from
bob.learn.tensorflow.loss
import
TripletLoss
import
numpy
...
...
@@ -74,7 +74,8 @@ def main():
#n_classes = 200
cnn
=
True
if
cnn
:
architecture
=
Chopra
(
seed
=
SEED
,
fc1_output
=
n_classes
,
use_gpu
=
USE_GPU
)
architecture
=
FaceNet
(
seed
=
SEED
,
use_gpu
=
USE_GPU
)
#architecture = Chopra(seed=SEED, fc1_output=n_classes, use_gpu=USE_GPU)
#architecture = Lenet(default_feature_layer="fc2", n_classes=n_classes, conv1_output=8, conv2_output=16,use_gpu=USE_GPU)
#architecture = VGG(n_classes=n_classes, use_gpu=USE_GPU)
#architecture = Dummy(seed=SEED)
...
...
bob/learn/tensorflow/script/train_mobio.py
View file @
1db6d460
...
...
@@ -22,8 +22,8 @@ from docopt import docopt
import
tensorflow
as
tf
from
..
import
util
SEED
=
10
from
bob.learn.tensorflow.datashuffler
import
TripletWithSelectionDisk
,
TripletDisk
from
bob.learn.tensorflow.network
import
Lenet
,
MLP
,
LenetDropout
,
VGG
,
Chopra
,
Dummy
from
bob.learn.tensorflow.datashuffler
import
TripletWithSelectionDisk
,
TripletDisk
,
TripletWithFastSelectionDisk
from
bob.learn.tensorflow.network
import
Lenet
,
MLP
,
LenetDropout
,
VGG
,
Chopra
,
Dummy
,
FaceNet
from
bob.learn.tensorflow.trainers
import
SiameseTrainer
,
Trainer
,
TripletTrainer
from
bob.learn.tensorflow.loss
import
ContrastiveLoss
,
BaseLoss
,
TripletLoss
import
numpy
...
...
@@ -41,11 +41,11 @@ def main():
import
bob.db.mobio
db_mobio
=
bob
.
db
.
mobio
.
Database
()
directory
=
"/idiap/temp/tpereira/DEEP_FACE/CASIA
/preprocessed
"
directory
=
"/idiap/temp/tpereira/DEEP_FACE/CASIA
_WEBFACE/mobio/preprocessed/
"
# Preparing train set
#train_objects = db_mobio.objects(protocol="male", groups="world")
train_objects
=
db_mobio
.
objects
(
protocol
=
"male"
,
groups
=
"
dev
"
)
train_objects
=
db_mobio
.
objects
(
protocol
=
"male"
,
groups
=
"
world
"
)
train_labels
=
[
int
(
o
.
client_id
)
for
o
in
train_objects
]
n_classes
=
len
(
set
(
train_labels
))
...
...
@@ -53,9 +53,14 @@ def main():
directory
=
directory
,
extension
=
".hdf5"
)
for
o
in
train_objects
]
train_data_shuffler
=
TripletWithSelectionDisk
(
train_file_names
,
train_labels
,
input_shape
=
[
125
,
125
,
3
],
batch_size
=
BATCH_SIZE
)
#train_data_shuffler = TripletWithSelectionDisk(train_file_names, train_labels,
# input_shape=[125, 125, 3],
# batch_size=BATCH_SIZE)
train_data_shuffler
=
TripletWithFastSelectionDisk
(
train_file_names
,
train_labels
,
input_shape
=
[
224
,
224
,
3
],
batch_size
=
BATCH_SIZE
,
total_identities
=
8
)
# Preparing train set
validation_objects
=
db_mobio
.
objects
(
protocol
=
"male"
,
groups
=
"dev"
)
...
...
@@ -67,11 +72,12 @@ def main():
extension
=
".hdf5"
)
for
o
in
validation_objects
]
validation_data_shuffler
=
TripletDisk
(
validation_file_names
,
validation_labels
,
input_shape
=
[
125
,
125
,
3
],
batch_size
=
VALIDATION_BATCH_SIZE
)
input_shape
=
[
224
,
224
,
3
],
batch_size
=
VALIDATION_BATCH_SIZE
)
# Preparing the architecture
#architecture = Chopra(seed=SEED, fc1_output=n_classes)
architecture
=
Chopra
(
seed
=
SEED
,
fc1_output
=
n_classes
)
#architecture = Chopra(seed=SEED, fc1_output=n_classes)
architecture
=
FaceNet
(
seed
=
SEED
,
use_gpu
=
USE_GPU
)
optimizer
=
tf
.
train
.
GradientDescentOptimizer
(
0.00000001
)
...
...
@@ -89,12 +95,13 @@ def main():
# optimizer=optimizer,
# temp_dir="./LOGS_MOBIO/siamese-cnn-prefetch")
loss
=
TripletLoss
(
margin
=
4.
)
loss
=
TripletLoss
(
margin
=
1.
)
#optimizer = optimizer,
trainer
=
TripletTrainer
(
architecture
=
architecture
,
loss
=
loss
,
iterations
=
ITERATIONS
,
prefetch
=
False
,
optimizer
=
optimizer
,
temp_dir
=
"./LOGS_MOBIO/triplet-cnn"
)
#
trainer.train(train_data_shuffler, validation_data_shuffler)
trainer
.
train
(
train_data_shuffler
)
trainer
.
train
(
train_data_shuffler
,
validation_data_shuffler
)
#
trainer.train(train_data_shuffler)
bob/learn/tensorflow/script/train_siamese_casia_webface.py
View file @
1db6d460
...
...
@@ -22,7 +22,7 @@ from docopt import docopt
import
tensorflow
as
tf
from
..
import
util
SEED
=
10
from
bob.learn.tensorflow.datashuffler
import
TripletDisk
,
TripletWithSelectionDisk
from
bob.learn.tensorflow.datashuffler
import
TripletDisk
,
TripletWithSelectionDisk
,
TripletWithFastSelectionDisk
from
bob.learn.tensorflow.network
import
Lenet
,
MLP
,
LenetDropout
,
VGG
,
Chopra
,
Dummy
from
bob.learn.tensorflow.trainers
import
SiameseTrainer
,
TripletTrainer
from
bob.learn.tensorflow.loss
import
ContrastiveLoss
,
TripletLoss
...
...
@@ -56,9 +56,14 @@ def main():
extension
=
""
)
for
o
in
train_objects
]
train_data_shuffler
=
TripletWithSelectionDisk
(
train_file_names
,
train_labels
,
input_shape
=
[
125
,
125
,
3
],
batch_size
=
BATCH_SIZE
)
#train_data_shuffler = TripletWithSelectionDisk(train_file_names, train_labels,
# input_shape=[125, 125, 3],
# batch_size=BATCH_SIZE)
train_data_shuffler
=
TripletWithFastSelectionDisk
(
train_file_names
,
train_labels
,
input_shape
=
[
125
,
125
,
3
],
batch_size
=
BATCH_SIZE
)
# Preparing train set
directory
=
"/idiap/temp/tpereira/DEEP_FACE/CASIA/preprocessed"
...
...
@@ -85,11 +90,11 @@ def main():
# snapshot=VALIDATION_TEST,
# optimizer=optimizer)
loss
=
TripletLoss
(
margin
=
4
.
)
loss
=
TripletLoss
(
margin
=
1
.
)
trainer
=
TripletTrainer
(
architecture
=
architecture
,
loss
=
loss
,
iterations
=
ITERATIONS
,
prefetch
=
False
,
temp_dir
=
"./LOGS_CASIA/triplet-cnn"
)
temp_dir
=
"./LOGS_CASIA/triplet-cnn
-fast-selection
"
)
trainer
.
train
(
train_data_shuffler
,
validation_data_shuffler
)
...
...
bob/learn/tensorflow/trainers/Trainer.py
View file @
1db6d460
...
...
@@ -266,7 +266,8 @@ class Trainer(object):
# Preparing the optimizer
self
.
optimizer_class
.
_learning_rate
=
self
.
learning_rate
self
.
optimizer
=
self
.
optimizer_class
.
minimize
(
self
.
training_graph
,
global_step
=
tf
.
Variable
(
0
))
#self.optimizer = self.optimizer_class.minimize(self.training_graph, global_step=tf.Variable(0))
self
.
optimizer
=
self
.
optimizer_class
.
minimize
(
self
.
training_graph
)
# Train summary
self
.
summaries_train
=
self
.
create_general_summary
()
...
...
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