Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Missing Labels in FER2013 Dataset #8118 #8368

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions test/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2443,22 +2443,26 @@ def inject_fake_data(self, tmpdir, config):
os.makedirs(base_folder)

num_samples = 5
with open(os.path.join(base_folder, f"{config['split']}.csv"), "w", newline="") as file:
with open(os.path.join(base_folder, "icml_face_data.csv"), "w", newline="") as file:
writer = csv.DictWriter(
file,
fieldnames=("emotion", "pixels") if config["split"] == "train" else ("pixels",),
fieldnames=("emotion", "pixels","Usage"),
quoting=csv.QUOTE_NONNUMERIC,
quotechar='"',
)
writer.writeheader()
for _ in range(num_samples):
for i in range(num_samples):
row = dict(
pixels=" ".join(
str(pixel) for pixel in datasets_utils.create_image_or_video_tensor((48, 48)).view(-1).tolist()
)
)
if config["split"] == "train":
row["emotion"] = str(int(torch.randint(0, 7, ())))
row["emotion"] = str(int(torch.randint(0, 7, ())))

if config["split"] == "test":
row["Usage"] = "PublicTest" if i % 2 == 0 else "PrivateTest"
else:
row["Usage"] = "Training"

writer.writerow(row)

Expand Down
24 changes: 15 additions & 9 deletions torchvision/datasets/fer2013.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class FER2013(VisionDataset):
"""

_RESOURCES = {
"train": ("train.csv", "3f0dfb3d3fd99c811a1299cb947e3131"),
"test": ("test.csv", "b02c2298636a634e8c2faabbf3ea9a23"),
"train": ("icml_face_data.csv", "b114b9e04e6949e5fe8b6a98b3892b1d"),
"test": ("icml_face_data.csv", "b114b9e04e6949e5fe8b6a98b3892b1d"),
Comment on lines +26 to +27
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR @NSalberg .

The feature request looks reasonable, however I think we should avoid forcing the existence of icml_face_data.csv because that will potentially break existing users who only have downloaded train.csv and test.csv.

Perhaps we should instead check the existence of icml_face_data.csv and use it if it exists, and otherwise fall back to train.csv and test.csv?

}

def __init__(
Expand All @@ -48,13 +48,19 @@ def __init__(
)

with open(data_file, "r", newline="") as file:
self._samples = [
(
torch.tensor([int(idx) for idx in row["pixels"].split()], dtype=torch.uint8).reshape(48, 48),
int(row["emotion"]) if "emotion" in row else None,
)
for row in csv.DictReader(file)
]
reader = csv.DictReader(file)
self._samples = []
for row in reader:
cleaned_row = {name.strip().lower(): value for name, value in row.items()}
if self._split in cleaned_row["usage"].lower():
self._samples.append(
(
torch.tensor(
[int(idx) for idx in cleaned_row["pixels"].split()], dtype=torch.uint8
).reshape(48, 48),
int(cleaned_row["emotion"]) if "emotion" in cleaned_row else None,
)
)

def __len__(self) -> int:
return len(self._samples)
Expand Down