コード例 #1
0
    def __getitem__(self, index):
        ## Label
        filen = 'synset_words.txt'
        indicestosynsets, synsetstoindices, synsetstoclassdescriptions = gc.parsesynsetwords(
            filen)
        label, firstname = gc.parseclasslabel('val/' + self.labellist[index],
                                              synsetstoindices)
        ## Image
        img_as_np = io.imread('imagespart/' + self.imglist[index])
        img_as_img = Image.fromarray(img_as_np)
        #         img_as_img = img_as_img.convert('L')

        ## Resize smaller size to 224
        a, b = img_as_img.size[0], img_as_img.size[1]
        if a < 224 or b < 224:
            return None
        if a < b:
            percent = 224 / float(a)
            hsize = int((float(b) * float(percent)))
            img_as_img = img_as_img.resize((224, hsize), Image.BILINEAR)
        else:
            percent = 224 / float(b)
            hsize = int((float(a) * float(percent)))
            img_as_img = img_as_img.resize((hsize, 224), Image.BILINEAR)

        ## Read to numpy array
        img_as_np = np.array(img_as_img)
        if len(img_as_np.shape) != 3:
            return None
        ## Normalize [0, 1]
        img_as_np = (img_as_np - np.min(img_as_np)) / (np.max(img_as_np) -
                                                       np.min(img_as_np))
        # print(img_as_np[0:])
        ## swap axes
        img_as_np = np.swapaxes(img_as_np, 0, 2)
        img_as_np = np.swapaxes(img_as_np, 1, 2)
        # print(img_as_np)

        ## input normalization
        img_as_np[0:] = (img_as_np[0:] - 0.485) / 0.229
        img_as_np[1:] = (img_as_np[1:] - 0.456) / 0.224
        img_as_np[2:] = (img_as_np[2:] - 0.406) / 0.225
        ## center crop
        a = img_as_np.shape[1]
        b = img_as_np.shape[2]
        if a < b:
            x = int((b - 224) / 2)
            img_as_np = img_as_np[:, :, x:(x + 224)]
            # print(img_as_np.shape)
        else:
            x = int((a - 224) / 2)
            img_as_np = img_as_np[:, x:(x + 224), :]

        img_as_tensor = torch.tensor(img_as_np)

        # print(img_as_np.shape)
        if self.transforms is not None:
            img_as_tensor = self.transforms(img_as_img)

        return (img_as_tensor.type('torch.FloatTensor'), label)
コード例 #2
0
    def __init__(self, root_dir, file_prefix='ILSVRC2012_val_',
                 img_ext='.JPEG', val_ext='.xml', synset='synset_words.txt',
                 five_crop=False, data_limit=0, selector=None):
        '''
        NOTE: set up your root_dir directory to consist of 
        2 directories:
        - imagespart: where the images are
        - val: where the xml's are (for class values etc)
        '''
        self.meta = {'root_dir':root_dir,
                     'file_prefix':file_prefix,
                     'synset':synset,
                     'img_ext':img_ext, 'val_ext':val_ext,
                     'five_crop': five_crop}

        # assertion for the mentioned assumption
        assert os.path.exists(os.path.join(root_dir, 'imagespart'))
        assert os.path.exists(os.path.join(root_dir, 'val'))
        assert os.path.exists(os.path.join(root_dir, synset))

        # metadata
        self.classes = ginc.get_classes()
        _, s2i, s2d = ginc.parsesynsetwords(os.path.join(root_dir, synset))
        self.dataset = [filename[len(file_prefix):-len(img_ext)]
                        for filename in os.listdir(os.path.join(root_dir, 'imagespart'))]
        if data_limit > 0:
            self.dataset = self.dataset[:data_limit]

        if selector is not None:
            self.dataset = [d for d, s in zip(self.dataset, selector) if s]

        self._rev_dataset = s2i 
        self.data_description = s2d
コード例 #3
0
    def __getitem__(self, index):
        ## Label
        filen = 'synset_words.txt'
        indicestosynsets, synsetstoindices, synsetstoclassdescriptions = gc.parsesynsetwords(
            filen)
        label, firstname = gc.parseclasslabel('val/' + self.labellist[index],
                                              synsetstoindices)
        ## Image
        img_as_np = io.imread('imagespart/' + self.imglist[index])
        img_as_img = Image.fromarray(img_as_np)
        #         img_as_img = img_as_img.convert('L')

        if self.transforms is not None:
            img_as_tensor = self.transforms(img_as_img)

        return (img_as_tensor, label)
コード例 #4
0
    def __init__(self,
                 root_dir,
                 xmllabeldir,
                 synsetfile,
                 maxnum,
                 transform=None):
        """
    Args:

        root_dir (string): Directory with all the images.
        transform (callable, optional): Optional transform to be applied
            on a sample.
    """

        self.root_dir = root_dir
        self.xmllabeldir = xmllabeldir
        self.transform = transform
        self.imgfilenames = []
        self.labels = []
        self.ending = ".JPEG"

        self.clsdict = get_classes()

        indicestosynsets, self.synsetstoindices, synsetstoclassdescr = parsesynsetwords(
            synsetfile)

        for root, dirs, files in os.walk(self.root_dir):
            for ct, name in enumerate(files):
                nm = os.path.join(root, name)
                #print(nm)
                if (maxnum > 0) and ct >= (maxnum):
                    break
                self.imgfilenames.append(nm)
                label, firstname = parseclasslabel(self.filenametoxml(nm),
                                                   self.synsetstoindices)
                self.labels.append(label)
コード例 #5
0
ファイル: q3.py プロジェクト: chiayewken/sutd-materials
def get_label(path_words_txt, path_xml):
    indicestosynsets, synsetstoindices, synsetstoclassdescr = parsesynsetwords(
        path_words_txt)
    idx, label_id = parseclasslabel(path_xml, synsetstoindices)
    name = synsetstoclassdescr[indicestosynsets[idx]]
    return dict(label_idx=idx, label_name=name, label_id=label_id)
コード例 #6
0
ファイル: hw5_code.py プロジェクト: ktkr/arti-intel
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])

inv_transform = transforms.Compose([
    transforms.Normalize(mean=[0., 0., 0.],
                         std=[1 / 0.229, 1 / 0.224, 1 / 0.225]),
    transforms.Normalize(mean=[-0.485, -0.456, -0.406], std=[1., 1., 1.]),
    transforms.ToPILImage(),
])

resize_transform = transforms.Compose(
    [transforms.Resize(224),
     transforms.CenterCrop(224)])

i2s, s2i, s2d = ginc.parsesynsetwords('synset_words.txt')


def get_descript(class_no):
    s = i2s[class_no]
    desc = s2d[s]
    return desc


def get_trained_resnet(use_gpu=True):
    model_ft = models.resnet18(pretrained=True)
    if use_gpu:
        model_ft = model_ft.try_cuda()
    return model_ft