Exemple #1
0
def embed_data(x, dset):
    '''
    Convenience function: embeds x into the code space using the corresponding
    autoencoder (specified by dset).
    '''
    if not len(x):
        return np.zeros(shape=(0, 10))
    if dset == 'reuters':
        dset = 'reuters10k'

    json_path = '../pretrain_weights/ae_{}.json'.format(dset)
    weights_path = '../pretrain_weights/ae_{}_weights.h5'.format(dset)

    with open(json_path) as f:
        pt_ae = model_from_json(f.read())
    pt_ae.load_weights(weights_path)

    x = x.reshape(-1, np.prod(x.shape[1:]))

    get_embeddings = K.function([pt_ae.input], [pt_ae.layers[3].output])

    get_reconstruction = K.function([pt_ae.layers[4].input], [pt_ae.output])
    x_embedded = predict_with_K_fn(get_embeddings, x)[0]
    x_recon = predict_with_K_fn(get_reconstruction, x_embedded)[0]
    reconstruction_mse = np.mean(np.square(x - x_recon))
    print(
        "using pretrained embeddings; sanity check, total reconstruction error:",
        np.mean(reconstruction_mse))

    del pt_ae

    return x_embedded
Exemple #2
0
def do_activation(input_values, function_name, alpha=0.2):
    """Runs input array through activation function.

    :param input_values: numpy array (any shape).
    :param function_name: Name of activation function (must be accepted by ``).
    :param alpha: Slope parameter (alpha) for activation function.  This applies
        only for eLU and ReLU.
    :return: output_values: Same as `input_values` but post-activation.
    """

    architecture_utils.check_activation_function(
        activation_function_string=function_name, alpha_for_elu=alpha,
        alpha_for_relu=alpha
    )

    input_object = K.placeholder()

    if function_name == architecture_utils.ELU_FUNCTION_STRING:
        function_object = K.function(
            [input_object],
            [layers.ELU(alpha=alpha)(input_object)]
        )
    elif function_name == architecture_utils.RELU_FUNCTION_STRING:
        function_object = K.function(
            [input_object],
            [layers.LeakyReLU(alpha=alpha)(input_object)]
        )
    else:
        function_object = K.function(
            [input_object],
            [layers.Activation(function_name)(input_object)]
        )

    return function_object([input_values])[0]
    def create_mtcnn(self, sess, model_path):
        tf.disable_eager_execution()

        if not model_path:
            model_path, _ = os.path.split(os.path.realpath(__file__))

        with tf.variable_scope('pnet'):
            data = tf.placeholder(tf.float32, (None, None, None, 3), 'input')
            pnet = mtcnn_detect_face.PNet({'data': data})
            pnet.load(os.path.join(model_path, 'det1.npy'), sess)
        with tf.variable_scope('rnet'):
            data = tf.placeholder(tf.float32, (None, 24, 24, 3), 'input')
            rnet = mtcnn_detect_face.RNet({'data': data})
            rnet.load(os.path.join(model_path, 'det2.npy'), sess)
        with tf.variable_scope('onet'):
            data = tf.placeholder(tf.float32, (None, 48, 48, 3), 'input')
            onet = mtcnn_detect_face.ONet({'data': data})
            onet.load(os.path.join(model_path, 'det3.npy'), sess)
        self.pnet = K.function([pnet.layers['data']],
                               [pnet.layers['conv4-2'], pnet.layers['prob1']])
        self.rnet = K.function([rnet.layers['data']],
                               [rnet.layers['conv5-2'], rnet.layers['prob1']])
        self.onet = K.function([onet.layers['data']], [
            onet.layers['conv6-2'], onet.layers['conv6-3'],
            onet.layers['prob1']
        ])
Exemple #4
0
 def compute_activation(self, lidx, test):
     if lidx < 0:
         return test
     inp = self.model.input
     functor = K.function([inp, K.learning_phase()],
                          [self.model.layers[lidx].output])
     return functor([test])[0]
def main():
    model = load_model('/home/dzd/dzd/labwork/face/CNN/model/CNN_model.h5')

    image = cv2.imread(os.path.join('/home/dzd/dzd/labwork/face/yaleBExtData/yaleB01', 'yaleB01_P00A-005E-10.pgm'), cv2.IMREAD_GRAYSCALE)
#    images=cv2.imread("/home/dzd/dzd/labwork/face/yaleBExtData/yaleB01/yaleB01_P00A-005E-10.pgm")
#    cv2.imshow("Image", images)
#    cv2.waitKey(0)

    # Turn the image into an array.
    # 根据载入的训练好的模型的配置,将图像统一尺寸
#    image_arr = cv2.resize(image, (192, 168))
    image.resize(192, 168)
    image_arr = np.array(image).reshape(1,192,168,1)
#    image_arr = np.expand_dims(image_arr, axis=0)

    # 第一个 model.layers[0],不修改,表示输入数据;
    # 第二个model.layers[ ],修改为需要输出的层数的编号[]
    layer_1 = K.function([model.layers[0].input], [model.layers[6].output])
#    visualization_model = K.function([model.layers[0].input], [model.layers[1].output])
    # 只修改inpu_image
#    f1 = visualization_model.predict(images/255.0)
    f1 = layer_1([image_arr/255.0])[0]

    # 第一层卷积后的特征图展示,输出是(1,66,66,32),(样本个数,特征图尺寸长,特征图尺寸宽,特征图个数)
    for _ in range(32):
                show_img = f1[:, :, :, _]
                show_img = show_img.reshape(len(show_img[0]),len(show_img[0][0]))
#                show_img.shape = [66, 66]
                plt.subplot(4, 8, _ + 1)
                # plt.imshow(show_img, cmap='black')
                plt.imshow(show_img, cmap='gray')
                plt.axis('off')
    plt.show()
Exemple #6
0
    def __init__(self, restore=None, session=None, use_log=False):
        self.num_channels = 1
        self.image_size = 28
        self.num_labels = 10

        model = Sequential()
        model.add(Flatten(input_shape=(28, 28, 1)))
        model.add(Dense(1024))
        model.add(Lambda(lambda x: x * 10))
        model.add(Activation('softplus'))
        model.add(Lambda(lambda x: x * 0.1))
        model.add(Dense(10))
        # output log probability, used for black-box attack
        if use_log:
            model.add(Activation('softmax'))
        if restore:
            model.load_weights(restore)

        layer_outputs = []
        for layer in model.layers:
            if isinstance(layer, Conv2D) or isinstance(layer, Dense):
                layer_outputs.append(
                    K.function([model.layers[0].input], [layer.output]))

        self.layer_outputs = layer_outputs
        self.model = model
Exemple #7
0
    def __init__(self,
                 restore=None,
                 session=None,
                 use_log=False,
                 use_brelu=False):
        def bounded_relu(x):
            return K.relu(x, max_value=1)

        if use_brelu:
            activation = bounded_relu
        else:
            activation = 'relu'
        self.num_channels = 1
        self.image_size = 28
        self.num_labels = 10

        model = Sequential()

        model.add(Conv2D(32, (3, 3), input_shape=(28, 28, 1)))
        model.add(Activation(activation))
        model.add(Conv2D(32, (3, 3)))
        model.add(Activation(activation))
        model.add(MaxPooling2D(pool_size=(2, 2)))

        model.add(Conv2D(64, (3, 3)))
        model.add(Activation(activation))
        model.add(Conv2D(64, (3, 3)))
        model.add(Activation(activation))
        model.add(MaxPooling2D(pool_size=(2, 2)))

        model.add(Flatten())
        model.add(Dense(200))
        model.add(Activation(activation))
        model.add(Dense(200))
        model.add(Activation(activation))
        model.add(Dense(10))
        # output log probability, used for black-box attack
        if use_log:
            model.add(Activation('softmax'))
        if restore:
            model.load_weights(restore)

        layer_outputs = []
        for layer in model.layers:
            if isinstance(layer, Conv2D) or isinstance(layer, Dense):
                layer_outputs.append(
                    K.function([model.layers[0].input], [layer.output]))

        self.model = model
        self.layer_outputs = layer_outputs
def embed_data(x, dset, path):
  """embeds x into the code space using the autoencoder."""

  if x:
    return np.zeros(shape=(0, 10))
  # load model and weights
  json_path = os.path.join(path, 'ae_{}.json'.format(dset))
  print('load model from json file:', json_path)
  with gfile.Open(json_path) as f:
    pt_ae = model_from_json(f.read())
  weights_path = os.path.join(path, 'ae_{}_weights.h5'.format(dset))
  print('load code spase from:', weights_path)
  local_filename = weights_path.split('/')[-1]
  tmp_filename = os.path.join(tempfile.gettempdir(),
                              str(int(time.time())) + '_' + local_filename)
  gfile.Copy(weights_path, tmp_filename)
  pt_ae.load_weights(tmp_filename)
  gfile.Remove(tmp_filename)

  print('***********************', x.shape)
  x = x.reshape(-1, np.prod(x.shape[1:]))
  print('***********************', x.shape)

  get_embeddings = K.function([pt_ae.input], [pt_ae.layers[3].output])

  get_reconstruction = K.function([pt_ae.layers[4].input], [pt_ae.output])
  x_embedded = predict_with_k_fn(get_embeddings, x)[0]
  x_recon = predict_with_k_fn(get_reconstruction, x_embedded)[0]
  reconstruction_mse = np.mean(np.square(x - x_recon))
  print(
      'using pretrained embeddings; sanity check, total reconstruction error:',
      np.mean(reconstruction_mse))

  del pt_ae

  return x_embedded
