U-net with resnet34 backbone

Hello everyone!
I’m a complete newbie here, I’ve tried to search related info, but haven’t found any results so far.


According to this, I don’t really get what’s dls and arch, more what is a backbone for e.g;

learn = unet_learner(dls, models.resnet34, loss_func=CrossEntropyLossFlat(axis=1), y_range=(0,1))
Is it possible to visualize learn.summary() into an architecture?

Does anyone have a visualization of such architecture? Is it an original u-net architecture + resnet34 backbone, how does it look like?
Hope you guys could help me to understand.

I had to go deeper, so in case that if someone is interested in how to visualize neural networks from learn.summary() here it goes.
*Firstly I have exported the .pth format and opened it with Netron, which was not really what I was looking for.The best option is to import the .onnx format to Netron software to see the architecture.
I have found an article which describes the process of converting to onnx.
https://medium.com/@hungminhnguyen/convert-fast-ai-trained-image-classification-model-to-ios-app-via-onnx-and-apple-core-ml-5fdb612379f1
I have added verbose and export params to it.

torch.onnx.export(
learn.model,
torch.randn(1, 3, 224, 224).cuda(),
“your_model_name.onnx”,
verbose=True,
export_params=True,
input_names=[“image input”],
output_names=[“image output”]
)

But I’m still confused about the backbone in the Dynamic u-net, can someone refer me to it? I have read the official documentations and did some investigations into terms, but i still didnt get it. Thanks

The backbone of the U-Net is a ResNet34. If you type learn.model it will print the different layers for you.
In PyTorch the pretrained ResNets are all structured very similar:

Stem (ConvLayer + BatchNorm + ReLU)
                     |
                     V
Block 1 (Multiple ConvLayers + BatchNorm + ReLU)
                     |
                     V
Block 2 (Multiple ConvLayers + BatchNorm + ReLU)
                     |
                     V
Block 3 (Multiple ConvLayers + BatchNorm + ReLU)
                     |
                     V
Block 4 (Multiple ConvLayers + BatchNorm + ReLU)
                     |
                     V
Head (Flatten input, Linear Layers)

if you type model[0] you’ll get the stem, model[1] will give you the first block, model[-1] will give you the last layer (the head).
If you pass resnet34 to unet_learner the following will happen:

  1. the resnet34 is constructed and, if pretrained=True, the weights are loaded as well.
  2. the create_body function of fastai cuts off the head of the model, leaving only stem and encoder.
  3. hooks are placed into the model after each major block (so after the stem, block1, block2, …) which will catch the output of each layer, given the size of the feature maps did change in between layers, to use it in the skip connections.
  4. A BatchNorm, ReLU and double convolutional Layer is appended to the encoder.
  5. Multiple U-Net Blocks are added to the model.

The final model then looks something like this:

From resnet18                  
                                        OutLayer
 -------------                              ^
|             |                             |
|   Stem  - - | - - - hooked output - >  UNetBlock 4
|     |       |                             ^
|     V       |                             |
|   Block 1 - | - - -  hooked output - > UNetBlock 3
|     |       |                             ^
|     V       |                             |
|   Block 2 - | - - -  hooked output - > UNetBlock 2
|     |       |                             ^
|     V       |                             |
|   Block 3 - | - - -  hooked output - > UNetBlock 1
|     |       |                             ^
|     V       |                             |
|   Block 4 - | - - -  > DoubleConv  - - - -
|             |
 -------------
2 Likes

Big Ups! Awesome explanation!

Hello! I have made an initial architecture draft according to the model.summary() output. Could you please point me on my mistakes where should i fix it? I’m not really good at math i’ve literally copied this learn.summary() outputs as blocks, but im still confused at concatenations. Any ideas how to make it more simple and solid?
This is my model.summary() output:

> DynamicUnet (Input shape: 16)
===========================================================================
Layer (type)         Output Shape         Param #    Trainable 
===========================================================================
                     16 x 64 x 64 x 64   
Conv2d                                    9408       True      
BatchNorm2d                               128        True      
ReLU                                                           
MaxPool2d                                                      
Conv2d                                    36864      True      
BatchNorm2d                               128        True      
ReLU                                                           
Conv2d                                    36864      True      
BatchNorm2d                               128        True      
Conv2d                                    36864      True      
BatchNorm2d                               128        True      
ReLU                                                           
Conv2d                                    36864      True      
BatchNorm2d                               128        True      
Conv2d                                    36864      True      
BatchNorm2d                               128        True      
ReLU                                                           
Conv2d                                    36864      True      
BatchNorm2d                               128        True      
____________________________________________________________________________
                     16 x 128 x 16 x 16  
