首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从TF模型动物园导入模型及培训

从TF模型动物园导入模型及培训
EN

Stack Overflow用户
提问于 2021-08-11 10:55:58
回答 1查看 81关注 0票数 0

参考本教程https://www.tensorflow.org/tutorials/images/transfer_learning,我创建并训练了一个resnet模型。

代码语言:javascript
复制
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个误差(这个数字不是常数,每次得到不同的精度)。

这是我的推理代码

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

输出

代码语言:javascript
复制
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类模型(不管是划伤还是来自预先训练过的权重)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-17 08:02:26

当对一幅图像而不是model(img)进行推理时,请使用model(img)

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

https://stackoverflow.com/questions/68740677

复制
相关文章

相似问题

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