[Problem Solved] The training and verification accuracy is high, but the test accuracy is very low

Recap:

Use the ResNet50 pre-trained model to train your own image classification model. The accuracy rate in the training and verification stages is very high, but when a picture is randomly input, the prediction is still inaccurate in most cases.

(So ​​I started searching for various reasons for "high verification accuracy but low test accuracy"...)

Problem exploration:

1. My original data set only has 200 pictures, so I do offline data enhancement (contrast, saturation, cropping, flipping, color transformation) to expand the data set. It is said on the Internet that the data enhancement may be done too much, which leads to changes in the distribution of the training set .

But I don't think it should be, so this factor is temporarily passed .

2. It is said on the Internet that it may be overfitting .

But the verification accuracy rate is very high, so this factor is temporarily passed .

3. Suddenly came across an article! Save my life! Many thanks!

https://blog.csdn.net/qq_36949278/article/details/108930479?app_version=5.7.1&csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22108930479%22%2C%22source%22%3A%22ddddddggf%22%7D&ctrtid=kW0M8&utm_source=app

It is because the way of reading pictures has changed. When training the model, I used cv2.imread(), but when calling the trained model for prediction, I used PIL.Image.open(). The order of color channels read by the former is BGR, and the latter is RGB! !

Therefore, the image reading method during prediction is modified to be consistent with the training stage. But strangely, still inaccurate...

4. It suddenly occurred to me that cv2 could not read the Chinese path . And some of the pictures in my data set are named in Chinese! Although no error was reported, nothing was actually read. When printing (img), the result was None. In other words, I trained the model before, it was completely lonely...

(Success is at hand!)

So modify the file names of all Chinese names. (To make a digression, I searched the Internet for how to modify in batches, but it still feels very troublesome, so I modified it manually. Fortunately, there are not too many such pictures in my data set.)

After modification, retrain the model! It worked!

Here are the predicted results when read differently:

 Here are the predictions when read the same way:

The accuracy rate is basically the same as that in the training phase~

Guess you like

Origin blog.csdn.net/ddddddggf/article/details/126239663