Tensorflow keras模型几乎达到100%的验证精度,但预测总是返回1
我是tensorflow的新手,正在尝试建立一个模型来对两类图像进行分类 验证准确率在12个阶段后达到98%(这似乎异常高)。预测时,无论输入的图像是什么,它总是输出:[[1.]] 加载数据:Tensorflow keras模型几乎达到100%的验证精度,但预测总是返回1,tensorflow,machine-learning,keras,image-classification,Tensorflow,Machine Learning,Keras,Image Classification,我是tensorflow的新手,正在尝试建立一个模型来对两类图像进行分类 验证准确率在12个阶段后达到98%(这似乎异常高)。预测时,无论输入的图像是什么,它总是输出:[[1.]] 加载数据: import numpy as np import os import cv2 from tqdm import tqdm import random import pickle dataDir = "C:/optimised_dataset" categories = [&quo
import numpy as np
import os
import cv2
from tqdm import tqdm
import random
import pickle
dataDir = "C:/optimised_dataset"
categories = ["demented", "healthy"]
IMG_WIDTH = 44
IMG_HEIGHT = 52
lim = 0
training_data = []
def create_training_data():
for category in categories:
path = os.path.join(dataDir, category) # path to demented or healthy dir
class_num = categories.index(category)
lim = 0
for img in tqdm(os.listdir(path)):
if lim < 3000:
try:
img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE)
new_array = cv2.resize(img_array, (IMG_WIDTH, IMG_HEIGHT))
training_data.append([new_array, class_num])
lim+=1
except Exception as e:
pass
else:
break
create_training_data()
random.shuffle(training_data)
X = []
Y = []
for features, label in training_data:
X.append(features)
Y.append(label)
X = np.array(X).reshape(-1, IMG_WIDTH, IMG_HEIGHT, 1)
Y = np.array(Y)
pickle_out = open("X.pickle", "wb")
pickle.dump(X, pickle_out)
pickle_out.close()
pickle_out = open("Y.pickle", "wb")
pickle.dump(Y, pickle_out)
pickle_out.close()
预测:
import cv2
import tensorflow as tf
categories = ["demented", "healthy"]
def prepare(filepath):
IMG_WIDTH = 44
IMG_HEIGHT = 52
img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
img_array = img_array / 255.0
new_array = cv2.resize(img_array, (IMG_WIDTH, IMG_HEIGHT))
return new_array.reshape(-1, IMG_WIDTH, IMG_HEIGHT, 1)
model = tf.keras.models.load_model("DD1.model")
prediction = model.predict([prepare('D:/test.png')])
print(prediction)
当我删除
img\u array=img\u array/255.0
时,它会输出一个介于0和1之间的看似随机的十进制数。正如我已经指出的,这种情况的原因是大多数情况下的类不平衡
比如说,你有两个班,A班有96个样本,B班有4个样本。在这种极端情况下,如果我们从一个总是预测a类的模型开始,它将达到96%的准确率
要解决此问题,您可以尝试-
正如我已经指出的,在大多数情况下,造成这种情况的原因是阶级不平衡 比如说,你有两个班,A班有96个样本,B班有4个样本。在这种极端情况下,如果我们从一个总是预测a类的模型开始,它将达到96%的准确率 要解决此问题,您可以尝试-
检查你的数据是否高度不平衡。嗯。。我用另一个数据集替换了这些数据,结果成功了。但我不明白,如果你的数据高度不平衡,为什么我的原始数据会产生98%的验证准确率。嗯。。我用另一个数据集替换了这些数据,结果成功了。但我不明白为什么我的原始数据产生98%的验证准确率
import cv2
import tensorflow as tf
categories = ["demented", "healthy"]
def prepare(filepath):
IMG_WIDTH = 44
IMG_HEIGHT = 52
img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
img_array = img_array / 255.0
new_array = cv2.resize(img_array, (IMG_WIDTH, IMG_HEIGHT))
return new_array.reshape(-1, IMG_WIDTH, IMG_HEIGHT, 1)
model = tf.keras.models.load_model("DD1.model")
prediction = model.predict([prepare('D:/test.png')])
print(prediction)
from sklearn.utils import class_weight
class_weights = class_weight.compute_class_weight('balanced',
np.unique(y_train),
y_train)
model.fit(X_train, y_train, class_weight=class_weights)