如何在tf.data.Dataset.map中使用预训练的keras模型进行推理? [英] How to use a pre-trained keras model for inference in tf.data.Dataset.map?

查看:74
本文介绍了如何在tf.data.Dataset.map中使用预训练的keras模型进行推理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个预先训练的模型,并且我正在尝试构建另一个模型,该模型将前一个模型的输出作为输入.我不想端到端地训练模型,并且只想将第一个模型用于推理.第一个模型是使用 tf.data.Dataset 管道进行训练的,而我的第一个倾向是将该模型作为另一个 dataset.map()操作集成到模型的尾部.管道,但是我遇到了问题.在此过程中,我遇到了20个不同的错误,每个错误都与上一个错误无关.批处理规范化层似乎尤其是一个痛点.

I have a pre-trained model, and I'm trying to build another model that takes as input the output of the previous model. I don't want to train the models end-to-end, and want to use the first model for inference only. The first model was trained using tf.data.Dataset pipeline, and my first inclination was to integrate the model as just another dataset.map() operation at the tail of the pipeline, but I'm having issues with that. I've encountered 20 different errors in the process, each unrelated to the prior one. The batch-normalization layer particularly seems to be a pain-point for this.

下面是一个最小的入门示例,它说明了该问题.它是用R编写的,但也欢迎使用python回答.

Below is a minimal starter example that illustrates the issue. It's written in R but answers in python are also welcome.

我正在使用tensorflow-gpu版本1.13.1和 tf.keras

I'm using tensorflow-gpu version 1.13.1 and keras from tf.keras

library(reticulate)
library(tensorflow)
library(keras)
library(tfdatasets)
use_implementation("tensorflow")

model_weights_path <- 'model-weights.h5'

arr <- function(...) 
  np_array(array(seq_len(prod(unlist(c(...)))), unlist(c(...))), dtype = 'float32')

new_model <- function(load_weights = TRUE) {
  model <- keras_model_sequential() %>% 
    layer_conv_1d(5, 5, activation = 'relu', input_shape = shape(150, 10)) %>%
    layer_batch_normalization() %>%
    layer_flatten() %>%
    layer_dense(10, activation = 'softmax')
  if (load_weights)
    load_model_weights_hdf5(model, model_weights_path)
  freeze_weights(model)
  model
}

if(!file.exists(model_weights_path)) {
  model <- new_model(FALSE) 
  save_model_weights_hdf5(model, model_weights_path)
}

model <- new_model()

data <- arr(20, 150, 10)
ds <- tfdatasets::tensors_dataset(data) %>% 
  dataset_repeat()

ds2 <- ds %>% 
  dataset_map(function(x) {
    model(x)
  })

try(nb <- next_batch(ds2))

sess <- k_get_session()
it <- make_iterator_initializable(ds2)
sess$run(iterator_initializer(it))
nb <- it$get_next()

try(sess$run(nb))

sess$run(tf$initialize_all_variables())

try(sess$run(nb))

推荐答案

也许由于我不熟悉R,因此无法直接回答您的问题.但是我最近使用 tf.data构建了输入管道.

Maybe this will not answer your question directly as I am not familiar with R. But I have recently built an input pipeline using tf.data.

generate_images 函数是使用 .map 映射的,并使用经过训练的生成器模型来生成新图像.

The generate_images function is mapped using .map and uses a trained generator model to generate new images.

gen_model = tf.keras.models.load_model(artifact_dir+'/'+generators[-1], compile=False)

NOISE_DIM = 100

def generate_images(l):
    # generate images using the trained generator
    noise = tf.random.normal([BATCH_SIZE, NOISE_DIM])
    images = gen_model(noise)

    # prepare the images for resize_and_preprocess function
    images = tf.squeeze(images, axis=-1)
    images = images*0.5+0.5
    images = tf.image.convert_image_dtype(images, dtype=tf.uint8)

    return images

genloader = tf.data.Dataset.from_tensors([1])

genloader = (
    genloader
    .map(generate_images, num_parallel_calls=AUTO)
    .map(resize_and_preprocess, num_parallel_calls=AUTO)
    .prefetch(AUTO)
)

关于批次归一化,它在训练和推断阶段的行为有所不同.在基于Python的TensorFlow中,使用具有批处理规范化层的预训练模型时,需要传递 training = False .

Regarding Batch Normalization, it behaves differently during the training and inference phase. In Python-based TensorFlow one needs to pass training=False when using a pretrained model that has batch normalization layer.

这篇关于如何在tf.data.Dataset.map中使用预训练的keras模型进行推理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