Commit 565464b0 authored by Tiago de Freitas Pereira's avatar Tiago de Freitas Pereira
Browse files

Change all losses, data augmentation and architectures to functions

parent bdbc9988
Pipeline #13145 failed with stages
in 3 minutes and 48 seconds
......@@ -8,7 +8,6 @@ import tensorflow as tf
import bob.ip.base
import numpy
import six
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
class Base(object):
......@@ -55,7 +54,7 @@ class Base(object):
batch_size=32,
seed=10,
data_augmentation=None,
normalizer=Linear(),
normalizer=None,
prefetch=False,
prefetch_capacity=50,
prefetch_threads=5):
......@@ -218,6 +217,9 @@ class Base(object):
For the time being I'm only scaling from 0-1
"""
if self.normalizer is None:
return x
else:
return self.normalizer(x)
def _aggregate_batch(self, data_holder, use_list=False):
......
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# @author: Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
# @date: Sun 16 Oct 2016 14:32:36 CEST
import numpy
class DataAugmentation(object):
"""
Base class for applying common real-time data augmentation.
This class is meant to be used as an argument of `input_data`. When training
a model, the defined augmentation methods will be applied at training
time only.
"""
def __init__(self, seed=10):
self.filter_bank = []
numpy.random.seed(seed)
def __call__(self, image):
"""
Apply a random filter to and image
"""
if len(self.filter_bank) <= 0:
raise ValueError("There is not filters in the filter bank")
filter = self.filter_bank[numpy.random.randint(len(self.filter_bank))]
return filter(image)
......@@ -11,7 +11,6 @@ import bob.core
from .Base import Base
logger = bob.core.log.setup("bob.learn.tensorflow")
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
class Disk(Base):
......@@ -53,7 +52,7 @@ class Disk(Base):
batch_size=1,
seed=10,
data_augmentation=None,
normalizer=Linear(),
normalizer=None,
prefetch=False,
prefetch_capacity=10,
prefetch_threads=5
......
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# @author: Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
# @date: Wed 11 May 2016 09:39:36 CEST
import bob.ip.base
import numpy
from .DataAugmentation import DataAugmentation
class ImageAugmentation(DataAugmentation):
def add_gaussian_blur(image, seed=10):
"""
Class for applying common real-time random data augmentation for images.
Add random gaussian blur
"""
numpy.random.seed(seed)
def __init__(self, seed=10):
super(ImageAugmentation, self).__init__(seed=seed)
self.filter_bank = [self.__add_none,
self.__add_none,
self.__add_gaussian_blur,
self.__add_left_right_flip,
self.__add_none,
self.__add_salt_and_pepper]
#self.__add_rotation,
def __add_none(self, image):
return image
def __add_gaussian_blur(self, image):
possible_sigmas = numpy.arange(0.1, 3., 0.1)
possible_radii = [1, 2, 3]
......@@ -40,22 +23,30 @@ class ImageAugmentation(DataAugmentation):
return gaussian_filter(image)
def __add_left_right_flip(self, image):
return bob.ip.base.flop(image)
def __add_rotation(self, image):
def add_rotation(image):
"""
Add random rotation
"""
possible_angles = numpy.arange(-15, 15, 0.5)
angle = possible_angles[numpy.random.randint(len(possible_angles))]
return bob.ip.base.rotate(image, angle)
def __add_salt_and_pepper(self, image):
def add_salt_and_pepper(image):
"""
Add random salt and pepper
"""
possible_levels = numpy.arange(0.01, 0.1, 0.01)
level = possible_levels[numpy.random.randint(len(possible_levels))]
return self.compute_salt_and_peper(image, level)
return compute_salt_and_peper(image, level)
def compute_salt_and_peper(self, image, level):
def compute_salt_and_peper(image, level):
"""
Compute a salt and pepper noise
"""
......@@ -70,3 +61,4 @@ class ImageAugmentation(DataAugmentation):
image[indexes_255] = 255.0
return image
......@@ -5,7 +5,6 @@
import numpy
from .Base import Base
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
import tensorflow as tf
......@@ -47,7 +46,7 @@ class Memory(Base):
batch_size=1,
seed=10,
data_augmentation=None,
normalizer=Linear(),
normalizer=None,
prefetch=False,
prefetch_capacity=10,
prefetch_threads=5
......
......@@ -4,49 +4,24 @@
import numpy
class ScaleFactor(object):
def scale_factor(x, scale_factor=0.00390625):
"""
Normalize a sample by a scale factor
"""
return x * scale_factor
def __init__(self, scale_factor=0.00390625):
self.scale_factor = scale_factor
def __call__(self, x):
return x * self.scale_factor
class MeanOffset(object):
def mean_offset(x, mean_offset):
"""
Normalize a sample by a mean offset
"""
def __init__(self, mean_offset):
self.mean_offset = mean_offset
def __call__(self, x):
for i in range(len(self.mean_offset)):
x[:, :, i] = x[:, :, i] - self.mean_offset[i]
return x
for i in range(len(mean_offset)):
x[:, :, i] = x[:, :, i] - mean_offset[i]
class Linear(object):
def __init__(self):
pass
def __call__(self, x):
return x
class PerImageStandarization(object):
def __init__(self):
pass
def __call__(self, x):
def per_image_standarization(x):
mean = numpy.mean(x)
std = numpy.std(x)
......
......@@ -11,8 +11,6 @@ logger = bob.core.log.setup("bob.learn.tensorflow")
from .Disk import Disk
from .Siamese import Siamese
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
class SiameseDisk(Siamese, Disk):
"""
......@@ -52,7 +50,7 @@ class SiameseDisk(Siamese, Disk):
batch_size=1,
seed=10,
data_augmentation=None,
normalizer=Linear(),
normalizer=None,
prefetch=False,
prefetch_capacity=10,
prefetch_threads=5
......
......@@ -8,7 +8,6 @@ import six
from .Memory import Memory
from .Siamese import Siamese
import tensorflow as tf
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
class SiameseMemory(Siamese, Memory):
......@@ -50,7 +49,7 @@ class SiameseMemory(Siamese, Memory):
batch_size=32,
seed=10,
data_augmentation=None,
normalizer=Linear(),
normalizer=None,
prefetch=False,
prefetch_capacity=50,
prefetch_threads=10
......
......@@ -6,7 +6,6 @@ import numpy
import tensorflow as tf
import bob.ip.base
import numpy
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
class TFRecord(object):
......
......@@ -7,7 +7,6 @@ import numpy
import tensorflow as tf
import bob.ip.base
import numpy
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
from .TFRecord import TFRecord
class TFRecordImage(TFRecord):
......
......@@ -15,7 +15,6 @@ import tensorflow as tf
from .Disk import Disk
from .Triplet import Triplet
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
class TripletDisk(Triplet, Disk):
......@@ -57,7 +56,7 @@ class TripletDisk(Triplet, Disk):
batch_size=1,
seed=10,
data_augmentation=None,
normalizer=Linear(),
normalizer=None,
prefetch=False,
prefetch_capacity=50,
prefetch_threads=10
......
......@@ -8,7 +8,6 @@ import tensorflow as tf
import six
from .Memory import Memory
from .Triplet import Triplet
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
class TripletMemory(Triplet, Memory):
......@@ -50,7 +49,7 @@ class TripletMemory(Triplet, Memory):
batch_size=1,
seed=10,
data_augmentation=None,
normalizer=Linear(),
normalizer=None,
prefetch=False,
prefetch_capacity=50,
prefetch_threads=10
......
......@@ -13,7 +13,6 @@ from scipy.spatial.distance import euclidean, cdist
import logging
logger = logging.getLogger("bob.learn")
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
class TripletWithFastSelectionDisk(Triplet, Disk, OnlineSampling):
......@@ -67,7 +66,7 @@ class TripletWithFastSelectionDisk(Triplet, Disk, OnlineSampling):
seed=10,
data_augmentation=None,
total_identities=10,
normalizer=Linear()):
normalizer=None):
super(TripletWithFastSelectionDisk, self).__init__(
data=data,
......
......@@ -10,11 +10,9 @@ from .Disk import Disk
from .Triplet import Triplet
from .OnlineSampling import OnlineSampling
from scipy.spatial.distance import euclidean
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
import logging
logger = logging.getLogger("bob.learn.tensorflow")
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
class TripletWithSelectionDisk(Triplet, Disk, OnlineSampling):
......@@ -57,7 +55,7 @@ class TripletWithSelectionDisk(Triplet, Disk, OnlineSampling):
seed=10,
data_augmentation=None,
total_identities=10,
normalizer=Linear()):
normalizer=None):
super(TripletWithSelectionDisk, self).__init__(
data=data,
......
......@@ -9,7 +9,6 @@ import tensorflow as tf
from .OnlineSampling import OnlineSampling
from .Memory import Memory
from .Triplet import Triplet
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
from scipy.spatial.distance import euclidean, cdist
import logging
......@@ -68,7 +67,7 @@ class TripletWithSelectionMemory(Triplet, Memory, OnlineSampling):
seed=10,
data_augmentation=None,
total_identities=10,
normalizer=Linear()):
normalizer=None):
super(TripletWithSelectionMemory, self).__init__(
data=data,
......
......@@ -15,10 +15,7 @@ from .SiameseDisk import SiameseDisk
from .TripletDisk import TripletDisk
from .TripletWithSelectionDisk import TripletWithSelectionDisk
from .DataAugmentation import DataAugmentation
from .ImageAugmentation import ImageAugmentation
from .Normalizer import ScaleFactor, MeanOffset, Linear, PerImageStandarization
from .Normalizer import scale_factor, mean_offset, per_image_standarization
from .DiskAudio import DiskAudio
from .TFRecord import TFRecord
......@@ -53,9 +50,7 @@ __appropriate__(
SiameseDisk,
TripletDisk,
TripletWithSelectionDisk,
DataAugmentation,
ImageAugmentation,
ScaleFactor, MeanOffset, Linear,
scale_factor, mean_offset, per_image_standarization,
DiskAudio,
TFRecord,
TFRecordImage
......
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# @author: Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
# @date: Wed 11 May 2016 09:39:36 CEST
"""
Dummy architecture
"""
import tensorflow as tf
class Dummy(object):
def __init__(self,
conv1_kernel_size=3,
conv1_output=1,
fc1_output=2,
seed=10,
n_classes=None):
def dummy(conv1_kernel_size=3, conv1_output=1, fc1_output=2, seed=10):
"""
Create all the necessary variables for this CNN
**Parameters**
conv1_kernel_size=3,
conv1_output=2,
n_classes=10
conv1_kernel_size:
conv1_output:
fc1_output:
seed = 10
"""
self.conv1_output = conv1_output
self.conv1_kernel_size = conv1_kernel_size
self.fc1_output = fc1_output
self.seed = seed
self.n_classes = n_classes
def __call__(self, inputs, reuse=False, end_point="logits"):
slim = tf.contrib.slim
end_points = dict()
initializer = tf.contrib.layers.xavier_initializer(uniform=False, dtype=tf.float32, seed=self.seed)
initializer = tf.contrib.layers.xavier_initializer(uniform=False, dtype=tf.float32, seed=seed)
graph = slim.conv2d(inputs, self.conv1_output, self.conv1_kernel_size, activation_fn=tf.nn.relu,
graph = slim.conv2d(inputs, conv1_output, conv1_kernel_size, activation_fn=tf.nn.relu,
stride=1,
weights_initializer=initializer,
scope='conv1')
......@@ -52,15 +30,11 @@ class Dummy(object):
graph = slim.flatten(graph, scope='flatten1')
end_points['flatten1'] = graph
graph = slim.fully_connected(graph, self.fc1_output,
graph = slim.fully_connected(graph, fc1_output,
weights_initializer=initializer,
activation_fn=None,
scope='fc1')
end_points['fc1'] = graph
if self.n_classes is not None:
# Appending the logits layer
graph = append_logits(graph, self.n_classes, reuse)
end_points['logits'] = graph
return graph, end_points
return end_points[end_point]
......@@ -6,7 +6,6 @@
import tensorflow as tf
from bob.learn.tensorflow.utils.session import Session
from bob.learn.tensorflow.datashuffler import Linear
class Embedding(object):
......@@ -20,8 +19,8 @@ class Embedding(object):
graph: Embedding graph
"""
def __init__(self, input, graph, normalizer=Linear()):
self.input = input
def __init__(self, inputs, graph, normalizer=None):
self.inputs = inputs
self.graph = graph
self.normalizer = normalizer
......@@ -32,6 +31,6 @@ class Embedding(object):
for i in range(data.shape[0]):
data[i] = self.normalizer(data[i])
feed_dict = {self.input: data}
feed_dict = {self.inputs: data}
return session.run([self.graph], feed_dict=feed_dict)[0]
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# @author: Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
import tensorflow as tf
from bob.learn.tensorflow.layers import maxout
from .utils import append_logits
class LightCNN29(object):
"""Creates the graph for the Light CNN-9 in
Wu, Xiang, et al. "A light CNN for deep face representation with noisy labels." arXiv preprint arXiv:1511.02683 (2015).
"""
def __init__(self,
seed=10,
n_classes=10):
self.seed = seed
self.n_classes = n_classes
def __call__(self, inputs, reuse=False, end_point="logits"):
slim = tf.contrib.slim
end_points = dict()
initializer = tf.contrib.layers.xavier_initializer(uniform=False, dtype=tf.float32, seed=self.seed)
graph = slim.conv2d(inputs, 96, [5, 5], activation_fn=tf.nn.relu,
stride=1,
weights_initializer=initializer,
scope='Conv1',
reuse=reuse)
end_points['conv1'] = graph
graph = maxout(graph,
num_units=48,
name='Maxout1')
graph = slim.max_pool2d(graph, [2, 2], stride=2, padding="SAME", scope='Pool1')
####
graph = slim.conv2d(graph, 96, [1, 1], activation_fn=tf.nn.relu,
stride=1,
weights_initializer=initializer,
scope='Conv2a',
reuse=reuse)
graph = maxout(graph,
num_units=48,
name='Maxout2a')
graph = slim.conv2d(graph, 192, [3, 3], activation_fn=tf.nn.relu,
stride=1,
weights_initializer=initializer,
scope='Conv2',
reuse=reuse)
end_points['conv2'] = graph
graph = maxout(graph,
num_units=96,
name='Maxout2')
graph = slim.max_pool2d(graph, [2, 2], stride=2, padding="SAME", scope='Pool2')
#####
graph = slim.conv2d(graph, 192, [1, 1], activation_fn=tf.nn.relu,
stride=1,
weights_initializer=initializer,
scope='Conv3a',
reuse=reuse)
graph = maxout(graph,
num_units=96,
name='Maxout3a')
graph = slim.conv2d(graph, 384, [3, 3], activation_fn=tf.nn.relu,
stride=1,
weights_initializer=initializer,
scope='Conv3',
reuse=reuse)
end_points['conv3'] = graph
graph = maxout(graph,
num_units=192,
name='Maxout3')
graph = slim.max_pool2d(graph, [2, 2], stride=2, padding="SAME", scope='Pool3')
#####
graph = slim.conv2d(graph, 384, [1, 1], activation_fn=tf.nn.relu,
stride=1,
weights_initializer=initializer,
scope='Conv4a',
reuse=reuse)
graph = maxout(graph,
num_units=192,
name='Maxout4a')
graph = slim.conv2d(graph, 256, [3, 3], activation_fn=tf.nn.relu,