コード例 #1
0
    def sample(self, nb_classes, nb_samples_per_class,
               nb_samples_per_class_test):
        sampled_characters = random.sample(self.data.keys(),
                                           nb_classes)  # list of keys
        labels_and_images_support = []
        labels_and_images_test = []
        for (k, char) in enumerate(sampled_characters):
            deg = random.sample(range(4), 1)[0]
            _imgs = self.data[char]
            _ind = random.sample(
                range(len(_imgs)),
                nb_samples_per_class + nb_samples_per_class_test)
            _ind_tr = _ind[:nb_samples_per_class]
            labels_and_images_support.extend([(k, rotate_right(_imgs[i],
                                                               deg).flatten())
                                              for i in _ind_tr])
            _ind_test = _ind[nb_samples_per_class:]
            labels_and_images_test.extend([(k, rotate_right(_imgs[i],
                                                            deg).flatten())
                                           for i in _ind_test])

        random.shuffle(labels_and_images_support)
        labels_tr, images_tr = zip(*labels_and_images_support)

        random.shuffle(labels_and_images_test)
        labels_test, images_test = zip(*labels_and_images_test)
        return images_tr, labels_tr, images_test, labels_test
コード例 #2
0
 def sample(self, nb_classes, nb_samples_per_class):
     sampled_characters = random.sample(self.data.keys(), nb_classes) # list of keys
     labels_and_images = []
     for (k, char) in enumerate(sampled_characters):
         deg = random.sample(range(4), 1)[0]
         _imgs = self.data[char]
         _ind = random.sample(range(len(_imgs)), nb_samples_per_class)
         labels_and_images.extend([(k, rotate_right(_imgs[i], deg).flatten()) for i in _ind])
         
     random.shuffle(labels_and_images)
     sequence_length = len(labels_and_images)
     labels, images = zip(*labels_and_images)
     return images, labels
コード例 #3
0
ファイル: generators.py プロジェクト: pukekaka/dogs
    def sample(self, nb_classes, nb_samples_per_class):
        sampled_characters = random.sample(self.data.keys(),
                                           nb_classes)  # list of keys
        labels_and_images = []
        for (k, char) in enumerate(sampled_characters):
            deg = random.sample(range(4), 1)[0]
            _imgs = self.data[char]
            _ind = random.sample(range(len(_imgs)), nb_samples_per_class)
            labels_and_images.extend([(k, rotate_right(_imgs[i],
                                                       deg).flatten())
                                      for i in _ind])

        random.shuffle(labels_and_images)
        sequence_length = len(labels_and_images)
        labels, images = zip(*labels_and_images)
        return images, labels
コード例 #4
0
    def next(self):
        if (self.max_iter is None) or (self.num_iter < self.max_iter):
            labels_and_images_support = []
            labels_and_images_test = []

            self.num_iter += 1
            sampled_characters = random.sample(self.data.keys(),
                                               self.nb_classes)  # list of keys

            for _ in range(self.batchsize):
                support_set = []

                for k, char in enumerate(sampled_characters):
                    deg = random.sample(range(4), 1)[0]
                    _imgs = self.data[char]
                    _ind = random.sample(
                        range(len(_imgs)), self.nb_samples_per_class +
                        self.nb_samples_per_class_test)
                    _ind_tr = _ind[:self.nb_samples_per_class]
                    if self.augment:
                        support_set.extend([(k,
                                             self.xp.array(
                                                 rotate_right(_imgs[i],
                                                              deg).flatten()))
                                            for i in _ind_tr])
                    else:
                        support_set.extend([(k,
                                             self.xp.array(_imgs[i].flatten()))
                                            for i in _ind_tr])
                    _ind_test = _ind[self.nb_samples_per_class:]
                    if self.augment:
                        labels_and_images_test.extend([
                            (k,
                             self.xp.array(
                                 rotate_right(_imgs[i], deg).flatten()))
                            for i in _ind_test
                        ])
                    else:
                        labels_and_images_test.extend([
                            (k, self.xp.array(_imgs[i].flatten()))
                            for i in _ind_test
                        ])

                random.shuffle(support_set)
                labels_tr, images_tr = zip(*support_set)

                labels_and_images_support.append((images_tr, labels_tr))

            _images, _labels = zip(*labels_and_images_support)
            # images_tr = [
            # 	self.xp.concatenate(map(lambda x: x.reshape((1,-1)), _img), axis=0)
            # 	for _img in zip(*_images)]
            labels_tr = [_lbl for _lbl in zip(*_labels)]

            random.shuffle(labels_and_images_test)
            labels_test, images_test = zip(*labels_and_images_test)
            images_test = self.xp.concatenate(
                [img.reshape((1, -1)) for img in images_test], axis=0)

            return (self.num_iter - 1), (images_tr, labels_tr, images_test,
                                         labels_test)
        else:
            raise StopIteration()
コード例 #5
0
    def next(self):
        if (self.max_iter is None) or (self.num_iter < self.max_iter):
            labels_and_images_support = []
            labels_and_images_test = []

            self.num_iter += 1
            sampled_characters = random.sample(self.data.keys(),
                                               self.nb_classes)  # list of keys

            for _ in xrange(self.batchsize):
                support_set = []

                for k, char in enumerate(sampled_characters):
                    deg = random.sample(range(4), 1)[0]
                    _imgs = self.data[char]
                    _ind = random.sample(
                        range(len(_imgs)), self.nb_samples_per_class +
                        self.nb_samples_per_class_test)
                    _ind_tr = _ind[:self.nb_samples_per_class]
                    if self.augment:
                        support_set.extend([
                            (k, self.xp.array(rotate_right(_imgs[i], deg)))
                            for i in _ind_tr
                        ])
                    else:
                        support_set.extend([(k, self.xp.array(_imgs[i]))
                                            for i in _ind_tr])
                    _ind_test = _ind[self.nb_samples_per_class:]
                    if self.augment:
                        labels_and_images_test.extend([
                            (k, self.xp.array(rotate_right(_imgs[i], deg)))
                            for i in _ind_test
                        ])
                    else:
                        labels_and_images_test.extend([
                            (k, self.xp.array(_imgs[i])) for i in _ind_test
                        ])

                random.shuffle(support_set)
                labels_tr, images_tr = zip(*support_set)

                labels_and_images_support.append((images_tr, labels_tr))

            _images, _labels = zip(*labels_and_images_support)
            images_tr = [
                # NOTE: be careful here!
                # I had to permute the dimensions due the output image format from preporcessing
                # please make sure your img dimensions are correct
                self.xp.concatenate(map(
                    lambda x: np.transpose(x, (2, 0, 1)).reshape(
                        (1, 3, 84, 84)), _img),
                                    axis=0) for _img in zip(*_images)
            ]
            labels_tr = [_lbl for _lbl in zip(*_labels)]

            random.shuffle(labels_and_images_test)
            labels_test, images_test = zip(*labels_and_images_test)
            # NOTE: be careful here!
            # I had to permute the dimensions due the output image format from preporcessing
            # please make sure your img dimensions are correct
            images_test = self.xp.concatenate([
                np.transpose(img, (2, 0, 1)).reshape((1, 3, 84, 84))
                for img in images_test
            ],
                                              axis=0)

            return (self.num_iter - 1), (images_tr, labels_tr, images_test,
                                         labels_test)
        else:
            raise StopIteration()