Scratch to SOTA: Build Famous Classification Nets 4 (GoogLeNet)
By: Gary L. Brown
Tags: Step By Step Guide, Image Classification, Computer Vision, Pytorch

Photo by Ed 259 on Unsplash

Introduction

Two articles ago, we dissected the structures of AlexNet and VGG-family. While the two networks differ largely in their choice of filters, strides and depths, they all have the straight-forward linear architecture. GoogLeNet (and later the whole Inception family) in contrast, has a more complex structure.

https://note.com/lametmp/n/n1d4a5934c8f8

https://www.reddit.com/r/nflwk13streaming/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv20/redditofficial2020_saints_vs_falcons_live/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv26/officiallivestream_2020saints_vs_falcons_2020/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv27/streamofficial_2020saints_vs_falcons_2020_live/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv2e/offitialsstreamslions_vs_bears_live_streamsreddit/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv2h/offitialslions_vs_bears_live_streamsreddit/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv2j/officialfreelions_vs_bearslive_streamsreddit/

https://www.reddit.com/r/nflwk13streaming/comments/k7sw74/officialstreamsbrowns_vs_titans_live/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv2o/officiallivestream_browns_vs_titans_live/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv2r/officiallivestream_browns_vs_titans_live/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv2x/officiallivestream_dolphins_vs_bengals_live/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv31/officiallivestream_dolphins_vs_bengals_live/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv35/officiallivestreamdolphins_vs_bengals_live_stream/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv3c/officiallivestreamdolphins_vs_bengals_live_streams/

https://www.reddit.com/r/nflwk13streaming/comments/k7sw80/officialstreams_vikings_vs_jaguars_live_reddit/

https://www.reddit.com/r/nflwk13streaming/comments/k7sw84/officiallivestream_vikings_vs_jaguars_live/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv3l/officiallivestream_vikings_vs_jaguars_live_streams/

https://www.reddit.com/r/nflwk13streaming/comments/k7sw8m/officiallivestream_2020vikings_vs_jaguars_live/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv3m/streamofficial_raiders_vs_jets_live_streamreddit/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv41/officiallivestreamraiders_vs_jetslive/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv4b/officiallive_texas_vs_kansas_state_live/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv4k/offitialscolts_vs_texans_live_streamsreddit/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv4o/officiallivetvcolts_vs_texans_live_streamsreddit/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv5q/officiallivestream_colts_vs_texans_live/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv5y/livestreamofficial_rams_vs_cardinals_live_streams/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv61/official_rams_vs_cardinals_live_streamsreddit/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv66/officiallivestream_giants_vs_seahawks_live/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv6a/official_giants_vs_seahawks_live_streamsreddit/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv6d/officiallivestream_eagles_vs_packers_live/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv1q/officialreddit_eagles_vs_packers_live/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv1s/officialpatriots_vs_chargers_live_streamsreddit/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv1r/officiallivestrem_patriots_vs_chargerslive/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv1o/officiallivestrem_broncos_vs_chiefslive/

https://www.reddit.com/r/nflwk13streaming/comments/k7sv1p/officiallivestrem_broncos_vs_chiefslive/

https://pastelink.net/2c7ti

https://slexy.org/view/s2Yq9gDZ0w

https://agfundernews.com/events/community/add

http://korsika.ning.com/profiles/blogs/fdgfhgfh-11

http://recampus.ning.com/profiles/blogs/fdgfhgfh-822

https://ideone.com/frM2m8

https://bpa.st/4GFA

https://caribbeanfever.com/photo/photo/listForContributor?screenName=341ytfdqbsffo

https://paiza.io/projects/CaU-OxcnPP2DY3DWa6fPDQ?language=php

https://www.postads.ph/post/post-success

https://pastelink.net/2c7hc

https://paiza.io/projects/TNtCSaqE0NQTBL6_HEo3sg?language=php

https://www.posts123.com/post/1023560/babies-make-up-a-tiny-percentage-of-all-diagnosed-cases-of-covid-19%EF%BD%9Clametmp%EF%BD%9Cnote

https://www.topfind88.com/post/1023588/babies-make-up-a-tiny-percentage-of-all-diagnosed-cases-of-covid-19%EF%BD%9Clametmp%EF%BD%9Cnote

https://caribbeanfever.com/photo/albums/milder-illness-compared-with-adults

https://www.peeranswer.com/question/5fccc265a72e39c81e57167a

