Audio Classification: How to change input weights when working with a pretrained model?

shaneR: <p>In an Audio Classification problem, I am firstly loading a pretrained model, then running my own data through the model.<br>
In audio problems, I am searching for optimum parameters (hop length, window size, etc) for transforming features into Mel Spectrograms.  However, this changes the size of the original inputs.  Thus, there is an error when attemping to load_state_dict of the model.</p>
<pre><code class="lang-auto">RuntimeError: Error(s) in loading state_dict for Cnn14Extractor:
        size mismatch for spectrogram_extractor.stft.conv_real.weight: copying a param with shape torch.Size([513, 1, 1024]) from checkpoint, the shape in current model is torch.Size([552, 1, 1102]).
        size mismatch for spectrogram_extractor.stft.conv_imag.weight: copying a param with shape torch.Size([513, 1, 1024]) from checkpoint, the shape in current model is torch.Size([552, 1, 1102]).
        size mismatch for logmel_extractor.melW: copying a param with shape torch.Size([513, 64]) from checkpoint, the shape in current model is torch.Size([552, 64]).
</code></pre>
<p>All other discussions I have found talk about Image problems (size doesn’t change each time you change a parameter).</p>
<p>My main question is:<br>
How can I change these 3 layers weights?</p>
<p>Can I make the model accept the new weights shape? What are my options besides:</p>
<ol>
<li>Training from scratch (led to poorer results)</li>
<li>Resizing feature to fit the expected shape (loss of data?)</li>
</ol>

ptrblck: <aside class="quote no-group" data-username="shaneR" data-post="1" data-topic="103211">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/e9c0ed/48.png" class="avatar"> shaneR:</div>
<blockquote>
<p>However, this changes the size of the original inputs. Thus, there is an error when attemping to load_state_dict of the model.</p>
</blockquote>
</aside>
<p>Changing the input shape shouldn’t change the parameter shapes and the inputs would also not be stored in the <code>model.state_dict()</code>. It seems you’ve manipulated the model parameters instead.<br>
You would have to apply these manipulations to the original model before loading the <code>state_dict</code>.</p>

shaneR: <p>Is it possible to expand on this a bit?  It seems the problem is:</p>
<ol>
<li>I loaded a pretrained model &amp; weights with set parameters</li>
<li>I then created a new model, and attempted to load weights into it, but the parameters don’t match</li>
<li>So the answer would seem to be to change parameters of the <code>state_dict()</code>?</li>
</ol>
<p>Per the following code example, I attempted to overwrite the first layers of the <code>state_dict()</code> from scratch.  However, the problem persisted.</p>
<pre><code class="lang-auto">backbone = FeatureExtractor(sample_rate, window_size, hop_size, mel_bins)

if pretrained:
        state_dict = load_state_dict_from_url(model_urls['cnn_url'])

conv1 = nn.Conv1d(1, 552, kernel_size=(1102,) ~)
weight = torch.rand(552,1,1102)
state_dict["model"]["spectrogram_extractor.stft.conv_real.weight"] = weight
</code></pre>

ptrblck: <p>The approach should work and you can manipulate the parameters in the <code>state_dict</code> so that they match the new architecture. Here is a small example:</p>
<pre><code class="lang-python">modelA = models.resnet18()
modelB = models.resnet18()

# Change modelB
modelB.fc = nn.Linear(512, 10)

# Try to load the pretrained state_dict
state_dict = modelA.state_dict()
modelB.load_state_dict(state_dict) # error

# Manipulate state_dict to match parameter shape
state_dict['fc.weight'] = state_dict['fc.weight'][:10]
state_dict['fc.bias'] = state_dict['fc.bias'][:10]

# Load again
modelB.load_state_dict(state_dict) # works
</code></pre>