How to reacreate a single image with trained model?

ljeonjko: <p>Hello!</p>
<p>I have trained a CCGAN model and saved the generator and discriminator using</p>
<pre><code class="lang-auto">torch.save(gen.state_dict(), "GENERATOR/gen.pt")
torch.save(disc.state_dict(), "DISCRIMINATOR/disc.pt")
</code></pre>
<p>I now wish to test this model on a single image. (I have trained several models using several slightly different custom datasets, and I wish to see which dataset is the most fitting). How do I go about presenting the algorithm a single image in order to test the output? Is there a tutorial on this? Any tips are welcome.</p>
<p>Tank you!</p>

Matias_Vasquez: <p>From what I understand you want to pass a single image through a model.</p>
<p>Models usually take inputs for the <code>forward</code> method in the form of <code>BxCxHxW</code> (Batch x Channel x Height x Width).</p>
<p>So if you have a single image with the form <code>CxHxW</code> you need to add the <code>B</code> dimension through <code>unsqueeze(0)</code>.</p>
<pre><code class="lang-python">output = model(img.unsqueeze(0))
</code></pre>
<p>Hope this helps <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

ljeonjko: <p>I guess I’m supposed to run the image just through the generator part, but I’m unsure how to properly load the generator model and the saved states.</p>
<p>I’ll give it a try! Thanks for your help <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=12" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p>

Matias_Vasquez: <p>To save and load you can just use this.</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/c/1/c144ba8a33ae52dde84c55fedecba9fecf8d3949.png" alt="image" data-base62-sha1="rzJoWqlVzRoU6t1siaXEnUl6ipj" width="270" height="200"><br>
Taken from <a href="https://pytorch.org/tutorials/beginner/saving_loading_models.html#save-load-entire-model" rel="noopener nofollow ugc">here</a></p>

ljeonjko: <p>Hmm, using</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn
import torch.optim as optim
import tifffile as tiff
from generator import Generator

model = Generator(features_g = 64, num_channels = 3)

model.load_state_dict(torch.load('gen.pt'))

img = tiff.imread('test.tiff')
output = model(img.unsqueeze(0))
</code></pre>
<p>I get</p>
<pre><code class="lang-auto">RuntimeError: Error(s) in loading state_dict for Generator:
	Unexpected key(s) in state_dict: "down1.model.0.weight", 
"down2.model.0.weight", "down2.model.1.weight", "down2.model.1.bias", "down2.model.1.running_mean", "down2.model.1.running_var",...
</code></pre>
<p>Any idea?</p>

Matias_Vasquez: <p>Can you print the state dict that your model actually has and the one that is being loaded to see what is expected?</p>
<pre><code class="lang-python">for param in torch.load('gen.pt'):
    print(param)
</code></pre>
<pre><code class="lang-python">for param in model.state_dict():
    print(param)
</code></pre>
<p>Also, you should have the image inside a tensor before passing it to the model.</p>

ljeonjko: <p>Will keep in mind about putting the image in tensor.</p>
<pre><code class="lang-auto">for param in torch.load('gen.pt'):
    print(param)
</code></pre>
<p>prints out</p>
<pre><code class="lang-auto">down1.model.0.weight
down2.model.0.weight
down2.model.1.weight
down2.model.1.bias
down2.model.1.running_mean
down2.model.1.running_var
down2.model.1.num_batches_tracked
down3.model.0.weight
down3.model.1.weight
down3.model.1.bias
down3.model.1.running_mean
down3.model.1.running_var
down3.model.1.num_batches_tracked
down4.model.0.weight
down4.model.1.weight
down4.model.1.bias
down4.model.1.running_mean
down4.model.1.running_var
down4.model.1.num_batches_tracked
down5.model.0.weight
down5.model.1.weight
down5.model.1.bias
...
</code></pre>
<p>same keys as in the error.</p>

Matias_Vasquez: <p>Could you also post the output for the model.state_dict?</p>

ljeonjko: <p>Huh, there’s nothing in the print. I don’t understand…</p>

ljeonjko: <p>The reason it was empty is because I didn’t include in the model <code>self.build()</code> in <code>__init__</code>.</p>

Matias_Vasquez: <p>Do the parameters now match, or are they still different?</p>

ljeonjko: <p>Everything seems in order now. Hopefully the generator works properly. Thanks <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=12" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p>