tensorflow2.0之动手学机器视觉--1.安装及初步认识

1.安装必要的依赖

tensorflow2.0 目前支持 ubuntu16.04及以上,macOS10.12.6及以上,Windows7及以上,Raspbian9.0及以上。我们接下来的实验环境是ubuntu16.04,显卡是Nvidia P100.
我们从官网可以看到,tensorflow2.0需要pip的版本>19.0.这里我们使用pip --version查看,如下图所示。如果pip版本不够,可以用pip install --upgrade pip进行升级。
pip版本查看
接下来就是GPU的支持,这里我们可以在官网查到tensorflow2.0的GPU库支持,如下图:
在这里插入图片描述
安装CUDA10之前,首先查看本机gcc版本,这里我们使用命令gcc --version.如下图:
在这里插入图片描述
这里,我们的GCC版本为5.4,因此不需要进行升级或者降级。我们直接在从官网上选择安装方式:NVidia Cuda下载,这里选择runfile,不需要复杂的安装命令和后续的一些补充包的安装,直接一步到位。如下图所示:
在这里插入图片描述
在安装完CUDA后,还需要安装配套的cudnn,这个在这里CUDNN下载。下载解压后,将对应的文件复制到cuda安装包中,这个可以参考其他专门介绍的文章。
在安装完pip,cuda10.0,cudnn7.6.4后就可以进行tensorflow2.0的安装了。

2.安装tensorflow2.0

在官网中,我们可以看到,tensorflow的安装有三种形式,一种是pip包管理,一种是docker,最后一种是从源代码编译。这里我们采用的是pip的包管理模式,如下图所示。在这里插入图片描述

经过等待,终于安装完成,我们这个时候进去python终端进行测试,首先是导入tensorflow包,可以看到没有任何出错,然后查看版本号tf.__version__,然后是测试GPU的使用,输入tf.test.is_gpu_avaliable().当出现下图的样式时,说明一切都安装完成了,就可以开始之后的深度学习之旅了。在这里插入图片描述

3.小试牛刀

这里我们利用jupyter notebook 进行代码的练习,之后的也同样会使用notebook进行展示。
我们实现了一个简单的分类模型用来展示我们一般如何进行深度网络的构建。我们将整个过程分为以下几步:

  • 导入必要的包
#1.import software pack
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import Dense,Conv2D,Flatten
import numpy as np
import matplotlib.pyplot as plt
  • 加载数据
#2.load dataset and check shape
(x_train,y_train),(x_test,y_test)  = keras.datasets.mnist.load_data()
print(x_train.shape)
print(x_test.shape)
print(y_train.shape)
print(y_test.shape)
x_train = x_train.reshape((-1,28,28,1))/255.0
x_test = x_test.reshape((-1,28,28,1))/255.0
print(x_train.shape)
print(x_test.shape)
print(y_train.shape)
print(y_test.shape)
  • 数据预处理及可视化
#3.present some image
x = np.arange(0,3*np.pi,0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
plt.subplot(1,2,1)
plt.imshow(x_train[0].reshape((28,28)))
plt.title('x_train')
plt.colorbar()
plt.grid(False)
plt.subplot(1,2,2)
plt.imshow(x_test[0].reshape((28,28)))
plt.title('x_test')
plt.colorbar()
plt.grid(False)
plt.show()

  • 模型搭建
#build model
cls_model = keras.models.Sequential([
                                    Conv2D(4,3, padding='same',activation='relu',input_shape=(28,28,1)),
                                    Flatten(),
                                    Dense(64,activation='relu'),
                                    Dense(10,activation='softmax')
                                    ])

cls_model.compile(optimizer='adam',
                 loss = 'sparse_categorical_crossentropy',
                metrics=['accuracy'])
cls_model.summary()
  • 模型训练及可视化训练过程
# train the model 
history = cls_model.fit(x_train,y_train,batch_size=20,epochs=20,validation_split=0.2)
# show each epoch train result 
plt.subplot(2,1,1)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.xlabel('epoch')
plt.ylabel('loss')
plt.legend(['train', 'test'], loc='upper left')
plt.subplot(2,1,2)
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.xlabel('epoch')
plt.ylabel('acc')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
  • 模型评估
#evaluate the model
eval_loss,eval_acc  = cls_model.evaluate(x_test,y_test,verbose = 2)
print("eval loss is {},eval acc is {}".format(eval_loss,eval_acc))
  • 模型预测及可视化
#predict new data and show the result
predict_result = cls_model.predict(x_test)
plt.xlabel('predict result is {}'.format(np.argmax(predict_result[0])))
plt.imshow(x_test[0].reshape((28,28)))

完整的jupyter 代码和结果,请转至我的github 链接地址下查看.

参考资源

  1. tensorflow官网
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值