0

Here I want to plot (style_score, content_score and loss) vs iteration line graph in a single graph. How do I plot using matplotlib? Here is my code:

def run_style_transfer(cnn, normalization_mean, normalization_std,
                       content_img, style_img, input_img, num_steps=300,
                       style_weight=1000000, content_weight=1):
    """Run the style transfer."""
    print('Building the style transfer model..')
    model, style_losses, content_losses = get_style_model_and_losses(cnn,
        normalization_mean, normalization_std, style_img, content_img)
    optimizer = get_input_optimizer(input_img)
    print('Optimizing..')
    run = [0]
    while run[0] <= num_steps:
        def closure():
            # correct the values of updated input image
            input_img.data.clamp_(0, 1)
            optimizer.zero_grad()
            model(input_img)
            style_score = 0
            content_score = 0
            for sl in style_losses:
                style_score += sl.loss
            for cl in content_losses:
                content_score += cl.loss
            style_score *= style_weight
            content_score *= content_weight
            loss = style_score + content_score
            loss.backward()
            run[0] += 1
            if run[0] % 50 == 0:
                print("run {}:".format(run))
                print('Style Loss : {:4f} Content Loss: {:4f}'.format(
                    style_score.item(), content_score.item()))
                print()
            return style_score + content_score
        optimizer.step(closure)

    # a last correction to have the tensors between 0 and 1
    input_img.data.clamp_(0, 1)
    return input_img

output = run_style_transfer(cnn, cnn_normalization_mean, cnn_normalization_std,
                            content_img, style_img, input_img, num_steps=800)
Building the style transfer model..
Optimizing..

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:7: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
  import sys
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:8: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
  

run [50]:
Style Loss : 146.929642 Content Loss: 26.714472

run [100]:
Style Loss : 20.721758 Content Loss: 25.548061

run [150]:
Style Loss : 5.534753 Content Loss: 22.413723

run [200]:
Style Loss : 2.631659 Content Loss: 19.817213

run [250]:
Style Loss : 1.682667 Content Loss: 17.917061

run [300]:
Style Loss : 1.305469 Content Loss: 16.689680

run [350]:
Style Loss : 1.066183 Content Loss: 15.843134

run [400]:
Style Loss : 0.952005 Content Loss: 15.269234

run [450]:
Style Loss : 0.827443 Content Loss: 14.904546

run [500]:
Style Loss : 0.791187 Content Loss: 14.578773

run [550]:
Style Loss : 0.808383 Content Loss: 14.343610

run [600]:
Style Loss : 0.771382 Content Loss: 14.127987

run [650]:
Style Loss : 0.811431 Content Loss: 13.953921

run [700]:
Style Loss : 0.768540 Content Loss: 13.791541

run [750]:
Style Loss : 0.713468 Content Loss: 13.682455

run [800]:
Style Loss : 0.724343 Content Loss: 13.602742


Expected result:

enter image description here

Asif Exe
  • 111
  • 8
  • And your question is? – Mr. T Dec 26 '21 at 12:36
  • How do I plot loss vs iteration graph using matplotlib? – Asif Exe Dec 26 '21 at 12:36
  • So, it should be an interactive scatter plot like [this one](https://stackoverflow.com/a/42738014/8881141)? – Mr. T Dec 26 '21 at 12:39
  • I have edited my problem with graph. – Asif Exe Dec 26 '21 at 12:43
  • So, a simple line graph? Have you tried to adapt the examples provided as [tutorials on the matplotlib documentation](https://matplotlib.org/stable/tutorials/introductory/pyplot.html)? What was the specific problem you encountered? – Mr. T Dec 26 '21 at 12:45
  • @AsifExe if you want a useful answer, please provide a small example of the data and the **minimal** code to reproduce what you have. We don't need all the code you provided and that is not reproducible without the context. – mozway Dec 26 '21 at 13:51

0 Answers0