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 ...@@ -8,7 +8,6 @@ import tensorflow as tf
import bob.ip.base import bob.ip.base
import numpy import numpy
import six import six
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
class Base(object): class Base(object):
...@@ -55,7 +54,7 @@ class Base(object): ...@@ -55,7 +54,7 @@ class Base(object):
batch_size=32, batch_size=32,
seed=10, seed=10,
data_augmentation=None, data_augmentation=None,
normalizer=Linear(), normalizer=None,
prefetch=False, prefetch=False,
prefetch_capacity=50, prefetch_capacity=50,
prefetch_threads=5): prefetch_threads=5):
...@@ -218,7 +217,10 @@ class Base(object): ...@@ -218,7 +217,10 @@ class Base(object):
For the time being I'm only scaling from 0-1 For the time being I'm only scaling from 0-1
""" """
return self.normalizer(x) if self.normalizer is None:
return x
else:
return self.normalizer(x)
def _aggregate_batch(self, data_holder, use_list=False): def _aggregate_batch(self, data_holder, use_list=False):
size = len(data_holder[0]) size = len(data_holder[0])
......
#!/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 ...@@ -11,7 +11,6 @@ import bob.core
from .Base import Base from .Base import Base
logger = bob.core.log.setup("bob.learn.tensorflow") logger = bob.core.log.setup("bob.learn.tensorflow")
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
class Disk(Base): class Disk(Base):
...@@ -53,7 +52,7 @@ class Disk(Base): ...@@ -53,7 +52,7 @@ class Disk(Base):
batch_size=1, batch_size=1,
seed=10, seed=10,
data_augmentation=None, data_augmentation=None,
normalizer=Linear(), normalizer=None,
prefetch=False, prefetch=False,
prefetch_capacity=10, prefetch_capacity=10,
prefetch_threads=5 prefetch_threads=5
......
#!/usr/bin/env python #!/usr/bin/env python
# vim: set fileencoding=utf-8 : # vim: set fileencoding=utf-8 :
# @author: Tiago de Freitas Pereira <tiago.pereira@idiap.ch> # @author: Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
# @date: Wed 11 May 2016 09:39:36 CEST
import bob.ip.base import bob.ip.base
import numpy 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): possible_sigmas = numpy.arange(0.1, 3., 0.1)
possible_radii = [1, 2, 3]
super(ImageAugmentation, self).__init__(seed=seed) sigma = possible_sigmas[numpy.random.randint(len(possible_sigmas))]
radius = possible_radii[numpy.random.randint(len(possible_radii))]
self.filter_bank = [self.__add_none, gaussian_filter = bob.ip.base.Gaussian(sigma=(sigma, sigma),
self.__add_none, radius=(radius, radius))
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 gaussian_filter(image)
return image
def __add_gaussian_blur(self, image):
possible_sigmas = numpy.arange(0.1, 3., 0.1)
possible_radii = [1, 2, 3]
sigma = possible_sigmas[numpy.random.randint(len(possible_sigmas))] def add_rotation(image):
radius = possible_radii[numpy.random.randint(len(possible_radii))] """
Add random rotation
"""
gaussian_filter = bob.ip.base.Gaussian(sigma=(sigma, sigma), possible_angles = numpy.arange(-15, 15, 0.5)
radius=(radius, radius)) angle = possible_angles[numpy.random.randint(len(possible_angles))]
return gaussian_filter(image) return bob.ip.base.rotate(image, angle)
def __add_left_right_flip(self, image):
return bob.ip.base.flop(image)
def __add_rotation(self, image): def add_salt_and_pepper(image):
possible_angles = numpy.arange(-15, 15, 0.5) """
angle = possible_angles[numpy.random.randint(len(possible_angles))] Add random salt and pepper
"""
return bob.ip.base.rotate(image, angle) possible_levels = numpy.arange(0.01, 0.1, 0.01)
level = possible_levels[numpy.random.randint(len(possible_levels))]
def __add_salt_and_pepper(self, image): return compute_salt_and_peper(image, level)
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)
def compute_salt_and_peper(self, image, level): def compute_salt_and_peper(image, level):
""" """
Compute a salt and pepper noise Compute a salt and pepper noise
""" """
r = numpy.random.rand(*image.shape) r = numpy.random.rand(*image.shape)
# 0 noise
indexes_0 = r <= (level/0.5)
image[indexes_0] = 0.0
# 0 noise # 255 noise
indexes_0 = r <= (level/0.5) indexes_255 = (1 - level / 2) <= r;
image[indexes_0] = 0.0 image[indexes_255] = 255.0
# 255 noise return image
indexes_255 = (1 - level / 2) <= r;
image[indexes_255] = 255.0
return image
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
import numpy import numpy
from .Base import Base from .Base import Base
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
import tensorflow as tf import tensorflow as tf
...@@ -47,7 +46,7 @@ class Memory(Base): ...@@ -47,7 +46,7 @@ class Memory(Base):
batch_size=1, batch_size=1,
seed=10, seed=10,
data_augmentation=None, data_augmentation=None,
normalizer=Linear(), normalizer=None,
prefetch=False, prefetch=False,
prefetch_capacity=10, prefetch_capacity=10,
prefetch_threads=5 prefetch_threads=5
......
...@@ -4,52 +4,27 @@ ...@@ -4,52 +4,27 @@
import numpy import numpy
class ScaleFactor(object): def scale_factor(x, scale_factor=0.00390625):
""" """
Normalize a sample by a scale factor 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): def mean_offset(x, mean_offset):
return x * self.scale_factor
class MeanOffset(object):
""" """
Normalize a sample by a mean offset Normalize a sample by a mean offset
""" """
def __init__(self, mean_offset): for i in range(len(mean_offset)):
self.mean_offset = mean_offset x[:, :, i] = x[:, :, i] - mean_offset[i]
def __call__(self, x):
for i in range(len(self.mean_offset)):
x[:, :, i] = x[:, :, i] - self.mean_offset[i]
return x
class Linear(object):
def __init__(self):
pass
def __call__(self, x):
return x
class PerImageStandarization(object):
def __init__(self): return x
pass
def __call__(self, x): def per_image_standarization(x):
mean = numpy.mean(x) mean = numpy.mean(x)
std = numpy.std(x) std = numpy.std(x)
return (x-mean)/max(std, 1/numpy.sqrt(numpy.prod(x.shape))) return (x-mean)/max(std, 1/numpy.sqrt(numpy.prod(x.shape)))
...@@ -11,8 +11,6 @@ logger = bob.core.log.setup("bob.learn.tensorflow") ...@@ -11,8 +11,6 @@ logger = bob.core.log.setup("bob.learn.tensorflow")
from .Disk import Disk from .Disk import Disk
from .Siamese import Siamese from .Siamese import Siamese
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
class SiameseDisk(Siamese, Disk): class SiameseDisk(Siamese, Disk):
""" """
...@@ -52,7 +50,7 @@ class SiameseDisk(Siamese, Disk): ...@@ -52,7 +50,7 @@ class SiameseDisk(Siamese, Disk):
batch_size=1, batch_size=1,
seed=10, seed=10,
data_augmentation=None, data_augmentation=None,
normalizer=Linear(), normalizer=None,
prefetch=False, prefetch=False,
prefetch_capacity=10, prefetch_capacity=10,
prefetch_threads=5 prefetch_threads=5
......
...@@ -8,7 +8,6 @@ import six ...@@ -8,7 +8,6 @@ import six
from .Memory import Memory from .Memory import Memory
from .Siamese import Siamese from .Siamese import Siamese
import tensorflow as tf import tensorflow as tf
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
class SiameseMemory(Siamese, Memory): class SiameseMemory(Siamese, Memory):
...@@ -50,7 +49,7 @@ class SiameseMemory(Siamese, Memory): ...@@ -50,7 +49,7 @@ class SiameseMemory(Siamese, Memory):
batch_size=32, batch_size=32,
seed=10, seed=10,
data_augmentation=None, data_augmentation=None,
normalizer=Linear(), normalizer=None,
prefetch=False, prefetch=False,
prefetch_capacity=50, prefetch_capacity=50,
prefetch_threads=10 prefetch_threads=10
......
...@@ -6,7 +6,6 @@ import numpy ...@@ -6,7 +6,6 @@ import numpy
import tensorflow as tf import tensorflow as tf
import bob.ip.base import bob.ip.base
import numpy import numpy
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
class TFRecord(object): class TFRecord(object):
......
...@@ -7,7 +7,6 @@ import numpy ...@@ -7,7 +7,6 @@ import numpy
import tensorflow as tf import tensorflow as tf
import bob.ip.base import bob.ip.base
import numpy import numpy
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
from .TFRecord import TFRecord from .TFRecord import TFRecord
class TFRecordImage(TFRecord): class TFRecordImage(TFRecord):
......
...@@ -15,7 +15,6 @@ import tensorflow as tf ...@@ -15,7 +15,6 @@ import tensorflow as tf
from .Disk import Disk from .Disk import Disk
from .Triplet import Triplet from .Triplet import Triplet
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
class TripletDisk(Triplet, Disk): class TripletDisk(Triplet, Disk):
...@@ -57,7 +56,7 @@ class TripletDisk(Triplet, Disk): ...@@ -57,7 +56,7 @@ class TripletDisk(Triplet, Disk):
batch_size=1, batch_size=1,
seed=10, seed=10,
data_augmentation=None, data_augmentation=None,
normalizer=Linear(), normalizer=None,
prefetch=False, prefetch=False,
prefetch_capacity=50, prefetch_capacity=50,
prefetch_threads=10 prefetch_threads=10
......
...@@ -8,7 +8,6 @@ import tensorflow as tf ...@@ -8,7 +8,6 @@ import tensorflow as tf
import six import six
from .Memory import Memory from .Memory import Memory
from .Triplet import Triplet from .Triplet import Triplet
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
class TripletMemory(Triplet, Memory): class TripletMemory(Triplet, Memory):
...@@ -50,7 +49,7 @@ class TripletMemory(Triplet, Memory): ...@@ -50,7 +49,7 @@ class TripletMemory(Triplet, Memory):
batch_size=1, batch_size=1,
seed=10, seed=10,
data_augmentation=None, data_augmentation=None,
normalizer=Linear(), normalizer=None,
prefetch=False, prefetch=False,
prefetch_capacity=50, prefetch_capacity=50,
prefetch_threads=10 prefetch_threads=10
......
...@@ -13,7 +13,6 @@ from scipy.spatial.distance import euclidean, cdist ...@@ -13,7 +13,6 @@ from scipy.spatial.distance import euclidean, cdist
import logging import logging
logger = logging.getLogger("bob.learn") logger = logging.getLogger("bob.learn")
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
class TripletWithFastSelectionDisk(Triplet, Disk, OnlineSampling): class TripletWithFastSelectionDisk(Triplet, Disk, OnlineSampling):
...@@ -67,7 +66,7 @@ class TripletWithFastSelectionDisk(Triplet, Disk, OnlineSampling): ...@@ -67,7 +66,7 @@ class TripletWithFastSelectionDisk(Triplet, Disk, OnlineSampling):
seed=10, seed=10,
data_augmentation=None, data_augmentation=None,
total_identities=10, total_identities=10,
normalizer=Linear()): normalizer=None):
super(TripletWithFastSelectionDisk, self).__init__( super(TripletWithFastSelectionDisk, self).__init__(
data=data, data=data,
......
...@@ -10,11 +10,9 @@ from .Disk import Disk ...@@ -10,11 +10,9 @@ from .Disk import Disk
from .Triplet import Triplet from .Triplet import Triplet
from .OnlineSampling import OnlineSampling from .OnlineSampling import OnlineSampling
from scipy.spatial.distance import euclidean from scipy.spatial.distance import euclidean
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
import logging import logging
logger = logging.getLogger("bob.learn.tensorflow") logger = logging.getLogger("bob.learn.tensorflow")
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
class TripletWithSelectionDisk(Triplet, Disk, OnlineSampling): class TripletWithSelectionDisk(Triplet, Disk, OnlineSampling):
...@@ -57,7 +55,7 @@ class TripletWithSelectionDisk(Triplet, Disk, OnlineSampling): ...@@ -57,7 +55,7 @@ class TripletWithSelectionDisk(Triplet, Disk, OnlineSampling):
seed=10, seed=10,
data_augmentation=None, data_augmentation=None,
total_identities=10, total_identities=10,
normalizer=Linear()): normalizer=None):
super(TripletWithSelectionDisk, self).__init__( super(TripletWithSelectionDisk, self).__init__(
data=data, data=data,
......
...@@ -9,7 +9,6 @@ import tensorflow as tf ...@@ -9,7 +9,6 @@ import tensorflow as tf
from .OnlineSampling import OnlineSampling from .OnlineSampling import OnlineSampling
from .Memory import Memory from .Memory import Memory
from .Triplet import Triplet from .Triplet import Triplet
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
from scipy.spatial.distance import euclidean, cdist from scipy.spatial.distance import euclidean, cdist
import logging import logging
...@@ -68,7 +67,7 @@ class TripletWithSelectionMemory(Triplet, Memory, OnlineSampling): ...@@ -68,7 +67,7 @@ class TripletWithSelectionMemory(Triplet, Memory, OnlineSampling):
seed=10, seed=10,
data_augmentation=None, data_augmentation=None,
total_identities=10, total_identities=10,
normalizer=Linear()): normalizer=None):
super(TripletWithSelectionMemory, self).__init__( super(TripletWithSelectionMemory, self).__init__(
data=data, data=data,
......
...@@ -15,10 +15,7 @@ from .SiameseDisk import SiameseDisk ...@@ -15,10 +15,7 @@ from .SiameseDisk import SiameseDisk
from .TripletDisk import TripletDisk from .TripletDisk import TripletDisk
from .TripletWithSelectionDisk import TripletWithSelectionDisk from .TripletWithSelectionDisk import TripletWithSelectionDisk
from .DataAugmentation import DataAugmentation from .Normalizer import scale_factor, mean_offset, per_image_standarization
from .ImageAugmentation import ImageAugmentation
from .Normalizer import ScaleFactor, MeanOffset, Linear, PerImageStandarization
from .DiskAudio import DiskAudio from .DiskAudio import DiskAudio
from .TFRecord import TFRecord from .TFRecord import TFRecord
...@@ -53,9 +50,7 @@ __appropriate__( ...@@ -53,9 +50,7 @@ __appropriate__(
SiameseDisk, SiameseDisk,
TripletDisk, TripletDisk,
TripletWithSelectionDisk, TripletWithSelectionDisk,
DataAugmentation, scale_factor, mean_offset, per_image_standarization,
ImageAugmentation,
ScaleFactor, MeanOffset, Linear,
DiskAudio, DiskAudio,
TFRecord, TFRecord,
TFRecordImage TFRecordImage
......
#!/usr/bin/env python #!/usr/bin/env python
# vim: set fileencoding=utf-8 : # vim: set fileencoding=utf-8 :
# @author: Tiago de Freitas Pereira <tiago.pereira@idiap.ch> # @author: Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
# @date: Wed 11 May 2016 09:39:36 CEST
"""
Dummy architecture
"""
import tensorflow as tf import tensorflow as tf
def dummy(conv1_kernel_size=3, conv1_output=1, fc1_output=2, seed=10):
"""
Create all the necessary variables for this CNN
class Dummy(object): **Parameters**
conv1_kernel_size:
def __init__(self, conv1_output:
conv1_kernel_size=3, fc1_output:
conv1_output=1, seed = 10
"""
fc1_output=2,
seed=10,
n_classes=None):
"""
Create all the necessary variables for this CNN
**Parameters**
conv1_kernel_size=3,
conv1_output=2,
n_classes=10
seed = 10 slim = tf.contrib.slim
"""
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"): end_points = dict()
slim = tf.contrib.slim
initializer = tf.contrib.layers.xavier_initializer(uniform=False, dtype=tf.float32, seed=seed)
end_points = dict() graph = slim.conv2d(inputs, conv1_output, conv1_kernel_size, activation_fn=tf.nn.relu,
stride=1,
initializer = tf.contrib.layers.xavier_initializer(uniform=False, dtype=tf.float32, seed=self.seed) weights_initializer=initializer,
scope='conv1')
end_points['conv1'] = graph
graph = slim.conv2d(inputs, self.conv1_output, self.conv1_kernel_size, activation_fn=tf.nn.relu, graph = slim.flatten(graph, scope='flatten1')
stride=1, end_points['flatten1'] = graph
weights_initializer=initializer,
scope='conv1')
end_points['conv1'] = graph
graph = slim.flatten(graph, scope='flatten1') graph = slim.fully_connected(graph, fc1_output,
end_points['flatten1'] = graph weights_initializer=initializer,
activation_fn=None,
scope='fc1')
end_points['fc1'] = graph
graph = slim.fully_connected(graph, self.fc1_output, return graph, end_points
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 end_points[end_point]