How to see prediction images at testing?

username: Lane
cooked: <p>At this link at the end, there are some image and it’s prediction. How can you got this?<br>
<a href="https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html#torchvision-object-detection-finetuning-tutorial" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html#torchvision-object-detection-finetuning-tutorial</a></p>
<p>And as I can see, you don’t have any model class defined.<br>
I can’t load my saved models with</p>
<pre><code class="lang-auto">model = torch.load(PATH)
or 
model.load_state_dict(torch.load(PATH))
</code></pre>
<p>because it require some model class, but I followed this tutorial and I don’t see any class.</p>
<p><span class="mention">@ptrblck_de</span> I saw you are answering a lot and I’m apologize that I mention you, but I really need help, ty.</p>

username: ptrblck
cooked: <p>The tutorial mentions the full source code at the end which you can download from <a href="https://pytorch.org/tutorials/_static/tv-training-code.py">here</a> and which should also include the code to visualize the predictions.</p>

username: Lane
cooked: <p>Yes, I downloaded that and work in this template, just put my own dataset and modificate <code>train_one_epoch(</code>) function.</p>
<p>There is no code for visualize the predictions.</p>

username: ptrblck
cooked: <p>If you cannot find the corresponding code snippet check the linked Colab notebook which shows the visualization.</p>

username: Lane
cooked: <p>I tired follow this also <a href="https://colab.research.google.com/github/pytorch/tutorials/blob/gh-pages/_downloads/torchvision_finetuning_instance_segmentation.ipynb#scrollTo=ovjC81FZfWat" class="inline-onebox" rel="noopener nofollow ugc">Google Colab</a>  and still nothing happens. I don’t know what I’m doing wrong?</p>
<p>Here is my main function: (the <code>get_model_instance_segmentation</code> and <code>get_transform</code> functions I wrote above <code>main</code> in the script, and also defined class Moj_Dataset_ArT at the beggining in my script)</p>
<pre><code class="lang-auto">def main():
    # train on the GPU or on the CPU, if a GPU is not available
    device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')

    # our dataset has two classes only - background and person
    num_classes = 2
    # use our dataset and defined transformations
    dataset = Moj_Dataset_ArT('Train/ArT', get_transform(train=True))
    dataset_val = Moj_Dataset_ArT('Train/ArT', get_transform(train=False))
    dataset_test = Moj_Dataset_ArT('Train/ArT', get_transform(train=False))

    # split the dataset in train and test set
    indices = torch.randperm(len(dataset)).tolist()
    dataset = torch.utils.data.Subset(dataset, indices[:-1159])
    #print(len(dataset)) -&gt; 2400
    dataset_val = torch.utils.data.Subset(dataset_val, indices[2400:-559]) 
    #print(len(dataset_val)) -&gt; 600
    dataset_test = torch.utils.data.Subset(dataset_test, indices[-559:])
    #print(len(dataset_test)) -&gt; 559

    # define training and validation data loaders
    data_loader = torch.utils.data.DataLoader(
        dataset, batch_size=4, shuffle=True, num_workers=4,
        collate_fn=utils.collate_fn)

    data_loader_val = torch.utils.data.DataLoader(
        dataset, batch_size=4, shuffle=False, num_workers=4,
        collate_fn=utils.collate_fn)

    data_loader_test = torch.utils.data.DataLoader(
        dataset_test, batch_size=1, shuffle=False, num_workers=4,
        collate_fn=utils.collate_fn)

    # get the model using our helper function
    model = get_model_instance_segmentation(num_classes)

    # move model to the right device
    model.to(device)

    # construct an optimizer
    params = [p for p in model.parameters() if p.requires_grad]
    optimizer = torch.optim.SGD(params, lr=0.001,
                                momentum=0.9, weight_decay=0.0005)
    # and a learning rate scheduler
    lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
                                                   step_size=30,
                                                   gamma=0.1) # gamma bila na 0.5 

    # let's train it for 10 epochs
    num_epochs = 2
    
    #PATH = '/home/Nezz/Train/ArT/TorchScript_format/'
    #model_scripted = torch.jit.script(model) # Export to TorchScript
    ml =[] 
    vl = []
    for epoch in range(num_epochs):
        # train for one epoch, printing every 10 iterations
        #loss_value =  train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10)
        train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10)
        #ml.append(loss_value)
        # update the learning rate
        lr_scheduler.step()
        # evaluate on the test dataset
        evaluate(model, data_loader_test, device=device)
        #val_loss  = evaluate_loss(model, data_loader_val, device=device) 
        #vl.append(val_loss)
        #torch.save(model, os.path.join(PATH, 'epoch-{}.pt'.format(epoch)))
        
    #print("train_loss", ml) 
    #print("val_loss", vl)
    #draw_loss(ml,vl)

    # pick one image from the test set
    img, _ = dataset[20]
    # put the model in evaluation mode
    model.eval()
    with torch.no_grad():
        prediction = model([img.to(device)])
    Image.fromarray(img.mul(255).permute(1, 2, 0).byte().numpy())
    Image.fromarray(prediction[0]['masks'][0, 0].mul(255).byte().cpu().numpy())

    #print("That's it!")
    
if __name__ == "__main__":
    main()
</code></pre>

username: ptrblck
cooked: <p><code>Image.fromarray</code> creates a <code>PIL.Image</code> which might be visualized by default in their notebook (or if executed in a standalone cell). Call <code>.show()</code> on the images and it should work.</p>

username: Lane
cooked: <p>Yeaaah, it wokrs. Thank you a lot man <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>