字幕表 動画を再生する
[MUSIC PLAYING]
{機械学習}
JOSH GORDON: Hey, everyone.
{機械学習}レシピ
Welcome back.
みなさん こんにちは
In this episode, I'll show you how
ようこそ
to train your own image classifier starting from just
今回は皆さん自身の画像分類器に どう学習させるか
a directory of images.
画像のディレクトリから始めて
For example, say you want to build a classifier that
説明します
can tell the difference between a picture of a T.
例えば T レックスとトライセラトップス を区別する
rex and a triceratops.
分類器を作りたいとします
Or maybe you want to classify a painting
もしくはモネの絵とピカソの絵に
as being a Monet or Picasso.
分類したいとかです
To do that, we'll work with a code lab
そうする為に TensorFlow for Poets というコードラボで作業します
called TensorFlow for Poets.
これは画像分類器の学習を 始め作業するのに
And this is a great way to get started
良い方法です
learning about and working with image classification.
では早速2つ
Now two things off the bat.
まず このコードラボは高レベルです
First, this code lab is high level.
分類器に学習させるため 基本的に2つのスクリプトを
To train our classifier, we'll basically just need
実行すればいいだけです
to run a couple of scripts.
ですが 素晴らしいことは 分類器が作るものは
But what's impressive is what the classifier will create
私が2,3年前に 書けただろうものより
is better than what I could have written
優れていることです
myself just a few years ago.
先に進んだらコードラボの 動作の様子をお見せして
As we go, I'll show you how the code lab looks in action,
その理由についてコンテキストと 背景を付け加えましょう
and I'll add context and background
では始めましょう
on why it works so well.
TensorFlow for Poets で 画像分類器に学習させるには
OK, let's get started.
1つ、学習データを 供給すればいいだけです
To train an image classifier with TensorFlow for Poets,
ここではそれは単に 画像でいっぱいのディレクトリです
we'll only need to provide one thing-- training data.
私の案は
Here that's just directories full of images.
5種の花を区別する 分類器を作ることです
My plan is to create a classifier
バラや ひまわりなどですね
to tell the difference between five types of flowers--
これが私の学習データの様子です
roses, sunflowers, and so on.
5つのディレクトリがあり
And here's what my training data looks like.
各種の花にそれぞれ1つです
Notice that I have five directories, one
各ディレクトリの中に たくさんの写真があります
for each type of flower.
花で作業する理由は
Inside each directory are lots of pictures.
私達がコードラボに このデータセットを供給したからです
And the reason I'm working with flowers
だから皆さんはすぐ始められます
is we provided this data set in the code lab
皆さんが自分の画像 例えば恐竜とか絵を使いたいなら
so you can get started right away.
ディレクトリを作ってそれを Web からとった画像で
If you want to use your own images, say, for dinosaurs
埋めればいいだけです
or paintings, all you need to do is create a directory
始めるには各ディレクトリに 約100個の画像が必要です
and fill it with images from the web.
学習データができたら 次は分類器を学習させる
We'll need about 100 images in each directory to start.
必要があります
Once we have our training data, the next thing we'll need to do
その為に TensorFlow を使います
is train our classifier.
TensorFlow はオープンソースの 機械学習ライブラリで
And for that, we'll use TensorFlow.
特に深層学習という 機械学習の一種で
TensorFlow as an open source machine learning library.
作業するのに役立ちます
And it's especially useful for working
深層学習はここ数年で 特に画像分類器のような領域で
with a branch of machine learning called deep learning.
大きな成果につながりました
Deep learning has led to great results in the last couple
それを今日やってみるわけです
years, especially in domains like image classification,
ここにその理由が1つあります
which we'll be working with today.
第1回で
And here's one reason why.
私達はりんごとオレンジの区分けの 話をしました
Recall that way back in episode one,
これを手作業でやるのは 不可能だと分かりました
we talked about telling the difference between apples
世界には変種があまりにも 多いですからね
and oranges.
でも今はまた分類器が入力として 特徴を捉えることも
We saw it's impossible to do this
知っています
by hand because there's too much variation in the world.
画像に関して 手作業で有用な特徴を抽出する
But now we also know that classifiers take features
コードを書くのは非常に難しいです
as input.
例えば 皆さんは1個の果物の 質感を検出する
And with images, it's incredibly hard
コードを書きたいと思わないでしょう
to write code to extract useful features by hand.
これを避けるために 深層学習を使います
For example, you wouldn't want to write code
それには画像の作業をする時に 大きな利点があるからです
to detect the texture of a piece of fruit.
こんな感じです
To get around this, we use deep learning
手作業で特徴を抽出する 必要がありません
because it has a major advantage when working with images.
代わりに 画像の特徴の 生ピクセルを使えます
And it's this.
あとは分類器がやります
You don't need to extract features manually.
学習データの様子の 違いを見る為に
Instead, you can use the raw pixels of the image's features,
アイリスのデータセットと
and the classifier will do the rest.
私達の画像のディレクトリを 比べてみましょう
To see the difference in our training data
アイリスで 各欄は 花を描く特徴です
looks, let's compare the Iris data set
手作業でこういう特徴が 出てきたわけですね
with our directories of images.
たとえば定規で花を 測るとかしてです
In Iris, each column is a feature
一方 TensorFlow for Poets での 学習データはこうです
that describes the flower.
それは単にラベル付きの名前です
And you can imagine we came up with these features
繰り返すと 分類器はただの関数
manually, say, by measuring the flower with a ruler.
f(x) = y です
Now by contrast, here's our training data
ここで X は画像のピクセルの 2次元配列で
in TensorFlow for Poets.
Y はバラのようなラベルです
It's just a list of labeled images.
深層学習の話では
And again, a classifier is just a function.
私達が使う分類器は ニューラルネットワークと呼ばれます
f of x equals y.
高レベルではそれはただ 別のタイプの分類器です
Here x is a 2D array of pixels from the image.
前回書いた最近接のものと 似たものです
And y is a label like rose.
違いはニューラルネットワークは
Now when we're talking about deep learning,
もっと複雑な関数を 覚えられることです
the classifier we'll be using is called a neural network.
このコードラボでは TensorFlow for Poets は
At a high level, that's just another type of classifier,
皆さんの代わりに背後で ニューラルネットワークを
like the nearest neighbor one wrote last time.
設定し学習させる仕事をします
The difference is a neural network
だからと言って TensorFlow コードが これまで見てきたものより
can learn more complex functions.
書くのが難しいわけではありません
In this code lab, TensorFlow for Poets
実は私が好きな TensorFlow プログラムの書き方は
takes care of setting up and training the neural network
TF Learn を使うことです
for you behind the scenes.
TF Learn は TensorFlow 上部の 高レベルの機械学習ライブラリです
That doesn't mean that TensorFlow code is any harder
構文はこれまで見てきたような scikit-learn に似ています
to write than what we've seen so far.
例えばここにお見せする コード片では
In fact, my favorite way of writing TensorFlow programs
ニューラルネットワークを インポートし
is by using TF Learn.
それを学習させ 新しいデータを 分類する方法を示します
And TF Learn is a high level machine learning library
皆さんは既に学んだ技術を使い これができます
on top of TensorFlow.
これについて今もっと学びたければ
And the syntax is similar to scikit-learn
動画説明欄にリンクを 書いておいたので
like we've seen so far.
確認してみてください
For example, here's a code snippet
では TensorFlow for Poets に戻って
that shows you how to import a neural network,
分類器に学習させましょう
train it, and use it to classify new data.
その為にまず このスクリプトから始めます
And you can do this using the skills you've already learned.
これはコードラボで 詳しくやったので
If you want to learn more, no pun intended,
ここではあまり多く話しませんが
about this stuff right now, I put links
あと2つのコンテキストを述べましょう
in the description you can check out.
皆さんが知りたいでしょうから
OK, now let's return to TensorFlow for Poets
まず このスクリプトは 分類器を学習させるのに
and train our classifier.
約20分かかります
To do that, we'll kick it off with this script.
その時間は長いかと 考えてみてください
It's covered in detail in the code lab,
答えは「いいえ」です
so I won't say too much about it here.
陰で TensorFlow for Poets は
But I will give you context on two more things
実は分類器を一から 学習させてはいるのではなく
you might want to know about.
Inception という既存の 分類器から始めています
First, the script takes about 20 minutes
Inception は Google の最良の 画像分類器の1つで
to train the classifier.
オープンソースです
Now ask yourself, is that a long time?
私達の学習データにはほんの 数千の画像があるだけなのに
The answer turns out to be no.
Inception は 1000 の異なるカテゴリの
Under the hood, TensorFlow for Poets
1200 万の画像で学習させられました
isn't actually training a classifier from scratch.
Inception を学習させるのは 8 GPU の高速デスクトップで
Instead, it's starting with an existing classifier called
約2週間かかりました
Inception.
TensorFlow for Poets では まず Inception で始め
And Inception is one of Google's best image classifiers.
リトレーニングという技術を使い
And it's open source.
私達の画像で働くように それを合わせます
Whereas we have just a couple thousand images in our training
これで Inception が前に学習した パラメーターのいくつかを再利用し
data, Inception was trained on 1.2 million images
はるかに少ない学習データで 新しい高精度の分類器を作れます
from 1,000 different categories.
学習が終わるまで早送りします
Training Inception took about two weeks on a fast desktop
学習させた分類器ができたら 試してみれます
with eight GPUs.
そのためにウィキメディア・コモンズから このバラの画像をダウンロードして
In TensorFlow for Poets, we'll begin with Inception
分類器を使って
and then use a technique called retraining
どの種の花か予測させます
to adjust it to work with our images.
ご覧のように答を 正しく得ます
This lets us re-use some of the parameters Inception
また他種の花の確率も見れます
has previously learned so we can create a new high accuracy
さて分類器は私達が示した
classifier with far less training data.
学習データについて 知ってるだけです
I'll fast forward til our training finishes.
だから 例えばローマの コロシアムの画像を分類させれば
And once we have a trained classifier, we can try it out.
分類器はそれが花の種類だとして 予測しなければなりません
To do that, I'll download this image of a rose
まあ確率は低く出るでしょうがね
from Wikimedia Commons and use our classifier
終わりにあと1、2言わせてください
to predict what type of flower it is.
良い画像分類器にするには
As we can see, it gets it right.
肝心な点は多様性と量です
And we can see the confidence distribution
多様性とは異なる種のバラの 画像が多ければ多いほど
for the other types of flowers as well.
うまくいきます
Now keep in mind our classifier only
例えば学習データに
knows about the training data we've shown it.
赤白黄のバラの写真を含みます
So if we ask it to classify an image,
また 上からとか横からの 異なる角度で写した写真も入れます
say, of the Roman Colosseum, it must predict
更に 前景のバラの写真と
that it's a type of flower.
背景の写真を入れます
Hopefully, though, the confidence will be low.
量とは学習データが多ければ 多いほど
Now let me give you one or two more closing thoughts.
良い分類器を作れる可能性が 大きいということです
To train a good image classifier,
バラのフォルダーには数百の 画像が入っています
the name of the game is diversity and quantity.
Inception を再学習させるのに それで充分です
By diversity, I mean the more images
もっと少ない画像でも うまくいきそうです
of different types of roses we have, the better off we'll be.
精度が減るでしょうけれどね
For example, our training data includes
今はこれでおしまいです
pictures of red, white, and yellow roses.
次の段階として 皆さんは
We also have pictures taken at different angles,
もっと掘り下げ自分のTensorFlow を 作りたいことでしょう
say, from above or to the side.
ここにそのやり方を示す チュートリアルのリンクがあります
And we've included pictures of roses in the foreground
ここで見たのと全く同じ 技術を使えます
as well as the background.
ご視聴どうもありがとう
Now by quantity, I mean the more training data we have,
では次回にお会いしましょう
the better a classifier we're likely to create.
There are several hundred images inside the roses folder.
That's enough to retrain Inception.
And you can probably get away with even fewer images,
though your accuracy might decrease.
OK, that's it for now.
As a next step, you'll probably want
to dig deeper and try writing your own TensorFlow code.
Here's a link to a tutorial that will show you how to do that.
And you can use exactly the same technology we saw here.
As always, thanks very much for watching.
And I'll see you guys next time.
[MUSIC PLAYING]