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 安装内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Numpy数组array和矩阵matrix转换方法

    Numpy数组array和矩阵matrix转换方法

    这篇文章主要介绍了Numpy数组array和矩阵matrix转换方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08
  • Python中is与==的使用区别详解

    Python中is与==的使用区别详解

    这篇文章小编主要给大家讲解的是Python中is与==的使用区别的相关资料,需要的下伙伴可以参考下面文章内容的具体详细资料
    2021-09-09
  • python管理包路径之pycharm自动解决包路径注册

    python管理包路径之pycharm自动解决包路径注册

    这篇文章主要介绍了python本管理包路径之pycharm自动解决包路径注册,文章通过围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-09-09
  • 浅析Python数字类型和字符串类型的内置方法

    浅析Python数字类型和字符串类型的内置方法

    这篇文章主要介绍了Python数字类型和字符串类型的内置方法,本文通过实例代码讲解的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-12-12
  • python中不能连接超时的问题及解决方法

    python中不能连接超时的问题及解决方法

    这篇文章主要介绍了python中不能连接超时的问题及解决方法,需要的朋友可以参考下
    2018-06-06
  • python的tkinter、socket库开发tcp的客户端和服务端详解

    python的tkinter、socket库开发tcp的客户端和服务端详解

    本文介绍了TCP通讯流程和开发步骤,包括客户端和服务端的实现,客户端使用Python的tkinter库实现图形化界面,服务端使用socket库监听连接并处理消息,文章还提供了客户端和服务端的代码示例
    2025-01-01
  • python tkinterEntry组件设置默认值方式

    python tkinterEntry组件设置默认值方式

    使用Tkinter库中的Entry组件创建文本输入框时,可以通过insert方法在指定位置插入默认文本作为提示,结合使用focus和focusin事件,可以实现用户点击时清除默认文本,以便输入自定义内容
    2024-09-09
  • 关于python 的legend图例,参数使用说明

    关于python 的legend图例,参数使用说明

    这篇文章主要介绍了关于python 的legend图例,参数使用说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • NumPy统计函数的实现方法

    NumPy统计函数的实现方法

    这篇文章主要介绍了NumPy统计函数的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01
  • Python实现的基数排序算法原理与用法实例分析

    Python实现的基数排序算法原理与用法实例分析

    这篇文章主要介绍了Python实现的基数排序算法,简单说明了基数排序的原理并结合实例形式分析了Python实现与使用基数排序的具体操作技巧,需要的朋友可以参考下
    2017-11-11

最新评论