How to load MoCo model weights that are stored as an state_dict?

Nikolaos_Sintoris: <p>Hello,</p>
<p>I am new to PyTorch and I am trying to load the MoCo model in order to use it.</p>
<p>In the following site: <a>https://github.com/facebookresearch/moco</a> I have found the code and also, I downloaded the pre-trained model (‘moco_v2_800ep_pretrain.pth.tar’) which is a state_dict with the model’s wheights.</p>
<p>I know that in order to use a model I need to create a model instance first and then load the state_dict.</p>
<p>My problem is that I can not create an instance model of MoCo. I saw the code on GiHub and I end up with the following code:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/0/b0c1db9e36f0184fc0c193176f2f701b3876c055.png" data-download-href="https://discuss.pytorch.org/uploads/default/b0c1db9e36f0184fc0c193176f2f701b3876c055" title="moco"><img src="https://discuss.pytorch.org/uploads/default/original/3X/b/0/b0c1db9e36f0184fc0c193176f2f701b3876c055.png" alt="moco" data-base62-sha1="pdFn9l7c3QgB9C2sU5yc3o5uDn7" width="690" height="102" data-dominant-color="EEEEED"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">moco</span><span class="informations">796×118 5.2 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I do not know what to put in the base_encoder parameter or tell me if there is another way to load the model.</p>
<p>Can anyone help me please?</p>

conrad: <p>The weights are not for the entire MoCo model (i.e. with encoder, momentum encoder, and queue) but just for ResNet50. Have a look at how they load the weights for ImageNet classification: <a href="https://github.com/facebookresearch/moco/blob/78b69cafae80bc74cd1a89ac3fb365dc20d157d3/main_lincls.py#L156-L177" class="inline-onebox" rel="noopener nofollow ugc">moco/main_lincls.py at 78b69cafae80bc74cd1a89ac3fb365dc20d157d3 · facebookresearch/moco · GitHub</a></p>
<p>In this case model = torchvision.models.resnet50()</p>

Nikolaos_Sintoris: <p>Thanks for your reply Conrad.</p>
<p>If I understand it right, first I need to load the Resnet50 model and then load the pre-trained weights to this model.</p>
<p>So, according to your answer and with the code that you mentioned, with the following code I have the Resnet50 model with the MoCo pre-trained weights:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/b/1bb2942d9bc1988156742f43859bd0717b0a2a8a.png" data-download-href="https://discuss.pytorch.org/uploads/default/1bb2942d9bc1988156742f43859bd0717b0a2a8a" title="l"><img src="https://discuss.pytorch.org/uploads/default/original/3X/1/b/1bb2942d9bc1988156742f43859bd0717b0a2a8a.png" alt="l" data-base62-sha1="3X1uH0ZQC1FdquAPDibCsHyxaI2" width="690" height="243" data-dominant-color="EFEFEF"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">l</span><span class="informations">813×287 12.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

conrad: <p>Yep, that looks right.</p>

Nikolaos_Sintoris: <p>Hello, I have one more question.</p>
<p>Based on the above and after I check again on the paper, I realised that MoCo model has an output vector of 128D, so I am trying to load this specific model right now, because the model on the previous posts has an output vector of 1000D.</p>
<p>According to the folloeing lines of code from the github page: <a>https://github.com/facebookresearch/moco/blob/78b69cafae80bc74cd1a89ac3fb365dc20d157d3/main_moco.py#L159-L215</a> my code is the following:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/d/bd338e4a7d58c238adc948557abac368fb36193e.png" data-download-href="https://discuss.pytorch.org/uploads/default/bd338e4a7d58c238adc948557abac368fb36193e" title="1"><img src="https://discuss.pytorch.org/uploads/default/original/3X/b/d/bd338e4a7d58c238adc948557abac368fb36193e.png" alt="1" data-base62-sha1="qZKHnS4zhJuajdNKEAcT0gPiVGe" width="690" height="273" data-dominant-color="F3F3F3"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">1</span><span class="informations">1003×398 26.4 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div> <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/d/ed0d5a9350e86858c2270fb45d48b47e448036c7.png" data-download-href="https://discuss.pytorch.org/uploads/default/ed0d5a9350e86858c2270fb45d48b47e448036c7" title="2"><img src="https://discuss.pytorch.org/uploads/default/original/3X/e/d/ed0d5a9350e86858c2270fb45d48b47e448036c7.png" alt="2" data-base62-sha1="xP3LNlOqZp6aw8lY1cDfJa9esbd" width="690" height="383" data-dominant-color="EEEEEE"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">2</span><span class="informations">1214×675 64.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>It seems that I have a false checkpoint, I am not sure, I can not understand what to load in my MoCo model.</p>
<p>Does anyone have an idea?</p>
<p>Thanks a lot.</p>

conrad: <p>They only released the weights for the ResNet50 layers not the entire MoCo model. You’d have to contact the authors if you want those parameters. What’s your goal? Are you trying to fine-tune the pre-trained weights?</p>

Nikolaos_Sintoris: <p>Yes exactly. I want to pass my own images to  MoCo model, take the output for every image, which is a vector 128D and then, train my own MLP.</p>

conrad: <p>Create the MoCo model and load the weights using:</p>
<pre><code class="lang-python">for k in list(state_dict.keys()):
    # retain only encoder_q up to before the embedding layer
    if k.startswith('module.encoder_q') and not k.startswith('module.encoder_q.fc'):
        # remove prefix
        k_no_prefix = k[len("module."):]
        state_dict[k_no_prefix] = state_dict[k]   # leave encoder_q in the param name
        # copy state from the query encoder into a new parameter for the key encoder
        state_dict[k_no_prefix.replace('encoder_q', 'encoder_k')] = state_dict[k]
    del state_dict[k]

msg = moco_model.load_state_dict(state_dict, strict=False)
</code></pre>
<p>This should give you the MoCo model with initialized query and key encoders (their parameters are copies of each other to begin with). The MLP and queue will still be randomly initialized; you’ll have to train them on your dataset.</p>