Exemple #9
0
def predict_stochastic(neural_net):
    """
    Have the neural network make predictions with the Dropout layers on, resulting in stochastic behavior from the
    neural net itself.

    Args:
        neural_net:
        data:

    Returns:

    """
    input = neural_net.input
    output = neural_net.output
    pred_func = K.function(input + [K.learning_phase()], [output])
    return pred_func
Exemple #10
0
    def inject(self, model):
        """Inject the Lookahead algorithm for the given model.
        The following code is modified from keras's _make_train_function method.
        See: https://github.com/keras-team/keras/blob/master/keras/engine/training.py#L497
        """
        if not hasattr(model, 'train_function'):
            raise RuntimeError('You must compile your model before using it.')

        model._check_trainable_weights_consistency()

        if model.train_function is None:
            inputs = (model._feed_inputs + model._feed_targets +
                      model._feed_sample_weights)
            if model._uses_dynamic_learning_phase():
                inputs += [K.learning_phase()]
            fast_params = model._collected_trainable_weights

            with K.name_scope('training'):
                with K.name_scope(model.optimizer.__class__.__name__):
                    training_updates = model.optimizer.get_updates(
                        params=fast_params, loss=model.total_loss)
                    slow_params = [K.variable(p) for p in fast_params]
                fast_updates = (model.updates + training_updates +
                                model.metrics_updates)

                slow_updates, copy_updates = [], []
                for p, q in zip(fast_params, slow_params):
                    slow_updates.append(K.update(q, q + self.alpha * (p - q)))
                    copy_updates.append(K.update(p, q))

                # Gets loss and metrics. Updates weights at each call.
                fast_train_function = K.function(inputs, [model.total_loss] +
                                                 model.metrics_tensors,
                                                 updates=fast_updates,
                                                 name='fast_train_function',
                                                 **model._function_kwargs)

                def F(inputs):
                    self.count += 1
                    R = fast_train_function(inputs)
                    if self.count % self.k == 0:
                        K.batch_get_value(slow_updates)
                        K.batch_get_value(copy_updates)
                    return R

                model.train_function = F
def grad_cam(input_model, image, cls, layer_name, H=320, W=320):
    """GradCAM method for visualizing input saliency."""
    y_c = input_model.output[0, cls]
    conv_output = input_model.get_layer(layer_name).output
    grads = K.gradients(y_c, conv_output)[0]

    gradient_function = K.function([input_model.input], [conv_output, grads])

    output, grads_val = gradient_function([image])
    output, grads_val = output[0, :], grads_val[0, :, :, :]

    weights = np.mean(grads_val, axis=(0, 1))
    cam = np.dot(output, weights)

    # Process CAM
    cam = cv2.resize(cam, (W, H), cv2.INTER_LINEAR)
    cam = np.maximum(cam, 0)
    cam = cam / cam.max()
    return cam
