rajeshkumargp
rajeshkumargp

Reputation: 105

How to handle Multi Label DataSet from Directory for image captioning in PyTorch

I need a help in PyTorch, Regarding Dataloader, and dataset Can someone aid/guide me

Here is my query : I am trying for Image Captioning using https://github.com/yunjey/pytorch-tutorial/tree/master/tutorials/03-advanced/image_captioning.

Here they have used Standard COCO Dataset.

I have dataset as images/ and captions/ directory .

Example

Directory Structure:

images/T001.jpg 
images/T002.jpg 
...
...
captions/T001.txt
captions/T002.txt
....
....

The above is the relation. Caption file has 'n' number of captions in each separate line.

I am able to create a custom Dataset class, in that the complete caption file content is being returned. But I want only one line alone gas to be returned.

Any guidance/suggestion on how to achieving this.

++++++++++++++++++++++++++++++++++++++++++++++++ Here is the class that i have designed:

from __future__ import print_function
import torch
from torchvision import datasets, models, transforms
from torchvision import transforms
from torch.autograd import Variable
from torch.nn.utils.rnn import pack_padded_sequence
import torch.optim as optim
import torch.nn as nn
#from torch import np
import numpy as np
import utils_c
from data_loader_c import get_cust_data_loader 
from models import CNN, RNN
from vocab_custom import Vocabulary, load_vocab
import os

class ImageCaptionDataSet(data.Dataset):
    def __init__(self, path, json, vocab=None, transform=None):
        self.vocab = vocab 
        self.transform = transform
        self.img_dir_path = path  
        self.cap_dir_path = json 
        self.all_imgs_path = glob.glob(os.path.join(self.img_dir_path,'*.jpg'))
        self.all_caps_path = glob.glob(os.path.join(self.cap_dir_path,'*.txt'))
        pass

    def __getitem__(self,index):
        vocab = self.vocab

        img_path = self.all_imgs_path[index]
        img_base_name = os.path.basename(img_path) 
        cap_base_name = img_base_name.replace(".jpg",".txt")
        cap_path  = os.path.join(self.cap_dir_path,cap_base_name)

        caption_all_for_a_image = open(cap_path).read().split("\n")

        image = Image.open(img_path)
        image = image.convert('RGB')

        if self.transform != None:
            # apply image preprocessing
            image = self.transform(image)

        #captions_combined = []
        #max_len = 0  
        #for caption in caption_all_for_a_image:
        #    caption_str = str(caption).lower()
        #    tokens = nltk.tokenize.word_tokenize(caption_str)
        #    m = len(tokens) + 2 
        #    if m>max_len:
        #        max_len = m 
        #    caption = torch.Tensor([vocab(vocab.start_token())] +
        #                           [vocab(token) for token in tokens] +
        #                           [vocab(vocab.end_token())])
        #    captions_combined.append(caption) 
        #    #yield image, caption
        #return image,torch.Tensor(captions_combined)

        caption_str = str(caption_all_for_a_image).lower()
        tokens = nltk.tokenize.word_tokenize(caption_str)
        caption = torch.Tensor([vocab(vocab.start_token())] +
                                   [vocab(token) for token in tokens] +
                                   [vocab(vocab.end_token())])

        return image,caption

    def __len__(self):
        return len(self.all_imgs_path)

+++++++++++++++++++++++++++++++++

Upvotes: 1

Views: 888

Answers (1)

Shai
Shai

Reputation: 114926

First, using str() to convert the list of captions into a single string (caption_str = str(caption_all_for_a_image)) is a bad idea:

cap = ['a sentence', 'bla bla bla']
str(cap)

Returns this sting:

"['a sentence', 'bla bla bla']"

Note that [', and ', ' are part of the resulting string!

You can pick one of the captions at random:

import random
...
cap_idx = random.randi(0, len(caption_all_for_a_image)-1)  # pick one at random
caption_str = caption_all_for_a_image[cap_idx].lower()  # actual selection

Upvotes: 1

Related Questions