コード例 #1
0
                x, rng.randint(1, x.shape[0]), axis=rng.choice([0, 1]))
        ]

        while True:

            rng.shuffle(didxs)
            didxs_cycle = cycle(didxs)

            for _ in range(nb_steps):

                ib = np.zeros(ib_shape, dtype=np.float16)
                tb = np.zeros(tb_shape, dtype=np.int16)
                db = np.zeros((self.cfg['trn_batch_size'], ), dtype=np.uint16)

                for bidx in range(self.cfg['trn_batch_size']):
                    db[bidx] = next(didxs_cycle)
                    img = imgs.get(str(db[bidx]))[...]
                    ib[bidx] = imresize(img, self.cfg['input_shape'])
                    tb[bidx] = tags[db[bidx]]
                    for aug in rng.choice(aug_funcs,
                                          rng.randint(0, nb_augment_max + 1)):
                        ib[bidx] = aug(ib[bidx])

                yield self.cfg['preprocess_imgs_func'](
                    ib), self.cfg['preprocess_tags_func'](tb)


if __name__ == "__main__":
    from planet.model_runner import model_runner
    model_runner(ResNet50())
コード例 #2
0
        for i in range(self.config['output_shape'][0]):
            # The name of the layer we want to visualize
            layer_name = 'output_%d' % i
            layer_idx = [
                idx for idx, layer in enumerate(self.net.layers)
                if layer.name == layer_name
            ][0]

            print('Working on %s' % layer_name)
            # Generate input image for each filter. Here `text` field is used to
            # overlay `filter_value` on top of the image.
            for idx in [1, 1, 1]:
                img = visualize_activation(self.net,
                                           layer_idx,
                                           filter_indices=idx,
                                           max_iter=500)
                # img = utils.draw_text(img, TAGS[i])
                vis_images.append(img)

        # Generate stitched image palette with 8 cols.
        stitched = utils.stitch_images(vis_images, cols=3)
        plt.axis('off')
        plt.imshow(stitched)
        plt.title(self.checkpoint_name)
        plt.show()


if __name__ == "__main__":
    from planet.model_runner import model_runner
    model_runner(Inception_V3())
コード例 #3
0
        # Lookup tags to images with those tags.
        tidxs = np.arange(len(TAGS))
        tidxs_cycle = cycle(tidxs)
        tidx_to_didxs = [np.where(tags[didxs, ti] == 1)[0] for ti in tidxs]

        while True:

            rng.shuffle(didxs)
            didxs_cycle = cycle(didxs)

            for _ in range(nb_steps):

                ib = np.zeros(ib_shape, dtype=np.float16)
                tb = np.zeros(tb_shape, dtype=np.int16)

                for bidx in range(self.cfg['trn_batch_size']):
                    didx = rng.choice(tidx_to_didxs[next(tidxs_cycle)]) if balanced else next(didxs_cycle)
                    ib[bidx] = imresize(imgs[didx, ...], self.cfg['input_shape'])
                    tb[bidx] = tags[didx]
                    for aug in rng.choice(aug_funcs, rng.randint(0, nb_augment_max + 1)):
                        ib[bidx] = aug(ib[bidx])

                yield ib, tb

    def predict_batch(self, imgs_batch):
        return self.net.predict_on_batch(imgs_batch)

if __name__ == "__main__":
    from planet.model_runner import model_runner
    model_runner(CNNFinetune)
コード例 #4
0
            return yp
        else:
            return self.net.predict_on_batch(imgs_batch)

    def predict(self, dataset):

        path = self.cfg['hdf5_path_trn'] if dataset == 'train' else self.cfg[
            'hdf5_path_tst']
        data = h5py.File(path)
        yt = data.get('tags')[...]
        yp = np.zeros(yt.shape, dtype=np.float32)
        imgs = data.get('images')
        names = data.attrs['names'].split(',')
        imgs_batch = np.zeros(
            (self.cfg['tst_batch_size'], *self.cfg['input_shape']),
            dtype=np.float32)

        for didx in tqdm(range(0, len(imgs), self.cfg['tst_batch_size'])):
            bsz = min(self.cfg['tst_batch_size'], len(imgs) - didx)
            for bidx in range(bsz):
                imgs_batch[bidx] = imresize(imgs[didx + bidx, ...],
                                            self.cfg['input_shape'])
            yp[didx:didx + bsz] = self.predict_batch(imgs_batch[:bsz])

        return names, yt, yp


