Solve the problems encountered by pytorch saving model

  • 2021-09-16 07:19:34
  • OfStack

Today, I encountered bug when saving the model with pytorch

Can't pickle < class 'torch._C._VariableFunctions' >

Find the reason on google, and find that the whole model is saved when saving, and there are 1 user-defined parameters in the model

Will torch.save(model,save_path) Replace with torch.save(model.state_dict(),save_path)

Then load the model and make corresponding changes

Supplement: 1 pit of pytorch training model

1. Image reading

python and c + + of opencv have different image reading results, because python and c + + adopt different versions of opencv, thus using different decoding libraries, resulting in different reading results.

2. Image transformation

The results of resize operation of PIL and pytorch images are different from those of resize of opencv, which leads to the use of PIL in training and opencv in prediction, and the results are quite different, especially in detection and segmentation tasks.

3. Numerical calculations

In the calculation of torch. exp of pytorch and exp of c + +, there will be an error of 10e-3 in the value of 10e-6, so special attention should be paid to high-precision calculation, such as

The two inputs, 5.601597, 5.601601, become 270.85862343143174, 270.85970686809225 after exp calculation


Related articles: