import os
import urllib.request
import zipfile
# Create directory for annotations if it doesn't exist
os.makedirs('./coco_data/annotations', exist_ok=True)
# URL for COCO Captions annotations
annotations_url = 'http://images.cocodataset.org/annotations/annotations_trainval2017.zip'
annotations_zip = './coco_data/annotations_trainval2017.zip'
# Download the annotations
urllib.request.urlretrieve(annotations_url, annotations_zip)
# Unzip the annotations
with zipfile.ZipFile(annotations_zip, 'r') as zip_ref:
zip_ref.extractall('./coco_data/')
import torchvision
from torchvision import transforms
# Define transformation (optional)
transform = transforms.Compose([
transforms.ToTensor()
])
# Download the COCO Captions dataset
coco_captions = torchvision.datasets.CocoCaptions(
root='./coco_data', # Path where data will be saved
annFile='./coco_data/annotations/captions_train2017.json', # Annotations file
transform=transform,
)