AttributeError: 'dict' object has no attribute 'n_channels'

username: Varun_Tirupathi
cooked: <p>I am getting this error after loading the model and when the model is called. Below is my code:</p>
<pre><code class="lang-auto">def main(hparams):
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    net = Unet.load_from_checkpoint(hparams.checkpoint)
    net.freeze()
    net.to(device)

    for fn in tqdm(os.listdir(hparams.img_dir)):
        fp = os.path.join(hparams.img_dir, fn)

        img = Image.open(fp)
        mask = predict(net, img, device=device)

        mask_img = mask_to_image(mask)
        mask_img.save(os.path.join(hparams.out_dir, fn))


if __name__ == '__main__':
    parent_parser = ArgumentParser(add_help=False)
    parent_parser.add_argument('--checkpoint', required=True)
    parent_parser.add_argument('--img_dir', required=True)
    parent_parser.add_argument('--out_dir', required=True)

    parser = Unet.add_model_specific_args(parent_parser)
    hparams = parser.parse_args()

    main(hparams)
</code></pre>
<p>getting the error at the last line “main(hparams)”. Can someone help me with this, please</p>

username: ptrblck
cooked: <p>Could you post a minimal, executable code snippet so that we could have a look, please? <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: akib62
cooked: <p>Hello <a class="mention" href="/u/varun_tirupathi">@Varun_Tirupathi</a> did you solve the problem? I am also getting the same problem!</p>

username: Varun_Tirupathi
cooked: <p>hi, <a class="mention" href="/u/akib62">@akib62</a> I got this error when I am testing the model from a separate file called test.py. As I didn’t find a solution I have changed the methodology to do the testing. I don’t have a solution to this I’m sorry <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=9" title=":frowning:" class="emoji" alt=":frowning:"></p>

username: akib62
cooked: <p>Hi, <a class="mention" href="/u/ptrblck">@ptrblck</a> I am writing the UNet code using <code>PyTorch Lightning</code>.</p>
<p>I am taking help from a <a href="https://github.com/hiepph/unet-lightning" rel="noopener nofollow ugc">repo</a></p>
<p>The <code>unet</code> and <code>train.py</code> is working fine. But at the time of <code>test.py</code>, it is showing the error (question title and description) is the same for me.</p>
<p>In my understanding, when, we are running <code>test.py</code> <a href="https://github.com/hiepph/unet-lightning/blob/master/test.py" rel="noopener nofollow ugc">here</a> and any images going through the pre-trained <code>unet model</code> <a href="https://github.com/hiepph/unet-lightning/blob/master/Unet.py" rel="noopener nofollow ugc">here</a> which requires <code>n_channels</code>!</p>
<p>But from the <code>test.py</code> we are not passing any arguments like <code>n_channels</code> because it is already defined! Most probably for that reason, the error is showing.</p>
<p>Any idea to solve the issue?</p>
<p>Updated:<br>
Problem solved</p>

username: Varun_Tirupathi
cooked: <p>Hi <a class="mention" href="/u/akib62">@akib62</a> please post the code snippet also for better understanding of the issue i mean where the problem is arising<br>
Thanks</p>

username: akib62
cooked: <p><a class="mention" href="/u/varun_tirupathi">@Varun_Tirupathi</a> same as your!</p>
<p>You already gave the code snippet and already described the problem</p>

username: akib62
cooked: <p>Hi <a class="mention" href="/u/varun_tirupathi">@Varun_Tirupathi</a> I solved the problem.</p>
<p>You have to use <code>self.save_hyperparameters()</code> in your <code>Unet model</code> to store the <code>hyperparameters</code></p>
<p>I found the solution in this post <a href="https://stackoverflow.com/questions/64131993/unable-to-load-model-from-checkpoint-in-pytorch-lightning" rel="noopener nofollow ugc">here</a></p>

username: Varun_Tirupathi
cooked: <p>Hi, <a class="mention" href="/u/akib62">@akib62</a> Thanks a lot for the solution. I will implement it.</p>

username: Varun_Tirupathi
cooked: <p>Hi <a class="mention" href="/u/akib62">@akib62</a> How you are running the test.py? what arguments you are passing is the same one im getting <strong>init</strong>() missing 1 required positional argument: ‘hparams’ this error now</p>
<pre><code class="lang-auto">python test.py --checkpoint lightning_logs/version_0/checkpoints/_ckpt_epoch_1.ckpt --img_dir dataset/carvana/test --out_dir result/carvana</code></pre>

username: akib62
cooked: <p>After changing the code for saving parameters you have to train the model.<br>
Yes,  I used the same command</p>