|
| 1 | +# Copyright 2016 Google Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | + |
| 16 | + |
| 17 | +from __future__ import absolute_import |
| 18 | +from __future__ import division |
| 19 | +from __future__ import print_function |
| 20 | + |
| 21 | +import numpy as np |
| 22 | + |
| 23 | + |
| 24 | +def labels_from_probs(probs): |
| 25 | + """ |
| 26 | + Helper function: computes argmax along last dimension of array to obtain |
| 27 | + labels (max prob or max logit value) |
| 28 | + :param probs: numpy array where probabilities or logits are on last dimension |
| 29 | + :return: array with same shape as input besides last dimension with shape 1 |
| 30 | + now containing the labels |
| 31 | + """ |
| 32 | + # Compute last axis index |
| 33 | + last_axis = len(np.shape(probs)) - 1 |
| 34 | + |
| 35 | + # Label is argmax over last dimension |
| 36 | + labels = np.argmax(probs, axis=last_axis) |
| 37 | + |
| 38 | + # Return as np.int32 |
| 39 | + return np.asarray(labels, dtype=np.int32) |
| 40 | + |
| 41 | + |
| 42 | +def noisy_max(logits, lap_scale, return_clean_votes=False): |
| 43 | + """ |
| 44 | + This aggregation mechanism takes the softmax/logit output of several models |
| 45 | + resulting from inference on identical inputs and computes the noisy-max of |
| 46 | + the votes for candidate classes to select a label for each sample: it |
| 47 | + adds Laplacian noise to label counts and returns the most frequent label. |
| 48 | + :param logits: logits or probabilities for each sample |
| 49 | + :param lap_scale: scale of the Laplacian noise to be added to counts |
| 50 | + :param return_clean_votes: if set to True, also returns clean votes (without |
| 51 | + Laplacian noise). This can be used to perform the |
| 52 | + privacy analysis of this aggregation mechanism. |
| 53 | + :return: pair of result and (if clean_votes is set to True) the clean counts |
| 54 | + for each class per sample and the the original labels produced by |
| 55 | + the teachers. |
| 56 | + """ |
| 57 | + |
| 58 | + # Compute labels from logits/probs and reshape array properly |
| 59 | + labels = labels_from_probs(logits) |
| 60 | + labels_shape = np.shape(labels) |
| 61 | + labels = labels.reshape((labels_shape[0], labels_shape[1])) |
| 62 | + |
| 63 | + # Initialize array to hold final labels |
| 64 | + result = np.zeros(int(labels_shape[1])) |
| 65 | + |
| 66 | + if return_clean_votes: |
| 67 | + # Initialize array to hold clean votes for each sample |
| 68 | + clean_votes = np.zeros((int(labels_shape[1]), 10)) |
| 69 | + |
| 70 | + # Parse each sample |
| 71 | + for i in xrange(int(labels_shape[1])): |
| 72 | + # Count number of votes assigned to each class |
| 73 | + label_counts = np.bincount(labels[:,i], minlength=10) |
| 74 | + |
| 75 | + if return_clean_votes: |
| 76 | + # Store vote counts for export |
| 77 | + clean_votes[i] = label_counts |
| 78 | + |
| 79 | + # Cast in float32 to prepare before addition of Laplacian noise |
| 80 | + label_counts = np.asarray(label_counts, dtype=np.float32) |
| 81 | + |
| 82 | + # Sample independent Laplacian noise for each class |
| 83 | + for item in xrange(10): |
| 84 | + label_counts[item] += np.random.laplace(loc=0.0, scale=float(lap_scale)) |
| 85 | + |
| 86 | + # Result is the most frequent label |
| 87 | + result[i] = np.argmax(label_counts) |
| 88 | + |
| 89 | + # Cast labels to np.int32 for compatibility with deep_cnn.py feed dictionaries |
| 90 | + result = np.asarray(result, dtype=np.int32) |
| 91 | + |
| 92 | + if return_clean_votes: |
| 93 | + # Returns several array, which are later saved: |
| 94 | + # result: labels obtained from the noisy aggregation |
| 95 | + # clean_votes: the number of teacher votes assigned to each sample and class |
| 96 | + # labels: the labels assigned by teachers (before the noisy aggregation) |
| 97 | + return result, clean_votes, labels |
| 98 | + else: |
| 99 | + # Only return labels resulting from noisy aggregation |
| 100 | + return result |
| 101 | + |
| 102 | + |
| 103 | +def aggregation_most_frequent(logits): |
| 104 | + """ |
| 105 | + This aggregation mechanism takes the softmax/logit output of several models |
| 106 | + resulting from inference on identical inputs and computes the most frequent |
| 107 | + label. It is deterministic (no noise injection like noisy_max() above. |
| 108 | + :param logits: logits or probabilities for each sample |
| 109 | + :return: |
| 110 | + """ |
| 111 | + # Compute labels from logits/probs and reshape array properly |
| 112 | + labels = labels_from_probs(logits) |
| 113 | + labels_shape = np.shape(labels) |
| 114 | + labels = labels.reshape((labels_shape[0], labels_shape[1])) |
| 115 | + |
| 116 | + # Initialize array to hold final labels |
| 117 | + result = np.zeros(int(labels_shape[1])) |
| 118 | + |
| 119 | + # Parse each sample |
| 120 | + for i in xrange(int(labels_shape[1])): |
| 121 | + # Count number of votes assigned to each class |
| 122 | + label_counts = np.bincount(labels[:,i], minlength=10) |
| 123 | + |
| 124 | + label_counts = np.asarray(label_counts, dtype=np.int32) |
| 125 | + |
| 126 | + # Result is the most frequent label |
| 127 | + result[i] = np.argmax(label_counts) |
| 128 | + |
| 129 | + return np.asarray(result, dtype=np.int32) |
| 130 | + |
| 131 | + |
0 commit comments