LeNet is trained with MNIST’s training set, and the code is not shown here.
directly loads the saved model
lenet = torch.load('resourses/trained_model/LeNet_trained.pkl')
Attached to the test code
print("Testing")
# Define conversion operations
# Read in the test image and transfer it to the model.
test_images = Image.open('resourses/LeNet_test/0.png')
img_to_tensor = transforms.Compose([
transforms.Resize(32),
transforms.Grayscale(num_output_channels=1),
transforms.ToTensor(),
transforms.Normalize([0.5], [0.5])])
input_images = img_to_tensor(test_images).unsqueeze(0)
# Move models and data to cuda for computation if cuda is available
USE_CUDA = torch.cuda.is_available()
if USE_CUDA:
input_images = input_images.cuda()
lenet = lenet.cuda()
output_data = lenet(input_images)
# Print test information
test_labels = torch.max(output_data, 1)[1].data.cpu().numpy().squeeze(0)
print(output_data)
print(test_labels)
At present, there is no correct rate according to my own picture, and I can’t find any reason. At present, the frequency of output 8 is very high.
later looked up relevant information, for the following reasons: </mark b>
-
- parsed MNIST data set, you will find that the pictures in the data set are white words on a black background, such as:
-
- , but our custom test pictures are generally black words on a white background, such as:
-
- , so I took the custom test pictures by pixel and then re-tested
- pixel reverse code is as follows:
from PIL import Image, ImageOps
image = Image.open('resourses/LeNet_test/0.png')
image_invert = ImageOps.invert(image)
image_invert.show()
After pixel reversal, the accuracy rate of the test reaches 50-60 percent, but the accuracy rate is still not ideal. Please refer to the following reasons
- MNIST data set contains the handwriting of foreigners. The handwriting style and habits of foreigners are slightly different from those of Chinese people, which is also a major factor affecting the accuracy of the test. But the owner of the building has not tested the correct rate of the image test after modifying the font.
Read More:
- [Solved] RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the
- Mxnet.gluon Load Pre Training
- [Solved] torchsummary Error: RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.F
- Python RuntimeError: Expected 4-dimensional input for 4-dimensional weight [32, 1, 5, 5]
- Error:output with shape [1, 224, 224] doesn‘t match the broadcast shape [3, 224, 224]
- Normalize error: TypeError: Input tensor should be a float tensor…
- Pytorch CUDA Error: UserWarning: CUDA initialization: CUDA unknown error…
- [ONNXRuntimeError] : 10 : INVALID_Graph loading model error
- RuntimeWarning: overflow encountered in ubyte_Scalars pixel addition and subtraction overflow exception
- RuntimeError: CUDA error: an illegal memory access was encountered
- [Solved] Pytorch Download CIFAR1 Datas Error: urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certi
- Python: RNN principle realized by numpy
- [Solved] Pytorch-transformers Error: AttributeError: ‘str‘ object has no attribute ‘shape‘
- Pytorch: How to Handle error warning conda.gateways.disk.delete:unlink_or_rename_to_trash(140)
- [Solved] Python Keras Error: AttributeError: ‘Sequential‘ object has no attribute ‘predict_classes‘
- [Solved] Python Error: An attempt has been made to start a new process before the current process has finished …
- Tesseract OCR text recognition using tess4j encapsulation
- [Solved] RuntimeError: Numpy is not available (Associated Torch or Tensorflow)
- Pytorch directly creates a tensor on the GPU error [How to Solve]
- To solve the problem that the loss of verification set of resnet50 pre-training model remains unchanged