Google全新AI實戰課發佈:從原理到代碼,手把手帶你入門機器學習

安妮 嶽排槐 發自 凹非寺

量子位 出品 | 公眾號 QbitAI

如果你的心裡只有一件事。

請問:是不是學習?

Google希望你是,而且還準備扶上馬,再送一程。

所以今天一早,大禮包又來了。

Google全新AI實戰課發佈:從原理到代碼,手把手帶你入門機器學習

手把手教你

今年春天,Google發佈了機器學習速成課,英文簡稱MLCC。而且這套基本全程都有中文的課程,還是完全免費的。

Google全新AI實戰課發佈:從原理到代碼,手把手帶你入門機器學習

這還不夠。

Google覺得光學理論還不夠,必須教你理論與實戰相結合。

所謂:知行合一。

於是,Google發佈了最新的一套課程:Machine Learning Practica(機器學習實踐)。這套課程會示範Google如何在產品中使用機器學習。

Google全新AI實戰課發佈:從原理到代碼,手把手帶你入門機器學習

課程地址在此:

https://developers.google.com/machine-learning/practica/

(.cn域名地址親測可用)

Google全新AI實戰課發佈:從原理到代碼,手把手帶你入門機器學習

與之前的課程不同,這套動手實踐課程中,包括視頻、文檔和交互式編程練習。目前已經上線的第一課是圖像分類。

在圖像分類的實踐課程中,可以學習Google如何開發利用最先進的圖像分類模型,這也是Google相冊背後的核心技術。

迄今為止,已有超過1萬名Google員工利用這個實踐課程來訓練他們自己的圖像分類器,最終實現可以識別照片中的貓貓狗狗。

課前準備

想要學習這套課程,也有一些基礎要求。

主要是兩點:

  • 學過Google機器學習速成課,或者瞭解機器學習的基本概念
  • 有不錯的編程基礎知識,以及有一些Python編程經驗

這套實踐課程使用了Keras API。以及課程中的編程練習,使用了Colab。使用Colab不要求之前有過Keras經驗。

課程中代碼基本可算是提供了逐步的解釋。

目前這套實踐課程只發布了圖像分類一組,但Google表示更多的實踐課程正在:骯!嘖!味!

Google全新AI實戰課發佈:從原理到代碼,手把手帶你入門機器學習

課程簡介

在這個課程中,Google首先介紹了圖像分類的基本原理,講述了卷積神經網絡(CNN)的構建,以及池化、全連接等概念。

Google全新AI實戰課發佈:從原理到代碼,手把手帶你入門機器學習

然後,Google會引導你從頭開始構建一個CNN網絡,並且學習如何防止過擬合,以及利用訓練模型進行特徵提取和微調。

實踐課程一共包括三組練習,分別是:

  • Exercise 1: Build a Convnet for Cat-vs-Dog Classification
  • 帶你構建一個貓狗分類的卷積網絡。
  • Exercise 2: Preventing Overfitting
  • 教你如何防止過擬合,改善提高CNN模型。
  • Exercise 3: Feature Extraction and Fine-Tuning
  • 教你如何通過特徵提取和微調來使用Google的Inception v3模型,併為上面兩個練習完成的分類器獲取更好的準確性。

課程示範

量子位潛入這個課程內部,帶回了第二個實踐練習。在這堂課裡,谷歌想教會大家在貓狗圖像分類中,如何減少過擬合。大家感受一下——

Google全新AI實戰課發佈:從原理到代碼,手把手帶你入門機器學習

練習2:減少過擬合

預計完成時間:30分鐘

在本節練習中,我們將基於在練習1中創建的模型將貓狗分類,並通過一些策略減少過擬合:也就是數據增強(Data Augmentation)和正則化方法dropout,從而提高準確性。

和大象被關進冰箱一樣,這得分四步走:

  1. 通過對訓練圖像進行隨機轉換,來探索數據增強的玩法
  2. 在我們數據處理的過程中應用數據增強
  3. 在轉換中加入dropout
  4. 重新訓練模型,評估損失和精確度

Let’s get started吧!

Google全新AI實戰課發佈:從原理到代碼,手把手帶你入門機器學習

數據增強の探索

數據增強是減少視覺模型過擬合的基本方法了,因為我們手頭的訓練實例為數不多,為了充分利用,我們可通過一些隨機的變換“增強”它們,對模型來說,這是不同的圖像~

這可以通過在ImageDataGenerator實例讀取的圖像上增加一些隨機轉換來實現,比如:

 1from keras.preprocessing.image import ImageDataGenerator
2
3datagen = ImageDataGenerator(
4 rotation_range=40,
5 width_shift_range=0.2,
6 height_shift_range=0.2,
7 shear_range=0.2,
8 zoom_range=0.2,
9 horizontal_flip=True,
10 fill_mode='nearest')

