有没有办法用预训练的Inception-v3网络对一批图像进行分类?

时间:2016-08-11 12:24:21

标签: python tensorflow gpgpu image-recognition

我正在尝试使用TensorFlow对图像进行分类。

example code on GitHub中是这样的:

createDateConstraint

现在,我正在寻找一次性分类多个图像的解决方案,因为我想在我的GPU上计算分类,我不想一个接一个地将图像移动到GPU,因为这会降低性能。

围绕DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper(); DataValidationConstraint constraint = dataValidationHelper.createDateConstraint(OperatorType.EQUAL, "2014/10/25", null, null); CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0); DataValidation validation = dataValidationHelper.createValidation(constraint, addressList); sheet.addValidationData(validation); 周围所有图片的循环没有达到我想要的效果:每个图像仍然单独发送到GPU。

predictions = sess.run(softmax_tensor, {'DecodeJpeg/contents:0': image_data})

3 个答案:

答案 0 :(得分:4)

查看Google的github,了解他们的Inception深度CNN分类器。

按照他们的指南,我能够微调网络以对酒瓶标签进行分类。只需设置较大的批量大小,即可在一次运行中对许多图像进行分类。

整个指南很有帮助,但您可能对Fine-Tuning a Pre-Trained Model特别感兴趣

答案 1 :(得分:0)

经过大量的反复试验后,我找到了一个对我有正确行为的解决方案。 但我不确定它是否是最优雅的。

pool = ThreadPool()

def operation(sess, softmax, image, image_number):
    prediction = sess.run(softmax, {'DecodeJpeg:0': image})
    return prediction, image_number

with tf.Graph().as_default() as imported_graph:
    tf.import_graph_def(graph_def, name='')

with tf.Session(graph=imported_graph) as sess:
    with tf.device("/gpu:0"):
        softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
        threads = [pool.apply_async(operation, args=(sess, softmax_tensor, np_images[image_number], image_number,)) for
                   image_number in range(len(np_images))]
        result = []
        for thread in threads:
            result.append(thread.get())

关键是使用多线程解决方案。

答案 2 :(得分:0)

Faced with the same task. The valid solution is feed model with batches of images as described here: https://www.tensorflow.org/tutorials/load_data/images

You create a pipeline using tf.data package and feed. Take a note that in the tutorial there is eager mode, while it is no problem to do the same in a 'normal' mode.