未登录,请登录后再发表信息
最新评论 (0)
播放视频

5分钟写出TensorFlow图片分类器

Build a TensorFlow Image Classifier in 5 Min

大家好 我是西拉杰
Hello world. It’s Siraj.
在本集中我们将通过30行Python代码 使用Tensorflow来构建一个图像分类器
In this episode we’re going to build an image classifier using Tensorflow in 30 lines of Python.
我说的可不是那种 能够识别手写数字或者鸢尾花的分类器
And I don’t mean a classifier that can detect handwritten digits or Iris flowers.
我说的那种分类器 你可以训练它 识别任何你想识别的东西 包括识别巧克力
I’m talking literally anything you want you’ll be able to train this thing to classify chocolate if you wanted
这种分类器存在无限的可能性
The possibilities are endless.
许多行业都会被这种简单的技术方案颠覆:
There’s so many industries that can be disrupted by just this simple solution:
有个日本的黄瓜种植户建造了一个机器 它能分辨出每根黄瓜属于九种黄瓜中的哪一种
A Japanese Cucumber Farmer built a machine to detect whether each of his cucumbers was one of nine different types using this thing.
比如我们要构建一个西拉杰分类器
Let’s say we want to build a Siraj classifier.
如果使用传统的编程思想
If we think about this problem in the traditional programming paradigm,
我们得手动输入一大堆特征
we want to handcraft a bunch of features
或许还要进行边缘检测来保存我的发型
maybe we could do some edge Detection to save the shape of my hair
或者用彩色直方图来保存我牙齿的颜色
or use a color histogram to save the color of my teeth.
可问题是西拉杰需要太多变量来描述
The problem with that is there’s so much variance in Siraj’s.
我头发很多 而且一直在变
My hair is a lot. Seriously, it’s never the same.
这里就需要引入卷积神经网络(CNN)了
This is where convolutional neural networks come into play
它们本质上是一个能构建特征的黑盒 这样就不用我们自己手动输入特征
They’re essentially a black box that constructs features that we would otherwise have to handcraft
这些从训练中提取的抽象特征非常一般化 所以可解释这些变化原因
And these abstract features they create from training are so generalized that they account for variance.
如果我们想自行训练CNN网络
If we wanted to train a CNN ourselves,
我们需要很强的计算能力和很长的时间
we need a lot of computing power and a lot of time.
而这两样我们都没有
Both of which we don’t have.
我甚至没有时间洗碗(抱歉室友们)
I don’t even have time to do my dishes (sorry room mates).
因此我们要用一个已经提前训练过的CNN网络叫做 Inception
That’s why we want to use a pre-trained CNN model called Inception.
Inception 已经被谷歌用数千种类型的10万个图像训练过
Inception was trained by Google on 100 K images with a thousand categories.
在本视频中我们将会给“达斯·维德”的图片做分类
Our use case in this video will be classifying Darth vader pictures,
但之前 Inception 没有在维德上做过训练
But Inception wasn’t trained on vader
因此我们会进行一个“训练转移”的操作
So we’re going to perform a process called ‘’Transfer learning‘’
意味着我们将之前学习到的部分应用到新的训练中来
That means applying the learnings from a previous training session to a new training session
如果我们观察Inception模型
If we look at the inception model
我们可以看到 每次导入一个图片
We can see that when we feed in an image as an input at each layer
它的每一层将对数据进行一系列的操作 直到输出一个标签和分类百分比
it will perform a series of operations on that data until it outputs a label and classification percentage
每一层都是一个不同的抽象集合
Each layer is a different set of abstractions
第一层主要学会了做边缘检测 中间层学会了做形状检测
In the first layers, it’s basically taught itself Edge Detection then Shape Detection in the middle layers
然后逐渐抽象化直到最后一层
and they get increasingly more abstract up until the end
如果我们观察最后几层 会发现这些是对整体来说最高层级的侦测器
If we look at the last few layers these are the highest level detectors for whole objects
“训练转移”基本上是想在最后几层再训练“达斯·维德”的特征
For Transfer learning will basically just want to retrain that last layer on features of Darth Vader,
以便于让神经网络把他的特征加入到它对应的知识库中
So it can add a representation of him to its repository of knowledge.
我们将用7步解决问题
So this is going to be a seven step process
接下来将带你依次进行每一步 听起来不错吧?
and we’re going to go through each step in order. Sounds good?
第一步是安装 Docker
Step one is to install Docker,
这个工具在你的电脑里创建一个能运行应用程序的虚拟机
which is a tool for creating a virtual container on your machine for running apps.
使用 Docker 的好处是你不用在电脑上再安装任何依赖库了
The benefit of Docker is you don’t have to install any dependencies on your machine.
我们下好了所需的 Docker 镜像
So we’ll eventually download a Docker image
这里面内建了 TensorFlow 需要的所有依赖库
that has all the necessary dependencies for Tensorflow built in.
只需下载一个Docker工具箱 就能完成整个安装过程
Just download a Docker toolbox, go through the installation process
然后你可以在任何时候
And then you can launch your Docker container anytime easily
双击Docker快速启动图标轻松启动Docker 酷!
by double-clicking the Docker QuickStart terminal. Cool!
我们现在打开 Docker 进入到第二步
Now that we have Docker opened that brings us the step two:
粘贴这行代码来安装Tensorflow Docker镜像
Installing the Tensorflow Docker image by pasting in this line.
这需要几分钟 然后我们进入第三步
It’ll take a few minutes. Then once it’s installed we’ll move on to Step 3.
把我们的图片数据集下载到本地电脑上 按Ctrl-D可暂停
Downloading our image dataset to our local machine or stop Docker with control-D
然后在本地home目录下新建一个文件夹叫“tf_files/star_wars”
and create a directory called tf_files/star_wars in our home directory locally
我们需要一个名为Darth vader 的文件夹 在里面放几百张“达斯·维德”的图片
We want to put a folder labeled Darth vader that contains a couple hundred vader pics in here
我发现了一个很炫酷的Chrome 插件
There’s this dope Chrome extension
叫做 Fatkun Batch Download Image 它可以批量下载你在 Google image 上搜索到的图片
I found called Fatkun Batch Download Image that bulk downloads all the images from your Google image search results
在 Google image 键入”darth vader” 然后就可以开始下载了
Just go to Google image search, type in”darth vader” and start downloading all of those images.
下载好后放入“tf_files/star_wars”文件夹中
Once we got them we’ll just drag that folder into our tf_files/star_wars folder
然后我们进入第四步
That brings us to step four.
现在我们已经在目录 tf_files 存放了我们需要的图片 然后我们通过 这条命令把它链接到我们的 Docker容器中
Now that we have our images in tf_files directory, we want to link them to our Docker container with this command.
很棒!所有图片都有了链接
Boom! All linked up!
第五步是从 Git 上下载训练脚本
Step 5 is to download the training script via Git.
输入命令 cd 进入TensorFlow 的文件夹下执行 git pull
Just cd into the Tensorflow directory then run “git pull”.
这个命令可让我们通过刚刚准备好的“达斯·维德”图像数据集来再次训练Inception分类器
This code will allow us to retrain the Inception classifier with a newly linked Darth Vader image dataset.
第六步就是真正的再训练部分了
Step 6 is the actual retraining part
bottleneck目录用来缓存磁盘上底层的输出
The bottleneck directory will be used to cache the outputs of the lower layers on disk,
这样可以避免重复计算
So they don’t have to repeatedly be recalculated.
我们运行这个例子时进行500次迭代 下个标识代表哪里存放训练过的模型
We’ll run this example for 500 iterations the next flag asks where to store our trained model our output graph
这个文件夹存储我们可以在Tensor Board中看到的输出图像
which we can later view in tensor board, name
这个和训练数据文件夹名字相同的文件夹存储输出标签
our output labels, which will be the same as our training data folder
这个是用来存放“达斯·维德” 图片的目录
and the image directory where we stored our Vader images.
我们直接在终端上运行这个脚本
Let’s go ahead and run this script right from terminal.
训练我们的分类器差不多需要30分钟
It’ll take about 30 minutes or so to train our classifier,
我们来做点有用的事情
So do something productive.
脚本完成后会输出一个准确率在85%到99%之间的训练模型
The Script should output a training accuracy somewhere between 85 and 99 percent when it’s done.
接着进入最后一步
And this brings us to our final step.
我们想要写一个脚本 它使用再训练的分类器来检测含“达斯·维德”的新图片
We want to write a script that will use our new retrained classifier to detect a novel image contains Darth Vader.
这个脚本要我们自己来写
we write this ourselves
首先第一步 先导入Tensorflow库
First things first, we’ll import tensorflow
然后创建一个变量来保存用户输入图片的路径
then we want to create a a variable to store the user input image path.
再创建一个变量来保存图片数据 另外一个变量用来加载来自标签文件的图片标签
We’ll create another variable to store the data from that image and one more to load the label of that image from the label file
然后我们从之前保存的再训练图片文件中获取模型
Next we’ll want to grab our model from the saved retrained graph file,
保存在变量graph_def中 然后开始解析
store it in the graph_def variable and parse it.
现在我们准备好了图片和模型
Now that we have our image and model ready
这时可以将图片数据填入到再训练模型中得到预测结果
It’s time to make a prediction by feeding the image data into our retrained model to get our prediction output.
为此我们需要创建一个Tensorflow会话
In order to do this will create a Tensorflow session.
它将提供一个可以操作张量数据的环境
This will give us an environment to perform operations on our tensor data in.
首先我们通过会话从模型中的最后一层获得softmax函数张量
The first thing we’ll do in a session is get our softmax funcion tensor from the last layer of our model.
softmax使用最后一层把输入数据映射为期望输出的概率
The softmax function is using the final layer to map input data into probabilities of an expected output.
我们将通过会话执行softmax张量函数来处理输入的图像数据
We will execute our softmax tensor function on our input image data via a session run function
它会以数组形式输出预测结果
It will output our predictions as an array
接下来我们按照置信度来对预测标签进行排序
We’ll next want to sort our prediction labels in order of confidence and lastly for every prediction we have
最后 我们可以得到每一个预测的标签以及得分 然后把它输出到终端
We can get the predicted label and the score and print it out to terminal.
现在我们用脚本来处理一张维德的图片
Let’s take the script and run it on one of our Vader pictures.
处理结果非常好!
The result is pretty good!
Tensorflow 让图像分类变得非常容易
Tensorflow makes it much easier to classify an image.
我给大家提供了本课的一个挑战任务
And I’ve got a challenge for you guys on this episode.
任务是创建一个你认为
The challenge is to create a classifier
可能对科学家们有用的分类器
that you think would be a useful tool for scientist to have
可以是你喜欢的任何科学领域
It can be any field of science you’d like.
把你的代码提交到GitHub 然后在readme中描述一下科学家应该怎样使用它
Upload your code to GitHub and then the readme write up a few sentences on how a scientist would use this.
把你的代码库写进评论区
Post your repository in the comment section
我会基于实用性和准确性进行评判
and I’ll judge them based on utility and accuracy.
获胜者将在两周内两期视频后得到我的表扬
The winner gets a shoutout of me two videos from now so in two weeks,
我将会免费送出一本我签名的《去中心化应用》
and I’ll also send you a free signed copy of my book 《Decentralized Applications》
现在我要去买 iPhone7了
For now I’ve got to now buy the iPhone 7.
谢谢观看
So thanks for watching.

发表评论

译制信息
视频概述

使用 Python 编写识别达斯维达的图片

听录译者

收集自网络

翻译译者

ID007185

审核员

审核员X

视频来源

https://www.youtube.com/watch?v=QfNvhPx5Px8

相关推荐