Giter Club home page Giter Club logo

Comments (4)

v-iashin avatar v-iashin commented on July 17, 2024 1

I also remember that during training gpu load was ~50-70% while during the 1-by-1 prediction it ramped up to 100%. So i would assume it is related to torchtext parts.

from mdvc.

XIUXIUXIUBIUA avatar XIUXIUXIUBIUA commented on July 17, 2024 1

Yes, with your help ,I know how to solve this problem. Thanks a lot !

from mdvc.

v-iashin avatar v-iashin commented on July 17, 2024

Yes. I am glad you asked.

I am not sure if both are related because I remember my CPU was using 16 threads at 100% load when training the model. It is even with the num_workers = 0 (I mean the default option).

About the data loader. Yes, you are right it is a bit strange. I came up with this workaround because previously (maybe even still) torchtext and PyTorch were using different approaches for data loading which made it very painful to combine the good stuff about text from torchtext with vision-style pipelines.

An explanation of how it works is a bit tricky:

It is a tricky part.

The DataLoader which is going to wrap this dataset

MDVC/main.py

Lines 220 to 221 in df3b88a

# make sure that DataLoader has batch_size = 1!
train_loader = DataLoader(train_dataset, collate_fn=train_dataset.dont_collate)

cannot pad text (without collate_fn). One possible solution to this would be a custom collate_fn in PyTorch but
we already have torchtext package with BucketIterator that did precisely
this and back then I felt it would take too much time to implement. Hence, caption_iterator() is defined above in dataset.py:
def caption_iterator(start_token, end_token, pad_token, train_meta_path, val_1_meta_path,

Besides the iterator (datasetloader variable) that automatically pads sentences to be of the same length, this caption_iterator() outputs the
train_vocab which can be used as token-to-word mapping which is also cumbersome to implement on your own because you need to build a vocabulary (ew!).

Anyway, the data items which we get from self.caption_loader defined here:

self.train_vocab, self.train_subs_vocab, self.caption_loader = caption_iterator(

, in turn, contain both a batch of padded tokenized captions and a batch of indices to the meta.csv (or filtered) rows and, hence, to self.features_dataset. Check out slicing here:
to_return = caption_data, *self.features_dataset[caption_data.idx]

which uses the same csv file (meta) which contains paths to precalculated video feats, start and end segment entries
.

So, to get the video features, I need to iterate through the self.caption_loader which I got from the caption_iterator()
to get the indices to the dataset rows (the paths to precalculated features). However, I can't index self.caption_loader with an index somehow to retrieve these captions and indices – it fails. So, I came up with just
doing it in the opposite way: adding the dataset row index as a field in self.caption_loader See here:

('idx', INDEX),

and here:

MDVC/dataset/dataset.py

Lines 443 to 446 in df3b88a

caption_data = next(self.caption_loader_iter)
# a note about "*": 1, 2, *('a', 'b', 'c') -> 1, 2, 'a', 'b', 'c'
to_return = caption_data, *self.features_dataset[caption_data.idx]
.
Each next() call on the iterator returns a shuffled set of indices and captions (caption_data). Then, these indices are used in self.features_dataset which returns vid features for each index
entry in meta – see here:
video_id, caption, start, end, duration, category, _, _, _ = self.dataset.iloc[idx]

Finally, the padding of features is performed in AudioVideoFeaturesDataset because BucketIterator can digest only 'text' data but we also need to pad features.

As a result, the batch size is not 1 as it is defined in the PyTorch DataLoader here:

MDVC/main.py

Lines 220 to 224 in df3b88a

# make sure that DataLoader has batch_size = 1!
train_loader = DataLoader(train_dataset, collate_fn=train_dataset.dont_collate)
val_1_loader = DataLoader(val_1_dataset, collate_fn=val_1_dataset.dont_collate)
val_2_loader = DataLoader(val_2_dataset, collate_fn=val_2_dataset.dont_collate)
val_pred_prop_loader = DataLoader(val_pred_prop_dataset, collate_fn=val_2_dataset.dont_collate)

but rather the one defined in caption_iterator() defined here:

MDVC/dataset/dataset.py

Lines 209 to 211 in df3b88a

datasetloader = data.BucketIterator(
dataset, batch_size, sort_key=sort_key, device=device, repeat=False, shuffle=True
)

So, on a high level: I am wrapping a torchtext BucketIterator inside of the PyTorch DataLoader with batch size = 1. This means that num_workers won't do much here because it loads a batch of one item.

This was the price of having text-padding out-of-the-box back then along with PyTorch dataset class.

Q: Have you ensured that the indices that caption_iterator returned with captions
(caption_data) are unique when you do caption_data = next(self.caption_loader_iter)?
A: Yes, and they are shuffled after every epoch in update_iterator(self): (defined here:

MDVC/dataset/dataset.py

Lines 452 to 456 in df3b88a

def update_iterator(self):
'''
This should be called after every epoch
'''
self.caption_loader_iter = iter(self.caption_loader)
).

from mdvc.

XIUXIUXIUBIUA avatar XIUXIUXIUBIUA commented on July 17, 2024

Oh, thanks a lot. I see what you're trying to do. Really nice work! I only have 2 threads assigned to this task when training the model so it took too much time in fetching dataset. I think I should use more threads if I want to improve the utilization of GPU. When I have 8 threads assigned to the task, it looks good.
gpu_cpu

from mdvc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.