-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Add ssTEM dataset #900
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
Closed
Closed
Add ssTEM dataset #900
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b935c02
Add ssTEM dataset
adamjstewart a8fc5ec
Documentation fix
adamjstewart 625f546
Changes requested during review
adamjstewart bd1dce2
Call __init__ of super class
adamjstewart b36b410
Remove lines obsoleted by super class init
adamjstewart 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
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
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,131 @@ | ||
from __future__ import print_function | ||
|
||
import os | ||
|
||
from PIL import Image | ||
import torch.utils.data as data | ||
|
||
from .utils import download_url, check_integrity | ||
from .vision import VisionDataset | ||
|
||
|
||
class ssTEM(VisionDataset): | ||
"""Dataset for `ISBI Challenge: Segmentation of neuronal structures | ||
in EM stacks <http://brainiac2.mit.edu/isbi_challenge/>`_. | ||
|
||
Args: | ||
root (string): Root directory where dataset exists or will be saved | ||
to if download is set to True. | ||
train (bool, optional): If True, creates dataset from training set, | ||
otherwise creates from test set. | ||
transforms (callable, optional): A function/transform that takes in | ||
two PIL images and returns transformed versions. | ||
download (bool, optional): If true, downloads the dataset from the | ||
internet and puts it in root directory. If dataset is already | ||
downloaded, it is not downloaded again. | ||
""" | ||
base_url = 'http://brainiac2.mit.edu/isbi_challenge/sites/default/files/' | ||
|
||
meta = { | ||
'train': { | ||
'data': { | ||
'filename': 'train-volume.tif', | ||
'md5': '465461edbe0254630c4ec5577f1e7764' | ||
}, | ||
'labels': { | ||
'filename': 'train-labels.tif', | ||
'md5': '657fe6b728c6dd0152e295c6d800001d' | ||
} | ||
}, | ||
'test': { | ||
'data': { | ||
'filename': 'test-volume.tif', | ||
'md5': '9767660d7abe4e0ecbdd0061a16058ad' | ||
} | ||
} | ||
} | ||
|
||
def __init__(self, root, train=True, transforms=None, download=False): | ||
# Lazy import | ||
import skimage.io | ||
|
||
super(ssTEM, self).__init__(root, transforms) | ||
|
||
self.root = os.path.expanduser(root) | ||
adamjstewart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.train = train | ||
self.transforms = transforms | ||
adamjstewart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if download: | ||
self.download() | ||
|
||
if not self._check_integrity(): | ||
raise RuntimeError('Dataset not found or corrupted.' + | ||
' You can use download=True to download it') | ||
|
||
if train: | ||
self.data = skimage.io.imread(os.path.join( | ||
self.root, self.meta['train']['data']['filename'])) | ||
self.labels = skimage.io.imread(os.path.join( | ||
self.root, self.meta['train']['labels']['filename'])) | ||
else: | ||
self.data = skimage.io.imread(os.path.join( | ||
self.root, self.meta['test']['data']['filename'])) | ||
|
||
def __getitem__(self, index): | ||
""" | ||
Args: | ||
index (int): Index | ||
Returns: | ||
tuple: (image, target) where target is index of the target class. | ||
""" | ||
img = self.data[index] | ||
img = Image.fromarray(img) | ||
|
||
target = None | ||
if self.train: | ||
target = self.labels[index] | ||
target = Image.fromarray(img) | ||
|
||
if self.transforms is not None: | ||
img, target = self.transforms(img, target) | ||
|
||
return img, target | ||
|
||
def __len__(self): | ||
return len(self.data) | ||
|
||
def _check_integrity(self): | ||
if self.train: | ||
dataset = 'train' | ||
records = ['data', 'labels'] | ||
else: | ||
dataset = 'test' | ||
records = ['data'] | ||
|
||
for record in records: | ||
filename = self.meta[dataset][record]['filename'] | ||
fpath = os.path.join(self.root, filename) | ||
md5 = self.meta[dataset][record]['md5'] | ||
|
||
if not check_integrity(fpath, md5): | ||
return False | ||
|
||
return True | ||
|
||
def download(self): | ||
if self._check_integrity(): | ||
print('Files already downloaded and verified') | ||
return | ||
|
||
if self.train: | ||
dataset = 'train' | ||
records = ['data', 'labels'] | ||
else: | ||
dataset = 'test' | ||
records = ['data'] | ||
|
||
for record in records: | ||
filename = self.meta[dataset][record]['filename'] | ||
md5 = self.meta[dataset][record]['md5'] | ||
|
||
download_url(self.base_url + filename, self.root, filename, md5) |
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.
Uh oh!
There was an error while loading. Please reload this page.