# 8.3. Network in Network (NiN)¶ Open the notebook in Colab Open the notebook in Colab Open the notebook in Colab Open the notebook in SageMaker Studio Lab

LeNet, AlexNet, and VGG all share a common design pattern: extract features exploiting spatial structure via a sequence of convolutions and pooling layers and post-process the representations via fully connected layers. The improvements upon LeNet by AlexNet and VGG mainly lie in how these later networks widen and deepen these two modules.

This design poses two major challenges. First, the fully connected layers at the end of the architecture consume tremendous numbers of parameters. For instance, even a simple model such as VGG-11 requires a monstrous $$25088 \times 4096$$ matrix, occupying almost 400MB of RAM. This is a significant impediment to speedy computation, in particular on mobile and embedded devices. Second, it is equally impossible to add fully connected layers earlier in the network to increase the degree of nonlinearity: doing so would destroy the spatial structure and require potentially even more memory.

The network in network (NiN) blocks of offer an alternative, capable of solving both problems in one simple strategy. They were proposed based on a very simple insight: (i) use $$1 \times 1$$ convolutions to add local nonlinearities across the channel activations and (ii) use global average pooling to integrate across all locations in the last representation layer. Note that global average pooling would not be effective, were it not for the added nonlinearities. Let’s dive into this in detail.

## 8.3.1. NiN Blocks¶

Recall Section 7.4.3. In it we discussed that the inputs and outputs of convolutional layers consist of four-dimensional tensors with axes corresponding to the example, channel, height, and width. Also recall that the inputs and outputs of fully connected layers are typically two-dimensional tensors corresponding to the example and feature. The idea behind NiN is to apply a fully connected layer at each pixel location (for each height and width). The resulting $$1 \times 1$$ convolution can be thought as a fully connected layer acting independently on each pixel location.

Fig. 8.3.1 illustrates the main structural differences between VGG and NiN, and their blocks. Note both the difference in the NiN blocks (the initial convolution is followed by $$1 \times 1$$ convolutions, whereas VGG retains $$3 \times 3$$ convolutions) and in the end where we no longer require a giant fully connected layer.

Fig. 8.3.1 Comparing architectures of VGG and NiN, and their blocks.

import torch
from torch import nn
from d2l import torch as d2l

return nn.Sequential(
nn.ReLU(),
nn.LazyConv2d(out_channels, kernel_size=1), nn.ReLU(),
nn.LazyConv2d(out_channels, kernel_size=1), nn.ReLU())

from mxnet import init, np, npx
from mxnet.gluon import nn
from d2l import mxnet as d2l

npx.set_np()

blk = nn.Sequential()
activation='relu'),
nn.Conv2D(num_channels, kernel_size=1, activation='relu'),
nn.Conv2D(num_channels, kernel_size=1, activation='relu'))
return blk

import tensorflow as tf
from d2l import tensorflow as d2l

return tf.keras.models.Sequential([
tf.keras.layers.Conv2D(out_channels, kernel_size, strides=strides,
tf.keras.layers.Activation('relu'),
tf.keras.layers.Conv2D(out_channels, 1),
tf.keras.layers.Activation('relu'),
tf.keras.layers.Conv2D(out_channels, 1),
tf.keras.layers.Activation('relu')])


## 8.3.2. NiN Model¶

NiN uses the same initial convolution sizes as AlexNet (it was proposed shortly thereafter). The kernel sizes are $$11\times 11$$, $$5\times 5$$, and $$3\times 3$$, respectively, and the numbers of output channels match those of AlexNet. Each NiN block is followed by a max-pooling layer with a stride of 2 and a window shape of $$3\times 3$$.

The second significant difference between NiN and both AlexNet and VGG is that NiN avoids fully connected layers altogether. Instead, NiN uses a NiN block with a number of output channels equal to the number of label classes, followed by a global average pooling layer, yielding a vector of logits. This design significantly reduces the number of required model parameters, albeit at the expense of a potential increase in training time.

class NiN(d2l.Classifier):
def __init__(self, lr=0.1, num_classes=10):
super().__init__()
self.save_hyperparameters()
self.net = nn.Sequential(
nn.MaxPool2d(3, stride=2),
nn.MaxPool2d(3, stride=2),
nn.MaxPool2d(3, stride=2),
nn.Dropout(0.5),
nn.Flatten())
self.net.apply(d2l.init_cnn)

class NiN(d2l.Classifier):
def __init__(self, lr=0.1, num_classes=10):
super().__init__()
self.save_hyperparameters()
self.net = nn.Sequential()
nn.MaxPool2D(pool_size=3, strides=2),
nn.MaxPool2D(pool_size=3, strides=2),
nn.MaxPool2D(pool_size=3, strides=2),
nn.Dropout(0.5),
nn.GlobalAvgPool2D(),
nn.Flatten())
self.net.initialize(init.Xavier())

