Keras model.predict is nearly always incorrect on training dataset; even when training it to near 100% accuracy

huangapple go评论76阅读模式
英文:

Keras model.predict is nearly always incorrect on training dataset; even when training it to near 100% accuracy

问题

以下是您要翻译的内容:

I am trying to do multiclass classification with keras for videogame characters. The problem is that even when the training accuracy/validation accuracy hits near 100% and 70% respectively; when I actually run model.predict() on an image that I literally just trained on; it classifies it completely incorrectly. I have around 300 images per class.

I have tried everything I can think of; tried many ways to load the data initially, changed the model architecture a million times and the preprocessing functions to prepare the image that model.predict will be ran on.

Here is my code:

  1. class_names = ['ana', 'ashe','baby', 'ball','bap', 'bastion','brig', 'cass','doom', 'dva'
  2. ,'echo', 'genji','hanzo', 'hog','jq', 'junkrat','kiriko', 'lucio','mei', 'mercy','moira', 'orisa'
  3. ,'pharah', 'ram','reaper', 'rein','sigma', 'sojourn','soldier', 'sombra','sym', 'torb','tracer', 'widow'
  4. ,'winston', 'zarya','zen']
  5. class_names_label = {class_name:i for i, class_name in enumerate(class_names)}
  6. nb_classes = len(class_names)
  7. print(class_names_label)
  8. IMAGE_SIZE = (128,128)
  9. def load_data():
  10. DIRECTORY = r'D:\crop-id\enemy-crop'
  11. CATEGORY = ['train', 'test']
  12. output = []
  13. for category in CATEGORY:
  14. path = os.path.join(DIRECTORY, category)
  15. print(path)
  16. images = []
  17. labels = []
  18. print('Loading {}'.format(category))
  19. for folder in os.listdir(path):
  20. label = class_names_label[folder]
  21. #iterate through each img in folder
  22. for file in os.listdir(os.path.join(path,folder)):
  23. #get path name of img
  24. img_path = os.path join(os.path.join(path, folder), file)
  25. #open and resize img
  26. image = cv.imread(img_path)
  27. image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
  28. image = cv.resize(image, IMAGE_SIZE)
  29. #append the image and its corresponding label to output
  30. images.append(image)
  31. labels.append(label)
  32. images = np.array(images, dtype = 'float32')
  33. labels = np.array(labels, dtype='int32')
  34. output.append((images, labels))
  35. return output
  36. (train_images, train_labels), (test_images, test_labels) = load_data()
  37. train_images, train_labels = shuffle(train_images, train_labels, random_state=25)
  38. model = Sequential()
  39. model.add(Conv2D(32, (3,3), activation='relu', input_shape=(128, 128, 3))
  40. model.add(MaxPooling2D())
  41. model.add(BatchNormalization())
  42. model.add(Conv2D(64, (3,3), activation='relu'))
  43. model.add(MaxPooling2D())
  44. model.add(BatchNormalization())
  45. model.add(Conv2D(64, (3,3), activation='relu'))
  46. model.add(MaxPooling2D())
  47. model.add(BatchNormalization()
  48. model.add(Flatten())
  49. model.add(Dense(256, activation='relu'))
  50. model.add(Dense(37, activation='softmax'))
  51. model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001), loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])
  52. model.summary()
  53. model.fit(train_images, train_labels, batch_size=32, epochs=10, validation_split=.2)
  54. model.save(os.path.join('models','plzwork14.h5'))
  55. predictions = model.predict(test_images)
  56. pred_labels = np.argmax(predictions, axis = 1)
  57. print(classification_report(test_labels, pred_labels))

That is for the model and this is the preprocessing I run on the image for the model.predict():

  1. def prepare(img):
  2. img = cv.resize(img, (128,128))
  3. img = np.reshape(img, (128,128,3))
  4. img = np.expand_dims(img/255, 0)
  5. prediction = model.predict(img)
  6. prediction = prediction[0]
  7. print(prediction)
  8. print(class_names[np.argmax(prediction)])
  9. img1 = cv.imread(r'C:\Users\andrew\Desktop\sombra.jpg')
  10. prepare(img1)

Thanks for your help!

英文:

I am trying to do multiclass classification with keras for videogame characters. The problem is that even when the training accuracy/validation accuracy hits near 100% and 70% respectively; when I actually run model.predict() on an image that I literally just trained on; it classifies it completely incorrectly. I have around 300 images per class.