Exemple #12
0
 def __init__(self, model_path=None):
     self.model_path = model_path
     self.model_path_start = "/".join(model_path.split("/")[:-1])
     self.model_config = model_path.split("/")[-1].split("_")[2]
     self.model = load_model(self.model_path,
                             custom_objects={
                                 "Interpolate1D": Interpolate1D,
                                 "ConcreteDropout": ConcreteDropout,
                                 "Split1D": Split1D,
                                 "Scale": Scale,
                                 "AutoScale": AutoScale
                             })
     self.pred_func = K.function(self.model.input + [K.learning_phase()],
                                 [self.model.output])
     self.x_scaling_file = join(
         self.model_path_start,
         "gan_X_scaling_values_{0}.csv".format(self.model_config))
     self.y_scaling_file = join(
         self.model_path_start,
         "gan_Y_scaling_values_{0}.csv".format(self.model_config))
     self.y_scaling_values = pd.read_csv(self.y_scaling_file,
                                         index_col="Channel")
     self.x_scaling_values = pd.read_csv(self.x_scaling_file,
                                         index_col="Channel")
    def on_train_begin(self, logs={}):
        if not os.path.exists(self.cfg['SAVE_DIR']):
            print("Making directory", self.cfg['SAVE_DIR'])
            os.makedirs(self.cfg['SAVE_DIR'])

        # Indexes of the layers which we keep track of. Basically, this will be any layer
        # which has a 'kernel' attribute, which is essentially the "Dense" or "Dense"-like layers
        self.layerixs = []

        # Functions return activity of each layer
        self.layerfuncs = []

        # Functions return weights of each layer
        self.layerweights = []
        for lndx, l in enumerate(self.model.layers):
            if hasattr(l, 'kernel'):
                self.layerixs.append(lndx)
                self.layerfuncs.append(
                    K.function(self.model.inputs, [
                        l.output,
                    ]))
                self.layerweights.append(l.kernel)

        # print(self.model._feed_inputs)
        # print(self.model._feed_sample_weights)
        # print(self.model._feed_targets)

        inputs = [
            self.model._feed_inputs, self.model._feed_targets,
            self.model._feed_sample_weights,
            K.learning_phase()
        ]

        #         inputs = [self.model.input,
        #                       self.model._feed_targets,
        #                   self.model._feed_sample_weights,
        #                   backend.symbollearning_phase()
        #                  ]
        #         inputs =(3277,12)
        #         print(inputs)

        # grads_fn = K.function(inputs=[model.inputs[0],
        #                       model._feed_targets[0],
        #                       backend.symbolic_learning_phase()],
        #               outputs=grads)

        # g_learning = grads_fn([x, x, True])
        # g_not_learning = grads_fn([x, x, False])

        #   [model.inputs[0], model._feed_targets[0], model.sample_weights[0]]

        # Get gradients of all the relevant layers at once
        grads = self.model.optimizer.get_gradients(self.model.total_loss,
                                                   self.layerweights)
        self.get_gradients = K.function(inputs=inputs, outputs=grads)

        # Get cross-entropy loss
        self.get_loss = K.function(inputs=inputs,
                                   outputs=[
                                       self.model.total_loss,
                                   ])
Exemple #14
0
def activations_norm(images, labels, batch_size, model, layer_regex,
                     daug_params, norms=['fro']):
    """
    Computes the norm of the activation of all feature maps

    Parameters
    ----------
    images : h5py Dataset
        The set of images

    labels : h5py Dataset
        The ground truth labels

    batch_size : int
        Batch size

    model : Keras Model
        The model

    daug_params : dict
        Dictionary of data augmentation parameters

    Returns
    -------
    results_dict : dict
        Dictionary containing some performance metrics
    """
    def _update_stats(mean_norm, std_norm, norm):
        mean_norm_batch = np.mean(norm, axis=0)
        std_norm_batch = np.std(norm, axis=0)
        mean_norm = init / float(end) * mean_norm + \
                    batch_size / float(end) * mean_norm_batch
        std_norm = init / float(end) * std_norm ** 2 + \
                    batch_size / float(end) * std_norm_batch ** 2 + \
                    (init * batch_size) / float(end ** 2) * \
                    (mean_norm - mean_norm_batch) ** 2
        std_norm = np.sqrt(std_norm)

        return mean_norm, std_norm

    def _frobenius_norm(activations):
        norm = np.linalg.norm(
                activations, ord='fro', 
                axis=tuple(range(1, len(activations.shape) - 1)))
        return norm

    def _inf_norm(activations):
        norm = np.max(np.abs(activations),
                      axis=tuple(range(1, len(activations.shape) - 1)))
        return norm

    n_images = images.shape[0]
    n_batches_per_epoch = int(np.ceil(float(n_images) / batch_size))

    # Create batch generator
    image_gen = get_generator(images, **daug_params)
    batch_gen = batch_generator(image_gen, images, labels, batch_size, 
                                aug_per_im=1, shuffle=False)

    # Initialize list to store the mean norm of the activations
    results_dict = {'activations_norm': {}, 'summary': {}} 

    # Iterate over the layers
    model = del_extra_nodes(model)
    for layer in model.layers:
        if re.match(layer_regex, layer.name):
            layer_name = layer.name.encode('utf-8')
            print('\nLayer {}'.format(layer_name))
            output = model.get_layer(layer_name)\
                    .outbound_nodes[0].input_tensors[0]
            get_output = K.function([model.input, K.learning_phase()], 
                                    [output])
            n_channels = K.int_shape(output)[-1]
            results_dict['activations_norm'].update({layer_name: 
                {n: {'mean': np.zeros(n_channels), 
                     'std': np.zeros(n_channels)} for n in norms}})
            layer_dict = results_dict['activations_norm'][layer_name]
            init = 0
            batch_gen.image_gen.reset()
            for _ in tqdm(range(n_batches_per_epoch)):
                batch_images, _ = next(batch_gen())
                batch_size = batch_images.shape[0]
                end = init + batch_size
                activations = get_output([batch_images, 0])[0]
                for norm_key in norms:
                    mean_norm = layer_dict[norm_key]['mean']
                    std_norm = layer_dict[norm_key]['std']
                    if norm_key == 'fro':
                        norm = _frobenius_norm(activations)
                    elif norm_key == 'inf':
                        norm = _inf_norm(activations)
                    else:
                        raise NotImplementedError('Implemented norms are fro '
                                'and inf')
                    mean_norm, std_norm = _update_stats(mean_norm, std_norm, 
                                                        norm)
                    layer_dict[norm_key]['mean'] = mean_norm
                    layer_dict[norm_key]['std'] = std_norm
                init = end

    # Compute summary statistics across the channels
    for layer, layer_dict in results_dict['activations_norm'].items():
        results_dict['summary'].update({layer: {}})
        for norm_key, norm_dict in layer_dict.items():
            results_dict['summary'][layer].update({norm_key: {
                'mean': np.mean(norm_dict['mean']), 
                'std': np.mean(norm_dict['std'])}})

    return results_dict
