TFhub、Keras预训练模型进行推理简单示例

导入预训练模型进行推理。以mobilenetv2 Imagenet预训练模型为例:

import tensorflow as tf
import tensorflow_hub as hub
import numpy as np

# 加载图片
image = tf.image.decode_image(tf.io.read_file('/path/to/your/image'),channels=3)
image = tf.image.resize(image,(224,224))
image = tf.expand_dims(image, axis=0)

# Keras预测
i = tf.keras.layers.Input([224, 224, 3], dtype = tf.uint8)
x = tf.cast(i, tf.float32)
x = tf.keras.applications.mobilenet.preprocess_input(x)
core = tf.keras.applications.MobileNetV2()
x = core(x)
model = tf.keras.Model(inputs=[i], outputs=[x])
preds = model(image).numpy()
print(tf.keras.applications.mobilenet_v2.decode_predictions(preds, top=5))

# TFHub预测
path = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/classification/4"
IMAGE_SHAPE = (224, 224)
tfhub_model = tf.keras.Sequential([
    hub.KerasLayer(path, input_shape=IMAGE_SHAPE+(3,), outpu
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值