class NiN(d2l.Classifier):
def __init__(self, lr=0.1, num_classes=10):
super().__init__()
self.save_hyperparameters()
self.net = tf.keras.models.Sequential([
tf.keras.layers.MaxPool2D(pool_size=3, strides=2),
tf.keras.layers.MaxPool2D(pool_size=3, strides=2),
tf.keras.layers.MaxPool2D(pool_size=3, strides=2),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.GlobalAvgPool2D(),
tf.keras.layers.Flatten()])


We create a data example to see the output shape of each block.

model = NiN()
X = torch.randn(1, 1, 224, 224)
for layer in model.net:
X = layer(X)
print(layer.__class__.__name__,'output shape:\t', X.shape)

Sequential output shape:     torch.Size([1, 96, 54, 54])
MaxPool2d output shape:      torch.Size([1, 96, 26, 26])
Sequential output shape:     torch.Size([1, 256, 26, 26])
MaxPool2d output shape:      torch.Size([1, 256, 12, 12])
Sequential output shape:     torch.Size([1, 384, 12, 12])
MaxPool2d output shape:      torch.Size([1, 384, 5, 5])
Dropout output shape:        torch.Size([1, 384, 5, 5])
Sequential output shape:     torch.Size([1, 10, 5, 5])
AdaptiveAvgPool2d output shape:      torch.Size([1, 10, 1, 1])
Flatten output shape:        torch.Size([1, 10])
/home/d2l-worker/miniconda3/envs/d2l-en-release-1/lib/python3.8/site-packages/torch/nn/modules/lazy.py:178: UserWarning: Lazy modules are a new feature under heavy development so changes to the API or functionality can happen at any moment.
warnings.warn('Lazy modules are a new feature under heavy development '

model = NiN()
X = np.random.randn(1, 1, 224, 224)
for layer in model.net:
X = layer(X)
print(layer.__class__.__name__,'output shape:\t', X.shape)

Sequential output shape:     (1, 96, 54, 54)
MaxPool2D output shape:      (1, 96, 26, 26)
Sequential output shape:     (1, 256, 26, 26)
MaxPool2D output shape:      (1, 256, 12, 12)
Sequential output shape:     (1, 384, 12, 12)
MaxPool2D output shape:      (1, 384, 5, 5)
Dropout output shape:        (1, 384, 5, 5)
Sequential output shape:     (1, 10, 5, 5)
GlobalAvgPool2D output shape:        (1, 10, 1, 1)
Flatten output shape:        (1, 10)

model = NiN()
X = tf.random.normal((1, 224, 224, 1))
for layer in model.net.layers:
X = layer(X)
print(layer.__class__.__name__,'output shape:\t', X.shape)

Sequential output shape:     (1, 54, 54, 96)
MaxPooling2D output shape:   (1, 26, 26, 96)
Sequential output shape:     (1, 26, 26, 256)
MaxPooling2D output shape:   (1, 12, 12, 256)
Sequential output shape:     (1, 12, 12, 384)
MaxPooling2D output shape:   (1, 5, 5, 384)
Dropout output shape:        (1, 5, 5, 384)
Sequential output shape:     (1, 5, 5, 10)
GlobalAveragePooling2D output shape:         (1, 10)
Flatten output shape:        (1, 10)


## 8.3.3. Training¶

As before we use Fashion-MNIST to train the model. NiN’s training is similar to that for AlexNet and VGG.

model = NiN(lr=0.05)
trainer = d2l.Trainer(max_epochs=10, num_gpus=1)
data = d2l.FashionMNIST(batch_size=128, resize=(224, 224))
trainer.fit(model, data)

model = NiN(lr=0.05)
trainer = d2l.Trainer(max_epochs=10, num_gpus=1)
data = d2l.FashionMNIST(batch_size=128, resize=(224, 224))
trainer.fit(model, data)

trainer = d2l.Trainer(max_epochs=10)
data = d2l.FashionMNIST(batch_size=128, resize=(224, 224))
with d2l.try_gpu():
model = NiN(lr=0.05)
trainer.fit(model, data)


## 8.3.4. Summary¶

NiN has dramatically fewer parameters than AlexNet and VGG. This stems from the fact that it needs no giant fully connected layers and fewer convolutions with wide kernels. Instead, it uses local $$1 \times 1$$ convolutions and global average pooling. These design choices influenced many subsequent CNN designs.

## 8.3.5. Exercises¶

1. Why are there two $$1\times 1$$ convolutional layers per NiN block? What happens if you add one? What happens if you reduce this to one?

2. What happens if you replace the global average pooling by a fully connected layer (speed, accuracy, number of parameters)?

3. Calculate the resource usage for NiN.

1. What is the number of parameters?

2. What is the amount of computation?

3. What is the amount of memory needed during training?

4. What is the amount of memory needed during prediction?

4. What are possible problems with reducing the $$384 \times 5 \times 5$$ representation to a $$10 \times 5 \times 5$$ representation in one step?

5. Use the structural design decisions in VGG that led to VGG-11, VGG-16, and VGG-19 to design a family of NiN-like networks.