PacktPublishing/Modern-Computer-Vision-with-PyTorch

cannot import name 'Field' from 'torchtext.data'

andysingal opened this issue · 1 comments

While working on:

from torchtext.data import Field
from pycocotools.coco import COCO
from collections import defaultdict

captions = Field(sequential=False, init_token='', eos_token='')
all_captions = data[data['train']]['caption'].tolist()
all_tokens = [[w.lower() for w in c.split()] for c in all_captions]
all_tokens = [w for sublist in all_tokens for w in sublist]
captions.build_vocab(all_tokens)

i got error:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
[<ipython-input-17-700040b5c201>](https://localhost:8080/#) in <cell line: 1>()
----> 1 from torchtext.data import Field
      2 from pycocotools.coco import COCO
      3 from collections import defaultdict
      4 
      5 captions = Field(sequential=False, init_token='', eos_token='')

ImportError: cannot import name 'Field' from 'torchtext.data' (/usr/local/lib/python3.10/dist-packages/torchtext/data/__init__.py)

---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

The legacy datasets have moved to legacy folder in the latest release (torchtext >= 0.9.0).
Instead of using from torchtext.data import Field use from torchtext.legacy.data import Field

You can also refer to Release notes.