還有一些可用的選擇:

  • rotation_range是在0-180之間的一個值,可在此角度內隨機旋轉圖片。
  • width_shift和height_shift是個範圍,指的總寬度或高度的一部分,圖像可在此範圍內垂直或水平隨機轉換
  • shear_range用於隨機剪切
  • zoom_range用來隨機縮放圖片的。
  • horizontal_flip用於水平隨機翻轉圖像的一半
  • fill_mode是用來填充新創造的像素,在圖像隨機垂直或水平變換後可能用到

注意:此練習中使用的2000張圖片摘自Kaggle上的“狗vs貓”數據集,包含25000張圖片。為了節約訓練時間,這裡我們只用到其中的一個子集。

1!wget --no-check-certificate \
2 https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip -O \
3 /tmp/cats_and_dogs_filtered.zip
 1import os
2import zipfile
3
4local_zip = '/tmp/cats_and_dogs_filtered.zip'
5zip_ref = zipfile.ZipFile(local_zip, 'r')
6zip_ref.extractall('/tmp')
7zip_ref.close()
8
9base_dir = '/tmp/cats_and_dogs_filtered'
10train_dir = os.path.join(base_dir, 'train')
11validation_dir = os.path.join(base_dir, 'validation')
12
13# Directory with our training cat pictures
14train_cats_dir = os.path.join(train_dir, 'cats')

15
16# Directory with our training dog pictures
17train_dogs_dir = os.path.join(train_dir, 'dogs')
18
19# Directory with our validation cat pictures
20validation_cats_dir = os.path.join(validation_dir, 'cats')
21
22# Directory with our validation dog pictures
23validation_dogs_dir = os.path.join(validation_dir, 'dogs')
24
25train_cat_fnames = os.listdir(train_cats_dir)
26train_dog_fnames = os.listdir(train_dogs_dir)

接下來,我們將datagen轉換應用到訓練集裡的貓咪圖像,生成5個隨機變量。這個單元需多運行幾次,找到新批次中的隨機變量。

 1%matplotlib inline
2
3import matplotlib.pyplot as plt
4import matplotlib.image as mpimg
5
6from keras.preprocessing.image import array_to_img, img_to_array, load_img
7
8img_path = os.path.join(train_cats_dir, train_cat_fnames[2])
9img = load_img(img_path, target_size=(150, 150)) # this is a PIL image
10x = img_to_array(img) # Numpy array with shape (150, 150, 3)
11x = x.reshape((1,) + x.shape) # Numpy array with shape (1, 150, 150, 3)
12
13# The .flow() command below generates batches of randomly transformed images
14# It will loop indefinitely, so we need to `break` the loop at some point!
15i = 0
16for batch in datagen.flow(x, batch_size=1):
17 plt.figure(i)
18 imgplot = plt.imshow(array_to_img(batch[0]))
19 i += 1
20 if i % 5 == 0:
21 break

在數據處理過程中應用數據增強

現在,將上述增強的數據應用到數據預處理配置中——

 1# Adding rescale, rotation_range, width_shift_range, height_shift_range,
2# shear_range, zoom_range, and horizontal flip to our ImageDataGenerator
3train_datagen = ImageDataGenerator(
4 rescale=1./255,
5 rotation_range=40,
6 width_shift_range=0.2,
7 height_shift_range=0.2,
8 shear_range=0.2,
9 zoom_range=0.2,
10 horizontal_flip=True,)
11
12# Note that the validation data should not be augmented!
13test_datagen = ImageDataGenerator(rescale=1./255)
14
15# Flow training images in batches of 32 using train_datagen generator
16train_generator = train_datagen.flow_from_directory(
17 train_dir, # This is the source directory for training images
18 target_size=(150, 150), # All images will be resized to 150x150
19 batch_size=20,
20 # Since we use binary_crossentropy loss, we need binary labels
21 class_mode='binary')
22
23# Flow validation images in batches of 32 using test_datagen generator
24validation_generator = test_datagen.flow_from_directory(
25 validation_dir,
26 target_size=(150, 150),
27 batch_size=20,
28 class_mode='binary')

神奇之處是,若用增強的數據來訓練模型,則不會被認為是相同示例(雖然它們都是從一張圖片上得到的)。不過模型眼中這些輸入仍緊密相關的,所以還不足以完全消除過擬合

加入Dropout

不過~還有另外一種流行的策略能減少過擬合,即dropout。

如果你想了解過擬合的基本概念,這裡自賣自誇推薦兩個之前免費課程中的相關介紹:

https://developers.google.com/machine-learning/crash-course/training-neural-networks/video-lecture

https://developers.google.com/machine-learning/crash-course/

我們從練習1重新配置我們的convnet架構,在最後的分類層前試圖添加一些dropout。

 1from keras.models import Model
