首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的神经网络在训练和测试中对相同的图像有不同的表现?

为什么我的神经网络在训练和测试中对相同的图像有不同的表现?
EN

Stack Overflow用户
提问于 2022-08-16 12:18:51
回答 1查看 71关注 0票数 0

我使用tensorflow构建了一个神经网络,将新冠肺炎快速测试的图像分为三类(负面正向空的)。

在训练期间,拉伸板日志表示验证的准确性在90%左右。但是当我用训练过的图像训练网络时,分类性能要差得多(~60%)。当我用不同的图像训练网络时,我观察到了同样的行为(参见What I have tried部分)。

在训练过程中,将图像预处理到灰度,并在输入模型之前调整大小。批次大小是16。

代码语言:javascript
复制
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.resize(image, (height, width))

为了增加我拥有的稀疏数据(~450张图像),我使用了keras.preprocessing.image.ImageDataGenerator,它的参数是:width_shift_range=0.1brightness_range=[0.9, 1.1]height_shift_range=0.1zoom_range=0.2horizontal_flip=Truerotation_range=10shear_range=0.2fill_mode="nearest"samplewise_center=Truesamplewise_std_normalization=True

我正在将模型转换为tflite,因为我们需要它用于移动平台。我正在使用以下代码片段:

代码语言:javascript
复制
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)

我尝试过的:

  • 将图像裁剪到收银台的条带上,重新训练和测试网络。
  • 签入测试(推断)脚本,如果标签正确
  • 在测试过程中输入网络之前,检查图像是否被转换为灰度并调整大小。
  • 在将模型转换为tflite之前,使用tensorflow.keras.models测试模型

模型:

代码语言:javascript
复制
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"])

这是训练的图板图。这条直线是另一次训练的结果。

测试/推理脚本:

代码语言:javascript
复制
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]])

问题可能在哪里?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-22 09:21:01

我发现了@Djinn提示的问题。这是推理脚本中的规范化。应该是input_arr = (input_arr - np.mean(input_arr)) / np.std(input_arr)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73373884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档