使用预训练(Tensorflow)CNN提取特征 [英] Extract features using pre-trained (Tensorflow) CNN

查看:6146
本文介绍了使用预训练(Tensorflow)CNN提取特征的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

深度学习已经成功应用于几个大型数据集,用于对少数类(猫,狗,汽车,飞机等)进行分类,其中性能击败更简单的描述符,如SIFT的特征袋,颜色直方图等。



然而,训练这样的网络需要每个类的大量数据和大量的训练时间。然而,在花费一个设计和训练这样的设备并收集训练数据之前,通常没有足够的数据或者只想知道卷积神经网络可能做得多好。



在这种特殊情况下,可能最理想的做法是使用一些基准数据集来配置和训练网络,这些数据集由最先进的出版物使用,并简单地应用于某些数据集可能具有作为特征提取器。



这导致每个图像的一组特征,其可以馈送到经典分类方法如SVM,逻辑回归,神经网络,等等。



特别是当没有足够的数据来训练CNN时,我可以期望这优于在少数样本上训练CNN的流水线。 / p>

我正在查看tensorflow教程,但他们总是似乎有一个明确的训练/测试阶段。我找不到带有预先配置的CNN特征提取器的pickle文件(或类似文件)。



我的问题是:存在这样的预先训练的网络,我找到他们。或者,这种方法有意义吗?在哪里可以找到CNN +权重?



EDIT
W.r.t. @ john的评论我试过使用'DecodeJpeg:0''DecodeJpeg / contents:0' (:S)

  import cv2,requests ,numpy 
import tensorflow.python.platform
import tensorflow as tf


response = requests.get('https://i.stack.imgur.com /LIW6C.jpg?s=328&g=1')
data = numpy.asarray(bytearray(response.content),dtype = np.uint8)
image = cv2.imdecode(data, 1)

compression_worked,jpeg_data = cv2.imencode('。jpeg',image)
如果不是compression_worked:
raise异常(在opencv中将图像压缩为jpeg格式库)
jpeg_data = jpeg_data.tostring()

with open('./ deep_learning_models / inception-v3 / classify_image_graph_def.pb','rb')as graph_file:
graph_def = tf.GraphDef()
graph_def.ParseFromString(graph_file.read())
tf.import_graph_def(graph_def,name ='')

with tf.Session as sess:
softmax_tensor = sess.graph.get_tensor_by_name('pool_3:0')

arr0 = numpy.squeeze(sess.run(
softmax_tensor,
{ 'decodeJpeg:0':image}
))

arr1 = numpy.squeeze(sess.run(
softmax_tensor,
{'DecodeJpeg / :jpeg_data}
))

print(numpy.abs(arr0 - arr1).max())

所以最大绝对差是 1.27649 ,一般来说所有的元素都不同(特别是因为 arr0 arr1 本身介于0 - 0.5之间。



也会期望'DecodeJpeg:0'需要一个jpeg字符串,而不是一个numpy数组,为什么名称包含Jpeg。 @john:你能说一下
确定你是你的评论吗?



所以我想我不知道什么是什么,训练的神经网络是确定性的(但最多是混乱的)。

解决方案

TensorFlow团队最近发布了一个深入的CNN数据集。您可以从此处。相关的图片识别教程提供了有关该模型的更多详细信息。



虽然当前模型没有专门打包以供后续训练步骤使用,但您可以探索修改脚本以重复使用模型的部分和您自己网络中训练的权重。 p>

Deep Learning has been applied successfully on several large data sets for the classification of a handful of classes (cats, dogs, cars, planes, etc), with performances beating simpler descriptors like Bags of Features over SIFT, color histograms, etc.

Nevertheless, training such a network requires a lot of data per class and a lot of training time. However, very often one doesn't have enough data or just wants to get an idea of how well a convolutional neural network might do, before spending time one designing and training such a device and gathering the training data.

In this particular case, it might be ideal to have a network configured and trained using some benchmark data set used by the state of the art publications, and to simply apply it to some data set that you might have as a feature extractor.

This results in a set of features for each image, which one could feed to a classical classification method like SVM's, logistic regression, neural networks, etc.

In particular when one does not have enough data to train the CNN, I may expect this to outperform a pipeline where the CNN was trained on few samples.

I was looking at the tensorflow tutorials, but they always seem to have a clear training / testing phase. I couldn't find a pickle file (or similar) with a pre-configured CNN feature extractor.

My questions are: do such pre-trained networks exist and where can I find them. Alternatively: does this approach make sense? Where could I find a CNN+weights ?

EDIT W.r.t. @john's comment I tried using 'DecodeJpeg:0' and 'DecodeJpeg/contents:0' and checked the outputs, which are different (:S)

import cv2, requests, numpy
import tensorflow.python.platform
import tensorflow as tf


response = requests.get('https://i.stack.imgur.com/LIW6C.jpg?s=328&g=1')
data = numpy.asarray(bytearray(response.content), dtype=np.uint8)
image = cv2.imdecode(data,-1)

compression_worked, jpeg_data = cv2.imencode('.jpeg', image)
if not compression_worked:
    raise Exception("Failure when compressing image to jpeg format in opencv library")
jpeg_data = jpeg_data.tostring()

with open('./deep_learning_models/inception-v3/classify_image_graph_def.pb', 'rb') as graph_file:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(graph_file.read())
    tf.import_graph_def(graph_def, name='')

with tf.Session() as sess:
    softmax_tensor = sess.graph.get_tensor_by_name('pool_3:0')

    arr0 = numpy.squeeze(sess.run(
        softmax_tensor,
        {'DecodeJpeg:0': image}
    ))

    arr1 = numpy.squeeze(sess.run(
        softmax_tensor,
        {'DecodeJpeg/contents:0': jpeg_data}
    ))

    print(numpy.abs(arr0 - arr1).max())

So the max absolute difference is 1.27649, and in general all the elements differ (especially since the average value of the arr0 and arr1 themselves lies between 0 - 0.5).

I also would expect that 'DecodeJpeg:0' needs a jpeg-string, not a numpy array, why else does the name contain 'Jpeg'. @john: Could you state how sure you are about your comment?

So I guess I'm not sure what is what, as I would expect a trained neural network to be deterministic (but chaotic at most).

解决方案

The TensorFlow team recently released a deep CNN trained on the ImageNet dataset. You can download the script that fetches the data (including the model graph and the trained weights) from here. The associated Image Recognition tutorial has more details about the model.

While the current model isn't specifically packaged to be used in a subsequent training step, you could explore modifying the script to reuse parts of the model and the trained weights in your own network.

这篇关于使用预训练(Tensorflow)CNN提取特征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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