脚本专栏 发布日期:2024/10/21 浏览次数:1
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
安装时勾选Add Python 3.7 to PATH,把python添加到环境变量。
1.2安装tensorflow
打开命令行,执行
pip install tensorflow==2.1.0
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"text-align: center">
高版本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数据集为例,现实中可以使用自己需要的数据集。
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]])
总结