python學習—圖像識別
這是我從零基礎開始學習的圖像識別,固然用的是容易上手的python來寫,持續更新中,記錄我學習python基礎到圖像識別應用的一步步過程和踩過的一些坑。最終實現獲得本身的訓練模型(h5或者pb模型),可隨意更改須要識別的物品,只要有數據就行。(如有錯誤或問題,肯請指正)python
安裝編譯環境
此前確保已經安裝並配置好了Python環境,在此我選擇了比較流行的pycharm,具體安裝教程網上不少,也比較簡單。數組
安裝所需庫
我是利用了anaconda命令安裝的,本項目所需用的庫爲:
keras、numpy、tensorflow2.0(個人是GPU版本),
GPU版本速度快但安裝起來比較麻煩。
網絡
導包
import os from PIL import Image import numpy as np import matplotlib.pyplot as plt import tensorflow as tf from tensorflow.keras import datasets, layers, models
1、接下來就是處理你的圖片數據集
在這裏我只提供了須要的函數,若果是本身的數據學要修改其中的變量,包括圖片路徑、傳入參數等。架構
1.轉換圖片像素,使其大小一致
def read_image(paths): os.listdir(paths) filelist = [] for root, dirs, files in os.walk(paths): for file in files: if os.path.splitext(file)[1] == ".jpg": filelist.append(os.path.join(root, file)) return filelist def im_xiangsu(paths): for filename in paths: try: im = Image.open(filename) newim = im.resize((128, 128)) newim.save('F:/CNN/test/' + filename[12:-4] + '.jpg') print('圖片' + filename[12:-4] + '.jpg' + '像素轉化完成') except OSError as e: print(e.args)
2.圖片數據轉化爲數組
def im_array(paths): M=[] for filename in paths: im=Image.open(filename) im_L=im.convert("L") #模式L Core=im_L.getdata() arr1=np.array(Core,dtype='float32')/255.0 list_img=arr1.tolist() M.extend(list_img) return M
3.準備訓練數據
dict_label={0:'汽車',1:'飲料瓶'} train_images=np.array(M).reshape(len(filelist_all),128,128) label=[0]*len(filelist_1)+[1]*len(filelist_2) train_lables=np.array(label) #數據標籤 train_images = train_images[ ..., np.newaxis ] #數據圖片 print(train_images.shape)#輸出驗證一下(400, 128, 128, 1)
4.構建神經網絡並保存
model = models.Sequential() model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(128, 128, 1))) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activation='relu')) model.add(layers.Flatten()) model.add(layers.Dense(64, activation='relu')) model.add(layers.Dense(2, activation='softmax'))#注意這裏參數,我只有兩類圖片,因此是2. model.summary() # 顯示模型的架構 model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) #epochs爲訓練多少輪、batch_size爲每次訓練多少個樣本 model.fit(train_images, train_lables, epochs=5) model.save('my_model.h5') #保存爲h5模型 #tf.keras.models.save_model(model,"F:\python\moxing\model")#這樣是pb模型 print("模型保存成功!")
看一下準確度,還能夠,但因爲數據集太少,有可能會出現過擬合狀況。
app
2、用上面獲得的模型預測隨便一張圖片
新建一個py,直接放完整代碼函數
import os from PIL import Image import numpy as np import tensorflow as tf #導入圖像數據 #測試外部圖片 model= tf.keras.models.load_model('my_model.h5') model.summary() #看一下網絡結構 print("模型加載完成!") dict_label={0:'汽車',1:'飲料瓶'} def read_image(paths): os.listdir(paths) filelist = [] for root, dirs, files in os.walk(paths): for file in files: if os.path.splitext(file)[1] == ".jpg": filelist.append(os.path.join(root, file)) return filelist def im_xiangsu(paths): for filename in paths: try: im = Image.open(filename) newim = im.resize((128, 128)) newim.save('F:/CNN/test/' + filename[12:-4] + '.jpg') print('圖片' + filename[12:-4] + '.jpg' + '像素轉化完成') except OSError as e: print(e.args) def im_array(paths): im = Image.open(paths[0]) im_L = im.convert("L") # 模式L Core = im_L.getdata() arr1 = np.array(Core, dtype='float32') / 255.0 list_img = arr1.tolist() images = np.array(list_img).reshape(-1,128, 128,1) return images test='F:/CNN/test/' #你要測試的圖片的路徑 filelist=read_image(test) im_xiangsu(filelist) img=im_array(filelist) #預測圖像 predictions_single=model.predict(img) print("預測結果爲:",dict_label[np.argmax(predictions_single)]) #這裏返回數組中機率最大的那個 print(predictions_single)
最後結果
數組內的兩個值分別表示爲汽車和瓶子的機率大小。
學習
3、總結
因爲剩餘時間有限,本項目用了兩類圖片汽車和瓶子進行訓練預測,每類圖片200張,共400張,因此頗有可能出現過擬合,但增長數據集會在處理圖片時耗費大量時間,因此咱們儘可能作個折中。一類幾千張差很少就行。
圖片數據不夠的話能夠擴充。
步驟:
一、調用上述函數,處理圖片,我是把的全部圖片的像素大小改爲了128*128,
對應input_shape=(128, 128, 1)。
二、圖片數據轉成數組。
三、準備訓練數據(train_images, train_lables)。
四、構建神經網絡並保存模型
測試
最後附一張我調用函數的流程:
spa