Tensorflow 预测始终为零
Tensorflow Prediction always zero
我是 Tensorflow 新手。我使用 convNetKerasLarge.py 生成模型并保存为 tflite 模型。
我正在尝试按如下方式测试这个保存的模型
import tensorflow as tf
import numpy as np
import glob
from skimage.transform import resize
from skimage import io
# out of previously used training and test set
start = 4001
# no of images
row_count = 1
end = start + row_count
n_image_rows = 106
n_image_cols = 106
np_val_images = np.zeros(shape=(1, 1))
np_val_labels = np.zeros(shape=(1, 1))
def prepare_validation_set():
global np_val_images
global np_val_labels
positive_samples = glob.glob('datasets/drunk_resize_frontal_faces/pos/*')[start:end]
# negative_samples = glob.glob('datasets/drunk_resize_frontal_faces/neg/*')[start:end]
# negative_samples = random.sample(negative_samples, len(positive_samples))
val_images = []
val_labels = []
for i in range(len(positive_samples)):
val_images.append(resize(io.imread(positive_samples[i]), (n_image_rows, n_image_cols)))
val_labels.append(1)
# for i in range(len(negative_samples)):
# val_images.append(resize(io.imread(negative_samples[i]), (n_image_rows, n_image_cols)))
# val_labels.append(0)
np_val_images = np.array(val_images)
np_val_labels = np.array(val_labels)
def run_tflite_model(tflite_file, index):
prepare_validation_set()
# Initialize the interpreter
interpreter = tf.lite.Interpreter(model_path=str(tflite_file))
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()[0]
output_details = interpreter.get_output_details()[0]
test_image = np_val_images[index]
test_image = np.expand_dims(test_image, axis=0).astype(input_details["dtype"])
interpreter.set_tensor(input_details["index"], test_image)
interpreter.invoke()
output = interpreter.get_tensor(output_details["index"])[0]
print(output_details)
prediction = output.argmax()
print(prediction)
if __name__ == '__main__':
test_image_index = 1
tflite_model_file = "models/converted/model.tflite"
run_tflite_model(tflite_model_file, 0)
如果我 运行 这个我得到的预测是 0
即使标签应该是 1
因为我输入的是正图像。 (仅供参考:Test loss: 0.08881912380456924 Test accuracy: 0.9729166626930237
有 10 个时期)。我确信我的代码中存在导致此问题的错误,请帮助我找到它。
您链接的脚本在训练前通过减去平均值(这里 0.5
)并除以标准差(这里 1
)对数据进行归一化:
mean = np.array([0.5,0.5,0.5])
std = np.array([1,1,1])
X_train = X_train.astype('float')
X_test = X_test.astype('float')
for i in range(3):
X_train[:,:,:,i] = (X_train[:,:,:,i]- mean[i]) / std[i]
X_test[:,:,:,i] = (X_test[:,:,:,i]- mean[i]) / std[i]
如果您在使用模型进行预测之前不重复相同的操作,您传递给模型的输入将不会具有与您训练时相同的特征。
您可以在准备数据时通过减去图像的平均值 (0.5) 来修复它,即:
np_val_images = np.array(val_images) - 0.5
我是 Tensorflow 新手。我使用 convNetKerasLarge.py 生成模型并保存为 tflite 模型。
我正在尝试按如下方式测试这个保存的模型
import tensorflow as tf
import numpy as np
import glob
from skimage.transform import resize
from skimage import io
# out of previously used training and test set
start = 4001
# no of images
row_count = 1
end = start + row_count
n_image_rows = 106
n_image_cols = 106
np_val_images = np.zeros(shape=(1, 1))
np_val_labels = np.zeros(shape=(1, 1))
def prepare_validation_set():
global np_val_images
global np_val_labels
positive_samples = glob.glob('datasets/drunk_resize_frontal_faces/pos/*')[start:end]
# negative_samples = glob.glob('datasets/drunk_resize_frontal_faces/neg/*')[start:end]
# negative_samples = random.sample(negative_samples, len(positive_samples))
val_images = []
val_labels = []
for i in range(len(positive_samples)):
val_images.append(resize(io.imread(positive_samples[i]), (n_image_rows, n_image_cols)))
val_labels.append(1)
# for i in range(len(negative_samples)):
# val_images.append(resize(io.imread(negative_samples[i]), (n_image_rows, n_image_cols)))
# val_labels.append(0)
np_val_images = np.array(val_images)
np_val_labels = np.array(val_labels)
def run_tflite_model(tflite_file, index):
prepare_validation_set()
# Initialize the interpreter
interpreter = tf.lite.Interpreter(model_path=str(tflite_file))
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()[0]
output_details = interpreter.get_output_details()[0]
test_image = np_val_images[index]
test_image = np.expand_dims(test_image, axis=0).astype(input_details["dtype"])
interpreter.set_tensor(input_details["index"], test_image)
interpreter.invoke()
output = interpreter.get_tensor(output_details["index"])[0]
print(output_details)
prediction = output.argmax()
print(prediction)
if __name__ == '__main__':
test_image_index = 1
tflite_model_file = "models/converted/model.tflite"
run_tflite_model(tflite_model_file, 0)
如果我 运行 这个我得到的预测是 0
即使标签应该是 1
因为我输入的是正图像。 (仅供参考:Test loss: 0.08881912380456924 Test accuracy: 0.9729166626930237
有 10 个时期)。我确信我的代码中存在导致此问题的错误,请帮助我找到它。
您链接的脚本在训练前通过减去平均值(这里 0.5
)并除以标准差(这里 1
)对数据进行归一化:
mean = np.array([0.5,0.5,0.5])
std = np.array([1,1,1])
X_train = X_train.astype('float')
X_test = X_test.astype('float')
for i in range(3):
X_train[:,:,:,i] = (X_train[:,:,:,i]- mean[i]) / std[i]
X_test[:,:,:,i] = (X_test[:,:,:,i]- mean[i]) / std[i]
如果您在使用模型进行预测之前不重复相同的操作,您传递给模型的输入将不会具有与您训练时相同的特征。
您可以在准备数据时通过减去图像的平均值 (0.5) 来修复它,即:
np_val_images = np.array(val_images) - 0.5