I have tried everything I can think of; tried many ways to load the data initially, changed the model architecture a million times and the preprocessing functions to prepare the image that model.predict will be ran on.

Here is my code:

  1. `class_names = ['ana', 'ashe','baby', 'ball','bap', 'bastion','brig', 'cass','doom', 'dva'
  2. ,'echo', 'genji','hanzo', 'hog','jq', 'junkrat','kiriko', 'lucio','mei', 'mercy','moira', 'orisa'
  3. ,'pharah', 'ram','reaper', 'rein','sigma', 'sojourn','soldier', 'sombra','sym', 'torb','tracer', 'widow'
  4. ,'winston', 'zarya','zen']
  5. class_names_label = {class_name:i for i, class_name in enumerate(class_names)}
  6. nb_classes = len(class_names)
  7. print(class_names_label)
  8. IMAGE_SIZE = (128,128)
  9. def load_data():
  10. DIRECTORY = r'D:\crop-id\enemy-crop'
  11. CATEGORY = ['train', 'test']
  12. output = []
  13. for category in CATEGORY:
  14. path = os.path.join(DIRECTORY, category)
  15. print(path)
  16. images = []
  17. labels = []
  18. print('Loading {}'.format(category))
  19. for folder in os.listdir(path):
  20. label = class_names_label[folder]
  21. #iterate through each img in folder
  22. for file in os.listdir(os.path.join(path,folder)):
  23. #get path name of img
  24. img_path = os.path.join(os.path.join(path, folder), file)
  25. #open and resize img
  26. image = cv.imread(img_path)
  27. image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
  28. image = cv.resize(image, IMAGE_SIZE)
  29. #append the image and its corresponding label to output
  30. images.append(image)
  31. labels.append(label)
  32. images = np.array(images, dtype = 'float32')
  33. labels = np.array(labels, dtype='int32')
  34. output.append((images, labels))
  35. return output
  36. (train_images, train_labels), (test_images, test_labels) = load_data()
  37. train_images, train_labels = shuffle(train_images, train_labels, random_state=25)
  38. model = Sequential()
  39. model.add(Conv2D(32, (3,3), activation='relu', input_shape=(128, 128, 3)))
  40. model.add(MaxPooling2D())
  41. model.add(BatchNormalization())
  42. model.add(Conv2D(64, (3,3), activation='relu'))
  43. model.add(MaxPooling2D())
  44. model.add(BatchNormalization())
  45. model.add(Conv2D(64, (3,3), activation='relu'))
  46. model.add(MaxPooling2D())
  47. model.add(BatchNormalization())
  48. model.add(Flatten())
  49. model.add(Dense(256, activation='relu'))
  50. model.add(Dense(37, activation='softmax'))
  51. model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001), loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])
  52. model.summary()
  53. model.fit(train_images, train_labels, batch_size=32, epochs=10, validation_split=.2)
  54. model.save(os.path.join('models','plzwork14.h5'))
  55. predictions = model.predict(test_images)
  56. pred_labels = np.argmax(predictions, axis = 1)
  57. print(classification_report(test_labels, pred_labels))`

That is for the model and this is the preprocessing I run on the image for the model.predict():

  1. `def prepare(img):
  2. img = cv.resize(img, (128,128))
  3. img = np.reshape(img, (128,128,3))
  4. img = np.expand_dims(img/255, 0)
  5. prediction = model.predict(img)
  6. prediction = prediction[0]
  7. print(prediction)
  8. print(class_names[np.argmax(prediction)])
  9. img1 = cv.imread(r'C:\Users\andrew\Desktop\sombra.jpg')
  10. prepare(img1)`

Thanks for your help!

答案1

得分: 1

在生成训练和测试图像的代码中,您有以下代码:

  1. image = cv.cvtColor(image, cv.COLOR_BGR2RGB)

因此,您的训练和测试数据是RGB图像。然而,在进行预测时,您没有这行代码,所以您要预测的图像是BGR图像。因此,只需添加代码来将要预测的图像从BGR转换为RGB。

英文:

in the code for generation of your training and test images you have the code

  1. image = cv.cvtColor(image, cv.COLOR_BGR2RGB)

Thus your train and test data are RGB images. However when you do your predictions you do not have this line of code so the image your trying to predict on are BGR images. So just add the code to convert from BGR to RGB for the images you want to predict

huangapple
  • 本文由 发表于 2023年2月27日 11:50:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定