Pytorch (iii)--Test your pictures with a trained model

Source: Internet
Author: User
Tags pytorch

Directory Connections
(1) Data processing
(2) Build and customize the network
(3) Test your pictures with a well-trained model
(4) Processing of video data
(5) Pytorch source code modification to increase the CONVLSTM layer
(6) Understanding of gradient reverse transfer (backpropogate)
(total) Pytorch encounters fascinating bug Pytorch learning and use (iii)

In the previous article, we implemented how to add a custom loss to Siamese network for example. Now it's time to test your handwritten digital pictures using the trained network. You first need to save the weights at the time of training and then load them directly when you test.

Torch.save (NET, path)
torch.load (path)

Can. and then handle your own handwritten pictures.

Put the picture you want to test into a folder, then use the image data to load, the image data is normalized and resized to (B,C,H,W).

Transform = Transforms.compose ([Transforms. Totensor (), transforms. Normalize ((1.1618,), (1.1180,))]

def get_files (directory):
    return [Os.path.join (directory, F) for F in sorted (List (Os.listdir (directory)))
            If Os.path.isfile (os.path.join (directory, f))]
images = Np.array ([])
file = Get_files ('./data/figure ')
For I, item in enumerate (file):
    print (' Processing%i of%i (%s) '% (i+1, len (file), item))
    image = Transform (image . Open (item). CONVERT (' L '))
    images = np.append (images, Image.numpy ())

img = Images.reshape ( -1, 1, approx.)
img = Torch.from_numpy (img). Float ()
label = Torch.ones (5,1). Long ()

Its loaded data is visualized as:
finally load the model and test it.

torch.load ('./savet.pt ') def test (data, label): Net.eval () data, label = Variable (data,  Volatile=true), Variable (label) output = Net (data) out = Output.view ( -1, 4) Test_loss = criterion (out[:, 0:2], out[:, 2:4], label). data[0] pred = classify (out.data[:, 0:2], out.data[:, 2:4]) correct = Pred.eq (label.data). Sum ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.