Skip to content

Commit

Permalink
Create interfaces for text and image classifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
Irina Nicolae authored and Irina Nicolae committed Aug 29, 2018
1 parent 6dfb571 commit 7fd01ef
Showing 1 changed file with 91 additions and 39 deletions.
130 changes: 91 additions & 39 deletions art/classifiers/classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,20 @@

class Classifier(ABC):
"""
Base class for all classifiers.
Base abstract class for all classifiers.
"""
def __init__(self, clip_values, channel_index, defences=None, preprocessing=(0, 1)):
def __init__(self, defences=None, preprocessing=(0, 1)):
"""
Initialize a `Classifier` object.
:param clip_values: Tuple of the form `(min, max)` representing the minimum and maximum values allowed
for features.
:type clip_values: `tuple`
:param channel_index: Index of the axis in data containing the color channels or features.
:type channel_index: `int`
Initialize a :class:`Classifier` object.
:param defences: Defences to be activated with the classifier.
:type defences: `str` or `list(str)`
:param preprocessing: Tuple of the form `(substractor, divider)` of floats or `np.ndarray` of values to be
used for data preprocessing. The first value will be substracted from the input. The input will then
be divided by the second one.
:type preprocessing: `tuple`
"""
if len(clip_values) != 2:
raise ValueError('`clip_values` should be a tuple of 2 floats containing the allowed data range.')
self._clip_values = clip_values

self._channel_index = channel_index
print('init Classifier')
self._parse_defences(defences)

if len(preprocessing) != 2:
Expand Down Expand Up @@ -86,32 +78,6 @@ def nb_classes(self):
"""
return self._nb_classes

@property
def input_shape(self):
"""
Return the shape of one input.
:return: Shape of one input for the classifier.
:rtype: `tuple`
"""
return self._input_shape

@property
def clip_values(self):
"""
:return: Tuple of the form `(min, max)` representing the minimum and maximum values allowed for features.
:rtype: `tuple`
"""
return self._clip_values

@property
def channel_index(self):
"""
:return: Index of the axis in data containing the color channels or features.
:rtype: `int`
"""
return self._channel_index

@abc.abstractmethod
def class_gradient(self, x, label=None, logits=False):
"""
Expand Down Expand Up @@ -244,3 +210,89 @@ def _apply_processing_gradient(self, grad):
div = np.asarray(div, dtype=grad.dtype)
res = grad / div
return res


class ImageClassifier(Classifier):
def __init__(self, clip_values, channel_index, defences=None, preprocessing=(0, 1)):
"""
Initialize a `Classifier` object.
:param clip_values: Tuple of the form `(min, max)` representing the minimum and maximum values allowed
for features.
:type clip_values: `tuple`
:param channel_index: Index of the axis in data containing the color channels or features.
:type channel_index: `int`
:param defences: Defences to be activated with the classifier.
:type defences: `str` or `list(str)`
:param preprocessing: Tuple of the form `(substractor, divider)` of floats or `np.ndarray` of values to be
used for data preprocessing. The first value will be substracted from the input. The input will then
be divided by the second one.
:type preprocessing: `tuple`
"""
print('init ImageClassifier')
Classifier.__init__(self, defences=defences, preprocessing=preprocessing)

if len(clip_values) != 2:
raise ValueError('`clip_values` should be a tuple of 2 floats containing the allowed data range.')
self._clip_values = clip_values

self._channel_index = channel_index

@property
def input_shape(self):
"""
Return the shape of one input.
:return: Shape of one input for the classifier.
:rtype: `tuple`
"""
return self._input_shape

@property
def clip_values(self):
"""
:return: Tuple of the form `(min, max)` representing the minimum and maximum values allowed for features.
:rtype: `tuple`
"""
return self._clip_values

@property
def channel_index(self):
"""
:return: Index of the axis in data containing the color channels or features.
:rtype: `int`
"""
return self._channel_index


class TextClassifier(Classifier):

def __init__(self, defences=None, preprocessing=(0, 1)):
"""
Initialize a :class:`TextClassifier` object.
:param defences: Defences to be activated with the classifier.
:type defences: `str` or `list(str)`
:param preprocessing: Tuple of the form `(substractor, divider)` of floats or `np.ndarray` of values to be
used for data preprocessing. The first value will be substracted from the input. The input will then
be divided by the second one.
:type preprocessing: `tuple`
"""
print('init TextClassifier')
Classifier.__init__(self, defences=defences, preprocessing=preprocessing)

def word_gradient(self, x):
raise NotImplementedError

def predict_from_embedding(self, x_emb, batch_size):
# TODO new function or new parameter in predict?
raise NotImplementedError

def to_embedding(self, x):
raise NotImplementedError

def to_text(self, x):
raise NotImplementedError

def to_id(self, x):
raise NotImplementedError

0 comments on commit 7fd01ef

Please sign in to comment.