Skip to content
Snippets Groups Projects
Commit 4fc9c91e authored by Tiago de Freitas Pereira's avatar Tiago de Freitas Pereira
Browse files

Added VGG networks

parent cea41d17
Branches
Tags
1 merge request!60Style transfer
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# @author: Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
"""
VGG16 and VGG19 wrappers
"""
import tensorflow as tf
from tensorflow.contrib.slim.python.slim.nets import vgg
import tensorflow.contrib.slim as slim
def vgg_19(inputs,
reuse=None,
mode=tf.estimator.ModeKeys.TRAIN, **kwargs):
"""
Oxford Net VGG 19-Layers version E Example from tf-slim
https://raw.githubusercontent.com/tensorflow/models/master/research/slim/nets/vgg.py
**Parameters**:
inputs: a 4-D tensor of size [batch_size, height, width, 3].
reuse: whether or not the network and its variables should be reused. To be
able to reuse 'scope' must be given.
mode:
Estimator mode keys
"""
with slim.arg_scope(
[slim.conv2d],
trainable=mode==tf.estimator.ModeKeys.TRAIN):
return vgg.vgg_19(inputs, spatial_squeeze=False)
def vgg_16(inputs,
reuse=None,
mode=tf.estimator.ModeKeys.TRAIN, **kwargs):
"""
Oxford Net VGG 16-Layers version E Example from tf-slim
https://raw.githubusercontent.com/tensorflow/models/master/research/slim/nets/vgg.py
**Parameters**:
inputs: a 4-D tensor of size [batch_size, height, width, 3].
reuse: whether or not the network and its variables should be reused. To be
able to reuse 'scope' must be given.
mode:
Estimator mode keys
"""
with slim.arg_scope(
[slim.conv2d],
trainable=mode==tf.estimator.ModeKeys.TRAIN):
return vgg.vgg_16(inputs, spatial_squeeze=False)
...@@ -5,6 +5,7 @@ from .MLP import mlp ...@@ -5,6 +5,7 @@ from .MLP import mlp
from .InceptionResnetV2 import inception_resnet_v2, inception_resnet_v2_batch_norm from .InceptionResnetV2 import inception_resnet_v2, inception_resnet_v2_batch_norm
from .InceptionResnetV1 import inception_resnet_v1, inception_resnet_v1_batch_norm from .InceptionResnetV1 import inception_resnet_v1, inception_resnet_v1_batch_norm
from . import SimpleCNN from . import SimpleCNN
from .Vgg import vgg_19, vgg_16
# gets sphinx autodoc done right - don't remove it # gets sphinx autodoc done right - don't remove it
......
...@@ -4,7 +4,8 @@ ...@@ -4,7 +4,8 @@
import tensorflow as tf import tensorflow as tf
from bob.learn.tensorflow.network import inception_resnet_v2, inception_resnet_v2_batch_norm,\ from bob.learn.tensorflow.network import inception_resnet_v2, inception_resnet_v2_batch_norm,\
inception_resnet_v1, inception_resnet_v1_batch_norm inception_resnet_v1, inception_resnet_v1_batch_norm,\
vgg_19, vgg_16
def test_inceptionv2(): def test_inceptionv2():
...@@ -43,3 +44,41 @@ def test_inceptionv1(): ...@@ -43,3 +44,41 @@ def test_inceptionv1():
tf.reset_default_graph() tf.reset_default_graph()
assert len(tf.global_variables()) == 0 assert len(tf.global_variables()) == 0
def test_vgg():
# Testing VGG19 Training mode
inputs = tf.placeholder(tf.float32, shape=(1, 224, 224, 3))
graph, _ = vgg_19(inputs)
assert len(tf.trainable_variables()) == 38
tf.reset_default_graph()
assert len(tf.global_variables()) == 0
# Testing VGG19 predicting mode
inputs = tf.placeholder(tf.float32, shape=(1, 224, 224, 3))
graph, _ = vgg_19(inputs, mode=tf.estimator.ModeKeys.PREDICT)
assert len(tf.trainable_variables()) == 0
tf.reset_default_graph()
assert len(tf.global_variables()) == 0
# Testing VGG 16 training mode
inputs = tf.placeholder(tf.float32, shape=(1, 224, 224, 3))
graph, _ = vgg_16(inputs)
assert len(tf.trainable_variables()) == 32
tf.reset_default_graph()
assert len(tf.global_variables()) == 0
# Testing VGG 16 predicting mode
inputs = tf.placeholder(tf.float32, shape=(1, 224, 224, 3))
graph, _ = vgg_16(inputs, mode=tf.estimator.ModeKeys.PREDICT)
assert len(tf.trainable_variables()) == 0
tf.reset_default_graph()
assert len(tf.global_variables()) == 0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment