我在 TPU 上使用 TF 2 的对象检测 API 成功训练了一个模型,该模型保存为 .pb(SavedModel 格式)。然后我使用它重新加载它tf.saved_model.load,它在使用转换为 shape 的张量的单个图像预测框时工作正常(1, w, h, 3)。
import tensorflow as tf
import numpy as np
# Load Object Detection APIs model
detect_fn = tf.saved_model.load('/path/to/saved_model/')
image = tf.io.read_file(image_path)
image_np = tf.image.decode_jpeg(image, channels=3).numpy()
input_tensor = np.expand_dims(image_np, 0)
detections = detect_fn(input_tensor) # This works fine
Run Code Online (Sandbox Code Playgroud)
问题是我需要进行批量预测以将其缩放到 50 万张图像,但该模型的输入签名似乎仅限于处理具有 shape 的数据(1, w, h, 3)。这也意味着我不能在 Tensorflow Serving 中使用批处理。我怎么解决这个问题?我可以只更改模型签名来处理批量数据吗?
所有工作(加载模型 + 预测)都在随对象检测 API 一起发布的官方容器内执行(来自此处)
我已经实现了以下自定义Layer,seed_vectors根据x使用函数的输入大小,在调用时修改可学习参数的大小repeat。
import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow import repeat
from tensorflow.keras.layers import LayerNormalization
class PoolingMultiHeadAttention(tf.keras.layers.Layer):
def __init__(self, d, k, h):
"""
Arguments:
d: an integer, input dimension.
k: an integer, number of seed vectors.
h: an integer, number of heads.
"""
super(PoolingMultiHeadAttention, self).__init__()
self.seed_vectors = self.add_weight(initializer='uniform',
shape=(1, k, d),
trainable=True)
def call(self, z):
"""
Arguments:
z: a float tensor with shape [b, n, d].
Returns:
a float tensor …Run Code Online (Sandbox Code Playgroud) 我知道ADVI / MCMC之间的数学差异,但是我试图理解使用其中一种的实际含义。我对以这种方式创建的数据运行一个非常简单的logistic回归示例:
import pandas as pd
import pymc3 as pm
import matplotlib.pyplot as plt
import numpy as np
def logistic(x, b, noise=None):
L = x.T.dot(b)
if noise is not None:
L = L+noise
return 1/(1+np.exp(-L))
x1 = np.linspace(-10., 10, 10000)
x2 = np.linspace(0., 20, 10000)
bias = np.ones(len(x1))
X = np.vstack([x1,x2,bias]) # Add intercept
B = [-10., 2., 1.] # Sigmoid params for X + intercept
# Noisy mean
pnoisy = logistic(X, B, noise=np.random.normal(loc=0., scale=0., size=len(x1)))
# dichotomize pnoisy …Run Code Online (Sandbox Code Playgroud)