Keras實戰學習圖像分類





我們一般用深度學習做圖片分類的入門教材都是MNIST或者CIFAR-10,因爲數據都是別人準備好的,有的甚至是一個函數就把所有數據都load進來了,所以跑起來都很簡單,但是跑完了,好像自己還沒掌握圖片分類的完整流程,因爲他們沒有經歷數據處理的階段,所以談不上走過一遍深度學習的分類實現過程。今天我想給大家分享兩個比較貼近實際的分類項目,從數據分析和處理說起,以Keras爲工具,徹底掌握圖像分類任務。

這兩個分類項目就是:交通標誌分類和票據分類。交通標誌分類在無人駕駛或者與交通相關項目都有應用,而票據分類任務就更加貼切生活了,同時該項目也是我現在做的一個大項目中的子任務。這兩個分類任務都是很貼近實際的練手好項目,希望經過這兩個實際任務可以掌握好Keras這個工具,並且搭建一個用於圖像分類的通用框架,以後做其他圖像分類項目也可以得心應手。

先說配置環境:

  1. Python 3.5
  2. Keras==2.0.1,TesnsorFlow後端,CPU訓練

一、交通標誌分類

首先是觀察數據,看看我們要識別的交通標誌種類有多少,以及每一類的圖片有多少。打開一看,這個交通標誌的數據集已經幫我們分出了訓練集和數據集。

每個文件夾的名字就是其標籤。

每一類的標誌圖片數量在十來張到數十張,是一個小數據集,總的類別是62。

那我們開始以Keras爲工具搭建一個圖片分類器通用框架。

搭建CNN

用深度學習做圖片分類選的網絡肯定是卷積神經網絡,但是現在CNN的種類這麼多,哪一個會在我們這個標誌分類任務表現最好?在實驗之前,沒有人會知道。一般而言,先選一個最簡單又最經典的網絡跑一下看看分類效果是的策略是明智的選擇,那麼LeNet肯定是最符合以上的要求啦,實現簡單,又相當經典。那我們先單獨寫一個lenet.py的文件,然後實現改進版的LeNet類。

  1. # import the necessary packages
  2. from keras.models import Sequential
  3. from keras.layers.convolutional import Conv2D
  4. from keras.layers.convolutional import MaxPooling2D
  5. from keras.layers.core import Activation
  6. from keras.layers.core import Flatten
  7. from keras.layers.core import Dense
  8. from keras import backend as K
  9. class LeNet:
  10. @staticmethod
  11. def build(width, height, depth, classes):
  12. # initialize the model
  13. model = Sequential()
  14. inputShape = (height, width, depth)
  15. # if we are using "channels last", update the input shape
  16. if K.image_data_format() == "channels_first": #for tensorflow
  17. inputShape = (depth, height, width)
  18. # first set of CONV => RELU => POOL layers
  19. model.add(Conv2D(20, (5, 5),padding="same",input_shape=inputShape))
  20. model.add(Activation("relu"))
  21. model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
  22. #second set of CONV => RELU => POOL layers
  23. model.add(Conv2D(50, (5, 5), padding="same"))
  24. model.add(Activation("relu"))
  25. model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
  26. # first (and only) set of FC => RELU layers
  27. model.add(Flatten())
  28. model.add(Dense(500))
  29. model.add(Activation("relu"))
  30. # softmax classifier
  31. model.add(Dense(classes))
  32. model.add(Activation("softmax"))
  33. # return the constructed network architecture
  34. return model

其中conv2d表示執行卷積,maxpooling2d表示執行最大池化,Activation表示特定的激活函數類型,Flatten層用來將輸入“壓平”,用於卷積層到全連接層的過渡,Dense表示全連接層(500個神經元)。

參數解析器和一些參數的初始化

首先我們先定義好參數解析器。

  1. # set the matplotlib backend so figures can be saved in the background
  2. import matplotlib
  3. matplotlib.use("Agg")
  4. # import the necessary packages
  5. from keras.preprocessing.image import ImageDataGenerator
  6. from keras.optimizers import Adam
  7. from sklearn.model_selection import train_test_split
  8. from keras.preprocessing.image import img_to_array
  9. from keras.utils import to_categorical
  10. from imutils import paths
  11. import matplotlib.pyplot as plt
  12. import numpy as np
  13. import argparse
  14. import random
  15. import cv2
  16. import os
  17. import sys
  18. sys.path.append('..')
  19. from net.lenet import LeNet
  20. def args_parse():
  21. # construct the argument parse and parse the arguments
  22. ap = argparse.ArgumentParser()
  23. ap.add_argument("-dtest", "--dataset_test", required=True,
  24. help="path to input dataset_test")
  25. ap.add_argument("-dtrain", "--dataset_train", required=True,
  26. help="path to input dataset_train")
  27. ap.add_argument("-m", "--model", required=True,
  28. help="path to output model")
  29. ap.add_argument("-p", "--plot", type=str, default="plot.png",
  30. help="path to output accuracy/loss plot")
  31. args = vars(ap.parse_args())
  32. return args

