This module contains many layer classes that we might be interested in using in our models. These layers complement the default Pytorch layers which we can also use as predefined layers.
from fastai import *
from fastai.vision import *
from fastai.gen_doc.nbdoc import *
show_doc(AdaptiveConcatPool2d, doc_string=False)
from fastai.gen_doc.nbdoc import *
from fastai.layers import *
Layer that concats AdaptiveAvgPool2d
and AdaptiveMaxPool2d
. Output will be 2*sz
or 2 if sz
is None.
The AdaptiveConcatPool2d
object uses adaptive average pooling and adaptive max pooling and concatenates them both. We use this because it provides the model with the information of both methods and improves performance. This technique is called adaptive
because it allows us to decide on what output dimensions we want, instead of choosing the input's dimensions to fit a desired output size.
Let's try training with Adaptive Average Pooling first, then with Adaptive Max Pooling and finally with the concatenation of them both to see how they fare in performance.
We will first define a simple_cnn
using Adapative Max Pooling by changing the source code a bit.
path = untar_data(URLs.MNIST_SAMPLE)
data = ImageDataBunch.from_folder(path)
def simple_cnn_max(actns:Collection[int], kernel_szs:Collection[int]=None,
strides:Collection[int]=None) -> nn.Sequential:
"CNN with `conv2d_relu` layers defined by `actns`, `kernel_szs` and `strides`"
nl = len(actns)-1
kernel_szs = ifnone(kernel_szs, [3]*nl)
strides = ifnone(strides , [2]*nl)
layers = [conv2d_relu(actns[i], actns[i+1], kernel_szs[i], stride=strides[i])
for i in range(len(strides))]
layers.append(nn.Sequential(nn.AdaptiveMaxPool2d(1), Flatten()))
return nn.Sequential(*layers)
model = simple_cnn_max((3,16,16,2))
learner = Learner(data, model, metrics=[accuracy])
learner.fit(1)
Total time: 00:03 epoch train_loss valid_loss accuracy 1 0.091847 0.078211 0.975466 (00:03)
Now let's try with Adapative Average Pooling now.
def simple_cnn_avg(actns:Collection[int], kernel_szs:Collection[int]=None,
strides:Collection[int]=None) -> nn.Sequential:
"CNN with `conv2d_relu` layers defined by `actns`, `kernel_szs` and `strides`"
nl = len(actns)-1
kernel_szs = ifnone(kernel_szs, [3]*nl)
strides = ifnone(strides , [2]*nl)
layers = [conv2d_relu(actns[i], actns[i+1], kernel_szs[i], stride=strides[i])
for i in range(len(strides))]
layers.append(nn.Sequential(nn.AdaptiveAvgPool2d(1), Flatten()))
return nn.Sequential(*layers)
model = simple_cnn_avg((3,16,16,2))
learner = Learner(data, model, metrics=[accuracy])
learner.fit(1)
Total time: 00:02 epoch train_loss valid_loss accuracy 1 0.137756 0.125046 0.950442 (00:02)
Finally we will try with the concatenation of them both AdaptiveConcatPool2d
. We will see that, in fact, it increases our accuracy and decreases our loss considerably!
def simple_cnn(actns:Collection[int], kernel_szs:Collection[int]=None,
strides:Collection[int]=None) -> nn.Sequential:
"CNN with `conv2d_relu` layers defined by `actns`, `kernel_szs` and `strides`"
nl = len(actns)-1
kernel_szs = ifnone(kernel_szs, [3]*nl)
strides = ifnone(strides , [2]*nl)
layers = [conv2d_relu(actns[i], actns[i+1], kernel_szs[i], stride=strides[i])
for i in range(len(strides))]
layers.append(nn.Sequential(AdaptiveConcatPool2d(1), Flatten()))
return nn.Sequential(*layers)
model = simple_cnn((3,16,16,2))
learner = Learner(data, model, metrics=[accuracy])
learner.fit(1)
Total time: 00:02 epoch train_loss valid_loss accuracy 1 0.097507 0.074090 0.976938 (00:02)
Lambda allows us to define functions and use them as layers in our networks inside a Sequential object.
So, for example, say we want to apply a log_softmax loss and we need to change the shape of our output batches to be able to use this loss. We can add a layer that applies the necessary change in shape by calling:
Lambda(lambda x: x.view(x.size(0),-1))
Let's see an example of how the shape of our output can change when we add this layer.
model = nn.Sequential(
nn.Conv2d(3, 16, kernel_size=3, stride=2, padding=1), nn.ReLU(),
nn.Conv2d(16, 16, kernel_size=3, stride=2, padding=1), nn.ReLU(),
nn.Conv2d(16, 10, kernel_size=3, stride=2, padding=1), nn.ReLU(),
nn.AdaptiveAvgPool2d(1),
)
model.cuda()
for xb, yb in data.train_dl:
out = (model(*[xb]))
print(out.size())
break
torch.Size([64, 10, 1, 1])
model = nn.Sequential(
nn.Conv2d(3, 16, kernel_size=3, stride=2, padding=1), nn.ReLU(),
nn.Conv2d(16, 16, kernel_size=3, stride=2, padding=1), nn.ReLU(),
nn.Conv2d(16, 10, kernel_size=3, stride=2, padding=1), nn.ReLU(),
nn.AdaptiveAvgPool2d(1),
Lambda(lambda x: x.view(x.size(0),-1))
)
model.cuda()
for xb, yb in data.train_dl:
out = (model(*[xb]))
print(out.size())
break
torch.Size([64, 10])
show_doc(Flatten)
Flatten
[source]
Flatten
() →Tensor
Flattens x
to a single dimension, often used at the end of a model.
The function we build above is actually implemented in our library as Flatten
. We can see that it returns the same size when we run it.
model = nn.Sequential(
nn.Conv2d(3, 16, kernel_size=3, stride=2, padding=1), nn.ReLU(),
nn.Conv2d(16, 16, kernel_size=3, stride=2, padding=1), nn.ReLU(),
nn.Conv2d(16, 10, kernel_size=3, stride=2, padding=1), nn.ReLU(),
nn.AdaptiveAvgPool2d(1),
Flatten(),
)
model.cuda()
for xb, yb in data.train_dl:
out = (model(*[xb]))
print(out.size())
break
torch.Size([64, 10])
show_doc(PoolFlatten)
PoolFlatten
[source]
PoolFlatten
() →Sequential
Apply nn.AdaptiveAvgPool2d
to x
and then flatten the result.
We can combine these two final layers (AdaptiveAvgPool2d and Flatten
) by using PoolFlatten
.
model = nn.Sequential(
nn.Conv2d(3, 16, kernel_size=3, stride=2, padding=1), nn.ReLU(),
nn.Conv2d(16, 16, kernel_size=3, stride=2, padding=1), nn.ReLU(),
nn.Conv2d(16, 10, kernel_size=3, stride=2, padding=1), nn.ReLU(),
PoolFlatten()
)
model.cuda()
for xb, yb in data.train_dl:
out = (model(*[xb]))
print(out.size())
break
torch.Size([64, 10])
show_doc(ResizeBatch)
ResizeBatch
[source]
ResizeBatch
(size
:int
) →Tensor
Layer that resizes x to size
, good for connecting mismatched layers.
Another use we give to the Lambda function is to resize batches with ResizeBatch
when we have a layer that expects a different input than what comes from the previous one. Let's see an example:
a = torch.tensor([[1., -1.], [1., -1.]])
print(a)
tensor([[ 1., -1.], [ 1., -1.]])
out = ResizeBatch(4)
print(out(a))
tensor([[ 1., -1., 1., -1.]])
show_doc(StdUpsample, doc_string=False)
Increases the dimensionality of our data from n_in
to n_out
by applying a transposed convolution layer to the input and with batchnorm and a RELU activation.
show_doc(CrossEntropyFlat, doc_string=False)
class
CrossEntropyFlat
[source]
CrossEntropyFlat
(weight
=None
,size_average
=None
,ignore_index
=-100
,reduce
=None
,reduction
='elementwise_mean'
) ::CrossEntropyLoss
Same as nn.CrossEntropyLoss, but flattens input and target. Is used to calculate cross entropy on arrays (which Pytorch will not let us do with their nn.CrossEntropyLoss function). An example of a use case is image segmentation models where the output in an image (or an array of pixels).
The parameters are the same as nn.CrossEntropyLoss: weight
to rescale each class, size_average
whether we want to sum the losses across elements in a batch or we want to add them up, ignore_index
what targets do we want to ignore, reduce
on whether we want to return a loss per batch element and reduction
specifies which type of reduction (if any) we want to apply to our input.
show_doc(MSELossFlat)
class
MSELossFlat
[source]
MSELossFlat
(size_average
=None
,reduce
=None
,reduction
='elementwise_mean'
) ::MSELoss
Same as nn.MSELoss
, but flattens input and target.
show_doc(Debugger)
The debugger module allows us to peek inside a network while its training and see in detail what is going on. We can see inputs, ouputs and sizes at any point in the network.
For instance, if you run the following:
model = nn.Sequential(
nn.Conv2d(3, 16, kernel_size=3, stride=2, padding=1), nn.ReLU(),
Debugger(),
nn.Conv2d(16, 16, kernel_size=3, stride=2, padding=1), nn.ReLU(),
nn.Conv2d(16, 10, kernel_size=3, stride=2, padding=1), nn.ReLU(),
)
model.cuda()
learner = Learner(data, model, metrics=[accuracy])
learner.fit(5)
... you'll see something like this:
/home/ubuntu/fastai/fastai/layers.py(74)forward()
72 def forward(self,x:Tensor) -> Tensor:
73 set_trace()
---> 74 return x
75
76 class StdUpsample(nn.Module):
ipdb>
show_doc(bn_drop_lin, doc_string=False)
The bn_drop_lin
function returns a sequence of batch normalization, dropout and a linear layer. This custom layer is usually used at the end of a model.
n_in
represents the number of size of the input n_out
the size of the output, bn
whether we want batch norm or not, p
is how much dropout and actn
is an optional parameter to add an activation function at the end.
show_doc(conv2d)
show_doc(conv2d_relu, doc_string=False)
conv2d_relu
[source]
conv2d_relu
(ni
:int
,nf
:int
,ks
:int
=3
,stride
:int
=1
,padding
:int
=None
,bn
:bool
=False
,bias
:bool
=False
) →Sequential
Create a conv2d
layer with nn.ReLU
activation and optional(bn
) nn.BatchNorm2d
: ni
input, nf
out filters, ks
kernel, stride
:stride, padding
:padding, bn
: batch normalization.
show_doc(conv2d_trans)
conv2d_trans
[source]
conv2d_trans
(ni
:int
,nf
:int
,ks
:int
=2
,stride
:int
=2
,padding
:int
=0
,bias
=False
) →ConvTranspose2d
Create nn.ConvTranspose2d
layer: ni
inputs, nf
outputs, ks
kernel size, stride
: stride. padding
defaults to 0.
show_doc(conv_layer, doc_string=False)
conv_layer
[source]
conv_layer
(ni
:int
,nf
:int
,ks
:int
=3
,stride
:int
=1
) →Sequential
The conv_layer
function returns a sequence of nn.Conv2D, BatchNorm2d and a leaky RELU activation function.
n_in
represents the number of size of the input n_out
the size of the output, ks
kernel size, stride
the stride with which we want to apply the convolutions.
Create an embedding layer with input size ni
and output size nf
.
show_doc(simple_cnn)
simple_cnn
[source]
simple_cnn
(actns
:Collection
[int
],kernel_szs
:Collection
[int
]=None
,strides
:Collection
[int
]=None
,bn
=False
) →Sequential
CNN with conv2d_relu
layers defined by actns
, kernel_szs
and strides
, plus batchnorm if bn
.
show_doc(std_upsample_head, doc_string=False)
Create a sequence of upsample layers with a RELU at the beggining and a nn.ConvTranspose2d.
nfs
is a list with the input and output sizes of each upsample layer and c
is the output size of the final 2D Transpose Convolutional layer.
show_doc(trunc_normal_)
trunc_normal_
[source]
trunc_normal_
(x
:Tensor
,mean
:float
=0.0
,std
:float
=1.0
) →Tensor
Truncated normal initialization.
show_doc(Debugger.forward)
forward
[source]
forward
(x
:Tensor
) →Tensor
Defines the computation performed at every call. Should be overridden by all subclasses.
.. note::
Although the recipe for forward pass needs to be defined within
this function, one should call the :class:Module
instance afterwards
instead of this since the former takes care of running the
registered hooks while the latter silently ignores them.
show_doc(StdUpsample.forward)
forward
[source]
forward
(x
:Tensor
) →Tensor
Defines the computation performed at every call. Should be overridden by all subclasses.
.. note::
Although the recipe for forward pass needs to be defined within
this function, one should call the :class:Module
instance afterwards
instead of this since the former takes care of running the
registered hooks while the latter silently ignores them.
show_doc(MSELossFlat.forward)
forward
[source]
forward
(input
:Tensor
,target
:Tensor
) →Rank0Tensor
Defines the computation performed at every call. Should be overridden by all subclasses.
.. note::
Although the recipe for forward pass needs to be defined within
this function, one should call the :class:Module
instance afterwards
instead of this since the former takes care of running the
registered hooks while the latter silently ignores them.
show_doc(CrossEntropyFlat.forward)
forward
[source]
forward
(input
:Tensor
,target
:Tensor
) →Rank0Tensor
Defines the computation performed at every call. Should be overridden by all subclasses.
.. note::
Although the recipe for forward pass needs to be defined within
this function, one should call the :class:Module
instance afterwards
instead of this since the former takes care of running the
registered hooks while the latter silently ignores them.
show_doc(Lambda.forward)
forward
[source]
forward
(x
)
Defines the computation performed at every call. Should be overridden by all subclasses.
.. note::
Although the recipe for forward pass needs to be defined within
this function, one should call the :class:Module
instance afterwards
instead of this since the former takes care of running the
registered hooks while the latter silently ignores them.
show_doc(AdaptiveConcatPool2d.forward)
forward
[source]
forward
(x
)
Defines the computation performed at every call. Should be overridden by all subclasses.
.. note::
Although the recipe for forward pass needs to be defined within
this function, one should call the :class:Module
instance afterwards
instead of this since the former takes care of running the
registered hooks while the latter silently ignores them.