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 ()