Python 在推理过程中将自定义图像馈送到tensorflow中
我有一个这样的模特Python 在推理过程中将自定义图像馈送到tensorflow中,python,numpy,tensorflow,machine-learning,Python,Numpy,Tensorflow,Machine Learning,我有一个这样的模特 def inference(images, reuse=False, trainable=True): coarse1_conv = conv2d('coarse1', images, [11, 11, 3, 96], [96], [1, 4, 4, 1], padding='VALID', reuse=reuse, trainable=trainable) coarse1 = tf.nn.max_pool(coarse1_conv, ksize=[1, 3,
def inference(images, reuse=False, trainable=True):
coarse1_conv = conv2d('coarse1', images, [11, 11, 3, 96], [96], [1, 4, 4, 1], padding='VALID', reuse=reuse, trainable=trainable)
coarse1 = tf.nn.max_pool(coarse1_conv, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID', name='pool1')
coarse2_conv = conv2d('coarse2', coarse1, [5, 5, 96, 256], [256], [1, 1, 1, 1], padding='VALID', reuse=reuse, trainable=trainable)
coarse2 = tf.nn.max_pool(coarse2_conv, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool1')
coarse3 = conv2d('coarse3', coarse2, [3, 3, 256, 384], [384], [1, 1, 1, 1], padding='VALID', reuse=reuse, trainable=trainable)
coarse4 = conv2d('coarse4', coarse3, [3, 3, 384, 384], [384], [1, 1, 1, 1], padding='VALID', reuse=reuse, trainable=trainable)
coarse5 = conv2d('coarse5', coarse4, [3, 3, 384, 256], [256], [1, 1, 1, 1], padding='VALID', reuse=reuse, trainable=trainable)
coarse6 = fc('coarse6', coarse5, [6*10*256, 4096], [4096], reuse=reuse, trainable=trainable)
coarse7 = fc('coarse7', coarse6, [4096, 4070], [4070], reuse=reuse, trainable=trainable)
coarse7_output = tf.reshape(coarse7, [-1, 55, 74, 1])
return coarse7_output
我称之为使用
logits = model.inference(images, keep_conv, keep_hidden)
这里的图像基本上是从这样的queuerunner读取的
images, depths, invalid_depths = dataset.csv_inputs(TRAIN_FILE)
def csv_inputs(self, csv_file_path):
filename_queue = tf.train.string_input_producer([csv_file_path], shuffle=True)
reader = tf.TextLineReader()
_, serialized_example = reader.read(filename_queue)
filename, depth_filename = tf.decode_csv(serialized_example, [["path"], ["annotation"]])
# input
jpg = tf.read_file(filename)
image = tf.image.decode_jpeg(jpg, channels=3)
image = tf.cast(image, tf.float32)
# target
depth_png = tf.read_file(depth_filename)
depth = tf.image.decode_png(depth_png, channels=1)
depth = tf.cast(depth, tf.float32)
depth = tf.div(depth, [255.0])
#depth = tf.cast(depth, tf.int64)
# resize
image = tf.image.resize_images(image, (IMAGE_HEIGHT, IMAGE_WIDTH))
depth = tf.image.resize_images(depth, (TARGET_HEIGHT, TARGET_WIDTH))
invalid_depth = tf.sign(depth)
# generate batch
images, depths, invalid_depths = tf.train.batch(
[image, depth, invalid_depth],
batch_size=self.batch_size,
num_threads=4,
capacity= 50 + 3 * self.batch_size,
)
return images, depths, invalid_depths
我现在试图通过提供一个图像来运行模型
logits_val = sess.run([ logits, what_do_i_have-to put here], feed_dict={keep_conv: 0.8, keep_hidden: 0.5})
我尝试了不同的阅读和插入图像的方法。然而,我认为我做得不对。如何将数据输入到模型中?好吧,您必须将其放在其他地方!:)也就是说,在feed_dict参数中。的第一个参数是要返回的值,所以这只是logits。因此,从
csv_inputs()
的返回值中选择一个图像,假设您将该图像分配给my_image
(确保保持批处理维度,即使它仅为1),则推断其上网络的命令为:
logits_val = sess.run([ logits ], feed_dict={ images: my_image, keep_conv: 0.8, keep_hidden: 0.5})
请注意,您必须小心使用
图像
,因为它在代码的不同部分具有不同的含义。一旦它成为数据的占位符,在其他部分它就会保存数据本身。让我试试看