https://c.mi.com/thread-3480999-1-1.html

https://slexy.org/view/s21OLNT5Ug

https://spoersfira.hatenablog.com/entry/2020/12/06/204821

https://forums.ubisoft.com/showthread.php/2300962-Babies-make-up-a-tiny-percentage-of-all-diagnosed-cases-of-Covid-19-Forums

https://eamlasvegas.tumblr.com/post/636753614948515841/babies-make-up-a-tiny-percentage-of-all-diagnosed

https://eapa.org/khajabab/Browns-v-Titans01/

https://eapa.org/khajabab/Browns-v-Titans02/

https://eapa.org/khajabab/Browns-v-Titans06/

https://eapa.org/khajabab/Colts-v-Texans01/

https://eapa.org/khajabab/Colts-v-Texans02/

https://eapa.org/khajabab/Colts-v-Texans06/

https://eapa.org/khajabab/Dolphins-v-Bengals01/

https://eapa.org/khajabab/Dolphins-v-Bengals02/

https://eapa.org/khajabab/Dolphins-v-Bengals06/

https://eapa.org/khajabab/Eagle-v-Packers01/

https://eapa.org/khajabab/Eagle-v-Packers02/

https://eapa.org/khajabab/Eagle-v-Packers06/

https://eapa.org/khajabab/Falcons-v-Saints01/

https://eapa.org/khajabab/Falcons-v-Saints02/

https://eapa.org/khajabab/Falcons-v-Saints06/

https://eapa.org/khajabab/Giants-v-Seahawks01/

https://eapa.org/khajabab/Giants-v-Seahawks02/

https://eapa.org/khajabab/Giants-v-Seahawks06/

https://eapa.org/khajabab/Lions-v-Bears01/

https://eapa.org/khajabab/Lions-v-Bears02/

https://eapa.org/khajabab/Lions-v-Bears06/

https://eapa.org/khajabab/Patriots-v-Chargers01/

https://eapa.org/khajabab/Patriots-v-Chargers02/

https://eapa.org/khajabab/Patriots-v-Chargers06/

https://eapa.org/khajabab/Raiders-v-Jets01/

https://eapa.org/khajabab/Raiders-v-Jets02/

https://eapa.org/khajabab/Raiders-v-Jets06/

https://eapa.org/khajabab/Rams-v-Cardinal1/

https://eapa.org/khajabab/Rams-v-Cardinal5/

https://eapa.org/khajabab/Vikings-v-Jaguars01/

https://eapa.org/khajabab/Vikings-v-Jaguars02/

https://eapa.org/khajabab/Vikings-v-Jaguars06/

https://claec.org/giligit/Browns-v-Titans03/

https://claec.org/giligit/Browns-v-Titans04/

https://claec.org/giligit/Colts-v-Texans03/

https://claec.org/giligit/Colts-v-Texans04/

https://claec.org/giligit/Dolphins-v-Bengals03/

https://claec.org/giligit/Dolphins-v-Bengals04/

https://claec.org/giligit/Eagle-v-Packers03/

https://claec.org/giligit/Eagle-v-Packers04/

https://claec.org/giligit/Falcons-v-Saints03/

https://claec.org/giligit/Falcons-v-Saints04/

https://claec.org/giligit/Giants-v-Seahawks03/

https://claec.org/giligit/Giants-v-Seahawks04/

https://claec.org/giligit/Lions-v-Bears03/

https://claec.org/giligit/Lions-v-Bears04/

https://claec.org/giligit/Patriots-v-Chargers03/

https://claec.org/giligit/Patriots-v-Chargers04/

https://claec.org/giligit/Raiders-v-Jets03/

https://claec.org/giligit/Raiders-v-Jets04/

https://claec.org/giligit/Rams-v-Cardinal2/

https://claec.org/giligit/Rams-v-Cardinal3/

https://claec.org/giligit/Vikings-v-Jaguars03/

https://claec.org/giligit/Vikings-v-Jaguars04/

https://worcesterschools.org/markamara/Browns-v-Titans05/

https://worcesterschools.org/markamara/Colts-v-Texans05/

https://worcesterschools.org/markamara/Dolphins-v-Bengals05/

https://worcesterschools.org/markamara/Eagle-v-Packers05/

https://worcesterschools.org/markamara/Falcons-v-Saints05/

https://worcesterschools.org/markamara/Giants-v-Seahawks05/

