You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A `pytorch-toolbelt` is a Python library with a set of bells and whistles for PyTorch for fast R&D prototyping and Kaggle farming:
8
8
@@ -25,57 +25,135 @@ During 2018 I achieved a [Kaggle Master](https://www.kaggle.com/bloodaxe) badge
25
25
Very often I found myself re-using most of the old pipelines over and over again.
26
26
At some point it crystallized into this repository.
27
27
28
-
This lib is not meant to replace catalyst / ignite / fast.ai. Instead it's designed to complement them.
28
+
This lib is not meant to replace catalyst / ignite / fast.ai high-level frameworks. Instead it's designed to complement them.
29
29
30
30
# Installation
31
31
32
32
`pip install pytorch_toolbelt`
33
33
34
-
# Showcase
34
+
# How do I ...
35
+
36
+
## Model creation
35
37
36
-
##Encoder-decoder models construction
38
+
### Create Encoder-Decoder U-Net model
37
39
40
+
Below a code snippet that creates vanilla U-Net model for binary segmentation.
41
+
By design, both encoder and decoder produces a list of tensors, from fine (high-resolution, indexed `0`) to coarse (low-resolution) feature maps.
42
+
Access to all intermediate feature maps is beneficial if you want to apply deep supervision losses on them or encoder-decoder of object detection task,
43
+
where access to intermediate feature maps is necessary.
44
+
38
45
```python
46
+
from torch import nn
39
47
from pytorch_toolbelt.modules import encoders as E
40
48
from pytorch_toolbelt.modules import decoders as D
### Change number of input channels for the Encoder
86
+
87
+
All encoders from `pytorch_toolbelt` supports changing number of input channels. Simply call `encoder.change_input_channels(num_channels)` and first convolution layer will be changed.
88
+
Whenever possible, existing weights of convolutional layer will be re-used (in case new number of channels is greater than default, new weight tensor will be padded with randomly-initialized weigths).
89
+
Class method returns `self`, so this call can be chained.
90
+
91
+
92
+
```python
93
+
from pytorch_toolbelt.modules import encoders as E
94
+
95
+
encoder = E.SEResnet101Encoder()
96
+
encoder = encoder.change_input_channels(6)
97
+
```
98
+
99
+
100
+
## Misc
101
+
102
+
103
+
## Count number of parameters in encoder/decoder and other modules
104
+
105
+
When designing a model and optimizing number of features in neural network, I found it's quite useful to print number of parameters in high-level blocks (like `encoder` and `decoder`).
106
+
Here is how to do it with `pytorch_toolbelt`:
107
+
108
+
109
+
```python
110
+
from torch import nn
111
+
from pytorch_toolbelt.modules import encoders as E
112
+
from pytorch_toolbelt.modules import decoders as D
113
+
from pytorch_toolbelt.utils import count_parameters
There are multiple ways to combine multiple losses, and high-level DL frameworks like Catalyst offers way more flexible way to achieve this, but here's 100%-pure PyTorch implementation of mine:
67
136
68
137
```python
69
138
from pytorch_toolbelt import losses as L
70
139
140
+
# Creates a loss function that is a weighted sum of focal loss
141
+
# and lovasz loss with weigths 1.0 and 0.5 accordingly.
71
142
loss = L.JointLoss(L.FocalLoss(), 1.0, L.LovaszLoss(), 0.5)
72
143
```
73
144
74
-
## Test-time augmentation
145
+
146
+
## TTA / Inferencing
147
+
148
+
### Apply Test-time augmentation (TTA) for the model
149
+
150
+
Test-time augmetnation (TTA) can be used in both training and testing phases.
75
151
76
152
```python
77
153
from pytorch_toolbelt.inference import tta
78
154
155
+
model = UNet()
156
+
79
157
# Truly functional TTA for image classification using horizontal flips:
Quite often, there is a need to perform image segmentation for enormously big image (5000px and more). There are a few problems with such a big pixel arrays:
171
+
1. There are size limitations on maximum size of CUDA tensors (Concrete numbers depends on driver and GPU version)
172
+
2. Heavy CNNs architectures may eat up all available GPU memory with ease when inferencing relatively small 1024x1024 images, leaving no room to bigger image resolution.
173
+
174
+
One of the solutions is to slice input image into tiles (optionally overlapping) and feed each through model and concatenate the results back.
175
+
In this way you can guarantee upper limit of GPU ram usage, while keeping ability to process arbitrary-sized images on GPU.
176
+
91
177
92
178
```python
93
179
import numpy as np
94
-
import torch
180
+
from torch.utils.data import DataLoader
95
181
import cv2
96
182
97
183
from pytorch_toolbelt.inference.tiles import ImageSlicer, CudaTileMerger
0 commit comments