if __name__ == "__main__":
    from planet.model_runner import model_runner
    model_runner(Godard)
コード例 #5
0
                    imgs_batch[batch_idx] = self.img_path_to_img(img_pths[img_idx])
                    tags_batch[batch_idx] = img_tags[img_idx]

                    if transform:
                        imgs_batch[batch_idx] = random_transforms(imgs_batch[batch_idx], 0, 4)

                yield imgs_batch, tags_batch

    def img_path_to_img(self, img_path):
        img = Image.open(img_path).convert('RGB')
        img.thumbnail(self.config['input_shape'][:2])
        img = np.asarray(img, dtype=np.float32) / 255.
        return img

    def predict(self, imgs_names):

        imgs_dir = self.config['trn_imgs_dir'] if 'train' in imgs_names[0] else self.config['tst_imgs_dir']
        imgs_paths = ['%s/%s.%s' % (imgs_dir, name, self.config['extension']) for name in imgs_names]
        shape = [len(imgs_names), ] + self.config['input_shape']
        imgs_batch = np.zeros(shape, dtype=np.float32)

        for bidx, img_path in enumerate(imgs_paths):
            imgs_batch[bidx] = self.img_path_to_img(img_path)

        return self.net.predict(imgs_batch).round().astype(np.uint8)

if __name__ == "__main__":
    from planet.model_runner import model_runner
    model_runner(ResNet50_softmax())
コード例 #6
0
                img = imread(imgs_paths[data_idx], mode='RGB')
                img = img_preprocess(img)
                img = resize(img, self.config['input_shape'], preserve_range=True, mode='constant')
                if transform:
                    img = random_transforms(img, nb_min=0, nb_max=6)
                imgs_batch[batch_idx] = img
                tags_batch[batch_idx] = tagset_to_ints(tag_sets[data_idx])

            yield imgs_batch, tags_batch

    def predict(self, img_batch):

        # Get the mean image
        imgs_paths = listdir(self.config['trn_imgs_dir'])
        mean_img_path = '%s/mean_img_trn.jpg' % self.cpdir
        mean_img = self.get_mean_img(imgs_paths, mean_img_path).astype(np.float32) / 255.
        mean_img_mean = np.mean(mean_img)
        img_preprocess = lambda img: img.astype(np.float32) / 255. - mean_img_mean

        for idx in range(len(img_batch)):
            img_batch[idx] = img_preprocess(img_batch[idx])

        tags_pred = self.net.predict(img_batch)
        tags_pred = tags_pred.round().astype(np.uint8)
        return tags_pred

if __name__ == "__main__":
    from planet.model_runner import model_runner
    model = VGGNet()
    model_runner(model)
コード例 #7
0
        aug_funcs = [
            lambda x: x, lambda x: np.flipud(x), lambda x: np.fliplr(x),
            lambda x: np.rot90(x, rng.randint(1, 4))
            # lambda x: rotate(x, rng.randint(0, 360), reshape=False, mode='reflect')
        ]

        while True:

            rng.shuffle(didxs)
            didxs_cycle = cycle(didxs)

            for _ in range(nb_steps):

                ib = np.zeros(ib_shape, dtype=np.float16)
                tb = np.zeros(tb_shape, dtype=np.int16)
                db = np.zeros((self.cfg['trn_batch_size'],), dtype=np.uint16)

                for bidx in range(self.cfg['trn_batch_size']):
                    db[bidx] = next(didxs_cycle)
                    img = imgs.get(str(db[bidx]))[...]
                    ib[bidx] = imresize(img, self.cfg['input_shape'])
                    tb[bidx] = tags[db[bidx]]
                    for aug in rng.choice(aug_funcs, rng.randint(0, nb_augment_max)):
                        ib[bidx] = aug(ib[bidx])

                yield self.cfg['preprocess_imgs_func'](ib), self.cfg['preprocess_tags_func'](tb)

if __name__ == "__main__":
    from planet.model_runner import model_runner
    model_runner(LuckyLoser())
コード例 #8
0
ファイル: autoknn.py プロジェクト: alexklibisz/kaggle-planet
            x0 = rng.randint(0, img.shape[1] - w)
            y1 = y0 + h
            x1 = x0 + w
            return img[y0:y1, x0:x1, :]

        def get_img(idx):
            if idx < len(imgs_trn):
                return imgs_trn.get(str(idx))[...]
            else:
                return imgs_tst.get(str(idx - len(imgs_trn)))[...]

        while True:

            rng.shuffle(didxs)
            didxs_cycle = cycle(didxs)

            for _ in range(nb_steps):
                ib = np.zeros(ib_shape, dtype=np.float16)

                for bidx in range(self.cfg['trn_batch_size']):
                    didx = next(didxs_cycle)
                    img = get_img(didx)
                    ib[bidx] = crop(img)

                ib = self.cfg['preprocess_imgs_func'](ib)
                yield ib, ib

if __name__ == "__main__":
    from planet.model_runner import model_runner
    model_runner(AutoKNN())
コード例 #9
0
                             self.config['input_shape'][:2],
                             preserve_range=True,
                             mode='constant')
                if self.config['trn_transform']:
                    imgs_batch[batch_idx] = scale(
                        random_transforms(img, nb_min=0, nb_max=5))
                else:
                    imgs_batch[batch_idx] = scale(img)
                tags_batch[batch_idx] = tagset_to_onehot(tag_sets[data_idx])

            yield imgs_batch, tags_batch

    def predict(self, img_batch):

        scale = lambda x: (x - np.min(x)) / (np.max(x) - np.min(x)) * 2 - 1

        for idx in range(len(img_batch)):
            img_batch[idx] = scale(img_batch[idx])

        tags_pred = self.net.predict(img_batch)
        tags_pred = tags_pred.round().astype(np.uint8)

        # Convert from onehot to an array of bools
        tags_pred = tags_pred[:, :, 1]
        return tags_pred


if __name__ == "__main__":
    from planet.model_runner import model_runner
    model_runner(IndiConv)
コード例 #10
0
            tags_batch = np.zeros([self.config['batch_size'], ] + self.config['output_shape'], dtype=np.uint8)

            # Sample *self.config['batch_size']* random rows and build the batches.
            for batch_idx in range(self.config['batch_size']):
                data_idx = self.rng.randint(0, len(img_names))

                img = resize(imread(img_names[data_idx], mode='RGB'), self.config[
                    'input_shape'][:2], preserve_range=True, mode='constant')
                if self.config['trn_transform']:
                    imgs_batch[batch_idx] = scale(random_transforms(img, nb_min=0, nb_max=2))
                else:
                    imgs_batch[batch_idx] = scale(img)
                tags_batch[batch_idx] = tagset_to_boolarray(tag_sets[data_idx])

            yield imgs_batch, tags_batch

    def predict(self, img_batch):
        scale = lambda x: (x - np.min(x)) / (np.max(x) - np.min(x)) * 2 - 1

        for idx in range(len(img_batch)):
            img_batch[idx] = scale(img_batch[idx])

        tags_pred = self.net.predict(img_batch)
        tags_pred = tags_pred.round().astype(np.uint8)

        return tags_pred

if __name__ == "__main__":
    from planet.model_runner import model_runner
    model_runner(ResNet50_sigmoid)