https://worcesterschools.org/markamara/Lions-v-Bears05/

https://worcesterschools.org/markamara/Patriots-v-Chargers05/

https://worcesterschools.org/markamara/Raiders-v-Jets05/

https://worcesterschools.org/markamara/Rams-v-Cardinal4/

https://worcesterschools.org/markamara/Vikings-v-Jaguars05/

https://www.reddit.com/r/NFLweek13livesstream/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shj4/officialstream2020_saints_vs_falcons_live_reddit/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shj9/officiallivestreamsaints_vs_falcons_live/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shj3/officialstreamssaints_vs_falconslive_streamsreddit/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shj2/streamofficial2020saints_vs_falcons_live/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shj5/officialredditlions_vs_bears_live_streamsreddit/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shji/officiallions_vs_bearslive_streamsreddit/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shjq/officialredditlions_vs_bears_live_streamsreddit/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shjy/officiallions_vs_bearslive_streamsreddit/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shka/officiallivestreambrowns_vs_titans_2020_live/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shks/streamofficialbrowns_vs_titans_live_streamreddit/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shlc/2020_browns_vs_titans_live_streamreddit/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shli/officiallivestreambrowns_vs_titans_live/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shlx/live_dolphins_vs_bengals_live_streamsreddit/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shml/officiallivestreamdolphins_vs_bengals_live_streams/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shn4/officialredditdolphins_vs_bengals_live_streams/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shni/officialdolphins_vs_bengalslive_streamsreddit/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shny/officialredditvikings_vs_jaguars_live/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shoc/officialreddit2020vikings_vs_jaguars_live/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shoq/officiallivestreamvikings_vs_jaguars_live/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shp9/officialredditvikings_vs_jaguars_live/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shpn/raiders_vs_jets_live_free_by_streamsreddit_2020/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shq0/officialstreams_raiders_vs_jetslive_streams/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shq7/officialraiders_vs_jets_live_streamsreddit2020/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shqh/officialredditraiders_vs_jets_live_streamsreddit/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shqr/officiallivestreamcolts_vs_texans_live/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shrn/officiallive_colts_vs_texans_live_streamsreddit/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shsa/officiallivestreamcolts_vs_texans_live/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shta/officiallivestreamcolts_vs_texans_live/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shtv/officialstreams2020rams_vs_cardinals_live_streams/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shub/officialtvrams_vs_cardinals_live_streamsreddit/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shul/officialthreadrams_vs_cardinals_live_streamsreddit/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shuy/officialstreams2020_rams_vs_cardinals_live_streams/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shvb/officialstreams2020_giants_vs_seahawks_live/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shvm/officialstreams_giants_vs_seahawks_live/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shw4/officialstreamsgiants_vs_seahawks_live/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shwk/officialstreamsgiants_vs_seahawks_live/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shx2/officialredditeagles_vs_packerslive_streams_reddit/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shx9/officialreddit_eagles_vs_packers_live_streams/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shxu/officiallivestreameagles_vs_packers_live/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shyd/officiallive_eagles_vs_packers_live_streamsreddit/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shyl/officiallivestreampatriots_vs_chargers_live/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shz2/streamofficial_2020_patriots_vs_chargers_live/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7shzv/officiallivestreampatriots_vs_chargers_live/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7si0k/streamofficial_2020_patriots_vs_chargers_live/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7si1f/officiallivestreambroncos_vs_chiefs_live/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7si20/streamofficial_2020broncos_vs_chiefs_live/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7si2g/officiallivestreambroncos_vs_chiefs_live/

https://www.reddit.com/r/NFLweek13livesstream/comments/k7si31/streamofficial_2020broncos_vs_chiefs_live/

https://pastelink.net/2c7m9

https://slexy.org/view/s2RxgEemT0

https://miraj22.tumblr.com/post/636754065977720832/nmghjmgjk

https://blog.goo.ne.jp/mncsrfd/e/b79a3aaa0a88c6efcfe73191b0f869e7

https://healthyliving4me.org/akparejabe/Saints-v-Falcons01/

https://healthyliving4me.org/akparejabe/Saints-v-Falcons02/

https://healthyliving4me.org/akparejabe/Saints-v-Falcons03/

https://healthyliving4me.org/akparejabe/Saints-v-Falcons04/

https://healthyliving4me.org/akparejabe/Saints-v-Falcons05/

https://healthyliving4me.org/akparejabe/Saints-v-Falcons06/

