我使用tensorflow构建了一个神经网络,将新冠肺炎快速测试的图像分为三类(负面、正向、空的)。
在训练期间,拉伸板日志表示验证的准确性在90%左右。但是当我用训练过的图像训练网络时,分类性能要差得多(~60%)。当我用不同的图像训练网络时,我观察到了同样的行为(参见What I have tried部分)。
在训练过程中,将图像预处理到灰度,并在输入模型之前调整大小。批次大小是16。
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.resize(image, (height, width))为了增加我拥有的稀疏数据(~450张图像),我使用了keras.preprocessing.image.ImageDataGenerator,它的参数是:width_shift_range=0.1、brightness_range=[0.9, 1.1]、height_shift_range=0.1、zoom_range=0.2、horizontal_flip=True、rotation_range=10、shear_range=0.2、fill_mode="nearest"、samplewise_center=True、samplewise_std_normalization=True
我正在将模型转换为tflite,因为我们需要它用于移动平台。我正在使用以下代码片段:
model = tf.keras.models.load_model(model_path)
converter = tf.lite.TFLiteConverter.from_keras_model(model) # path to the SavedModel directory
# converter.optimizations = [tf.lite.Optimize.DEFAULT] # optimizations
tflite_model = converter.convert()
# Save the model.
with open('rapid_test_strip_cleaned_model.tflite', 'wb') as f:
f.write(tflite_model)我尝试过的:
tensorflow.keras.models测试模型模型:
img_width, img_height = (256, 256)
model = Sequential()
inputShape = (img_width, img_height, 1)
model.add(Conv2D(32, (3, 3), activation="relu", input_shape=inputShape))
# to prevent overfitting
model.add(Dropout(0.25))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(64, (3, 3), activation="relu"))
# to prevent overfitting
model.add(Dropout(0.25))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(128, (3, 3), activation="relu"))
# to prevent overfitting
model.add(Dropout(0.25))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Flatten())
model.add(Dense(512, activation="relu"))
# to prevent overfitting
model.add(Dropout(0.5))
model.add(Dense(3, activation="softmax"))
opt = Adam(learning_rate=INIT_LR, decay=INIT_LR / EPOCHS)
model.compile(loss="categorical_crossentropy", optimizer=opt,
metrics=["accuracy"])这是训练的图板图。这条直线是另一次训练的结果。
测试/推理脚本:
interpreter = tf.lite.Interpreter(model_path=model_path)
# Load TFLite model and allocate tensors.
interpreter.allocate_tensors()
# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
height = input_details[0]['shape'][1]
width = input_details[0]['shape'][2]
labels = ["positive", "negative", "initial"]
# load image into numpy array
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.resize(image, (height, width))
input_arr = img_to_array(image)
input_arr = np.array([input_arr])
# normalize values
input_arr = input_arr / 255.0
interpreter.set_tensor(input_details[0]['index'], input_arr)
interpreter.invoke()
# The function `get_tensor()` returns a copy of the tensor data.
output_data = interpreter.get_tensor(output_details[0]['index'])
results = np.squeeze(output_data)
top_k = results.argsort()[-5:][::-1]
print(labels[top_k[0]])问题可能在哪里?
发布于 2022-08-22 09:21:01
我发现了@Djinn提示的问题。这是推理脚本中的规范化。应该是input_arr = (input_arr - np.mean(input_arr)) / np.std(input_arr)。
https://stackoverflow.com/questions/73373884
复制相似问题