Пример #1
0
class KerasWrapper:
    def __init__(self, model, preprocessing, identifier=None, *args, **kwargs):
        """
        :param model: a keras model with a function `preprocess_input`
            that will later be called on the loaded numpy image
        """
        self._model = model
        identifier = identifier or model.name
        self._extractor = ActivationsExtractorHelper(
            identifier=identifier,
            get_activations=self.get_activations,
            preprocessing=preprocessing,
            *args,
            **kwargs)
        self._extractor.insert_attrs(self)

    @property
    def identifier(self):
        return self._extractor.identifier

    @identifier.setter
    def identifier(self, value):
        self._extractor.identifier = value

    def __call__(
        self, *args, **kwargs
    ):  # cannot assign __call__ as attribute due to Python convention
        return self._extractor(*args, **kwargs)

    def get_activations(self, images, layer_names):
        from keras import backend as K
        input_tensor = self._model.input
        layers = [
            layer for layer in self._model.layers if layer.name in layer_names
        ]
        layers = sorted(layers,
                        key=lambda layer: layer_names.index(layer.name))
        if 'logits' in layer_names:
            layers.insert(layer_names.index('logits'), self._model.layers[-1])
        assert len(layers) == len(layer_names)
        layer_out_tensors = [layer.output for layer in layers]
        functor = K.function([input_tensor] + [K.learning_phase()],
                             layer_out_tensors)  # evaluate all tensors at once
        layer_outputs = functor([images, 0.])  # 0 to signal testing phase
        return OrderedDict([
            (layer_name, layer_output)
            for layer_name, layer_output in zip(layer_names, layer_outputs)
        ])

    def __repr__(self):
        return repr(self._model)

    def graph(self):
        import networkx as nx
        g = nx.DiGraph()
        for layer in self._model.layers:
            g.add_node(layer.name, object=layer, type=type(layer))
            for outbound_node in layer._outbound_nodes:
                g.add_edge(layer.name, outbound_node.outbound_layer.name)
        return g
Пример #2
0
class TensorflowWrapper:
    def __init__(self, identifier, inputs, endpoints: dict, session, *args, **kwargs):
        import tensorflow as tf
        self._inputs = inputs
        self._endpoints = endpoints
        self._session = session or tf.compat.v1.Session()
        self._extractor = ActivationsExtractorHelper(identifier=identifier, get_activations=self.get_activations,
                                                     preprocessing=None, *args, **kwargs)
        self._extractor.insert_attrs(self)

    @property
    def identifier(self):
        return self._extractor.identifier

    @identifier.setter
    def identifier(self, value):
        self._extractor.identifier = value

    def __call__(self, *args, **kwargs):  # cannot assign __call__ as attribute due to Python convention
        return self._extractor(*args, **kwargs)

    def get_activations(self, images, layer_names):
        layer_tensors = OrderedDict((layer, self._endpoints[
            layer if (layer != 'logits' or layer in self._endpoints) else next(reversed(self._endpoints))])
                                    for layer in layer_names)
        layer_outputs = self._session.run(layer_tensors, feed_dict={self._inputs: images})
        return layer_outputs

    def graph(self):
        import networkx as nx
        g = nx.DiGraph()
        for name, layer in self._endpoints.items():
            g.add_node(name, object=layer, type=type(layer))
        g.add_node("logits", object=self.logits, type=type(self.logits))
        return g
Пример #3
0
class PixelModel:
    def __init__(self):
        self._extractor = ActivationsExtractorHelper(
            identifier='pixels',
            preprocessing=None,
            get_activations=self._pixels_from_paths)
        self._extractor.insert_attrs(self)

    @property
    def identifier(self):
        return self._extractor.identifier

    @identifier.setter
    def identifier(self, value):
        self._extractor.identifier = value

    def __call__(
        self, *args, **kwargs
    ):  # cannot assign __call__ as attribute due to Python convention
        return self._extractor(*args, **kwargs)

    def _pixels_from_paths(self, paths, layer_names):
        np.testing.assert_array_equal(layer_names, ['pixels'])
        pixels = [self._parse_image(path) for path in paths]
        return OrderedDict([('pixels', np.array(pixels))])

    def _parse_image(self, path):
        image = Image.open(path)
        image = image.convert(
            'RGB')  # make sure everything is in RGB and not grayscale L
        image = image.resize((256, 256))  # resize all images to same size
        return np.array(image)
Пример #4
0
 def __init__(self, identifier, inputs, endpoints: dict, session, *args, **kwargs):
     import tensorflow as tf
     self._inputs = inputs
     self._endpoints = endpoints
     self._session = session or tf.compat.v1.Session()
     self._extractor = ActivationsExtractorHelper(identifier=identifier, get_activations=self.get_activations,
                                                  preprocessing=None, *args, **kwargs)
     self._extractor.insert_attrs(self)
Пример #5
0
 def __init__(self, model, preprocessing, identifier=None, *args, **kwargs):
     """
     :param model: a keras model with a function `preprocess_input`
         that will later be called on the loaded numpy image
     """
     self._model = model
     identifier = identifier or model.name
     self._extractor = ActivationsExtractorHelper(
         identifier=identifier,
         get_activations=self.get_activations,
         preprocessing=preprocessing,
         *args,
         **kwargs)
     self._extractor.insert_attrs(self)
Пример #6
0
 def _build_extractor(self, identifier, preprocessing, get_activations,
                      *args, **kwargs):
     return ActivationsExtractorHelper(identifier=identifier,
                                       get_activations=get_activations,
                                       preprocessing=preprocessing,
                                       *args,
                                       **kwargs)
Пример #7
0
 def __init__(self):
     self._extractor = ActivationsExtractorHelper(
         identifier='pixels',
         preprocessing=None,
         get_activations=self._pixels_from_paths)
     self._extractor.insert_attrs(self)