Skip to content

Commit

Permalink
Segmentation contrib (#194)
Browse files Browse the repository at this point in the history
* hash logdir

* meta unet init

* unet & linknet

* resnet-unet

* resnet-linknet

* refactoring

* fpn unet

* psp

* simplify & refactor

* no debug

* code cleanup
  • Loading branch information
Scitator committed May 29, 2019
1 parent 1923bcd commit 341b8a8
Show file tree
Hide file tree
Showing 63 changed files with 3,040 additions and 397 deletions.
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,14 @@ ENV/
.idea
.code

tmp/
logs/


*.bak
*.csv
*.tsv
*.ipynb

tmp/
logs/
data/
examples/data/
examples/logs/
notebooks/
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ jobs:

- stage: Examples
name: "Examples DL"
before_install: *before_install
install: *requirements
script:
- pip install tifffile
- wget -P ./data/ https://www.dropbox.com/s/0rvuae4mj6jn922/isbi.tar.gz
- tar -xf ./data/isbi.tar.gz -C ./data/
- for f in examples/_tests_scripts/*.py; do PYTHONPATH=./catalyst:${PYTHONPATH} python "$f"; done
- PYTHONPATH=./examples:./catalyst:${PYTHONPATH}
python catalyst/dl/scripts/run.py
--expdir=./examples/_tests_mnist_stages
Expand Down
2 changes: 1 addition & 1 deletion catalyst/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '19.06rc3'
__version__ = '19.06rc4'
4 changes: 2 additions & 2 deletions catalyst/contrib/criterion/dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def __init__(
self,
eps: float = 1e-7,
threshold: float = None,
activation: str = "sigmoid"
activation: str = "Sigmoid"
):
super(DiceLoss, self).__init__()

Expand All @@ -29,7 +29,7 @@ def __init__(
self,
eps: float = 1e-7,
threshold: float = None,
activation: str = "sigmoid"
activation: str = "Sigmoid"
):
super().__init__()
self.bce_loss = nn.BCEWithLogitsLoss()
Expand Down
35 changes: 10 additions & 25 deletions catalyst/contrib/criterion/iou.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,23 @@ class IoULoss(nn.Module):
"""
Intersection over union (Jaccard) loss
Args:
mode (str): one of ``['hard', 'soft']`` to calculate IoU
eps (float): epsilon to avoid zero division
threshold (float): threshold for outputs binarization
activation (str): An torch.nn activation applied to the outputs.
Must be one of ['none', 'sigmoid', 'softmax2d']
Must be one of ['none', 'Sigmoid', 'Softmax2d']
"""
def __init__(
self,
mode: str = "hard",
eps: float = 1e-7,
threshold: float = None,
activation: str = "sigmoid",
activation: str = "Sigmoid",
):
super().__init__()
assert mode in ["hard", "soft"], \
f"Mode must be one of ['hard', 'soft'], got {mode}."
self.mode = mode

if mode == "hard":
self.metric_fn = partial(
metrics.iou,
eps=eps,
threshold=threshold,
activation=activation)
else:
self.metric_fn = partial(
metrics.soft_iou,
eps=eps,
threshold=threshold,
activation=activation)
self.metric_fn = partial(
metrics.iou,
eps=eps,
threshold=threshold,
activation=activation)

def forward(self, outputs, targets):
iou = self.metric_fn(outputs, targets)
Expand All @@ -47,24 +34,22 @@ class BCEIoULoss(nn.Module):
"""
Intersection over union (Jaccard) with BCE loss
Args:
mode (str): one of ``['hard', 'soft']`` to calculate IoU
eps (float): epsilon to avoid zero division
threshold (float): threshold for outputs binarization
activation (str): An torch.nn activation applied to the outputs.
Must be one of ['none', 'sigmoid', 'softmax2d']
Must be one of ['none', 'Sigmoid', 'Softmax2d']
reduction (str): Specifies the reduction to apply to the output of BCE
"""
def __init__(
self,
mode: str = "hard",
eps: float = 1e-7,
threshold: float = None,
activation: str = "sigmoid",
activation: str = "Sigmoid",
reduction: str = "mean",
):
super().__init__()
self.bce_loss = nn.BCEWithLogitsLoss(reduction=reduction)
self.iou_loss = IoULoss(mode, eps, threshold, activation)
self.iou_loss = IoULoss(eps, threshold, activation)

def forward(self, outputs, targets):
iou = self.iou_loss.forward(outputs, targets)
Expand Down
8 changes: 6 additions & 2 deletions catalyst/contrib/models/classification/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from .mobilenet import MobileNetV2
from .mobilenetv2 import MobileNetV2
from .mobilenetv3 import MobileNetV3, MobileNetV3Small, MobileNetV3Large

__all__ = ["MobileNetV2"]
__all__ = [
"MobileNetV2",
"MobileNetV3", "MobileNetV3Small", "MobileNetV3Large"
]
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
arXiv preprint arXiv:1801.04381.
import from https://github.com/tonylins/pytorch-mobilenet-v2
source https://github.com/d-li14/mobilenetv2.pytorch
source: https://github.com/d-li14/mobilenetv2.pytorch
"""

import math
Expand All @@ -25,7 +25,7 @@ def _make_divisible(v, divisor, min_value=None):
This function is taken from the original tf repo.
It ensures that all layers have a channel number that is divisible by 8
It can be seen here:
https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py
https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py
:param v:
:param divisor:
:param min_value:
Expand Down Expand Up @@ -109,8 +109,14 @@ def __init__(
pretrained=None
):
super().__init__()

# building first layer
assert input_size % 32 == 0
input_channel = _make_divisible(32 * width_mult, 8)
layers = [conv_3x3_bn(3, input_channel, 2)]

# setting of inverted residual blocks
self.cfgs = [
inverted_residual_settings = [
# t, c, n, s
[1, 16, 1, 1],
[6, 24, 2, 2],
Expand All @@ -120,26 +126,23 @@ def __init__(
[6, 160, 3, 2],
[6, 320, 1, 1],
]

# building first layer
assert input_size % 32 == 0
input_channel = _make_divisible(32 * width_mult, 8)
layers = [conv_3x3_bn(3, input_channel, 2)]
# building inverted residual blocks
block = InvertedResidual
for t, c, n, s in self.cfgs:
for t, c, n, s in inverted_residual_settings:
output_channel = _make_divisible(c * width_mult, 8)
layers.append(block(input_channel, output_channel, s, t))
input_channel = output_channel
for i in range(1, n):
layers.append(block(input_channel, output_channel, 1, t))
input_channel = output_channel

# building last several layers
self.output_channel = _make_divisible(1280 * width_mult, 8) \
if width_mult > 1.0 \
else 1280
layers.append(conv_1x1_bn(input_channel, self.output_channel))

self.layers = layers
self.encoder = nn.Sequential(*layers)
self.avgpool = nn.AvgPool2d(input_size // 32, stride=1)
self.classifier = nn.Linear(self.output_channel, num_classes)
Expand Down Expand Up @@ -171,8 +174,10 @@ def _initialize_weights(self):

def forward(self, x):
x = self.encoder(x)
x = self.conv(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = self.classifier(x)
return x


__all__ = ["MobileNetV2"]

0 comments on commit 341b8a8

Please sign in to comment.