tensorflow 2.1.0 安裝與實(shí)戰(zhàn)教程(CASIA FACE v5)
1.0tensorflow的安裝
1.1安裝python
python下載 需要python3.x<=3.7
https://www.python.org/ftp/python/3.7.7/python-3.7.7-amd64.exe
安裝時(shí)勾選Add Python 3.7 to PATH,把python添加到環(huán)境變量。
1.2安裝tensorflow
打開命令行,執(zhí)行
pip install tensorflow==2.1.0
pip 會(huì)安裝tensorflow和一些其他的依賴
1.3安裝vc++2015-2019redist…
tensorflow的另一個(gè)依賴(很多tensorflow安裝失敗的原因就是這個(gè)沒安裝)
https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads
1.4安裝CUDA和CUDNN
cuda: https://developer.nvidia.com/cuda-downloads?target_os=Windows&target_arch=x86_64&target_version=10&target_type=exelocal
cudnn: https://developer.nvidia.com/rdp/cudnn-download(需要注冊(cè)nvidia賬號(hào))
cudnn下載后是個(gè)壓縮文件,要把他解壓出來(lái)放在CUDA里,如下圖
高版本CUDA缺失cudart64_101.dll,下載后放在C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\bin里
https://cn.dll-files.com/cudart64_101.dll.html
2.0CASIA實(shí)戰(zhàn)
2.1CASIA數(shù)據(jù)集
可以從網(wǎng)上下載casia數(shù)據(jù)集,
這里以casia數(shù)據(jù)集為例,現(xiàn)實(shí)中可以使用自己需要的數(shù)據(jù)集。
2.2數(shù)據(jù)集的處理
建立data和test兩個(gè)文件夾,把casia復(fù)制到里面
目錄是這樣的./data/000/000_0.bmp
data.py處理數(shù)據(jù),其實(shí)就是遍歷,匹配,刪除
import os data = './data' dirs = os.listdir(data) for dir in dirs: for file in os.listdir(data + '/' + dir): if file.endswith("4.bmp"): os.remove(data + '/' + dir + '/' + file) test = './test' tdirs = os.listdir(test) for dir in tdirs: for file in os.listdir(test + '/' + dir): if file.endswith("0.bmp"): os.remove(test + '/' + dir + '/' + file) if file.endswith("1.bmp"): os.remove(test + '/' + dir + '/' + file) if file.endswith("2.bmp"): os.remove(test + '/' + dir + '/' + file) if file.endswith("3.bmp"): os.remove(test + '/' + dir + '/' + file)
2.3訓(xùn)練代碼
casia.py
import os import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D from tensorflow.keras.preprocessing.image import ImageDataGenerator import numpy as np /*我直接建立了個(gè)0000,1111,...這樣的數(shù)組作為標(biāo)簽*/ #data標(biāo)簽 arr = [] for i in range(100): for j in range(4): arr.append(i) arr = np.array(arr) #test標(biāo)簽 tarr = [] for i in range(100): tarr.append(i) tarr = np.array(tarr) #訓(xùn)練集 pwd='./data' dirs = os.listdir(pwd) imgs = [] for dir in dirs: for file in os.listdir(pwd + '/' + dir): image = tf.io.read_file(pwd + '/' + dir + '/' + file) img = tf.image.decode_bmp(image,channels=3) imgs.append(img) print("[*]訓(xùn)練集加載完畢") print(imgs[0].shape) #驗(yàn)證集(測(cè)試集) tpwd='./test' tdirs = os.listdir(tpwd) timgs = [] for tdir in tdirs: for tfile in os.listdir(tpwd + '/' + tdir): timage = tf.io.read_file(tpwd + '/' + tdir + '/' + tfile) timg = tf.image.decode_bmp(timage,channels=3) timgs.append(timg) print("[*]驗(yàn)證集加載完畢") print(timgs[0].shape) #神經(jīng)網(wǎng)絡(luò)模型 model = Sequential([ Conv2D(16, (3,3), padding='same', activation='relu',input_shape=(480,640,3)), MaxPooling2D(), Conv2D(64, (3,3), padding='same', activation='relu'), MaxPooling2D(), Conv2D(128, (3,3), padding='same', activation='relu'), MaxPooling2D(), Flatten(), Dense(128, activation='relu'), Dense(100, activation='softmax'), ]) model.summary()//打印神經(jīng)網(wǎng)絡(luò)模型 #優(yōu)化器 model.compile(optimizer=tf.keras.optimizers.Adam(), loss='sparse_categorical_crossentropy', metrics=['accuracy']) #訓(xùn)練 ds = tf.data.Dataset.from_tensor_slices((imgs,arr)) ds = ds.batch(16) ds = ds.prefetch(buffer_size=tf.data.experimental.AUTOTUNE) model.fit(ds,epochs=20) tds = tf.data.Dataset.from_tensor_slices((timgs,tarr)) tds = ds.prefetch(buffer_size=tf.data.experimental.AUTOTUNE) model.evaluate(tds, verbose=2) #保存 tf.saved_model.save(model, "./tmp/")
2.4訓(xùn)練與驗(yàn)證
在命令行運(yùn)行 python casia.py進(jìn)行訓(xùn)練
predict.py
import os import tensorflow as tf import numpy as np /*這里顯卡內(nèi)存不夠了*/ from tensorflow.compat.v1 import ConfigProto from tensorflow.compat.v1 import InteractiveSession config = ConfigProto() config.gpu_options.allow_growth = True session = InteractiveSession(config=config) /*顯卡內(nèi)存*/ model_path = './tmp' //加載模型 test_path = "./test/002/002_4.bmp"http://這里就是個(gè)栗子 model = tf.keras.models.load_model(model_path, custom_objects=None, compile=True) image = tf.io.read_file(test_path) img = tf.image.decode_bmp(image,channels=3) img = img[tf.newaxis, ...] res = model.predict( img, batch_size=None, verbose=0, steps=None, callbacks=None, max_queue_size=10, workers=1, use_multiprocessing=False ) pred = tf.argmax(res, axis=1) print (pred[0]) print (res[0,pred[0]])
總結(jié)
到此這篇關(guān)于tensorflow 2.1.0 安裝與實(shí)戰(zhàn)(CASIA FACE v5)的文章就介紹到這了,更多相關(guān)tensorflow 2.1.0 安裝內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
帶你徹底搞懂python操作mysql數(shù)據(jù)庫(kù)(cursor游標(biāo)講解)
這篇文章主要介紹了帶你徹底搞懂python操作mysql數(shù)據(jù)庫(kù)(cursor游標(biāo)講解),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01Python無(wú)頭爬蟲下載文件的實(shí)現(xiàn)
這篇文章主要介紹了Python無(wú)頭爬蟲下載文件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04python matplotlib工具欄源碼探析二之添加、刪除內(nèi)置工具項(xiàng)的案例
這篇文章主要介紹了python matplotlib工具欄源碼探析二之添加、刪除內(nèi)置工具項(xiàng)的案例,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02Python快速實(shí)現(xiàn)分列轉(zhuǎn)到行的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Python快速實(shí)現(xiàn)分列轉(zhuǎn)到行的效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)一下2023-03-03python的launcher用法知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于python的launcher用法知識(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-08-08Python3.9.0 a1安裝pygame出錯(cuò)解決全過(guò)程(小結(jié))
這篇文章主要介紹了Python3.9.0 a1安裝pygame出錯(cuò)解決全過(guò)程(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Python 按比例獲取樣本數(shù)據(jù)或執(zhí)行任務(wù)的實(shí)現(xiàn)代碼
這篇文章主要介紹了Python 按比例獲取樣本數(shù)據(jù)或執(zhí)行任務(wù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12