参考本教程https://www.tensorflow.org/tutorials/images/transfer_learning,我创建并训练了一个resnet模型。
preprocess_input = tf.keras.applications.resnet50.preprocess_input
IMG_SHAPE = IMG_SIZE + (3,)
base_model = tf.keras.applications.resnet50.ResNet50(
include_top=False, weights='imagenet',input_shape=IMG_SHAPE, classes=2)
prediction_layer = tf.keras.layers.Dense(1)
global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
inputs = tf.keras.Input(shape=IMG_SHAPE)
x = data_augmentation(inputs)
x = preprocess_input(x)
x = base_model(x)
x = global_average_layer(x)
x = tf.keras.layers.Dropout(0.2)(x)
outputs = prediction_layer(x)
model = tf.keras.Model(inputs, outputs)那么,当我推断该模型是否执行数据增强?我希望模型在训练过程中进行数据增强,而不是在推理过程中。
同时,当我将图像作为批次推断和一次推断一个图像时,我得到了不同的结果。当我推断出一批图像时,我总是得到精度1(这是一个过度拟合的模型),当我一个接一个地推断图像时,我会得到2-4个误差(这个数字不是常数,每次得到不同的精度)。
这是我的推理代码
image_batch, label_batch = test_dataset.as_numpy_iterator().next()
class_list =['close','open']
model = tf.keras.models.load_model("shutter_model")
predictions = model.predict_on_batch(image_batch).flatten()
# Apply a sigmoid since our model returns logits
predictions = tf.nn.sigmoid(predictions)
predictions = tf.where(predictions < 0.5, 0, 1)
error = 0
for i in range(len(predictions)):
if predictions[i]!= label_batch[i]:
error+=1
print("number of errors when batch of images fed into the model: ",error)
print('=='*10)
error = 0
for i in range(len(image_batch)):
img = tf.expand_dims(image_batch[i], axis=0)
predictions = model(img)
predictions = tf.nn.sigmoid(predictions)
class_n = 1 if predictions[0][0] >0.5 else 0
if label_batch[i]!= class_n:
error+=1
print("number of errors when images fed into the model one by one: ",error)输出
number of errors when batch of images fed into the model: 0
====================
number of errors when images fed into the model one by one: 3我的目的是使用Resnet50体系结构来训练一个2类模型(不管是划伤还是来自预先训练过的权重)。
发布于 2021-08-17 08:02:26
当对一幅图像而不是model(img)进行推理时,请使用model(img)
https://stackoverflow.com/questions/68740677
复制相似问题