示例#1
0
    def generate(self, test=False):
        while True:
            batch_X = []
            batch_Y = []
            cur_batch_path = np.random.choice(self.all_id, 32)
            for p in cur_batch_path:
                cur_y = self.id2label[p]
                if test and p == -1:
                    continue
                # protect class images in train dataset
                elif p == -1:
                    cur_x = random.choice(self.protect_images_train)
                else:
                    if test:
                        cur_path = random.choice(self.id2pathtest[p])
                    else:
                        cur_path = random.choice(self.id2path[p])
                    im = image.load_img(cur_path, target_size=(224, 224))
                    cur_x = image.img_to_array(im)

                cur_x = preprocess(cur_x, 'imagenet')
                batch_X.append(cur_x)
                batch_Y.append(cur_y)

            batch_X = np.array(batch_X)
            batch_Y = to_categorical(np.array(batch_Y),
                                     num_classes=self.num_classes)

            yield batch_X, batch_Y
示例#2
0
    def clipping(self, imgs):

        imgs = reverse_preprocess(imgs, self.intensity_range)
        imgs = np.clip(imgs, 0, self.max_val)
        imgs = preprocess(imgs, self.intensity_range)

        return imgs