如何在Tensorflow中使用经过训练的CNN模型进行对象识别 [英] How to use a trained CNN model for object identification in Tensorflow

查看:402
本文介绍了如何在Tensorflow中使用经过训练的CNN模型进行对象识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用120张图片进行训练的CNN模型。
将图像转换为TFR记录并使用此方法标记

I have a CNN model that is trained using a set of 120 pictures. The images are converted in TFR record and labeled with this method

def write_records_file(dataset, record_location):
    """
    dataset : dict(list)
      Dictionary with each key being a label for the list of image filenames of its value.
    record_location : str
      Location to store the TFRecord output.
    """
    writer = None

    # Enumerating the dataset because the current index is used to breakup the files if they get over 100
    current_index = 0
    for breed, images_filenames in dataset.items():
        for image_filename in images_filenames:
            if current_index % 100 == 0:
                if writer:
                    writer.close()

                record_filename = "{record_location}-{current_index}.tfrecords".format(
                    record_location=record_location,
                    current_index=current_index)

                writer = tf.python_io.TFRecordWriter(record_filename)
            current_index += 1

            image_file = tf.read_file(image_filename)         
            image = tf.image.decode_jpeg(image_file)
            grayscale_image = tf.image.rgb_to_grayscale(image)
            resized_image = tf.image.resize_images(grayscale_image, 250, 151)

            image_bytes = sess.run(tf.cast(resized_image, tf.uint8)).tobytes()

            image_label = breed.encode("utf-8")

            example = tf.train.Example(features=tf.train.Features(feature={
                'label': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_label])),
                'image': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_bytes]))
            }))

            writer.write(example.SerializeToString())

write_records_file(testing_dataset, "./output/testing-images/testing-image")
write_records_file(training_dataset, "./output/training-images/training-image")

The整个 model + training 脚本以 train_prediction = tf.nn.softmax(final_fully_connected)
结尾,我得到2个.tfr文件作为输出(培训 test )。

The whole model+training script ends with train_prediction = tf.nn.softmax(final_fully_connected) and I get 2 .tfr files as output (training and test).

现在假设您有一张图片,想知道有什么更相似120图片样本的图片以进行识别。我该如何进行?

Now suppose you have a picture and want to know what is the more similar picture of the 120 pic sample to identify it. How I have to proceed?

train_prediction张量具有以下格式: shape =(3,120),dtype = float32 120是类别

train_prediction tensor has this format shape=(3, 120), dtype=float32120 is the total numbers of categories

不幸的是,在我正在阅读的书中,没有任何迹象表明,本章以这种训练有素的模型结尾,我不知道如何在真正的应用程序,以及在Internet上搜索,有许多相似的示例都在同一点结束。

In the book that I'm reading unfortunately there isn't any indication and the chapter end with this trained model that I don't know how to use in a real application, and searching in internet there are many similar sample that end at same point.

推荐答案

我不明白您的问题。通过120个样本,您是说有120个类别,每个类别的示例图像很少?在那种情况下,这是分类问题,您训练一个用于分类的模型,其中输入作为图像,输出作为输入属于120个类别之一的概率(+不属于任何这些类别的额外类别)。在这种情况下,将完全连接的层馈入softmax函数,该函数输出120个类的概率,该类本质上是长度为120的向量。但是,为此,每个类需要多个训练示例。

I didn't understand your question. By 120 samples, do you mean there are 120 classes with few examples images per class? In that case, this is classification problem where you train a model for classification with input as image and output as probability that the input will belong to one of the 120 classes (+ extra class that it doesnt belong to any of those). In this case, fully connected layer is fed into softmax function that outputs this probability for 120 classes which is essentially vector of length 120. But for this, you need multiple training examples per class.

如果您只有120张图像,并且您需要知道其他测试图像是否与这些图像之一相似,则可以为已经存在的神经网络的那120张图像获取完全连接的层之前的层输出矢量在较大的图像样本(例如初始模型)上进行训练,然后将测试图像向量与这120个向量进行相似性处理。

In case you just have 120 images and you need to know if another test image similarity with one of these images, you can just take output vector of layer before fully connected layer for those 120 images for a neural network that is already trained on a larger sample of images (like inception model) and then just similarity of the test image vector with those 120 vectors.

我主持了一个简单易用的演示程序,使用以上技术此处。检查是否可以为您解决,否则请进一步完善问题说明。

I have hosted a simple to use demo using above technique here. Check if works out for you otherwise refine the problem statement more.

这篇关于如何在Tensorflow中使用经过训练的CNN模型进行对象识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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