2from keras import layers
3from keras.optimizers import RMSprop
4from keras import backend as K
5
6import tensorflow as tf
7
8# Configure the TF backend session
9tf_config = tf.ConfigProto(
10 gpu_options=tf.GPUOptions(allow_growth=True))
11K.set_session(tf.Session(config=tf_config))
12
13# Our input feature map is 150x150x3: 150x150 for the image pixels, and 3 for
14# the three color channels: R, G, and B
15img_input = layers.Input(shape=(150, 150, 3))
16
17# First convolution extracts 16 filters that are 3x3
18# Convolution is followed by max-pooling layer with a 2x2 window
19x = layers.Conv2D(16, 3, activation='relu')(img_input)
20x = layers.MaxPooling2D(2)(x)
21
22# Second convolution extracts 32 filters that are 3x3
23# Convolution is followed by max-pooling layer with a 2x2 window
24x = layers.Conv2D(32, 3, activation='relu')(x)
25x = layers.MaxPooling2D(2)(x)
26
27# Third convolution extracts 64 filters that are 3x3
28# Convolution is followed by max-pooling layer with a 2x2 window
29x = layers.Convolution2D(64, 3, activation='relu')(x)
30x = layers.MaxPooling2D(2)(x)
31
32# Flatten feature map to a 1-dim tensor
33x = layers.Flatten()(x)
34
35# Create a fully connected layer with ReLU activation and 512 hidden units

36x = layers.Dense(512, activation='relu')(x)
37
38# Add a dropout rate of 0.5
39x = layers.Dropout(0.5)(x)
40
41# Create output layer with a single node and sigmoid activation
42output = layers.Dense(1, activation='sigmoid')(x)
43
44# Configure and compile the model
45model = Model(img_input, output)
46model.compile(loss='binary_crossentropy',
47 optimizer=RMSprop(lr=0.001),
48 metrics=['acc'])

重新訓練模型

隨著數據的增加和dropout的填入,我們需要重新訓練convnet模型。

這一次,我們訓練全部的2000張圖片,訓練了30輪,並對驗證了所有的1000個測試圖像。

這可能需要幾分鐘的時間,檢驗一下你是否能自己編寫代碼了。

1# WRITE CODE TO TRAIN THE MODEL ON ALL 2000 IMAGES FOR 30 EPOCHS, AND VALIDATE 
2# ON ALL 1,000 TEST IMAGES

評估結果

接下來,我們用數據增強和dropout評估模型訓練的結果。

 1# Retrieve a list of accuracy results on training and test data
2# sets for each training epoch
3acc = history.history['acc']
4val_acc = history.history['val_acc']

5
6# Retrieve a list of list results on training and test data
7# sets for each training epoch
8loss = history.history['loss']
9val_loss = history.history['val_loss']
10
11# Get number of epochs
12epochs = range(len(acc))
13
14# Plot training and validation accuracy per epoch
15plt.plot(epochs, acc)
16plt.plot(epochs, val_acc)
17plt.title('Training and validation accuracy')
18
19plt.figure()
20
21# Plot training and validation loss per epoch
22plt.plot(epochs, loss)
23plt.plot(epochs, val_loss)
24plt.title('Training and validation loss')

結果不錯!模型已經不再過擬合。

事實上,從我們的訓練資料來看,隨著訓練次數的增加,模型的準確度會達到80%!

清理

在運行練習3之前,我們還需要運行以下單元來釋放kernel和空閒的內存資源:

1import os, signal
2os.kill(os.getpid(), signal.SIGKILL)

One More Thing

不知道是不是忙中出錯,Google這套全新的課程,在我們發稿的時候,遇到了一個尷尬的問題:練習課程無法訪問。

你點擊練習之後,原本應該是轉入一個Colab頁面,但是卻把多數用戶擋在一個這樣的界面之上。如圖:

Google全新AI實戰課發佈:從原理到代碼,手把手帶你入門機器學習

鏈接地址:https://login.corp.google.com

這是啥?

其實,這就是大名鼎鼎的moma,一個Google內部的搜索工具。如果你是Google員工,就能登錄訪問,進入Google內網。

可能是因為這套實踐課程,和MLCC一樣,也是之前面向Google內部的課程,所以出現了現在略微尷尬的一幕。

估計,可能很快會修復這個問題。

所以你可以先看看上面量子位搬運的課程示範。

不急~

誠摯招聘

量子位正在招募編輯/記者,工作地點在北京中關村。期待有才氣、有熱情的同學加入我們!相關細節,請在量子位公眾號(QbitAI)對話界面,回覆“招聘”兩個字。

量子位 QbitAI · 頭條號簽約作者

վ'ᴗ' ի 追蹤AI技術和產品新動態


分享到:


相關文章: