What is the best way to do changes to a torchvision model

Deeply: <p>I would like to try some new regularization methods and thought of using some <code>torchvision</code> models.</p>
<p>I only want to work on one model; say for example <code>googlenet</code>, what is the best way to do this:<br>
1- Download <code>torchvision</code> from <a href="https://github.com/pytorch/vision/tree/master/torchvision" rel="noopener nofollow ugc">source</a>, or<br>
2 - Download the whole vision from <a href="https://github.com/pytorch/vision" rel="noopener nofollow ugc">source</a>, or<br>
3- Only <a href="https://github.com/pytorch/vision/blob/master/torchvision/models/googlenet.py" rel="noopener nofollow ugc">download googlenet</a>, which I noticed won’t work as it has some dependencies (have already tried this option).</p>
<p>Before spending time on this, I appreciate any thoughts on the best way to do this?</p>

ptrblck: <p>Downloading the source and building it after your manipulation might work, but the easier way would probably be to create a custom <code>nn.Module</code> and use the <code>torchvision</code> model as the base class.<br>
Inside your custom model you could then redefine the internal parameters and methods.</p>

Deeply: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="113849">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>model as the base class.<br>
Inside your custom model you could then redefine the internal parameters and methods</p>
</blockquote>
</aside>
<p>not sure if this would work. For example, I would like to remove <code>droupout</code> from <code>googlenet</code> and use another regularization method.</p>

ptrblck: <p>The dropout is used as a module, you could replace it with <code>nn.Identity</code>. Alternatively, if it’s used via the functional API, you could override the <code>forward</code> method and reimplement it.<br>
I still think this might be the faster and cleaner approach than to manipulate the original model and rebuild <code>torchvision</code>, but you might prefer it.</p>

Deeply: <p>Thank you! I agree that overriding the forward function would be the best way to do it. To do this, do you mean to build a new <code>googlenet_like class</code> inherited from from <code>googlenet class</code>, then, to override the forward function?</p>
<p>NB. Currently, <code>googlenet</code> in tochvision/googlenet.py is working out-of-the-box without any extra dependencies, except the need to complement it with utils.py  (<code>from utils import load_state_dict_from_url</code>); although this has little impact for my case as the pretrained model has dropout in its structure. Got acc1 of 20% after 9 epochs on CIFAR-100, trained from scratch.</p>
<p>Sounds I had the problem with the logits thing, and have resolved it already.</p>

ptrblck: <p>Yes, that what I was thinking. However, if simply copy-pasting the model code (without the unused <code>utils</code>) works fine, it could be easier to just to this. In any way, I would try to avoid changing the code directly in <code>torchvision</code> and rebuilding it, as these changes would live in you branch of <code>torchvision</code> and (if I’m understanding your use case correctly) you don’t want to commit these changes, as they are a standalone experiment.</p>

Deeply: <aside class="quote no-group" data-username="ptrblck" data-post="6" data-topic="113849">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>I would try to avoid changing the code directly in <code>torchvision</code> and rebuilding it</p>
</blockquote>
</aside>
<p>Absolutely, it is a separate project.</p>