Conv2d                                    73728      True      
BatchNorm2d                               256        True      
ReLU                                                           
Conv2d                                    147456     True      
BatchNorm2d                               256        True      
Conv2d                                    8192       True      
BatchNorm2d                               256        True      
Conv2d                                    147456     True      
BatchNorm2d                               256        True      
ReLU                                                           
Conv2d                                    147456     True      
BatchNorm2d                               256        True      
Conv2d                                    147456     True      
BatchNorm2d                               256        True      
ReLU                                                           
Conv2d                                    147456     True      
BatchNorm2d                               256        True      
Conv2d                                    147456     True      
BatchNorm2d                               256        True      
ReLU                                                           
Conv2d                                    147456     True      
BatchNorm2d                               256        True      
____________________________________________________________________________
                     16 x 256 x 8 x 8    
Conv2d                                    294912     True      
BatchNorm2d                               512        True      
ReLU                                                           
Conv2d                                    589824     True      
BatchNorm2d                               512        True      
Conv2d                                    32768      True      
BatchNorm2d                               512        True      
Conv2d                                    589824     True      
BatchNorm2d                               512        True      
ReLU                                                           
Conv2d                                    589824     True      
BatchNorm2d                               512        True      
Conv2d                                    589824     True      
BatchNorm2d                               512        True      
ReLU                                                           
Conv2d                                    589824     True      
BatchNorm2d                               512        True      
Conv2d                                    589824     True      
BatchNorm2d                               512        True      
ReLU                                                           
Conv2d                                    589824     True      
BatchNorm2d                               512        True      
Conv2d                                    589824     True      
BatchNorm2d                               512        True      
ReLU                                                           
Conv2d                                    589824     True      
BatchNorm2d                               512        True      
Conv2d                                    589824     True      
BatchNorm2d                               512        True      
ReLU                                                           
Conv2d                                    589824     True      
BatchNorm2d                               512        True      
____________________________________________________________________________
                     16 x 512 x 4 x 4    
Conv2d                                    1179648    True      
BatchNorm2d                               1024       True      
ReLU                                                           
Conv2d                                    2359296    True      
BatchNorm2d                               1024       True      
Conv2d                                    131072     True      
BatchNorm2d                               1024       True      
Conv2d                                    2359296    True      
BatchNorm2d                               1024       True      
ReLU                                                           
Conv2d                                    2359296    True      
BatchNorm2d                               1024       True      
Conv2d                                    2359296    True      
BatchNorm2d                               1024       True      
ReLU                                                           
Conv2d                                    2359296    True      
BatchNorm2d                               1024       True      
BatchNorm2d                               1024       True      
ReLU                                                           
____________________________________________________________________________
                     16 x 1024 x 4 x 4   
Conv2d                                    4719616    True      
ReLU                                                           
____________________________________________________________________________
                     16 x 512 x 4 x 4    
Conv2d                                    4719104    True      
ReLU                                                           
____________________________________________________________________________
                     16 x 1024 x 4 x 4   
Conv2d                                    525312     True      
ReLU                                                           
PixelShuffle                                                   
BatchNorm2d                               512        True      
Conv2d                                    2359808    True      
ReLU                                                           
Conv2d                                    2359808    True      
ReLU                                                           
ReLU                                                           
____________________________________________________________________________
                     16 x 1024 x 8 x 8   
Conv2d                                    525312     True      
ReLU                                                           
PixelShuffle                                                   
BatchNorm2d                               256        True      
Conv2d                                    1327488    True      
ReLU                                                           
Conv2d                                    1327488    True      
ReLU                                                           
ReLU                                                           
____________________________________________________________________________
                     16 x 768 x 16 x 16  
Conv2d                                    295680     True      
ReLU                                                           
PixelShuffle                                                   
BatchNorm2d                               128        True      
Conv2d                                    590080     True      
ReLU                                                           
Conv2d                                    590080     True      
ReLU                                                           
ReLU                                                           
____________________________________________________________________________
                     16 x 512 x 32 x 32  
Conv2d                                    131584     True      
ReLU                                                           
PixelShuffle                                                   
BatchNorm2d                               128        True      
____________________________________________________________________________
                     16 x 96 x 64 x 64   
Conv2d                                    165984     True      
ReLU                                                           
Conv2d                                    83040      True      
ReLU                                                           
ReLU                                                           
____________________________________________________________________________
                     16 x 384 x 64 x 64  
Conv2d                                    37248      True      
ReLU                                                           
PixelShuffle                                                   
ResizeToOrig                                                   
MergeLayer                                                     
Conv2d                                    88308      True      
ReLU                                                           
Conv2d                                    88308      True      
Sequential                                                     
ReLU                                                           
____________________________________________________________________________
                     16 x 3 x 128 x 128  
Conv2d                                    300        True      
____________________________________________________________________________

Total params: 41,221,268
Total trainable params: 41,221,268
Total non-trainable params: 0

The diagram I made

Does this make any sense? :smiley::smiley::smiley:

1 Like