forked from qubvel/segmentation_models
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Reorganize decoders, add deprecation for utils, add dataset * Fix imports
- Loading branch information
Showing
31 changed files
with
174 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -2,3 +2,6 @@ torchvision>=0.5.0 | |
pretrainedmodels==0.7.4 | ||
efficientnet-pytorch==0.6.3 | ||
timm==0.4.12 | ||
|
||
tqdm | ||
opencv-python-headless |
This file contains 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 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 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 @@ | ||
from .oxford_pet import OxfordPetDataset, SimpleOxfordPetDataset |
This file contains 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,125 @@ | ||
import os | ||
import cv2 | ||
import shutil | ||
import numpy as np | ||
from tqdm import tqdm | ||
from urllib.request import urlretrieve | ||
|
||
|
||
class OxfordPetDataset(torch.utils.data.Dataset): | ||
|
||
def __init__(self, root, mode="train", transform=None): | ||
|
||
assert mode in {"train", "valid", "test"} | ||
|
||
self.root = root | ||
self.mode = mode | ||
self.transform = transform | ||
|
||
self._download_dataset() # download only if it does not exist | ||
|
||
self.images_directory = os.path.join(self.root, "images") | ||
self.masks_directory = os.path.join(self.root, "annotations", "trimaps") | ||
|
||
self.filenames = self._read_split() # read train/valid/test splits | ||
|
||
def __len__(self): | ||
return len(self.filenames) | ||
|
||
def __getitem__(self, idx): | ||
|
||
filename = self.filenames[idx] | ||
image_path = os.path.join(self.images_directory, filename + ".jpg") | ||
mask_path = os.path.join(self.masks_directory, filename + ".png") | ||
|
||
image = cv2.imread(image_path) | ||
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) | ||
|
||
trimap = cv2.imread(mask_path, cv2.IMREAD_UNCHANGED) | ||
mask = self._preprocess_mask(trimap) | ||
|
||
sample = dict(image=image, mask=mask, trimap=trimap) | ||
if self.transform is not None: | ||
sample = self.transform(**sample) | ||
|
||
return sample | ||
|
||
@staticmethod | ||
def _preprocess_mask(mask): | ||
mask = mask.astype(np.float32) | ||
mask[mask == 2.0] = 0.0 | ||
mask[(mask == 1.0) | (mask == 3.0)] = 1.0 | ||
return mask | ||
|
||
def _read_split(self): | ||
split_filename = "test.txt" if self.mode == "test" else "trainval.txt" | ||
split_filepath = os.path.join(self.root, "annotations", split_filename) | ||
with open(split_filepath) as f: | ||
split_data = f.read().strip("\n").split("\n") | ||
filenames = [x.split(" ")[0] for x in split_data] | ||
if self.mode == "train": # 90% for train | ||
filenames = [x for i, x in enumerate(filenames) if i % 10 != 0] | ||
elif self.mode == "valid": # 10% for validation | ||
filenames = [x for i, x in enumerate(filenames) if i % 10 == 0] | ||
return filenames | ||
|
||
def _download_dataset(self): | ||
|
||
# load images | ||
filepath = os.path.join(self.root, "images.tar.gz") | ||
download_url( | ||
url="https://www.robots.ox.ac.uk/~vgg/data/pets/data/images.tar.gz", filepath=filepath, | ||
) | ||
extract_archive(filepath) | ||
|
||
# load annotations | ||
filepath = os.path.join(self.root, "annotations.tar.gz") | ||
download_url( | ||
url="https://www.robots.ox.ac.uk/~vgg/data/pets/data/annotations.tar.gz", filepath=filepath, | ||
) | ||
extract_archive(filepath) | ||
|
||
|
||
class SimpleOxfordPetDataset(OxfordPetDataset): | ||
"""Dataset for example without augmentations and transforms""" | ||
|
||
def __getitem__(self, *args, **kwargs): | ||
|
||
sample = super().__getitem__(*args, **kwargs) | ||
|
||
# resize images | ||
image = cv2.resize(sample["image"], (256, 256), cv2.INTER_LINEAR) | ||
mask = cv2.resize(sample["mask"], (256, 256), cv2.INTER_NEAREST) | ||
trimap = cv2.resize(sample["trimap"], (256, 256), cv2.INTER_NEAREST) | ||
|
||
# convert to other format HWC -> CHW | ||
sample["image"] = np.moveaxis(image, -1, 0) | ||
sample["mask"] = np.expand_dims(mask, 0) | ||
sample["trimap"] = np.expand_dims(trimap, 0) | ||
|
||
return sample | ||
|
||
|
||
class TqdmUpTo(tqdm): | ||
def update_to(self, b=1, bsize=1, tsize=None): | ||
if tsize is not None: | ||
self.total = tsize | ||
self.update(b * bsize - self.n) | ||
|
||
|
||
def download_url(url, filepath): | ||
directory = os.path.dirname(os.path.abspath(filepath)) | ||
os.makedirs(directory, exist_ok=True) | ||
if os.path.exists(filepath): | ||
return | ||
|
||
with TqdmUpTo(unit="B", unit_scale=True, unit_divisor=1024, miniters=1, desc=os.path.basename(filepath)) as t: | ||
urlretrieve(url, filename=filepath, reporthook=t.update_to, data=None) | ||
t.total = t.n | ||
|
||
|
||
def extract_archive(filepath): | ||
extract_dir = os.path.dirname(os.path.abspath(filepath)) | ||
dst_dir = os.path.splitext(filepath)[0] | ||
if not os.path.exists(dst_dir): | ||
shutil.unpack_archive(filepath, extract_dir) |
File renamed without changes.
File renamed without changes.
9 changes: 4 additions & 5 deletions
9
...ntation_models_pytorch/deeplabv3/model.py → ...odels_pytorch/decoders/deeplabv3/model.py
This file contains 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
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions
6
segmentation_models_pytorch/fpn/model.py → ...tion_models_pytorch/decoders/fpn/model.py
This file contains 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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...ntation_models_pytorch/linknet/decoder.py → ...odels_pytorch/decoders/linknet/decoder.py
This file contains 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
5 changes: 3 additions & 2 deletions
5
segmentation_models_pytorch/linknet/model.py → ..._models_pytorch/decoders/linknet/model.py
This file contains 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
File renamed without changes.
3 changes: 2 additions & 1 deletion
3
segmentation_models_pytorch/manet/decoder.py → ..._models_pytorch/decoders/manet/decoder.py
This file contains 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
6 changes: 3 additions & 3 deletions
6
segmentation_models_pytorch/manet/model.py → ...on_models_pytorch/decoders/manet/model.py
This file contains 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
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions
6
segmentation_models_pytorch/pan/model.py → ...tion_models_pytorch/decoders/pan/model.py
This file contains 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
File renamed without changes.
This file contains 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
6 changes: 2 additions & 4 deletions
6
segmentation_models_pytorch/pspnet/model.py → ...n_models_pytorch/decoders/pspnet/model.py
This file contains 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
File renamed without changes.
This file contains 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
6 changes: 3 additions & 3 deletions
6
segmentation_models_pytorch/unet/model.py → ...ion_models_pytorch/decoders/unet/model.py
This file contains 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
File renamed without changes.
This file contains 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
6 changes: 3 additions & 3 deletions
6
...tion_models_pytorch/unetplusplus/model.py → ...ls_pytorch/decoders/unetplusplus/model.py
This file contains 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
Empty file.
This file contains 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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
import warnings | ||
warnings.warn("`smp.utils` module is deprecated and will be removed in future releases.", DeprecationWarning) | ||
|
||
from . import train | ||
from . import losses | ||
from . import metrics | ||
from . import metrics |