我們還需要爲訓練設置一些參數,比如訓練的epoches,batch_szie等。這些參數不是隨便設的,比如batch_size的數值取決於你電腦內存的大小,內存越大,batch_size就可以設爲大一點。又比如norm_size(圖片歸一化尺寸)是根據你得到的數據集,經過分析後得出的,因爲我們這個數據集大多數圖片的尺度都在這個範圍內,所以我覺得32這個尺寸應該比較合適,但是不是最合適呢?那還是要通過實驗才知道的,也許64的效果更好呢?

  1. # initialize the number of epochs to train for, initial learning rate,
  2. # and batch size
  3. EPOCHS = 35
  4. INIT_LR = 1e-3
  5. BS = 32
  6. CLASS_NUM = 62
  7. norm_size = 32

載入數據

接下來我們需要讀入圖片和對應標籤信息。

  1. def load_data(path):
  2. print("[INFO] loading images...")
  3. data = []
  4. labels = []
  5. # grab the image paths and randomly shuffle them
  6. imagePaths = sorted(list(paths.list_images(path)))
  7. random.seed(42)
  8. random.shuffle(imagePaths)
  9. # loop over the input images
  10. for imagePath in imagePaths:
  11. # load the image, pre-process it, and store it in the data list
  12. image = cv2.imread(imagePath)
  13. image = cv2.resize(image, (norm_size, norm_size))
  14. image = img_to_array(image)
  15. data.append(image)
  16. # extract the class label from the image path and update the
  17. # labels list
  18. label = int(imagePath.split(os.path.sep)[-2])
  19. labels.append(label)
  20. # scale the raw pixel intensities to the range [0, 1]
  21. data = np.array(data, dtype="float") / 255.0
  22. labels = np.array(labels)
  23. # convert the labels from integers to vectors
  24. labels = to_categorical(labels, num_classes=CLASS_NUM)
  25. return data,labels

函數返回的是圖片和其對應的標籤。

訓練

  1. def train(aug,trainX,trainY,testX,testY,args):
  2. # initialize the model
  3. print("[INFO] compiling model...")
  4. model = LeNet.build(width=norm_size, height=norm_size, depth=3, classes=CLASS_NUM)
  5. opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
  6. model.compile(loss="categorical_crossentropy", optimizer=opt,
  7. metrics=["accuracy"])
  8. # train the network
  9. print("[INFO] training network...")
  10. H = model.fit_generator(aug.flow(trainX, trainY, batch_size=BS),
  11. validation_data=(testX, testY), steps_per_epoch=len(trainX) // BS,
  12. epochs=EPOCHS, verbose=1)
  13. # save the model to disk
  14. print("[INFO] serializing network...")
  15. model.save(args["model"])
  16. # plot the training loss and accuracy
  17. plt.style.use("ggplot")
  18. plt.figure()
  19. N = EPOCHS
  20. plt.plot(np.arange(0, N), H.history["loss"], label="train_loss")
  21. plt.plot(np.arange(0, N), H.history["val_loss"], label="val_loss")
  22. plt.plot(np.arange(0, N), H.history["acc"], label="train_acc")
  23. plt.plot(np.arange(0, N), H.history["val_acc"], label="val_acc")
  24. plt.title("Training Loss and Accuracy on traffic-sign classifier")
  25. plt.xlabel("Epoch #")
  26. plt.ylabel("Loss/Accuracy")
  27. plt.legend(loc="lower left")
  28. plt.savefig(args["plot"])

在這裏我們使用了Adam優化器,由於這個任務是一個多分類問題,可以使用類別交叉熵(categorical_crossentropy)。但如果執行的分類任務僅有兩類,那損失函數應更換爲二進制交叉熵損失函數(binary cross-entropy)

