我想转移基于tf.dataset的输入管道。所有教程都展示了如何进行图像分类或分割,并且完成此任务没有任何问题,但是我正在从事实时视频处理任务,并且需要将带有帧数据的先前遮罩传递给预测,以便暂时保持一致。目前,我在可训练模型中使用了基于numpy的keras生成器,用于生成蒙版(基于先前帧),并用作当前帧的先前蒙版(将4个通道馈入网络)。在新的批次创建过程中会发生先前的掩码生成。此代码运行良好。我尝试了许多方法,但是使用tf.dataset却没有成功。可能有人面临类似的问题并且可以共享解决方案吗?
先谢谢您!
一些实验代码
# Dummy network
g = tf.Graph()
with g.as_default():
inputs = tf.keras.Input(shape=(28,28,4))
x = tf.keras.layers.Conv2D(filters=32,
kernel_size=(3, 3),
activation=tf.nn.relu)(inputs)
x = tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=2)(x)
x = tf.keras.layers.Conv2D(filters=64,
kernel_size=(3, 3),
activation=tf.nn.relu)(x)
x = tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=2)(x)
out = tf.keras.layers.Conv2D(filters=2,
kernel_size=(3, 3),
activation=tf.nn.relu)(x)
model = tf.keras.Model(inputs=inputs, outputs=out)
optimizer = tf.train.AdamOptimizer(learning_rate=0.001)
model.compile(loss='categorical_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])
model.summary()
# Class for wrapping model
class prior_mask_creator():
def __init__(self, model_prior):
self.model_prior = model_prior
def make_prior_mask(self, x, y):
h = tf.get_session_handle(x)
h = K.get_session().run(h)
v_1 = K.get_session().run([self.model_prior.layers[-1].output], feed_dict={inputs:h })
return tf.cast(x, tf.float32), tf.cast(y, tf.float32)
# Image generator
c_g = custom_generator(x_data, y_data, batch)
# Create class-wrapper
mask_creator = prior_mask_creator(model)
# Create dataset
ds_test = tf.data.Dataset.from_generator(c_g.gen,
output_types=(tf.float32, tf.float32),
output_shapes=(tf.TensorShape([None, None, 4]), tf.TensorShape([None, None, 2])))
ds_test = ds_test.map(random_crop)
# Add prior prediction
ds_test = ds_test.map(mask_creator.make_prior_mask)
ds_test = ds_test.batch(batch)
iterator = ds_test.make_initializable_iterator()
next_batch = iterator.get_next()
with g.as_default():
with tf.Session(graph=g) as sess:
K.set_session(sess)
sess.run(tf.global_variables_initializer())
sess.run(iterator.initializer)
for i in range(epoch):
v = sess.run(next_batch)