def downloadImageNet():
    print("downloading ImageNet dataset.")

    classes, labels = classesOfClassifier(True)
    cls_urls = {}
    for cls, _, label in nested(labels, depth=2):
        csf_idx, name = label
        wnid = wnidOfName(name)
        urls = fetchImageUrlsOfWnid(wnid)
        if cls not in cls_urls:
            cls_urls[cls] = []
        cls_urls[cls] += [(url, csf_idx) for url in urls]

    counter = {cls: 0 for cls in classes}
    for cls, data in cls_urls.items():
        shuffle(data)
        _data = data[:100]
        data = data[100:]

        for url, csf_idx in _data:
            try:
                img = imread(url)
            except:
                _data.append(data[0])
                data = data[1:]
                continue
            if img is None:
                continue
            idx = counter[cls]
            counter[cls] += 1
            file_name = "{}_{}_{}.jpg".format(getClassName(cls), csf_idx, idx)
            file_path = join(PATH.DATA.IMAGENET.IMGS, file_name)
            saveImage(img, file_path, plugin='skimage')
Esempio n. 2
0
    def test_mask_image(self):
        self.log()
        img = self.getImage("2008_001979.jpg")
        mask = np.zeros(img.shape[:-1])

        center = np.asarray(mask.shape) // 2
        size = (100, 100)
        indices = [np.arange(c - s, c + s) for c, s in zip(center, size)]

        for r in indices[0]:
            for c in indices[1]:
                mask[r][c] = 1

        masks = [mask]

        mask = np.zeros(img.shape[:-1])
        center = (0, 0)
        size = (200, 200)
        indices = [np.arange(c, c + s) for c, s in zip(center, size)]

        for r in indices[0]:
            for c in indices[1]:
                mask[r][c] = 1
        masks.append(mask)

        img = maskImage(img, masks, alpha=0.6)
        # cv2.imshow("test plotter", img)
        # cv2.waitKey(0)
        saveImage(img, os.path.join(root_path, "test_plotter.png"))
Esempio n. 3
0
 def test_reveal_mask(self):
     self.log()
     bl = BatchLoader(amount=1)
     batch = bl.nextBatch()
     img = batch[1][0]
     annos = batch[2][0]
     for anno in annos:
         aid, mask = anno
         output = revealMask(img, mask)
         path = os.path.join(PATH.TEST.ROOT,
                             "{}.png".format(getClassName(aid, full=True)))
         saveImage(output, path)
Esempio n. 4
0
    def test_patch(self):
        self.log()
        bl = BatchLoader(amount=10)
        batch = bl.nextBatch()
        imgs = batch[1]
        annos = batch[2]

        imgs, aids = patch(imgs, annos)
        anames = getClassNames(aids)

        path = os.path.join(PATH.TEST.ROOT, "patch")
        idx = 0
        for img, aname in zip(imgs, anames):
            idx += 1
            file_name = "{}_{}.jpg".format(aname, idx)
            file_path = os.path.join(path, file_name)
            saveImage(img, file_path)
Esempio n. 5
0
 def test_visual_reflect(self):
     self.log()
     bl = BatchLoader(amount=2)
     model = ModelAgent(input_size=1)
     batch = bl.nextBatch()
     imgs = batch[1][-1:]
     annos = batch[2][-1:]
     activ_maps = model.getActivMaps(imgs, ["conv3_1", "conv4_1", "conv5_1"])
     field_maps = model.getFieldmaps()
     reflected = reflect(activ_maps, field_maps)
     img = imgs[0]
     path = os.path.join(PATH.TEST.ROOT, "output/")
     saveImage(img, os.path.join(path, "raw_img.jpg"))
     for unit, ref in reflected.items():
         ref = np.asarray(ref[0])
         saved = np.zeros(shape=ref.shape+(3,))
         indices = np.argwhere(ref>0)
         saved[indices[:,0], indices[:,1]] = [255, 0, 0]
         saveImage(saved, os.path.join(path, unit+".jpg"))
Esempio n. 6
0
 def test_deconv_activ(self):
     bl = BatchLoader(amount=1)
     model = ModelAgent(input_size=1, deconv=True)
     batch = bl.nextBatch()
     img = batch[1]
     activ_maps, switches = model.getActivMaps(img, ["pool5"])
     unit = "pool5_511"
     activ_maps = {unit : activ_maps[unit]}
     deconv = model.getDeconvMaps(activ_maps, switches)
     deconv = deconv[unit][0]
     saveImage(deconv, os.path.join(PATH.TEST.ROOT, "deconv.jpg"))
     deconv[deconv<0] = 0
     deconv[deconv>0] = 255
     for ri, row in enumerate(deconv):
         for ci, col in enumerate(row):
             if np.sum(col>0) > 0:
                 deconv[ri][ci] = [255, 255, 255]
                 
     saveImage(deconv, os.path.join(PATH.TEST.ROOT, "filtered.jpg"))