主函數

  1. #python train.py --dataset_train ../../traffic-sign/train --dataset_test ../../traffic-sign/test --model traffic_sign.model
  2. if name=='main':
  3. args = args_parse()
  4. train_file_path = args["dataset_train"]
  5. testfile_path = args["dataset_test"]
  6. trainX,trainY = load_data(train_file_path)
  7. testX,testY = load_data(test_file_path)
  8. # construct the image generator for data augmentation
  9. aug = ImageDataGenerator(rotation_range=30, widthshiftrange=0.1,
  10. heightshiftrange=0.1, shear_range=0.2, zoom_range=0.2,
  11. horizontal_flip=True, fill_mode="nearest")
  12. train(aug,trainX,trainY,testX,testY,args)

在正式訓練之前我們還使用了數據增廣技術(ImageDataGenerator)來對我們的小數據集進行數據增強(對數據集圖像進行隨機旋轉、移動、翻轉、剪切等),以加強模型的泛化能力。

訓練代碼已經寫好了,接下來開始訓練(圖片歸一化尺寸爲32,batch_size爲32,epoches爲35)。

python train.py --dataset_train ../../traffic-sign/train --dataset_test ../../traffic-sign/test --model traffic_sign.model

訓練過程:

Loss和Accuracy:

從訓練效果看來,準確率在94%左右,效果不錯了。

預測單張圖片

現在我們已經得到了我們訓練好的模型traffic_sign.model,然後我們編寫一個專門用於預測的腳本predict.py。

  1. # import the necessary packages
  2. from keras.preprocessing.image import img_to_array
  3. from keras.models import load_model
  4. import numpy as np
  5. import argparse
  6. import imutils
  7. import cv2
  8. norm_size = 32
  9. def args_parse():
  10. # construct the argument parse and parse the arguments
  11. ap = argparse.ArgumentParser()
  12. ap.add_argument("-m", "--model", required=True,
  13. help="path to trained model model")
  14. ap.add_argument("-i", "--image", required=True,
  15. help="path to input image")
  16. ap.add_argument("-s", "--show", action="store_true",
  17. help="show predict image",default=False)
  18. args = vars(ap.parse_args())
  19. return args
  20. def predict(args):
  21. # load the trained convolutional neural network
  22. print("[INFO] loading network...")
  23. model = load_model(args["model"])
  24. #load the image
  25. image = cv2.imread(args["image"])
  26. orig = image.copy()
  27. # pre-process the image for classification
  28. image = cv2.resize(image, (norm_size, norm_size))
  29. image = image.astype("float") / 255.0
  30. image = img_to_array(image)
  31. image = np.expand_dims(image, axis=0)
  32. # classify the input image
  33. result = model.predict(image)[0]
  34. #print (result.shape)
  35. proba = np.max(result)
  36. label = str(np.where(result==proba)[0])
  37. label = "{}: {:.2f}%".format(label, proba * 100)
  38. print(label)
  39. if args['show']:
  40. # draw the label on the image
  41. output = imutils.resize(orig, width=400)
  42. cv2.putText(output, label, (10, 25),cv2.FONT_HERSHEY_SIMPLEX,
  43. 0.7, (0, 255, 0), 2)
  44. # show the output image
  45. cv2.imshow("Output", output)
  46. cv2.waitKey(0)
  47. #python predict.py --model traffic_sign.model -i ../2.png -s
  48. if name == 'main':
  49. args = args_parse()
  50. predict(args)

預測腳本中的代碼編寫思路是:參數解析器-》載入訓練好的模型-》讀入圖片信息-》預測-》展示預測效果。值得注意的是,參數-s是用於可視化結果的,加上他的話我們就可以看出我們輸入的圖片以及模型預測的分類結果,很直觀。如果只需要得到分類結果,不加-s就可以了。

單張圖片的預測:

python predict.py --model traffic_sign.model -i ../2.png -s

至此,交通分類任務完成。

這裏分享一下這個項目的數據集來源:
你可以點擊這裏下載數據集。在下載頁面上面有很多的數據集,但是你只需要下載 BelgiumTS for Classification (cropped images) 目錄下面的兩個文件:

  • BelgiumTSC_Training (171.3MBytes)
  • BelgiumTSC_Testing (76.5MBytes)

值得注意的是,原始數據集的圖片格式是ppm,這是一種很老的圖片保存格式,很多的工具都已經不支持它了。這也就意味着,我們不能很方便的查看這些文件夾裏面的圖片。

爲了解決這個問題,我用opencv重新將這些圖片轉換爲png格式,這樣子我們就能很直觀地看到數據圖片了。

轉換腳本在這裏

同時我也把轉換好的數據集傳到百度雲了,不想自己親自轉換的童鞋可以自行獲取。

二、票據分類

