How should I extract feature from resnet 50 Pytorch pre-trained model for Deep Learning Coloring?

We usually extract the feature just the relu layer before the pooling layer in vgg19, but which layer I should pick for extracting the feature from Resnet 50? Maybe if you can give me some suggestions on how I should do it in code and picking layer in Resnet50? I am a newbie who is still keeping Deep learning in Pytorch in 2 months.
Pytorch VGG19 allows to access with index for extracting the feature layer, but Resnet does not. I can try using for loop, but I am not sure it will work or not.

Here is the project that I want to extract the feature to redraw, but it is not working great that I just use 3 layers out of 5 relu layers in vgg19. I am planning to train it again in Resnet50 on Colab.

You can print the layers of your resnet using print(model).
The output shows you how the submodules are defined.
Since these blocks are not defined by a main nn.Sequential container, you would have to use the attribute names to index specific layers, e.g.:

model.layer4[1].conv3.register_forward_hook

This line would index the block layer4 (which is an nn.Sequential container), the second Bottleneck and the conv3 layer inside it.
Let me know, if this would work for you.

I want to extract this deep features in the fully connected:

layer=model._modules.get('avgpool')
handle = model._modules.get('avgpool').register_forward_hook

Any help on how to write out

You are trying to register a hook for the avgpool layer, not a fully connected one.
Also, you could access the layer directly via model.avgpool.

What is not working or where are you stuck?

I used Resnet 50 for a quick toy model to achieve quick result:
Because I trained from nothing, the model can only color a few objects. But I will spend a little bit more time training next month. To answer your question, I suggest you use ptrblck’s way from above.

If you have more question, I think a nice guy, Ptrblck, and others will help you too. If you need some help from me, please feel free to shoot me any questions. If you need my code and data to redo the examples from below, please email me. I don’t have Physical GPU, but I trained it on Colab

Input Data----> Machine Coloring---------> correct color from original images:



I may post the code next month after improvement, and so far my repo only has the output. In fact, there are many coloring examples on Github from other people: