예제 #1
0
 def _get_activations_idx_kernel(self, data, idx, group, kernel):
     # support models from pysster v1.0
     if self.model.layers[0].name.startswith("dropout"):
         layer_idx = 1
     else:
         layer_idx = 2
     get_out = K.function(
         [self.model.layers[0].input,
          K.learning_phase()],
         [self.model.layers[layer_idx].output[:, :, kernel]])
     if '_data_gen_no_labels_meta' in dir(data):
         data_gen = data._data_gen_no_labels_meta(group,
                                                  self.params['batch_size'],
                                                  idx)
     else:
         data_gen = data._data_generator(group,
                                         self.params['batch_size'],
                                         False,
                                         False,
                                         idx,
                                         meta=False)
     n = max(
         len(idx) // self.params['batch_size'] +
         (len(idx) % self.params['batch_size'] != 0), 1)
     activations = []
     for _ in range(n):
         activations.append(get_out([next(data_gen), 0])[0])
     if len(activations) == 1:
         return activations[0]
     else:
         return np.vstack(activations)
예제 #2
0
    def get_max_activations(self, data, group):
        """ Get the network output of the first convolutional layer.

        The function returns the maximum activation (the maximum output of a kernel) for
        every kernel - input sequence pair. The return value is a dict containing the
        entries 'activations' (an array of shape (number of sequences, number of kernels)), 'labels'
        (an array of shape (number of sequences, number of classes)) and 'group' (the
        subset of the Data object used). 

        The 'group' argument can have the value 'train', 'val', 'test' or 'all'.

        Parameters
        ----------
        data : pysster.Data 
            A Data object.
        
        group : str
            The subset of the Data object that should be used.
        
        Returns
        -------
        results : dict
            A dict with 3 values ('activations', 'labels, 'group', see above)
        """
        if not self.model.layers[2].name.startswith("conv1d") and \
           not self.model.layers[0].name.startswith("dropout"):
            raise RuntimeError("First layer is not a convolutional layer.")
        if self.model.layers[0].name.startswith(
                "dropout"):  # support models from pysster v1.0
            tmp_model = KModel(self.model.input, self.model.layers[1].output)
        else:
            tmp_model = KModel(self.model.input, self.model.layers[2].output)
        data_gen = data._data_generator(group, self.params['batch_size'],
                                        False, False)
        idx = data._get_idx(group)
        n = max(
            len(idx) // self.params['batch_size'] +
            (len(idx) % self.params['batch_size'] != 0), 1)
        activations = []
        for _ in range(n):
            tmp = tmp_model.predict_on_batch(next(data_gen))
            activations.append(tmp.max(axis=1))
        return {
            'activations': np.vstack(activations),
            'labels': np.array([data.labels[x] for x in idx]),
            'group': group
        }
예제 #3
0
def get_activations(model,
                    model_inputs,
                    print_shape_only=True,
                    layer_name=None):
    print('----- activations -----')
    activations = []
    inp = model.input

    model_multi_inputs_cond = True
    if not isinstance(inp, list):
        # only one input! let's wrap it in a list.
        inp = [inp]
        model_multi_inputs_cond = False

    outputs = [
        layer.output for layer in model.layers
        if layer.name == layer_name or layer_name is None
    ]  # all layer outputs

    funcs = [K.function(inp + [K.learning_phase()], [out])
             for out in outputs]  # evaluation functions

    if model_multi_inputs_cond:
        list_inputs = []
        list_inputs.extend(model_inputs)
        list_inputs.append(0.)
    else:
        list_inputs = [model_inputs, 0.]

    # Learning phase. 0 = Test mode (no dropout or batch normalization)
    # layer_outputs = [func([model_inputs, 0.])[0] for func in funcs]
    layer_outputs = [func(list_inputs)[0] for func in funcs]
    for layer_activations in layer_outputs:
        activations.append(layer_activations)
        if print_shape_only:
            print(layer_activations.shape)
        else:
            print(layer_activations)
    return activations