Tensorflow Hub in C++

Tensorflow Hub in C++

本文关键字:C++ in Hub Tensorflow      更新时间:2023-10-16

是否可以在C++ API中使用TensorFlow Hub?我想转换以下示例。

# Load content and style images (see example in the attached colab).
content_image = plt.imread(content_image_path)
style_image = plt.imread(style_image_path)
# Convert to float32 numpy array, add batch dimension, and normalize to range [0, 1]. Example using numpy:
content_image = content_image.astype(np.float32)[np.newaxis, ...] / 255.
style_image = style_image.astype(np.float32)[np.newaxis, ...] / 255.
# Optionally resize the images. It is recommended that the style image is about
# 256 pixels (this size was used when training the style transfer network).
# The content image can be any size.
style_image = tf.image.resize(style_image, (256, 256))
# Load image stylization module.
hub_module = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
# Stylize image.
outputs = hub_module(tf.constant(content_image), tf.constant(style_image))
stylized_image = outputs[0]

TensorFlow Hub 模块通常还不能用于推理。人们通常需要在之前或之后附加额外的操作以适应其用例,然后创建一个可以在C++中使用的新 SavedModel。

这里的建议是创建一个 python 程序,将集线器模块转换为您的用例。 即具有 tf.function 的程序,其输入签名从文件名到生成的图像,或者从 2 个输入图像到新图像,然后将其保存到新的 SavedModel 中以便在C++中服务。