Exemple #15
0
def activations(images, labels, batch_size, model, layer_regex, nodaug_params, 
                daug_params, include_input=False, class_invariance=False, 
                n_daug_rep=0,  norms=['fro']):
    """
    Computes metrics from the activations, such as the norm of the feature
    maps, data augmentation invariance, class invariance, etc.

    Parameters
    ----------
    images : h5py Dataset
        The set of images

    labels : h5py Dataset
        The ground truth labels

    batch_size : int
        Batch size

    model : Keras Model
        The model

    nodaug_params : dict
        Dictionary of data augmentation parameters for the baseline

    daug_params : dict
        Dictionary of data augmentation parameters

    include_input : bool
        If True, the input layer is considered for the analysis

    class_invariance : bool
        If True, the class invariance score is computed

    n_daug_rep : int
        If larger than 0, the data augentation invariance score is computed,
        performing n_daug_rep repetitions of random augmentations

    norms : list
        List of keywords to specify the types of norms to compute on the 
        activations

    Returns
    -------
    results_dict : dict
        Dictionary containing some performance metrics
    """
    def _update_stats(mean_norm, std_norm, norm):
        mean_norm_batch = np.mean(norm, axis=0)
        std_norm_batch = np.std(norm, axis=0)
        mean_norm = init / float(end) * mean_norm + \
                    batch_size / float(end) * mean_norm_batch
        std_norm = init / float(end) * std_norm ** 2 + \
                    batch_size / float(end) * std_norm_batch ** 2 + \
                    (init * batch_size) / float(end ** 2) * \
                    (mean_norm - mean_norm_batch) ** 2
        std_norm = np.sqrt(std_norm)

        return mean_norm, std_norm

    def _frobenius_norm(activations):
        norm = np.linalg.norm(
                activations, ord='fro', 
                axis=tuple(range(1, len(activations.shape) - 1)))
        return norm

    def _inf_norm(activations):
        norm = np.max(np.abs(activations),
                      axis=tuple(range(1, len(activations.shape) - 1)))
        return norm

    model = del_extra_nodes(model)

    n_images = images.shape[0]
    n_batches_per_epoch = int(np.ceil(float(n_images) / batch_size))

    # Get relevant layers
    if include_input:
        layer_regex = '({}|.*input.*)'.format(layer_regex)
    else:
        layer_regex = layer_regex

    layers = [layer.name for layer in model.layers 
              if re.compile(layer_regex).match(layer.name)]

    # Initialize HDF5 to store the activations
#     filename = 'hdf5_aux_{}'.format(time.time())
#     activations_hdf5_aux = h5py.File(filename, 'w')
#     hdf5_aux = [filename]
# 
#     grp_activations = activations_hdf5_aux.create_group('activations')

    if class_invariance:
#         grp_labels = activations_hdf5_aux.create_group('labels')
        labels_true_da = []
        labels_pred_da = []
        predictions_da = []
