@@ -0,0 +1,2225 @@
+# -*- coding: utf-8 -*-%0A%22%22%22%0ACreated on Tue Jan 10 00:15:50 2017%0A%0A@author: sakurai%0A%22%22%22%0A%0Aimport numpy as np%0Afrom sklearn.preprocessing import LabelEncoder%0A%0Afrom datasets import get_cars196_streams%0A%0A%0Aclass DataProvider(object):%0A    def __init__(self, stream, batch_size):%0A        self._stream = stream%0A        if hasattr(stream, 'data_stream'):%0A            data_stream = stream.data_stream%0A        else:%0A            data_stream = stream%0A        self.num_examples = data_stream.dataset.num_examples%0A%0A        labels = %5Bstream.get_data(%5Bi%5D)%5B1%5D%5B0%5D for i in range(self.num_examples)%5D%0A        labels = np.ravel(labels)%0A%0A        self._label_encoder = LabelEncoder().fit(labels)%0A        self._labels = np.array(labels)%0A        self._classes = self._label_encoder.classes_%0A        self.num_classes = len(self._classes)%0A        assert batch_size %3C= self.num_classes, (%0A               %22batch_size must not be greather than the number of classes%22%0A               %22 (i.e. batch_size %3C= %7B%7D)%22.format(self.num_classes))%0A        self.batch_size = batch_size%0A%0A        self._class_to_indexes = %5B%5D%0A        for c in self._classes:%0A            self._class_to_indexes.append(%0A                np.argwhere(self._labels == c).ravel())%0A%0A    def next(self):%0A        anchor_indexes, positive_indexes = self._generate_indexes()%0A        indexes = anchor_indexes + positive_indexes%0A        return self._stream.get_data(indexes)%0A%0A    def _generate_indexes(self):%0A        random_classes = np.random.choice(%0A            self.num_classes, self.batch_size, False)%0A        anchor_indexes = %5B%5D%0A        positive_indexes = %5B%5D%0A        for c in random_classes:%0A            a, p = np.random.choice(self._class_to_indexes%5Bc%5D, 2, False)%0A            anchor_indexes.append(a)%0A            positive_indexes.append(p)%0A        return anchor_indexes, positive_indexes%0A%0A%0Aif __name__ == '__main__':%0A    train, _ = get_cars196_streams(load_in_memory=True)%0A    provider = DataProvider(train, 25)%0A%0A    anchor_indexes, positive_indexes = provider._generate_indexes()%0A    print anchor_indexes%0A    print positive_indexes%0A    a_classes = provider._labels%5Banchor_indexes%5D%0A    p_classes = provider._labels%5Bpositive_indexes%5D%0A    print np.all(a_classes == p_classes)%0A%0A    x, c = provider.next()%0A

Implement a class to feed minibatches for N-pair-mc training