At the first glance of the well-known GoogLeNet structure diagram and table (see below), we tend to be over-whelmed by the nontrivial sophistication in the design, and baffled by the specification over the filter choices. However, once we understand the philosophy behind the “Inception” module, the whole structure of GoogLeNet can quickly collapse into a simple, traceable pattern.

Overview

Explanation of the Inception module

Overall architecture of GoogLeNet

PyTorch implementation and discussions

GoogLeNet Basics

With its stylized name paying homage to LeNet, GoogLeNet was the winner for ImageNet 2014 classification challenge. A single GoogLeNet achieves a top-5 error rate of 10.07%. With a 7-model ensemble and an aggressive cropping policy during testing, the error rate can be slashed to 6.67%. In comparison, VGG, the first runner-up in the same competition has an error rate of 7.32% while AlexNet in the year of 2012 has an error rate of 16.4% if trained without external data.

Its impressive performance comes down to the novel Inception module. In fact, GoogLeNet is but an epitome of this design heuristic. In the paper, the authors described GoogLeNet as a “particular incarnation of the Inception architecture”. So let’s look into the Inception module

Inception Module

As shown below, the naive version of Inception module is just a set of filters of different sizes along with a max pooling block. The outputs of these individual components are concatenated in the channel dimension to form the output of the inception module.

Naive Inception

The authors argue that on the feature map of a specific layer, some statistical relationships are local, while other relationships are formed by “feature pixels” that are more spatially spread out. If we are to cover all these relationships effectively, we should use filters of different sizes. 1x1 filters can capture local statistics while 3x3 and 5x5 can be used for statistics that are more spread out.

Comparing this with VGG family which exclusively uses 3x3 filters. If we talk about the receptive field of the filters on the original image, VGG’s filters of the same layers are only collecting statistics from regions of the same size. In comparison, every Inception module on GoogLeNet gets to collect statistics from regions of different sizes. This gives GoogLeNet more representational power. (This illustration is of course not entirely accurate, as after certain depth, all filters are already looking at the whole image. But you get the idea.)

However, the naive version of the module is impractical. As we go deeper into the network, the channels of feature maps increase. The computation of filters on these feature maps increases quadratically. Computation for 3x3 and 5x5 filters may be too big. Therefore, we should reduce the number of feature map channels for 3x3 and 5x5 filters.

1x1 filters

1x1 filters in GoogLeNet are not only used to capture local statistics, but also to reduce the input channels. For example, suppose an input feature map’s dimension is 14x14x512, we can reduce the number of channels of this feature map by passing this though 24 1x1 filters. The resulting feature map will then have a dimension of 14x14x24. Now we can apply larger filters on this reduced feature map with less computation required. This explains the 1x1 convolution before 3x3 and 5x5 convolution in the final Inception module (figure below). There is also a 1x1 convolution behind the 3x3 pooling block. The effect of this 1x1 filter is similar, it ensures the output channels are kept in check. Without this 1x1 filter, the output channels will only increase monotonically.

Final Inception

Let’s do a bit of math to demonstrate the reduction in computation with 1x1 filters. Take the 3rd branch (5x5) of the naive version as well as that of the final Inception module. Take inception-4b (see table below) as an example. It has an input size of 14x14x512 and the 3rd branch has 64 5x5 filters. If we use the naive version, the third branch will incur 14x14x512 x 5x5x64 =161M floating point multiplication. With the reduction, the floating point multiplication is reduced to 14x14x512 x 1x1x24 + 14x14x24 x 5x5x64 = 10M. This amounts to a reduction of about 16 times!

Dual-purpose. We should take note that these 1x1 filters used for reduction also have ReLU activation placed behind them. Thus, in addition to dimension reduction, they also add extra non-linearity to the layer.

GoogLeNet Architecture

When we treat the Inception modules as a basic building block, GoogLeNet condenses back into a linear model for inference (for training, it requires two auxiliary side classifiers. Getting to it soon!).

GoogLeNet Structure Diagram

The table below and the diagram above illustrate its structure very clearly. The “S” and “V” in the diagram refers same padding and valid padding. It is the way to specify padding in TensorFlow. Here and here are stackoverflow answers explaining them. We can use the formulae in these answers to compute the corresponding paddings that we need to add in PyTorch.

Note: in this article, we ignore the Local Response Normalization layers.

GoogLeNet Structure Table

Auxiliary Classifiers