#         labels_true = grp_labels.create_dataset(
#                 'labels_true', shape=(n_images, ), dtype=np.uint8)
#         labels_pred = grp_labels.create_dataset(
#                 'labels_pred', shape=(n_images, ), dtype=np.uint8)
#         predictions = grp_labels.create_dataset(
#                 'predictions', shape=labels.shape, dtype=K.floatx())
        idx_softmax = model.output_names.index('softmax')
        store_labels = True
    else:
        store_labels = False

    # Initialize results dictionary
    results_dict = {'activations_norm': {}, 'summary': {}, 
                    'class_invariance': {}, 'daug_invariance': {}} 

    # Iterate over the layers
    for layer_name in layers:

        # Create batch generator
        image_gen = get_generator(images, **nodaug_params)
        batch_gen = generate_batches(image_gen, images, labels, batch_size,
                                     aug_per_im=1, shuffle=False)

        layer = model.get_layer(layer_name)
        layer_shape = layer.output_shape[1:]
        n_channels = layer_shape[-1]

        if re.compile('.*input.*').match(layer_name):
            layer_name = 'input'

        print('\nLayer {}\n'.format(layer_name))

        # Create a Dataset for the activations of the layer
#         activations_layer = grp_activations.create_dataset(
#                 layer_name, shape=(n_images, ) + layer_shape, 
#                 dtype=K.floatx())
        # Create dask array for the activations of the layer
        activations_layer_da = []

        # Initialize placeholders in the results dict for the layer
        results_dict['activations_norm'].update({layer_name: 
            {n: {'mean': np.zeros(n_channels), 
                 'std': np.zeros(n_channels)} for n in norms}})
        layer_dict = results_dict['activations_norm'][layer_name]

        activation_function = K.function([model.input, 
                                          K.learning_phase()], 
                                         [layer.output])

        # Iterate over the data set in batches
        init = 0
        for batch_images, batch_labels in tqdm(
                batch_gen, total=n_batches_per_epoch):

            batch_size = batch_images.shape[0]
            end = init + batch_size

            # Store labels
            if store_labels:
                preds = model.predict_on_batch(batch_images)
                if isinstance(preds, list):
                    preds = preds[idx_softmax]
                labels_pred_da.append(da.from_array(
                    np.argmax(preds, axis=1)))
                labels_true_da.append(da.from_array(
                    np.argmax(batch_labels, axis=1)))
                predictions_da.append(da.from_array(preds))
#                 labels_pred[init:end] = np.argmax(preds, axis=1)
#                 labels_true[init:end] = np.argmax(batch_labels, axis=1)
#                 predictions[init:end, :] = preds

            # Get and store activations
            activations = activation_function([batch_images, 0])[0]
            activations_layer_da.append(da.from_array(
                activations, chunks=activations.shape))
#             activations_layer[init:end] = activations

            # Compute norms
            for norm_key in norms:
                mean_norm = layer_dict[norm_key]['mean']
                std_norm = layer_dict[norm_key]['std']
                if norm_key == 'fro':
                    norm = _frobenius_norm(activations)
                elif norm_key == 'inf':
                    norm = _inf_norm(activations)
                else:
                    raise NotImplementedError('Implemented norms are fro '
                            'and inf')
                mean_norm, std_norm = _update_stats(mean_norm, std_norm, 
                                                    norm)
                layer_dict[norm_key]['mean'] = mean_norm
                layer_dict[norm_key]['std'] = std_norm

            init = end
            if init == n_images:
                store_labels = False
                break

        # Concatenate dask arrays
        activations_layer_da = da.concatenate(activations_layer_da, axis=0)
        activations_layer_da = activations_layer_da.reshape((n_images, -1))
        d_activations = activations_layer_da.shape[-1]

        if class_invariance:
            print('\nComputing class invariance\n')
            labels_pred_da = da.concatenate(labels_pred_da)
            labels_true_da = da.concatenate(labels_true_da)
            predictions_da = da.concatenate(predictions_da)
            n_classes = len(np.unique(labels_true_da))

        # Compute MSE matrix of the activations
        r = da.reshape(da.sum(da.square(activations_layer_da), 
                                        axis=1), (-1, 1))
        mse_matrix_da = (r - 2 * da.dot(activations_layer_da,
                                     da.transpose(activations_layer_da)) \
                     + da.transpose(r)) / d_activations
        mse_matrix_da = mse_matrix_da.rechunk((mse_matrix_da.chunksize[0],
                                               mse_matrix_da.shape[-1]))

        # Compute class invariance
        time0 = time()
        results_dict['class_invariance'].update({layer_name: {}})
        class_invariance_scores_da = []
        if class_invariance:
#             mse_matrix_mean = da.mean(mse_matrix_da).compute()
            for cl in tqdm(range(n_classes)):
                labels_cl = labels_pred_da == cl
                labels_cl = labels_cl.compute()
                mse_class = mse_matrix_da[labels_cl, :][:, labels_cl]
                mse_class = mse_class.rechunk((-1, -1))
