Explore the Flicker 8k dataset
Visually understanding the meaning behind expert judgements
Download instructions available on the jbrownlee's Datasets repo.
from pandas import read_csv
experts = read_csv(
"flicker8k/ExpertAnnotations.txt",
sep = "\t",
header=None,
names=["image_file_name", "caption_id", "expert_1", "expert_2", "expert_3"]
)
experts.head()
experts.shape
experts_agreement_bool = experts.apply(
lambda x: x["expert_1"] == x["expert_2"] and x["expert_2"] == x["expert_3"],
axis=1
)
experts_agreement = experts[experts_agreement_bool][["image_file_name", "caption_id", "expert_1"]].rename(columns={"expert_1":"expert"})
experts_agreement.head()
experts_agreement["expert"].value_counts().sort_index()
import os
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from textwrap import wrap
def load_image(file_name, relative_folder):
return mpimg.imread(os.path.join(relative_folder, file_name))
def get_caption(caption_id, captions):
return captions[captions["caption_id"] == caption_id]["caption"].values[0]
def plot_images_and_captions(image_names, caption_ids, relative_image_folder, wrap_value = 50):
assert len(image_names) == len(caption_ids) == 4, "Number of images and captions should be equal to 4"
fig = plt.figure(figsize=(10, 10))
for idx, (image_file_name, caption_id) in enumerate(zip(image_names, caption_ids)):
sub = fig.add_subplot(2,2,idx+1)
imgplot = plt.imshow(load_image(image_file_name, relative_folder=relative_image_folder))
sub.set_title("\n".join(wrap(get_caption(caption_id, captions), wrap_value)))
plt.tight_layout()
captions = read_csv("flicker8k/Flickr8k.token.txt", sep="\t", header=None, names=["caption_id", "caption"])
captions.head()
relevant_pairs = experts_agreement[experts_agreement["expert"] == 4].sample(4, random_state=675)[["image_file_name", "caption_id"]]
relevant_pairs
relevant_image_names = list(relevant_pairs["image_file_name"])
relevant_caption_ids = list(relevant_pairs["caption_id"])
plot_images_and_captions(
image_names=relevant_image_names,
caption_ids=relevant_caption_ids,
relative_image_folder="flicker8k/Flicker8k_Dataset/",
wrap_value=50
)
irrelevant_pairs = experts_agreement[experts_agreement["expert"] == 1].sample(4, random_state=675)[["image_file_name", "caption_id"]]
irrelevant_pairs
irrelevant_image_names = list(irrelevant_pairs["image_file_name"])
irrelevant_caption_ids = list(irrelevant_pairs["caption_id"])
plot_images_and_captions(
image_names=irrelevant_image_names,
caption_ids=irrelevant_caption_ids,
relative_image_folder="flicker8k/Flicker8k_Dataset/",
wrap_value=50
)
pairs_score_2 = experts_agreement[experts_agreement["expert"] == 2].sample(4, random_state=675)[["image_file_name", "caption_id"]]
image_names_score_2 = list(pairs_score_2["image_file_name"])
caption_ids_score_2 = list(pairs_score_2["caption_id"])
plot_images_and_captions(
image_names=image_names_score_2,
caption_ids=caption_ids_score_2,
relative_image_folder="flicker8k/Flicker8k_Dataset/",
wrap_value=50
)
pairs_score_3 = experts_agreement[experts_agreement["expert"] == 3].sample(4, random_state=675)[["image_file_name", "caption_id"]]
image_names_score_3 = list(pairs_score_3["image_file_name"])
caption_ids_score_3 = list(pairs_score_3["caption_id"])
plot_images_and_captions(
image_names=image_names_score_3,
caption_ids=caption_ids_score_3,
relative_image_folder="flicker8k/Flicker8k_Dataset/",
wrap_value=50
)