TensorFlow:保存的模型和 tfjs 模型的预测不一致

时间:2021-07-31 17:41:45

标签: tensorflow keras tensorflow.js

我已经使用 TensorFlow Model Maker 进行了训练并将其导出为 SavedModel 格式。使用以下脚本:

def preprocess_image(image):
    image = tf.cast(image, tf.float32)
    image /= tf.constant(255, dtype=image.dtype)
    image = tf.compat.v1.image.resize(image, [224, 224])
    return image


def predict_top_k(model, data, labels, k=1):
    predicted_prob = model.predict(data)
    topk_prob, topk_id = tf.math.top_k(predicted_prob, k=k)
    topk_label = np.array(labels)[topk_id.numpy()]

    label_prob = []
    for label, prob in zip(topk_label, topk_prob.numpy()):
        label_prob = list(zip(label, prob))

    return label_prob

model = tf.keras.models.load_model(model_path)
image = tf.keras.preprocessing.image.load_img(image_path)
input_arr = tf.keras.preprocessing.image.img_to_array(image)
preprocessed = preprocess_image(input_arr)
predicted = predict_top_k(
    model, np.expand_dims(preprocessed, axis=0), data.index_to_label, k=4
)

我对单个文件得到以下预测:

[('bk', 0.95859015), ('l', 0.017178109), ('p', 0.014439273), ('bg', 0.009792461)]

我已使用 tensorflowjs_converter 将此模型转换为 tfjs 格式,并在打字稿中使用它,如下所示:

const getTopKClasses = async (probs: tf.Tensor, topK: number = Infinity): Promise<Prediction[]> => {
  const predictions = tf.softmax(probs);
  const values = await predictions.data();
  return Array.from(values)
    .map((value: number, i: number) => ({ label: CLASSES[i], value }))
    .sort((a: Prediction, b: Prediction) => b.value - a.value)
    .slice(0, topK);
};

const predict = async (modelUrl: string, data: HTMLImageElement) => {
  const model = await tf.loadGraphModel(modelUrl);
  const input = tf.browser.fromPixels(data);
  const resized = tf.image.resizeBilinear(input, [224, 224]);
  const preprocessed = tf.div(resized.asType("float32"), 255).reshape([1, ...resized.shape]);
  const result = model.execute(preprocessed);
  const predictions = getTopKClasses(result);
  return predictions;
}

在同一个图像文件上,我用这种方法得到了不同的预测:

0: {label: "bk", value: 0.46371275186538696}
1: {label: "l", value: 0.17934054136276245}
2: {label: "p", value: 0.1789223849773407}
3: {label: "bg", value: 0.1780242621898651}

是什么导致了这种差异?

0 个答案:

没有答案
相关问题