#                 mse_class_mean = da.mean(mse_class).compute()
#                 class_invariance_score = 1. - np.divide(
#                         mse_class_mean, mse_matrix_mean)
#                 results_dict['class_invariance'][layer_name].update(
#                         {cl: class_invariance_score})
                class_invariance_scores_da.append(
                        1. - da.divide(da.mean(mse_class),
                                       da.mean(mse_matrix_da)))

        # Compute data augmentation invariance
        print('\nComputing data augmentation invariance\n')
        mse_daug_da = []

        results_dict['daug_invariance'].update({layer_name: {}})

        for r in range(n_daug_rep):
            print('Repetition {}'.format(r))

            image_gen_daug = get_generator(images, **daug_params)
            batch_gen_daug = generate_batches(image_gen_daug, images, labels, 
                                              batch_size, aug_per_im=1, 
                                              shuffle=False)

            activations_layer_daug_da = []

            # Iterate over the data set in batches to compute activations
            init = 0
            for batch_images, batch_labels in tqdm(
                    batch_gen, total=n_batches_per_epoch):

                batch_size = batch_images.shape[0]
                end = init + batch_size

                # Get and store activations
                activations = activation_function([batch_images, 0])[0]
                activations_layer_daug_da.append(da.from_array(
                    activations, chunks=activations.shape))

                init = end
                if init == n_images:
                    break

            activations_layer_daug_da = da.concatenate(
                    activations_layer_daug_da, axis=0)
            activations_layer_daug_da = activations_layer_daug_da.reshape(
                    (n_images, -1))
            activations_layer_daug_da = activations_layer_daug_da.rechunk(
                    (activations_layer_daug_da.chunksize[0],
                     activations_layer_daug_da.shape[-1]))

            # Compute MSE daug
            mse_daug_da.append(da.mean(da.square(activations_layer_da - \
                                                 activations_layer_daug_da), 
                                       axis=1))

        mse_daug_da = da.stack(mse_daug_da, axis=1)

        mse_sum = da.repeat(da.reshape(da.sum(mse_matrix_da, axis=1),
                                       (n_images, 1)), n_daug_rep, axis=1)

        daug_invariance_score_da = 1 - n_images * da.divide(mse_daug_da, mse_sum)

        time1 = time()

        # Compute dask results and update results dict
        results_dask = da.compute(class_invariance_scores_da,
                                  daug_invariance_score_da)

        time2 = time()

        results_dict['class_invariance'][layer_name].update(
                {cl: cl_inv_score 
                    for cl, cl_inv_score in enumerate(results_dask[0])})
        results_dict['daug_invariance'].update({layer_name: 
            {r: daug_inv_score 
                for r, daug_inv_score in enumerate(results_dask[1].T)}})
    # Compute summary statistics of the norms across the channels
    for layer, layer_dict in results_dict['activations_norm'].items():
        results_dict['summary'].update({layer: {}})
        for norm_key, norm_dict in layer_dict.items():
            results_dict['summary'][layer].update({norm_key: {
                'mean': np.mean(norm_dict['mean']), 
                'std': np.mean(norm_dict['std'])}})

    return results_dict
