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 dataset #5120

Merged
merged 5 commits into from
Jan 5, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor integrity check
  • Loading branch information
pmeier committed Jan 5, 2022
commit 68982c6ae14a06f939b788954faf0c1999bccccc
27 changes: 12 additions & 15 deletions torchvision/datasets/fer2013.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import csv
import os
import os.path
import pathlib
from typing import Any, Callable, Optional, Tuple

import torch
Expand Down Expand Up @@ -38,7 +37,17 @@ def __init__(
self._split = verify_str_arg(split, "split", self._RESOURCES.keys())
super().__init__(root, transform=transform, target_transform=target_transform)

with open(self._verify_integrity(), "r", newline="") as file:
base_folder = pathlib.Path(self.root) / "fer2013"
file_name, md5 = self._RESOURCES[self._split]
data_file = base_folder / file_name
if not check_integrity(str(data_file), md5=md5):
raise RuntimeError(
f"{file_name} not found in {base_folder} or corrupted. "
f"You can download it from "
f"https://www.kaggle.com/c/challenges-in-representation-learning-facial-expression-recognition-challenge"
)

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),
Expand All @@ -62,17 +71,5 @@ def __getitem__(self, idx: int) -> Tuple[Any, Any]:

return image, target

def _verify_integrity(self):
base_folder = os.path.join(self.root, "fer2013")
file_name, md5 = self._RESOURCES[self._split]
file = os.path.join(base_folder, file_name)
if not check_integrity(file, md5=md5):
raise RuntimeError(
f"{file_name} not found in {base_folder} or corrupted. "
f"You can download it from "
f"https://www.kaggle.com/c/challenges-in-representation-learning-facial-expression-recognition-challenge"
)
return file

def extra_repr(self) -> str:
return f"split={self._split}"