先分析任務和觀察數據。我們這次的分類任務是票據分類,現在我們手頭上的票據種類一共有14種,我們的任務就是訓練一個模型將他們一一分類。先看看票據的圖像吧。

票據種類一共14種,其圖片名字就是其label。

票據是以下面所示的文件夾排布存儲的。

然後我們再看一下每類圖片數據的情況,看一下可利用的數據有多少。

有的票據數據比較少,也就十來張

有的票據比較多,有上百張

這樣的數據分佈直接拿去去訓練的話,效果可能不會太好(這就是不平衡問題),但是這是後期模型調優時才需要考慮的問題,現在先放一邊。那我們繼續使用上面的圖片分類框架完成本次的票據分類任務。

這次的數據集的存儲方式與交通標誌分類任務的數據存儲不太一樣,這個數據集沒有把數據分成train和test兩個文件夾,所以我們在代碼中讀取數據時寫的函數應作出相應修改:我們先讀取所有圖片,再借助sklearn的“train_test_split”函數將數據集以一定比例分爲訓練集和測試集。

我寫了個load_data2()函數來適應這種數據存儲。

  1. def load_data2(path):
  2. print("[INFO] loading images...")
  3. data = []
  4. labels = []
  5. # grab the image paths and randomly shuffle them
  6. imagePaths = sorted(list(paths.list_images(path)))
  7. random.seed(42)
  8. random.shuffle(imagePaths)
  9. # loop over the input images
  10. for imagePath in imagePaths:
  11. # load the image, pre-process it, and store it in the data list
  12. image = cv2.imread(imagePath)
  13. image = cv2.resize(image, (norm_size, norm_size))
  14. image = img_to_array(image)
  15. data.append(image)
  16. # extract the class label from the image path and update the
  17. # labels list
  18. label = int(imagePath.split(os.path.sep)[-2])
  19. labels.append(label)
  20. # scale the raw pixel intensities to the range [0, 1]
  21. data = np.array(data, dtype="float") / 255.0
  22. labels = np.array(labels)
  23. # partition the data into training and testing splits using 75% of
  24. # the data for training and the remaining 25% for testing
  25. (trainX, testX, trainY, testY) = train_test_split(data,
  26. labels, test_size=0.25, random_state=42)
  27. # convert the labels from integers to vectors
  28. trainY = to_categorical(trainY, num_classes=CLASS_NUM)
  29. testY = to_categorical(testY, num_classes=CLASS_NUM)
  30. return trainX,trainY,testX,testY

我們使用了sklearn中的神器train_test_split做了數據集的切分,非常方便。可以看出,load_data2()的返回值就是訓練集圖片和標註+測試集圖片和標註。

在主函數也只需做些許修改就可以完成本次票據分類任務。

  1. if name=='main':
  2. args = args_parse()
  3. file_path = args["dataset"]
  4. trainX,trainY,testX,testY = load_data2(file_path)
  5. # construct the image generator for data augmentation
  6. aug = ImageDataGenerator(rotation_range=30, widthshiftrange=0.1,
  7. heightshift_range=0.1, shear_range=0.2, zoom_range=0.2,
  8. horizontal_flip=True, fill_mode="nearest")
  9. train(aug,trainX,trainY,testX,testY,args)

然後設定一些參數,比如圖片歸一化尺寸爲64*64,訓練35個epoches。設定完參數後我們開始訓練。

python train.py --dataset ../../invoice_all/train  --model invoice.model

訓練的過程不算久,大概十來分鐘。訓練過程如下:

繪製出Loss和Accuracy曲線,可以看出,我們訓練後的模型的準確率可以達到97%。直接使用一個LeNet網絡就可以跑出這個準確率還是讓人很開心的。

最後再用訓練好的模型預測單張票據,看看效果:

預測正確,deep learning 票據分類任務完成!

三、總結

我們使用了Keras搭建了一個基於LeNet的圖片分類器的通用框架,並用它成功完成兩個實際的分類任務。最後再說說我們現有的模型的一些改進的地方吧。第一,圖片歸一化的尺寸是否合適?比如票據分類任務中,圖片歸一化爲64,可能這個尺寸有點小,如果把尺寸改爲128或256,效果可能會更好;第二,可以考慮更深的網絡,比如VGG,GoogLeNet等;第三,數據增強部分還可以再做一做。

完整代碼和測試圖片可以在我的github上獲取。

參考資料:

https://www.pyimagesearch.com/2017/12/11/image-classification-with-keras-and-deep-learning/

http://www.jianshu.com/p/3c7f329b29ee



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章