Exemple #16
0
 def __init__(self,
              mean_inputs=1,
              hidden_layers=2,
              hidden_neurons=8,
              activation="selu",
              l2_weight=0.01,
              learning_rate=0.001,
              mean_loss="mse",
              res_loss="kullback_leibler_divergence",
              noise_sd=1,
              beta_1=0.9,
              model_path=None,
              dropout_alpha=0.5,
              num_epochs=10,
              batch_size=1024,
              val_split=0.5,
              verbose=0,
              model_config=0):
     self.config = dict(mean_inputs=mean_inputs,
                        hidden_layers=hidden_layers,
                        hidden_neurons=hidden_neurons,
                        activation=activation,
                        l2_weight=l2_weight,
                        learning_rate=learning_rate,
                        mean_loss=mean_loss,
                        res_loss=res_loss,
                        noise_sd=noise_sd,
                        beta_1=beta_1,
                        dropout_alpha=dropout_alpha,
                        model_path=model_path,
                        num_epochs=num_epochs,
                        batch_size=batch_size,
                        verbose=verbose,
                        model_config=model_config,
                        val_split=val_split)
     mean_model_file = join(
         model_path, "annres_config_{0:04d}_mean.nc".format(
             self.config["model_config"]))
     res_model_file = join(
         model_path,
         "annres_config_{0:04d}_res.nc".format(self.config["model_config"]))
     self.x_scaling_file = join(
         model_path,
         "annres_scaling_values_{0:04d}.csv".format(model_config))
     if model_path is None or not exists(mean_model_file):
         nn_input = Input((mean_inputs, ))
         nn_model = nn_input
         for h in range(hidden_layers):
             nn_model = Dense(hidden_neurons,
                              kernel_regularizer=l2(l2_weight))(nn_model)
             if activation == "leaky":
                 nn_model = LeakyReLU(0.1)(nn_model)
             else:
                 nn_model = Activation(activation)(nn_model)
         nn_model = Dense(1)(nn_model)
         nn_res_input_x = Input((mean_inputs, ))
         nn_res_input_res = Input((1, ))
         nn_res = concatenate([nn_res_input_x, nn_res_input_res])
         for h in range(hidden_layers):
             nn_res = Dense(hidden_neurons,
                            kernel_regularizer=l2(l2_weight))(nn_res)
             if activation == "leaky":
                 nn_res = LeakyReLU(0.1)(nn_res)
             else:
                 nn_res = Activation(activation)(nn_res)
             nn_res = Dropout(dropout_alpha)(nn_res)
             nn_res = GaussianNoise(noise_sd)(nn_res)
         nn_res = Dense(1)(nn_res)
         self.mean_model = Model(nn_input, nn_model)
         self.mean_model.compile(Adam(lr=learning_rate, beta_1=beta_1),
                                 loss=mean_loss)
         self.res_model = Model([nn_res_input_x, nn_res_input_res], nn_res)
         self.res_model.compile(Adam(lr=learning_rate, beta_1=beta_1),
                                loss=res_loss)
         self.x_scaling_values = None
     elif type(model_path) == str:
         self.mean_model = load_model(mean_model_file)
         self.res_model = load_model(res_model_file)
         self.x_scaling_values = pd.read_csv(self.x_scaling_file,
                                             index_col="Channel")
     self.res_predict = K.function(
         self.res_model.input + [K.learning_phase()],
         [self.res_model.output])
Exemple #17
0
    def __init__(self,
                 inputs=1,
                 hidden_layers=2,
                 hidden_neurons=8,
                 activation="selu",
                 l2_weight=0.01,
                 learning_rate=0.001,
                 loss="mse",
                 noise_sd=1,
                 beta_1=0.9,
                 model_path=None,
                 dropout_alpha=0.5,
                 num_epochs=10,
                 batch_size=1024,
                 verbose=0,
                 model_config=0,
                 **kwargs):
        self.config = dict(inputs=inputs,
                           hidden_layers=hidden_layers,
                           hidden_neurons=hidden_neurons,
                           activation=activation,
                           l2_weight=l2_weight,
                           learning_rate=learning_rate,
                           loss=loss,
                           noise_sd=noise_sd,
                           beta_1=beta_1,
                           dropout_alpha=dropout_alpha,
                           model_path=model_path,
                           num_epochs=num_epochs,
                           batch_size=batch_size,
                           verbose=verbose,
                           model_config=model_config,
                           corr=1,
                           res_sd=0)
        if model_path is None:
            nn_input = Input((inputs, ))
            nn_model = nn_input
            for h in range(hidden_layers):
                nn_model = Dense(hidden_neurons,
                                 kernel_regularizer=l2(l2_weight))(nn_model)
                if activation == "leaky":
                    nn_model = LeakyReLU(0.1)(nn_model)
                else:
                    nn_model = Activation(activation)(nn_model)
                if dropout_alpha > 0:
                    nn_model = Dropout(dropout_alpha)(nn_model)
                if noise_sd > 0:
                    nn_model = GaussianNoise(noise_sd)(nn_model)
            nn_model = Dense(1)(nn_model)
            self.model = Model(nn_input, nn_model)
            self.model.compile(Adam(lr=learning_rate, beta_1=beta_1),
                               loss=loss)
            self.x_scaling_file = None
            self.x_scaling_values = None
            self.sample_predict = K.function(
                [self.model.input, K.learning_phase()], [self.model.output])
        elif type(model_path) == str:
            model_path_start = join(*model_path.split("/")[:-1])
            self.model = load_model(model_path)
            self.sample_predict = K.function(
                [self.model.input, K.learning_phase()], [self.model.output])

            self.x_scaling_file = join(
                model_path_start, "ann_config_{0:04d}_scale.csv".format(
                    self.config["model_config"]))
            self.x_scaling_values = pd.read_csv(self.x_scaling_file,
                                                index_col="Channel")