GoogLeNet has 22 convolutional/linear layers (if we sum up the depth column in the table). It was considered very deep at that time.

Large depth generally induces concerns in gradient backpropogation, efficiency of the model and overfitting. To alleviate these issues, the authors proposed adding two auxiliary classifiers to the intermediate layers.

The auxiliary classifiers are due to two considerations. The first one comes from the insight that many shallow networks can have strong performance on the image classification tasks. It means that the feature maps after a few rounds of convolution should already be discriminative enough. Therefore, we attach classifiers to lower layers to ensure this property. In the process, we also force the parameters of the lower layers to generate more interpretable features, preventing them from just projecting the features to whatever space that best fit the training data. This constraint on the parameters offers regularization effects. (I have to admit that the last two sentences are my own rationalization of how the regularizing effects come about. I may have committed the mistake of over-interpreation 😐).

The second consideration comes from gradient flow. Many gradients in the backward pass can be killed by the ReLU unit or diminished by small weights/intermediate features, the auxiliary classifiers can provide more direct gradients to the lower layers, enhancing their training.

The final loss for our optimizer will be main_loss + 0.3 * aux_loss_1 + 0.3 * aux_loss_2 , as the auxiliary losses areweighted by a factor of 0.3.

Code

Don’t be daunted by the table and long diagram of GoogLeNet! Its actual implementation is in fact very straightforward. Just like how we understand its structure, once we finish implementing the Inception module, the whole GoogLeNet strcture is almost as linear as our previously coded AlexNet and VGG.

Firstly, let’s implement a helper class called Conv2dBn . We use this class to retrofit batchnorm to GoogLeNet by appending a nn.BatchNorm2d layer behind every nn.Conv2d .

Next, let’s implement the core of GoogLeNet, Inception . The arguments for the __init__() constructor refers to the channels for input feature maps and number of filters for each convolution, except use_bn which specifies if we are retrofitting batchnorm. In forward() , torch.cat() is used to concatenate the output of different branches.

There are two thing that is worth pointing out. For the official implementation of GoogLeNet in torchvision, the 5x5 convolution is replaced by 3x3 convolution. Yes, they used two 3x3 branches. It is known mistake as explained here. In our implementation, we stay true to the original paper.

Secondly, notice the ceil_mode = True for the nn.MaxPool2d() ? By default ceil_mode is set to False . The difference is that usually when we compute the output dimension, the formula is O = floor((W-K+2P)/S) + 1 . If ceil_mode is set to True , we will be using ceiling instead of floor in the formula. This extra flag is set to True to match the “same” padding implementation in TensorFlow.

We need one last ingredient before assembling them into the final GoogLeNet. Let’s code the SideClassifier now. We did not go through the structure of the side classifier in the article, but its structure can be easily found at the bottom of page 6 in the paper.

Now, we can define the GoogLeNet class. With all the previous preparation, coding the main structure is as easy as copying the numbers from the table. Below is the code for __init__() . self.stem combines the few standalone layers in the beginning of the model. self.side_classifier_1/2 and self.classifier are the auxiliary and main classifiers. All the inception modules can be initialized by copying the corresponding number of channels from the table.

The forward() method is coded below. The two auxiliary classifiers are attached after inception_4a and inception_4d respectively. If the model is in training mode i.e. by default or after we called model.train() , we will return the outputs of all classifiers. Else (if we called model.eval() ), we will only return the main classifiers’ output for inference.

So far, we have finished coding for GoogLeNet. However, to simplify our training script, we can create a helper class called GoogLeNetWithLoss . It includes the forward pass through the loss function, and weights the main loss and auxiliary losses.

With this, we have finished implementing GoogLeNet. If you have some idle GPU time, feel free to plug it into our training scripts for training!

Kudos!

Conclusion

As mentioned in the introduction, the use of “Inception” is not unique to GoogLeNet. Many later works expanded on the idea of “cardinality”, the number of paths/branches in the Inception module, to build even more powerful models. Many of them are as inspiring as GoogLeNet. Hope we may take a look at them in some later articles.

Based on the three networks we have learnt so far, it seems that deeper networks are generally more powerful. However, gradient flow can be an issue for deep architectures. GoogLeNet used auxiliary classifiers to overcome this hindrance, but is there a more systematic and elegant design to solve this problem?

Yes, there is. In the next article, we will go through skip connections and one of the most popular network families in computer vision, the ResNet family.