示例#1
0
    def __getitem__(self, index):
        """Return dict of PyTorch tensors for preprocessed images and text and tensor of labels"""
        # Text processing
        x_text = self.text_inputs[index].replace('\n', ' ').replace('\r', ' ')

        tokenized_x = (
            ['[CLS]'] +
            self.tokenizer.tokenize(x_text)[:self.max_seq_length - 2] +
            ['[SEP]'])

        input_ids = self.tokenizer.convert_tokens_to_ids(tokenized_x)

        padding = [0] * (self.max_seq_length - len(input_ids))
        input_ids += padding
        assert len(input_ids) == self.max_seq_length

        bert_text = torch.from_numpy(np.array(input_ids))

        # Image processing
        full_img = Image.open(self.img_inputs[index]).convert('RGB')

        cropped_img = center_crop_pil_image(full_img)

        full_img = self.transform(full_img)
        cropped_img = self.crop_transform(cropped_img)

        y_output = torch.from_numpy(np.array(self.y[index])).long()

        return {
            'bert_text': bert_text,
            'full_img': full_img,
            'crop_img': cropped_img
        }, y_output
示例#2
0
    def __getitem__(self, index):
        """Return tuple of images as PyTorch tensors and and tensor of labels"""
        label = self.y[index]
        full_img = Image.open(self.x[index]).convert('RGB')

        cropped_img = center_crop_pil_image(full_img)

        full_img = self.transform(full_img)
        cropped_img = self.crop_transform(cropped_img)

        label = torch.FloatTensor(label)

        return {'full_img': full_img, 'crop_img': cropped_img}, label
示例#3
0
    def __getitem__(self, index):
        """Return tuple of images as PyTorch tensors and and tensor of labels"""
        label = self.y[index]
        label = self.label_encoder.transform([label])[0]
        full_img = Image.open(self.x[index]).convert('RGB')

        cropped_img = center_crop_pil_image(full_img)

        full_img = self.transform(full_img)
        cropped_img = self.crop_transform(cropped_img)

        label = torch.from_numpy(np.array(label)).long()

        return {'full_img': full_img, 'crop_img': cropped_img}, label