예제 #1
0
    def __getitem__(self, idx):
        # img_name = self.root_dir + '/img_resized_1M/cities_instagram/' + self.indices[idx] + '.jpg'
        if self.split == '/anns/validation':
            img_name = '{}/{}/{}{}'.format(self.root_dir, 'img_val',
                                           self.indices[idx], '.jpg')
        else:
            img_name = '{}/{}/{}{}'.format(self.root_dir, 'img',
                                           self.indices[idx], '.jpg')
        try:
            image = Image.open(img_name)
            # print("FOUND " + img_name)
        except:
            print("Img file " + img_name + " not found, using hardcoded " +
                  img_name)
            img_name = '../../datasets/SocialMedia/img_resized_1M/cities_instagram/london/1481255189662056249.jpg'
            image = Image.open(img_name)

        try:
            width, height = image.size
            if self.RandomCrop >= width or self.RandomCrop >= height:
                image = image.resize((int(width * 1.5), int(height * 1.5)),
                                     Image.ANTIALIAS)

            if self.Rescale != 0:
                image = customTransform.Rescale(image, self.Rescale)

            if self.RandomCrop != 0:
                image = customTransform.RandomCrop(image, self.RandomCrop)

            if self.Mirror:
                image = customTransform.Mirror(image)

            im_np = np.array(image, dtype=np.float32)
            im_np = customTransform.PreprocessImage(im_np)

        except:
            print("Error on data aumentation, using hardcoded")
            img_name = self.root_dir + '/img_resized_1M/cities_instagram/london/1481255189662056249.jpg'
            image = Image.open(img_name)
            if self.RandomCrop != 0:
                image = customTransform.RandomCrop(image, self.RandomCrop)
            im_np = np.array(image, dtype=np.float32)
            im_np = customTransform.PreprocessImage(im_np)

        out_img = np.copy(im_np)

        # Simple Classification (class index)
        # label = torch.from_numpy(np.array([int(self.labels[idx])]))
        # label = label.type(torch.LongTensor)

        # Multilabel / Regression
        label = torch.from_numpy(np.array(self.labels[idx]))
        label = label.type(torch.FloatTensor)

        return torch.from_numpy(out_img), label
예제 #2
0
    def __getitem__(self, idx):
        img_name = '{}{}/{}{}'.format(self.root_dir, 'img_resized',
                                      self.tweet_ids[idx], '.jpg')  #, '.jpg')

        try:
            image = Image.open(img_name)
            # print("FOUND " + img_name)
        except:
            new_img_name = '../../../datasets/HateSPic/MMHS/img_resized/1037385299310112768.jpg'
            print("Img file " + img_name + " not found, using hardcoded " +
                  new_img_name)
            image = Image.open(new_img_name)

        try:
            width, height = image.size
            if self.RandomCrop >= width or self.RandomCrop >= height:
                image = image.resize((int(width * 1.5), int(height * 1.5)),
                                     Image.ANTIALIAS)

            if self.Rescale != 0:
                image = customTransform.Rescale(image, self.Rescale)

            if self.RandomCrop != 0:
                image = customTransform.RandomCrop(image, self.RandomCrop)

            if self.Mirror:
                image = customTransform.Mirror(image)

            im_np = np.array(image, dtype=np.float32)
            im_np = customTransform.PreprocessImage(im_np)

        except:
            print("Error in data aumentation with image " + img_name)
            img_name = '../../../datasets/HateSPic/MMHS/img_resized/1037385299310112768.jpg'
            print("Using hardcoded: " + img_name)
            image = Image.open(img_name)
            if self.RandomCrop != 0:
                image = customTransform.RandomCrop(image, self.RandomCrop)
            if self.Rescale != 0:
                image = customTransform.Rescale(image, self.Rescale)
            im_np = np.array(image, dtype=np.float32)
            im_np = customTransform.PreprocessImage(im_np)

        out_img = np.copy(im_np)

        # Multilabel / Regression
        img_text = torch.from_numpy(np.array(self.img_texts[idx]))
        label = torch.from_numpy(np.array(self.labels[idx]))
        tweet = torch.from_numpy(np.array(self.tweets[idx]))
        # print(out_img.shape)

        return torch.from_numpy(out_img), img_text, tweet, label
예제 #3
0
    def __getitem__(self, idx):

        if self.split == '/anns/validation':
            img_name = '{}/{}/{}{}'.format(self.root_dir, 'img_val',
                                           self.indices[idx], '.jpg')
        else:
            img_name = '{}/{}/{}{}'.format(self.root_dir, 'img',
                                           self.indices[idx], '.jpg')
        try:
            image = Image.open(img_name)

        except:
            print("Img file " + img_name + " not found, using hardcoded " +
                  img_name)
            img_name = '../../datasets/SocialMedia/img_resized_1M/cities_instagram/london/1481255189662056249.jpg'
            image = Image.open(img_name)

        if self.CenterCrop != 0:
            crop_size = 256
            image = customTransform.CenterCrop(image, crop_size,
                                               self.CenterCrop)

        im_np = np.array(image, dtype=np.float32)
        im_np = customTransform.PreprocessImage(im_np)

        out_img = np.copy(im_np)

        # Multilabel / Regression
        label = torch.from_numpy(np.array(self.labels[idx]))
        label = label.type(torch.FloatTensor)

        return torch.from_numpy(out_img), label, self.indices[idx]
예제 #4
0
    def __getitem__(self, idx):

        img_name = '{}{}/{}{}'.format(self.root_dir, 'img_resized',
                                      self.tweet_ids[idx], '.jpg')

        try:
            image = Image.open(img_name)
        except:
            new_img_name = '../../../datasets/HateSPic/MMHS/img_resized/1037385299310112768.jpg'
            print("Img file " + img_name + " not found, using hardcoded " +
                  new_img_name)
            image = Image.open(new_img_name)

        try:
            image = customTransform.Rescale(image, self.Rescale)
            im_np = np.array(image, dtype=np.float32)
            im_np = customTransform.PreprocessImage(im_np)

        except:
            img_name = '../../../datasets/HateSPic/MMHS/img_resized/1037385299310112768.jpg'
            print("Error on data aumentation, using hardcoded: " + img_name)
            image = Image.open(img_name)
            image = customTransform.Rescale(image, self.Rescale)
            im_np = np.array(image, dtype=np.float32)
            im_np = customTransform.PreprocessImage(im_np)

        out_img = np.copy(im_np)

        # Simple Classification (class index)
        label = torch.from_numpy(np.array([int(self.labels[idx])]))
        label = label.type(torch.LongTensor)

        # Set text embedding to 0!
        #self.img_texts[idx] = np.zeros(self.hidden_state_dim)
        #self.tweets[idx] = np.zeros(self.hidden_state_dim)

        # Set image to 0!
        #out_img = np.zeros((3, 299, 299), dtype=np.float32)

        # Multilabel / Regression
        img_text = torch.from_numpy(np.array(self.img_texts[idx]))
        tweet = torch.from_numpy(np.array(self.tweets[idx]))
        # print(out_img.shape)

        return self.tweet_ids[idx], torch.from_numpy(
            out_img), img_text, tweet, label