【问题标题】:Feeding in custom image into tensorflow during inference在推理过程中将自定义图像输入到张量流中
【发布时间】:2018-04-17 20:30:34
【问题描述】:

我有一个像这样的模特

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)

这里的图像基本上是从这样的队列运行器中读取的

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})

我尝试了不同的读取和插入图像的方法。但是,我认为我做得不对。如何将数据输入我的模型?

【问题讨论】:

    标签: python numpy tensorflow machine-learning


    【解决方案1】:

    好吧,你必须把它放在别的地方! :) 也就是说,在 feed_dict 参数中。 sess.run() 的第一个参数是你想要取回的值,所以这只是 logits。因此,从csv_inputs() 的返回值中挑选一张图像,假设您将其分配给my_image(确保保持批次维度,即使它只是 1),那么推断网络的命令将是:

    logits_val = sess.run([ logits ], feed_dict={ images: my_image, keep_conv: 0.8, keep_hidden: 0.5})
    

    请注意,您必须小心images,因为它在您的代码的不同部分意味着不同的东西。一旦它是数据的占位符,在其他部分它就保存数据本身。

    【讨论】:

    • 让我试试看
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 2018-02-01
    • 2019-05-07
    相关资源
    最近更新 更多