PyTorch torch.nn.InstanceNorm2d function
PyTorch torch.nn Reference Manual
torch.nn.InstanceNorm2d is the instance normalization module in PyTorch.
It normalizes each channel of each sample independently and is often used for style transfer.
Function definition
torch.nn.InstanceNorm2d(num_features, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
Parameters
num_features: number of channelsaffine: whether to use learnable parameters
Usage examples
Example 1: Basic usage
Example
import
torch
import
torch.
nn
as
nn
inorm
=
nn.
InstanceNorm2d
(
64
)
Input
x
=
torch.
randn
(
4
,
64
,
16
,
16
)
output
=
inorm
(
x
)
(
"Input:"
,
x.
shape
,
"-> Output:"
,
output.
shape
)
Example 2: Style transfer
Example
import
torch
import
torch.
nn
as
nn
Style transfer networks often use InstanceNorm
class
StyleNet
(
nn.
Module
)
:
def
init
(
self
)
:
super
(
StyleNet
,
self
)
.
init
(
)
self
.
conv1
=
nn.
Conv2d
(
3
,
32
,
3
,
padding
=
1
)
self
.
in1
=
nn.
InstanceNorm2d
(
32
)
self
.
conv2
=
nn.
Conv2d
(
32
,
64
,
3
,
padding
=
1
)
self
.
in2
=
nn.
InstanceNorm2d
(
64
)
def
forward
(
self
,
x
)
:
x
=
self
.
conv1
(
x
)
x
=
self
.
in1
(
x
)
x
=
torch.
relu
(
x
)
x
=
self
.
conv2
(
x
)
x
=
self
.
in2
(
x
)
return
x
net
=
StyleNet
(
)
x
=
torch.
randn
(
1
,
3
,
256
,
256
)
output
=
net
(
x
)
(
"Input:"
,
x.
shape
,
"-> Output:"
,
output.
shape
)
Example 3: Comparing with BatchNorm
Example
import
torch
import
torch.
nn
as
nn
bn
=
nn.
BatchNorm2d
(
32
)
inorm
=
nn.
InstanceNorm2d
(
32
)
x
=
torch.
randn
(
4
,
32
,
8
,
8
)
(
"BatchNorm mean:"
,
bn
(
x
)
.
mean
(
dim
=
(
0
,
2
,
3
)
)
[
:
5
]
.
tolist
(
)
)
(
"InstanceNorm mean:"
,
inorm
(
x
)
.
mean
(
dim
=
(
0
,
2
,
3
)
)
[
:
5
]
.
tolist
(
)
)
Use Cases
- Style transfer: NIN, AdaIN
- Texture synthesis
- Small batch: suitable for batch=1
Note: by default affine=False, so there are no learnable parameters.