text stringlengths 0 4.99k |
|---|
!wget -q https://github.com/jbrownlee/Datasets/releases/download/Flickr8k/Flickr8k_Dataset.zip |
!wget -q https://github.com/jbrownlee/Datasets/releases/download/Flickr8k/Flickr8k_text.zip |
!unzip -qq Flickr8k_Dataset.zip |
!unzip -qq Flickr8k_text.zip |
!rm Flickr8k_Dataset.zip Flickr8k_text.zip |
# Path to the images |
IMAGES_PATH = \"Flicker8k_Dataset\" |
# Desired image dimensions |
IMAGE_SIZE = (299, 299) |
# Vocabulary size |
VOCAB_SIZE = 10000 |
# Fixed length allowed for any sequence |
SEQ_LENGTH = 25 |
# Dimension for the image embeddings and token embeddings |
EMBED_DIM = 512 |
# Per-layer units in the feed-forward network |
FF_DIM = 512 |
# Other training parameters |
BATCH_SIZE = 64 |
EPOCHS = 30 |
AUTOTUNE = tf.data.AUTOTUNE |
Preparing the dataset |
def load_captions_data(filename): |
\"\"\"Loads captions (text) data and maps them to corresponding images. |
Args: |
filename: Path to the text file containing caption data. |
Returns: |
caption_mapping: Dictionary mapping image names and the corresponding captions |
text_data: List containing all the available captions |
\"\"\" |
with open(filename) as caption_file: |
caption_data = caption_file.readlines() |
caption_mapping = {} |
text_data = [] |
images_to_skip = set() |
for line in caption_data: |
line = line.rstrip(\"\n\") |
# Image name and captions are separated using a tab |
img_name, caption = line.split(\"\t\") |
# Each image is repeated five times for the five different captions. |
# Each image name has a suffix `#(caption_number)` |
img_name = img_name.split(\"#\")[0] |
img_name = os.path.join(IMAGES_PATH, img_name.strip()) |
# We will remove caption that are either too short to too long |
tokens = caption.strip().split() |
if len(tokens) < 5 or len(tokens) > SEQ_LENGTH: |
images_to_skip.add(img_name) |
continue |
if img_name.endswith(\"jpg\") and img_name not in images_to_skip: |
# We will add a start and an end token to each caption |
caption = \"<start> \" + caption.strip() + \" <end>\" |
text_data.append(caption) |
if img_name in caption_mapping: |
caption_mapping[img_name].append(caption) |
else: |
caption_mapping[img_name] = [caption] |
for img_name in images_to_skip: |
if img_name in caption_mapping: |
del caption_mapping[img_name] |
return caption_mapping, text_data |
def train_val_split(caption_data, train_size=0.8, shuffle=True): |
\"\"\"Split the captioning dataset into train and validation sets. |
Args: |
caption_data (dict): Dictionary containing the mapped caption data |
train_size (float): Fraction of all the full dataset to use as training data |
shuffle (bool): Whether to shuffle the dataset before splitting |
Returns: |
Traning and validation datasets as two separated dicts |
\"\"\" |
# 1. Get the list of all image names |
all_images = list(caption_data.keys()) |
# 2. Shuffle if necessary |
if shuffle: |
np.random.shuffle(all_images) |
# 3. Split into training and validation sets |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.