简体   繁体   English

如何在TensorFlow中为预训练的Inception v3模型添加新类别和培训?

[英]How do you add new categories and training to a pretrained Inception v3 model in TensorFlow?

I'm trying to utilize a pre-trained model like Inception v3 (trained on the 2012 ImageNet data set) and expand it in several missing categories. 我正在尝试使用预先训练的模型,例如Inception v3(在2012 ImageNet数据集上训练过),并将其扩展为几个缺失的类别。

I have TensorFlow built from source with CUDA on Ubuntu 14.04, and the examples like transfer learning on flowers are working great. 我在Ubuntu 14.04上使用CUDA从源代码构建了TensorFlow,并且像花朵上的转移学习这样的例子非常有用。 However, the flowers example strips away the final layer and removes all 1,000 existing categories, which means it can now identify 5 species of flowers, but can no longer identify pandas, for example. 然而,鲜花示例去除了最后一层并删除了所有1,000个现有类别,这意味着它现在可以识别5种花,但不能再识别大熊猫。 https://www.tensorflow.org/versions/r0.8/how_tos/image_retraining/index.html https://www.tensorflow.org/versions/r0.8/how_tos/image_retraining/index.html

How can I add the 5 flower categories to the existing 1,000 categories from ImageNet (and add training for those 5 new flower categories) so that I have 1,005 categories that a test image can be classified as? 如何将5种花类别添加到ImageNet的现有1,000种类别中(并添加针对这5种新花类别的培训),以便我有1,005种类别可将测试图像归类为? In other words, be able to identify both those pandas and sunflowers? 换句话说,能够识别那些大熊猫和向日葵吗?

I understand one option would be to download the entire ImageNet training set and the flowers example set and to train from scratch, but given my current computing power, it would take a very long time, and wouldn't allow me to add, say, 100 more categories down the line. 我理解一个选项是下载整个ImageNet训练集和花朵示例集并从头开始训练,但鉴于我目前的计算能力,它需要很长时间,并且不允许我添加,比方说,还有100多个类别。

One idea I had was to set the parameter fine_tune to false when retraining with the 5 flower categories so that the final layer is not stripped: https://github.com/tensorflow/models/blob/master/inception/README.md#how-to-retrain-a-trained-model-on-the-flowers-data , but I'm not sure how to proceed, and not sure if that would even result in a valid model with 1,005 categories. 我有一个想法是在使用5个花类重新训练时将参数fine_tune设置为false ,以便最后一层不被剥离: https//github.com/tensorflow/models/blob/master/inception/README.md#如何重新训练 - 训练模型 - 花卉数据 ,但我不知道如何继续,并不确定这是否会导致有1,005类别的有效模型。 Thanks for your thoughts. 谢谢你的想法。

Unfortunately, you cannot add categories to an existing graph; 遗憾的是,您无法向现有图表添加类别; you'll basically have to save a checkpoint and train that graph from that checkpoint onward. 你基本上必须保存一个检查点并从该检查点开始训练该图表。

After much learning and working in deep learning professionally for a few years now, here is a more complete answer: 经过多年的专业学习和深度学习,这里有一个更完整的答案:

The best way to add categories to an existing models (eg Inception trained on the Imagenet LSVRC 1000-class dataset) would be to perform transfer learning on a pre-trained model. 向现有模型添加类别的最佳方法(例如,在Imagenet LSVRC 1000级数据集上训练的初始化)将在预训练模型上执行传递学习。

If you are just trying to adapt the model to your own data set (eg 100 different kinds of automobiles), simply perform retraining/fine tuning by following the myriad online tutorials for transfer learning, including the official one for Tensorflow. 如果您只是想让模型适应您自己的数据集(例如100种不同类型的汽车),只需按照无数的在线转移学习教程(包括Tensorflow的官方教程)进行重新训练/微调。

While the resulting model can potentially have good performance, please keep in mind that the tutorial classifier code is highly un-optimized (perhaps intentionally) and you can increase performance by several times by deploying to production or just improving their code. 虽然生成的模型可能具有良好的性能,但请记住,教程分类器代码是高度未优化的(可能是有意的),您可以通过部署到生产或仅改进其代码来将性能提高几倍。

However, if you're trying to build a general purpose classifier that includes the default LSVRC data set (1000 categories of everyday images) and expand that to include your own additional categories, you'll need to have access to the existing 1000 LSVRC images and append your own data set to that set. 但是,如果您正在尝试构建包含默认LSVRC数据集(1000种日常图像)的通用分类器,并将其扩展为包含您自己的其他类别,则需要访问现有的1000 LSVRC图像并将您自己的数据集附加到该集合。 You can download the Imagenet dataset online, but access is getting spotier as time rolls on. 您可以在线下载Imagenet数据集,但随着时间的推移,访问变得越来越容易。 In many cases, the images are also highly outdated (check out the images for computers or phones for a trip down memory lane). 在许多情况下,图像也非常过时(检查计算机或手机的图像以便在记忆通道上行程)。

Once you have that LSVRC dataset, perform transfer learning as above but including the 1000 default categories along with your own images. 获得该LSVRC数据集后,执行上述传输学习,但包括1000个默认类别以及您自己的图像。 For your own images, a minimum of 100 appropriate images per category is generally recommended (the more the better), and you can get better results if you enable distortions (but this will dramatically increase retraining time, especially if you don't have a GPU enabled as the bottleneck files cannot be reused for each distortion; personally I think this is pretty lame and there's no reason why distortions couldn't also be cached as a bottleneck file, but that's a different discussion and can be added to your code manually). 对于您自己的图像,通常建议每个类别至少100个适当的图像(越多越好),如果您启用扭曲,您可以获得更好的结果(但这会大大增加再训练时间,特别是如果您没有启用GPU作为瓶颈文件不能重复用于每个失真;我个人认为这是非常蹩脚的,没有理由为什么失真也不能被缓存为瓶颈文件,但这是一个不同的讨论,可以手动添加到你的代码)。

Using these methods and incorporating error analysis, we've trained general purpose classifiers on 4000+ categories to state-of-the-art accuracy and deployed them on tens of millions of images. 使用这些方法并结合错误分析,我们已经在4000多个类别上训练了通用分类器,以达到最先进的精度,并将它们部署在数千万个图像上。 We've since moved on to proprietary model design to overcome existing model limitations, but transfer learning is a highly legitimate way to get good results and has even made its way to natural language processing via BERT and other designs. 我们已经转向专有模型设计以克服现有的模型限制,但转移学习是获得良好结果的高度合法方式,甚至通过BERT和其他设计进入自然语言处理。

Hopefully, this helps. 希望,这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM