-
Notifications
You must be signed in to change notification settings - Fork 7.2k
SVHN dataset for torchvision #98
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
bff9af0
lena() is no longer included in SciPy, replacing it with face()
uridah 00a6350
Merge pull request #1 from uridah/uridah-vision
uridah db5a830
Merge pull request #2 from pytorch/master
uridah 460f3a8
SVHN dataset
uridah f189f56
Adding svhn.py to __init__.py
uridah 060a170
Updating svhn.py based on the comments in last PR
uridah a04a59a
Add files via upload
uridah 73d0a38
Update svhn.py
uridah ae61433
Update svhn.py
uridah 8a51045
Update svhn.py
uridah 50bf838
Added entry for SVHN dataset
uridah e6b42fa
Correction in transpose and indentation
uridah 34e53b3
Commenting horizontal flip
uridah 28464c8
Delete sanity_checks1.ipynb
uridah 018c02b
Updated sanity checks (no horizontal flip)
uridah a96591d
Merge pull request #3 from uridah/patch-2
uridah 25e68de
Update README.rst
soumith 5475e30
Update svhn.py
soumith 2f4812e
Update svhn.py
soumith 2097c82
make dependency optional
soumith ed230e5
fix lint
soumith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| from __future__ import print_function | ||
| import scipy.io as sio | ||
| import torch.utils.data as data | ||
| from PIL import Image | ||
| import os | ||
| import os.path | ||
| import errno | ||
| import numpy as np | ||
| import sys | ||
|
|
||
|
|
||
| class SVHN(data.Dataset): | ||
| url = "" | ||
| filename = "" | ||
| file_md5 = "" | ||
|
|
||
| split_list = { | ||
| 'train': ["http://ufldl.stanford.edu/housenumbers/train_32x32.mat", | ||
| "train_32x32.mat", "e26dedcc434d2e4c54c9b2d4a06d8373"], | ||
| 'test': ["http://ufldl.stanford.edu/housenumbers/test_32x32.mat", | ||
| "test_32x32.mat", "eb5a983be6a315427106f1b164d9cef3"], | ||
| 'extra': ["http://ufldl.stanford.edu/housenumbers/extra_32x32.mat", | ||
| "extra_32x32.mat", "a93ce644f1a588dc4d68dda5feec44a7"]} | ||
|
|
||
| def __init__(self, root, split='train', transform=None, target_transform=None, download=False): | ||
| self.root = root | ||
| self.transform = transform | ||
| self.target_transform = target_transform | ||
| self.split = split # training set or test set or extra set | ||
|
|
||
| if self.split in self.split_list: | ||
| self.url = self.split_list[split][0] | ||
| self.filename = self.split_list[split][1] | ||
| self.file_md5 = self.split_list[split][2] | ||
|
|
||
| if download: | ||
| self.download() | ||
|
|
||
| if not self._check_integrity(): | ||
| raise RuntimeError('Dataset not found or corrupted.' + | ||
| ' You can use download=True to download it') | ||
|
|
||
| # reading(loading) mat file as array | ||
| loaded_mat = sio.loadmat(os.path.join(root, self.filename)) | ||
|
|
||
| if self.split != 'test': | ||
|
||
| self.train_data = loaded_mat['X'] | ||
| self.train_labels = loaded_mat['y'] | ||
| self.train_data = np.transpose(self.train_data, (3, 2, 1, 0)) | ||
| else: | ||
| self.test_data = loaded_mat['X'] | ||
| self.test_labels = loaded_mat['y'] | ||
| self.test_data = np.transpose(self.test_data, (3, 2, 1, 0)) | ||
| else: | ||
| print ("Wrong dataset entered! Please use split=train or split=extra or split=test") | ||
|
||
|
|
||
| def __getitem__(self, index): | ||
| if self.split == 'train' or self.split == 'extra': | ||
|
||
| img, target = self.train_data[index], self.train_labels[index] | ||
| elif self.split == 'test': | ||
| img, target = self.test_data[index], self.test_labels[index] | ||
|
|
||
| # doing this so that it is consistent with all other datasets | ||
| # to return a PIL Image | ||
| img = Image.fromarray(np.transpose(img, (1, 2, 0))) | ||
|
|
||
| if self.transform is not None: | ||
| img = self.transform(img) | ||
|
|
||
| if self.target_transform is not None: | ||
| target = self.target_transform(target) | ||
|
|
||
| return img, target | ||
|
|
||
| def __len__(self): | ||
| return len(self.train_data) | ||
|
||
|
|
||
| def _check_integrity(self): | ||
| import hashlib | ||
| root = self.root | ||
| md5 = self.split_list[self.split][2] | ||
| fpath = os.path.join(root, self.filename) | ||
| if not os.path.isfile(fpath): | ||
| return False | ||
| md5c = hashlib.md5(open(fpath, 'rb').read()).hexdigest() | ||
| if md5c != md5: | ||
| return False | ||
| return True | ||
|
|
||
| def download(self): | ||
| from six.moves import urllib | ||
| import tarfile | ||
| import hashlib | ||
|
|
||
| root = self.root | ||
| fpath = os.path.join(root, self.filename) | ||
|
|
||
| try: | ||
| os.makedirs(root) | ||
| except OSError as e: | ||
| if e.errno == errno.EEXIST: | ||
| pass | ||
| else: | ||
| raise | ||
|
|
||
| if self._check_integrity(): | ||
| print('Files already downloaded and verified') | ||
| return | ||
|
|
||
| print ("about to download") | ||
| # downloads file | ||
| if os.path.isfile(fpath): | ||
| print('Using downloaded file: ' + fpath) | ||
| else: | ||
| print('Downloading ' + self.url + ' to ' + fpath) | ||
| urllib.request.urlretrieve(self.url, fpath) | ||
| print ('Downloaded!') | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as off-topic.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.