tensorflow 2.1.0 安装与实战教程(CASIA FACE v5)

 更新时间:2020年06月30日 10:21:28   作者:博二兔  
这篇文章主要介绍了tensorflow 2.1.0 安装与实战(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

python安装

安装时勾选Add Python 3.7 to PATH,把python添加到环境变量。

1.2安装tensorflow

打开命令行,执行

pip install tensorflow==2.1.0

pip install tensorflow==2.1.0
pip install tensorflow 2

pip 会安装tensorflow和一些其他的依赖

1.3安装vc++2015-2019redist…

tensorflow的另一个依赖(很多tensorflow安装失败的原因就是这个没安装)
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(需要注册nvidia账号)
cudnn下载后是个压缩文件,要把他解压出来放在CUDA里,如下图

cudnn
dll缺失

高版本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实战

2.1CASIA数据集

casia

可以从网上下载casia数据集,
这里以casia数据集为例,现实中可以使用自己需要的数据集。

2.2数据集的处理

建立data和test两个文件夹,把casia复制到里面
目录是这样的./data/000/000_0.bmp
data.py处理数据,其实就是遍历,匹配,删除

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训练代码

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
/*我直接建立了个0000,1111,...这样的数组作为标签*/
#data标签
arr = []
for i in range(100):
 for j in range(4):
  arr.append(i)
arr = np.array(arr)
#test标签
tarr = []
for i in range(100):
 tarr.append(i)
tarr = np.array(tarr)

#训练集
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("[*]训练集加载完毕")
print(imgs[0].shape)
#验证集(测试集)
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("[*]验证集加载完毕")
print(timgs[0].shape)
#神经网络模型
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()//打印神经网络模型
#优化器
model.compile(optimizer=tf.keras.optimizers.Adam(),
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy'])
#训练
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训练与验证

在命令行运行 python casia.py进行训练
predict.py

import os
import tensorflow as tf
import numpy as np
/*这里显卡内存不够了*/
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession

config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)
/*显卡内存*/

model_path = './tmp' //加载模型
test_path = "./test/002/002_4.bmp"//这里就是个栗子
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]])

总结

到此这篇关于tensorflow 2.1.0 安装与实战(CASIA FACE v5)的文章就介绍到这了,更多相关tensorflow 2.1.0 安装内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • python实现网络五子棋

    python实现网络五子棋

    这篇文章主要为大家详细介绍了python实现网络五子棋,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-04-04
  • python 实现多线程下载m3u8格式视频并使用fmmpeg合并

    python 实现多线程下载m3u8格式视频并使用fmmpeg合并

    这篇文章主要介绍了python 实现多线程下载m3u8格式视频,使用fmmpeg合并的实例代码,需要的朋友可以参考下
    2019-11-11
  • python创建学生管理系统

    python创建学生管理系统

    这篇文章主要为大家详细介绍了python创建学生管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • python Tkinter版学生管理系统

    python Tkinter版学生管理系统

    这篇文章主要为大家详细介绍了python Tkinter版学生管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • Python 中包/模块的 `import` 操作代码

    Python 中包/模块的 `import` 操作代码

    这篇文章主要介绍了Python 中包/模块的 `import` 操作代码,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
    2019-04-04
  • Python httpx库入门指南(最新推荐)

    Python httpx库入门指南(最新推荐)

    Httpx 是一个用于发送 HTTP 请求的 Python 库,它提供了简单易用的 API,可以轻松地发送 GET、POST、PUT、DELETE 等请求,并接收响应,下面介绍下Python httpx库入门指南,感兴趣的朋友一起看看吧
    2023-12-12
  • Python3正则匹配re.split,re.finditer及re.findall函数用法详解

    Python3正则匹配re.split,re.finditer及re.findall函数用法详解

    这篇文章主要介绍了Python3正则匹配re.split,re.finditer及re.findall函数用法,结合实例形式详细分析了正则匹配re.split,re.finditer及re.findall函数的概念、参数、用法及操作注意事项,需要的朋友可以参考下
    2018-06-06
  • pandas DataFrame 警告(SettingWithCopyWarning)的解决

    pandas DataFrame 警告(SettingWithCopyWarning)的解决

    这篇文章主要介绍了pandas DataFrame 警告(SettingWithCopyWarning)的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • python创建与遍历List二维列表的方法

    python创建与遍历List二维列表的方法

    这篇文章主要介绍了python创建与遍历List二维列表的方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值 ,需要的朋友可以参考下
    2019-08-08
  • flask框架使用orm连接数据库的方法示例

    flask框架使用orm连接数据库的方法示例

    这篇文章主要介绍了flask框架使用orm连接数据库的方法,结合实例形式分析了flask框架使用flask_sqlalchemy包进行mysql数据库连接操作的具体步骤与相关实现技巧,需要的朋友可以参考下
    2018-07-07

最新评论