Esempio n. 1
0
    def get_feature_maps(self, layer_names, img_paths):
        """
        Return the feature maps outputted by specified layers during prediction of specified images.

        :param layer_names: a list of the name of the layer whose output you want to capture
        :param img_paths: a list of paths to the jpg or png file
        :return: refer to the description of param 'output_style'
        """
        util.enforce_list(layer_names, img_paths)

        model_with_output_layers = self.get_model_with_output_layers(layer_names)
        img_tensors = util.from_img_paths(img_paths, target_size=self.input_shape[1:-1], preprocess=self.preprocess_style)
        feature_maps = model_with_output_layers.predict(img_tensors)

        return util.to_indexable(util.enforce_list(feature_maps))
Esempio n. 2
0
 def wrapper(*args):
     outputs = []
     img_paths = args[1]
     img_paths = util.enforce_list(img_paths)
     for img_path in img_paths:
         outputs.append(func(args[0], img_path))
     return outputs[0] if (len(outputs) == 1) else outputs
Esempio n. 3
0
    def get_outputs_of_layers(self, layer_names):
        """
        Return a list of symbolic outputs of specified convolution layers.
        Helper method of `self.get_model_with_output_layers`.

        :param layer_names: a list of layer names
        :return: a list of symbolic outputs of selected convolution layers
        """
        # convert one-item `layer_names` into a list of that one item
        layer_names = util.enforce_list(layer_names)
        # append symbolic output tensors of convolution layers whose names are in `layer_names`
        outputs = []
        for layer_name in layer_names:
            layer = self.model.get_layer(layer_name)  # captures 'layer not found' errors

            if isinstance(layer, keras.layers.Conv2D):
                outputs.append(layer.output)
            else:
                raise ValueError('Layer with name "{}" is not a 2D convolution layer.'.format(layer_name))

        return outputs