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

Fer2013.py issue #8415

Closed
wants to merge 5 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions test/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2462,6 +2462,30 @@ def inject_fake_data(self, tmpdir, config):

writer.writerow(row)

with open(os.path.join(base_folder, "fer2013.csv"), "w", newline="") as file:
writer = csv.DictWriter(
file,
fieldnames=("emotion", "pixels", "Usage"),
quoting=csv.QUOTE_NONNUMERIC,
quotechar='"',
)
writer.writeheader()
for i in range(num_samples + 28709):
row = dict(
pixels=" ".join(
str(pixel) for pixel in datasets_utils.create_image_or_video_tensor((48, 48)).view(-1).tolist()
)
)
row["emotion"] = str(int(torch.randint(0, 7, ())))

usage_values = ["PrivateTest", "PublicTest"]
if i < 28709:
row["Usage"] = "Training"
else:
row["Usage"] = random.choice(usage_values)

writer.writerow(row)

return num_samples


Expand Down
44 changes: 44 additions & 0 deletions torchvision/datasets/fer2013.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import csv
import pathlib
import warnings
from itertools import islice
from typing import Any, Callable, Optional, Tuple, Union

warnings.simplefilter("always", Warning)

import torch
from PIL import Image

Expand All @@ -27,6 +31,8 @@ class FER2013(VisionDataset):
"test": ("test.csv", "b02c2298636a634e8c2faabbf3ea9a23"),
}

_RESOURCES_EXTRA = {"fer2013": ("fer2013.csv", "f8428a1edbd21e88f42c73edd2a14f95")}

def __init__(
self,
root: Union[str, pathlib.Path],
Expand Down Expand Up @@ -56,11 +62,49 @@ def __init__(
for row in csv.DictReader(file)
]

self.get_test_label = False
self.err_fer2013_warning = (
"The integrity of fer2013.csv has been compromised, "
"thus the test labels will not be provided. "
"You can download it from "
"https://www.kaggle.com/c/challenges-in-representation-learning-facial-expression-recognition-challenge"
)
if self._split == "test":
self.get_test_label = True

if self.get_test_label:
try:
file_name_entire, md5_entire = self._RESOURCES_EXTRA["fer2013"]
entire_data_file = base_folder / file_name_entire
if not check_integrity(str(entire_data_file), md5=md5_entire):
self.get_test_label = False
warnings.warn(self.err_fer2013_warning, Warning)
except KeyError:
self.get_test_label = False
warnings.warn(self.err_fer2013_warning, Warning)

if self.get_test_label:
with open(entire_data_file, "r", newline="") as file:
reader = csv.DictReader(file)
test_rows = islice(reader, 28709, None)
self._samples_alt = [
(
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 test_rows
]

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

def __getitem__(self, idx: int) -> Tuple[Any, Any]:
image_tensor, target = self._samples[idx]

if self.get_test_label:
_, target_alt = self._samples_alt[idx]
target = target_alt

image = Image.fromarray(image_tensor.numpy())

if self.transform is not None:
Expand Down
Loading