Block Layer
Stack multiple blocks together to create a block layer in ResNet.
We'll cover the following
Chapter Goals:
- Learn how a layer of blocks is organized
- Understand the function for creating a layer of blocks
A. Block layers
A ResNet model is made up of four block layers. Each block layer will contain a different number of blocks, depending on the total number of weight layers in the ResNet model. For example, an 18 layer ResNet model has 2 blocks in each block layer.
The blocks within each block layer are connected, so the output of block is the input of block in the same layer. Furthermore, the four block layers themselves are connected, so the output of block layer becomes the input of block layer .
The block layers account for a large majority of the weight layers in a ResNet model. However, they don't make up the entire structure of a ResNet model. The block layer is implemented in the ResNetModel
class as the block_layer
function:
import tensorflow as tfclass ResNetModel(object):# __init__ and other functions omitted# Creates a layer of blocksdef block_layer(self, inputs, filters, strides, num_blocks, is_training, index):with tf.compat.v1.variable_scope('block_layer{}'.format(index)):shortcut_filters = 4 * filters if self.bottleneck else filtersblock_fn = self.bottleneck_block if self.bottleneck else self.regular_blockblock_output = block_fn(inputs, filters, strides, is_training, 0,shortcut_filters=shortcut_filters)# stack the blocks in this layerfor i in range(1, num_blocks):block_output = block_fn(block_output, filters, 1, is_training, i)return block_output
As you can see, the function stacks multiple building blocks. The particular block to use (block_fn
) depends on whether or not the model uses bottlenecks.
Get hands-on with 1300+ tech skills courses.