深度学习入门与实践(一)
lyq1996

前言

最近申请了一个实践项目,主要做图像分类识别的,虽说不知道能不能立项吧(已立项),但是既然已经决定往软件方面转了,所以有必要先搭建一下环境。对比了一下现有的深度学习框架,决定先安装谷歌的TensorFlow,然后再安装keras。

来源

https://tensorflow.google.cn/install/

我的环境

  • 依旧是Windows 10子系统 Ubuntu 18.04 稍加思索,考虑到CUDA,故放弃了,使用Win10
  • Python 3.6

本文目的:

  1. 安装CUDA驱动程序和cuDNN,确保GPU能够运行深度学习代码
  2. 安装一个Keras后端:TensorFlow
  3. 安装Keras

anaconda安装TensorFlow-gpu

  1. 创建一个3.6的环境:
    conda create --name tfenv python=3.6
  2. 激活这个环境:
    activate tfenv
  3. 安装TensorFlow-gpu
    pip install --ignore-installed --upgrade tensorflow-gpu -i https://pypi.tuna.tsinghua.edu.cn/simple

安装CUDA和cuDNN

CUDA下载地址: https://developer.nvidia.com/cuda-toolkit-archive
cuDNN下载地址: https://developer.nvidia.com/rdp/cudnn-archive

我安装的是CUDA 10.0和cuDNN 7.4.1.5,cuDNN要放到任意文件夹,但环境变量的路径要添加。

安装keras

pip install --ignore-installed --upgrade keras -i https://pypi.tuna.tsinghua.edu.cn/simple

问题解决

当然,这鬼东西哪有那么容易就装上了呢?

问题1:

照着上面的步骤安装之后,import tensorflow提示DLL读取失败,搜了之后装了VS 2017 C++什么的库,并重启、不管用。搜了一下说是官方还未支持CUDA 10,需要安装别人自己编译的。

于是在这里:https://blog.csdn.net/qq_41895190/article/details/83892692 安装了他提供的TensorFlow 1.12。

问题2:

装了之后,依旧尼玛报错,这次提示ImportError: cannot import name 'abs',而且与kares有关,搜了一下,卸载Tensooflow-gpu和protobuf,不管用。

于是在这里:https://stackoverflow.com/questions/51299194/importerror-cannot-import-name-abs 找到了解决办法,卸载Tensooflow-gpu和protobuf,还要删除site-packages里面的TensorFlow文件夹,再次安装,解决。

测试TensorFlow

输出hello程序:

1
2
3
4
import tensorflow as tf
hello = tf.constant('hello,tf')
sess = tf.Session()
print(sess.run(hello))

输出:
image

测试GPU是否开启程序:

1
2
3
4
5
6
import tensorflow as tf
a = tf.test.is_built_with_cuda()
b = tf.test.is_gpu_available(cuda_only=False, min_cuda_compute_capability=None)

print(a)
print(b)

输出:
image

计算一个行列式相乘:

1
2
3
4
5
6
import tensorflow as tf
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
print(sess.run(c))

输出:

1
2
3
4
[
[22. 28.]
[49. 64.]
]

测试Keras

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K

batch_size = 128
num_classes = 10
epochs = 12

# input image dimensions
img_rows, img_cols = 28, 28

# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()

if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])

model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Using TensorFlow backend.
x_train shape: (60000, 28, 28, 1)
60000 train samples
10000 test samples
Train on 60000 samples, validate on 10000 samples
Epoch 1/12
2019-02-18 21:52:00.902085: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1432] Found device 0 with properties:
name: GeForce GTX 1060 major: 6 minor: 1 memoryClockRate(GHz): 1.6705
pciBusID: 0000:01:00.0
totalMemory: 3.00GiB freeMemory: 2.42GiB
2019-02-18 21:52:00.912455: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1511] Adding visible gpu devices: 0
2019-02-18 21:52:02.081765: I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-02-18 21:52:02.087401: I tensorflow/core/common_runtime/gpu/gpu_device.cc:988] 0
2019-02-18 21:52:02.091543: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1001] 0: N
2019-02-18 21:52:02.094894: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 2119 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1060, pci bus id: 0000:01:00.0, compute capability: 6.1)60000/60000 [==============================] - 12s 195us/step - loss: 0.2718 - acc: 0.9172 - val_loss: 0.0597 - val_acc: 0.9803
Epoch 2/12
60000/60000 [==============================] - 7s 116us/step - loss: 0.0929 - acc: 0.9720 - val_loss: 0.0406 - val_acc: 0.9860
Epoch 3/12
60000/60000 [==============================] - 7s 116us/step - loss: 0.0660 - acc: 0.9801 - val_loss: 0.0402 - val_acc: 0.9868
Epoch 4/12
60000/60000 [==============================] - 7s 116us/step - loss: 0.0558 - acc: 0.9832 - val_loss: 0.0334 - val_acc: 0.9889
Epoch 5/12
60000/60000 [==============================] - 7s 120us/step - loss: 0.0486 - acc: 0.9855 - val_loss: 0.0300 - val_acc: 0.9908
Epoch 6/12
60000/60000 [==============================] - 7s 117us/step - loss: 0.0426 - acc: 0.9869 - val_loss: 0.0281 - val_acc: 0.9905
Epoch 7/12
60000/60000 [==============================] - 7s 117us/step - loss: 0.0371 - acc: 0.9889 - val_loss: 0.0300 - val_acc: 0.9901
Epoch 8/12
60000/60000 [==============================] - 7s 118us/step - loss: 0.0362 - acc: 0.9891 - val_loss: 0.0301 - val_acc: 0.9906
Epoch 9/12
60000/60000 [==============================] - 7s 116us/step - loss: 0.0327 - acc: 0.9896 - val_loss: 0.0267 - val_acc: 0.9918
Epoch 10/12
60000/60000 [==============================] - 7s 116us/step - loss: 0.0299 - acc: 0.9909 - val_loss: 0.0267 - val_acc: 0.9914
Epoch 11/12
60000/60000 [==============================] - 7s 117us/step - loss: 0.0278 - acc: 0.9913 - val_loss: 0.0289 - val_acc: 0.9919
Epoch 12/12
60000/60000 [==============================] - 7s 116us/step - loss: 0.0267 - acc: 0.9922 - val_loss: 0.0253 - val_acc: 0.9928
Test loss: 0.025275636998070422
Test accuracy: 0.9928

结束语

安装挺麻烦的。。至于上面mnist手写数据识别是怎么实现的,我也不知道,慢慢学吧。

 评论
评论插件加载失败
正在加载评论插件
由 Hexo 驱动 & 主题 Keep
本站由 提供部署服务
访客数 访问量