How to retrain a saved model.pth with a new dataset

caroluslinus: <p>i trained a model on a dataset and saved the weight pth file.how do i load it and use the weights to train on a new dataset</p>

ptrblck: <p>You can load the parameters using <code>model.load_state_dict()</code>:</p>
<pre><code class="lang-python"># Initialize model
model = MyModel()
# Load state_dict
model.load_state_dict(torch.load('my_weights.pth'))
</code></pre>
<p>Have a look at the <a href="https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html#transfer-learning-tutorial" rel="nofollow noopener">Transfer Learning Tutorial</a> to see how you can fine-tune your model.</p>

caroluslinus: <p>Thank you for the help…after training and saving the weights to a pth file how to i predict(probabilities) on my test set form test dataloader</p>

ptrblck: <p>I assume you have already created your <code>DataLoader</code>. If so, you could write a simple loop iterating your <code>test_loader</code>:</p>
<pre><code class="lang-python">model.eval()
predictions = []
with torch.no_grad():
    for data in test_loader:
        output = model(data)
        pred = F.softmax(output, 1)  # assuming your model outputs logits
        predictions.append(pred)
predictions = torch.cat(predictions)
</code></pre>
<p><code>predictions</code> will now contain the probabilities for each sample and each class.</p>

Arun_Kumar4: <p>i need full code for this retraining model pt with new dataset and save the model with new pt file</p>

ptrblck: <p>In case you would like to retrain a classification model, the <a href="https://github.com/pytorch/examples/blob/cedca7729fef11c91e28099a0e45d7e98d03b66d/imagenet/main.py">ImageNet example</a> might be a good starter.</p>

Arun_Kumar4: <p>i solved by loaded pretrained model and loaded paramters iwth model and use train function to retrain the model it is working</p>

Arun_Kumar4: <p>i Have input image has tiff tfile uint8 format(0-255) and segmentaed image(target image0 in nrrd file uint16  (0-65535) how can i use this dataset corectly inot my dataloader for segmentaion using unet<br>
please help me for this</p>

ptrblck: <p>You could create a custom <code>Dataset</code> as described <a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html">here</a> and load the input as well as the target with any library that works for you (e.g. <code>pynrrd</code> for the nrrd file).</p>

Arun_Kumar4: <p>Bro I have nrrd file in 16 bit uint  which is not supported in pytorch</p>

Arun_Kumar4: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/8/285775b899b46f23bc932a96bf0e860780984e4a.png" data-download-href="https://discuss.pytorch.org/uploads/default/285775b899b46f23bc932a96bf0e860780984e4a" title="img"><img src="https://discuss.pytorch.org/uploads/default/original/3X/2/8/285775b899b46f23bc932a96bf0e860780984e4a.png" alt="img" data-base62-sha1="5KStNJ476JNVYAVqHr8U5tucfge" width="415" height="500" data-dominant-color="0C0C0C"><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">img</span><span class="informations">490×589 14.6 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><br>
my segmentation predict image like this first image i want to segment imAGE LIKE THIS<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/5/f5acd97cd85b9721aedc0307f32120bdd0905fd4.png" data-download-href="https://discuss.pytorch.org/uploads/default/f5acd97cd85b9721aedc0307f32120bdd0905fd4" title="img2"><img src="https://discuss.pytorch.org/uploads/default/original/3X/f/5/f5acd97cd85b9721aedc0307f32120bdd0905fd4.png" alt="img2" data-base62-sha1="z3lj8MG4HSSvQ9XvgzmsqkHd2ao" width="485" height="500" data-dominant-color="181818"><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">img2</span><span class="informations">503×518 1.49 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>

ptrblck: <aside class="quote no-group" data-username="Arun_Kumar4" data-post="10" data-topic="37749" data-full="true">
<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/arun_kumar4/48/41523_2.png" class="avatar"> Arun_Kumar4:</div>
<blockquote>
<p>Bro I have nrrd file in 16 bit uint which is not supported in pytorch</p>
</blockquote>
</aside>
<p>Why would it not be supported? <code>float32</code> can exactly represent all integers in <code>[0, 16777216]</code> which also includes <code>uint16</code>.</p>

Arun_Kumar4: <p><a href="https://www.geeksforgeeks.org/how-to-get-the-data-type-of-a-pytorch-tensor/!%5Bsuppooirteddatatype_img%7C655x458%5D(upload://jJczxBjyqty6mArLlGA3s4yvpgZ.png)" class="onebox" target="_blank" rel="noopener nofollow ugc">https://www.geeksforgeeks.org/how-to-get-the-data-type-of-a-pytorch-tensor/![suppooirteddatatype_img|655x458](upload://jJczxBjyqty6mArLlGA3s4yvpgZ.png)</a></p>

ptrblck: <p>I’m not sure where you are stuck, but why wouldn’t casting to <code>float32</code> work (which is the default dtype in PyTorch)?</p>

Arun_Kumar4: <p>how can i  correctly convert uint16 to flaot32 image where to use</p>

ptrblck: <p>Assuming your library uses <code>numpy</code> for the <code>uint16</code> images, use <code>astype</code>:</p>
<pre><code class="lang-python">arr = np.random.randint(0, 65000, (100,), dtype=np.uint16)
arr = arr.astype(np.float32)
</code></pre>
<p>then create the tensor via <code>torch.from_numpy</code>.</p>

Arun_Kumar4: <p>give me solution for this <a class="mention" href="/u/ptrblck">@ptrblck</a> bro</p>

Arun_Kumar4: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> bro give a solution for this</p>

ptrblck: <p>I’ve already posted the solution: transform the <code>uint16</code> array to <code>float32</code> and then to a tensor.</p>

Arun_Kumar4: <p>not for that bro<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/8/285775b899b46f23bc932a96bf0e860780984e4a.png" data-download-href="https://discuss.pytorch.org/uploads/default/285775b899b46f23bc932a96bf0e860780984e4a" title="img"><img src="https://discuss.pytorch.org/uploads/default/original/3X/2/8/285775b899b46f23bc932a96bf0e860780984e4a.png" alt="img" data-base62-sha1="5KStNJ476JNVYAVqHr8U5tucfge" width="415" height="500" data-dominant-color="0C0C0C"><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">img</span><span class="informations">490×589 14.6 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> predicated image is wrong<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/5/f5acd97cd85b9721aedc0307f32120bdd0905fd4.png" data-download-href="https://discuss.pytorch.org/uploads/default/f5acd97cd85b9721aedc0307f32120bdd0905fd4" title="img2"><img src="https://discuss.pytorch.org/uploads/default/original/3X/f/5/f5acd97cd85b9721aedc0307f32120bdd0905fd4.png" alt="img2" data-base62-sha1="z3lj8MG4HSSvQ9XvgzmsqkHd2ao" width="485" height="500" data-dominant-color="181818"><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">img2</span><span class="informations">503×518 1.49 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><br>
correct image will be like this <a class="mention" href="/u/ptrblck">@ptrblck</a></p>