cover_image

如何使用GPU运行TensorFlow(Win10)

冯小龙 Garden001
2019年12月27日 05:22

如何使用GPU运行TensorFlow

这里主要考虑如何让tensorflow和keras运行在GPU上:

1. 检查显卡类型和计算能力

查看笔记本显卡型号,以及计算能力下载个 GPU 查看器,名字为TechPowerUp GPU-Z 下载地址是: https://www.techpowerup.com/download/gpu-z/我的电脑显示是这样的:图片我笔记本独立显卡产品型号是NVIDA GeForce MX250,但是核心型号是GP108。

图片
image.png

确定对应显卡 GPU 的计算能力去 NVIDIA 官网查看 https://developer.nvidia.com/cuda-gpus不过我没有查到计算能力,只看到了相关产品参数[https://www.geforce.com/hardware/notebook-gpus/geforce-mx250/features](https://www.geforce.com/hardware/notebook-gpus/geforce-mx250/features)

2. 安装CUDA

下载地址:https://developer.nvidia.com/cuda-downloads安装包有点大,下载慢,需要耐心等待。安装 cuda 的时候,会询问是否安装显卡驱动,说明 cuda 安装程序里包含了的显卡驱动;建议先不要安装 cuda 里的显卡驱动,待安装完 cuda 后,执行例子程序,如果报错再检查显卡驱动是否正确,避免覆盖原来的显卡驱动。

安装完后执行 nvcc -V 检查图片

然后运行例子:例子在C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\extras\demo_suite/deviceQuery.exe

图片
image.png

至此已经安装 cuda 成功

3. 安装cuDNN

cuDNN 是一个为了优化深度学习计算的类库,它能将模型训练的计算优化之后,再通过 CUDA 调用 GPU 进行运算,当然你也可直接使用 GUDA,而不通过 cuDNN ,但运算效率会低好多

cuDNN 下载地址:https://developer.nvidia.com/cudnn下载过程会有一堆调查问卷,友好度不好!选择跟CUDA对应的版本 cuDNN 将文件解压,例如解压到D:\software\cuda
解压后有三个子目录:bin,include,lib。将bin目录(例如 D:\software\cuda\bin)添加到环境变量 PATH 中。或者将三个文件夹的内容拷贝到CUDA对应的目录即可。

图片
image.png

4. 重新安装tensorflow

之前安装的tensorflow这样安装的pip install tensorflow==1.13.0,现在我换成了pip install tensorflow-gpu==1.15.0.

5. 测试代码

最后对GPU进行一下测试,使用如下代码:

#导入相关的库import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_dataimport osimport timefrom tensorflow.contrib.tensorboard.plugins import projectorimport matplotlib.pyplot as pltimport numpy as np#这里用slim这个API来进行卷积网络构建slim = tf.contrib.slim
#定义卷积神经网络模型#网络架构是卷积网络--最大池化--卷积网络--最大池化---flatten---MLP-softmax的全连接MLPdef model(inputs, is_training, dropout_rate, num_classes, scope='Net'): inputs = tf.reshape(inputs, [-1, 28, 28, 1]) with tf.variable_scope(scope): with slim.arg_scope([slim.conv2d, slim.fully_connected], normalizer_fn=slim.batch_norm): net = slim.conv2d(inputs, 32, [5, 5], padding='SAME', scope='conv1') net = slim.max_pool2d(net, 2, stride=2, scope='maxpool1') tf.summary.histogram("conv1", net)
net = slim.conv2d(net, 64, [5, 5], padding='SAME', scope='conv2') net = slim.max_pool2d(net, 2, stride=2, scope='maxpool2') tf.summary.histogram("conv2", net)
net = slim.flatten(net, scope='flatten') fc1 = slim.fully_connected(net, 1024, scope='fc1') tf.summary.histogram("fc1", fc1)
net = slim.dropout(fc1, dropout_rate, is_training=is_training, scope='fc1-dropout') net = slim.fully_connected(net, num_classes, scope='fc2')
return net, fc1

def create_sprite_image(images): """更改图片的shape""" if isinstance(images, list): images = np.array(images) img_h = images.shape[1] img_w = images.shape[2] n_plots = int(np.ceil(np.sqrt(images.shape[0])))
sprite_image = np.ones((img_h * n_plots, img_w * n_plots))
for i in range(n_plots): for j in range(n_plots): this_filter = i * n_plots + j if this_filter < images.shape[0]: this_img = images[this_filter] sprite_image[i * img_h:(i + 1) * img_h, j * img_w:(j + 1) * img_w] = this_img
return sprite_image

def vector_to_matrix_mnist(mnist_digits): """把正常的mnist数字图片(batch,28*28)这个格式,转换为新的张量形状(batch,28,28)""" return np.reshape(mnist_digits, (-1, 28, 28))

def invert_grayscale(mnist_digits): """处理下图片颜色,黑色变白,白色边黑""" return 1 - mnist_digits

if __name__ == "__main__": # 定义参数 #学习率 learning_rate = 1e-4 #定义迭代参数 total_epoch = 600 #定义批量 batch_size = 200 #程序运行中打印频率 display_step = 20 #程序运行中保存结果的频率 save_step = 100 load_checkpoint = False checkpoint_dir = "checkpoint" checkpoint_name = 'model.ckpt' #结果存放的路径 logs_path = "logs" #定义我们使用多少个图片 test_size = 10000 #定义第二层路径 projector_path = 'projector'
# 网络参数 n_input = 28 * 28 # 每个图片是28*28个像素,也就是784个特征 n_classes = 10 # MNIST数据集有0-9是个类别的结果 dropout_rate = 0.5 # Dropout的比率
mnist = input_data.read_data_sets('MNIST-data', one_hot=True)
# 定义计算图 x = tf.placeholder(tf.float32, [None, n_input], name='InputData') y = tf.placeholder(tf.float32, [None, n_classes], name='LabelData') is_training = tf.placeholder(tf.bool, name='IsTraining') keep_prob = dropout_rate
logits, fc1 = model(x, is_training, keep_prob, n_classes)
with tf.name_scope('Loss'): loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y)) tf.summary.scalar("loss", loss)
with tf.name_scope('Accuracy'): correct_pred = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) tf.summary.scalar("accuracy", accuracy)
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)
projector_dir = os.path.join(logs_path, projector_path) path_metadata = os.path.join(projector_dir,'metadata.tsv') path_sprites = os.path.join(projector_dir, 'mnistdigits.png') # 检查结果目录的状态 if not os.path.exists(projector_dir): os.makedirs(projector_dir)
# 这里进行嵌入 mnist_test = input_data.read_data_sets('MNIST-data', one_hot=False) batch_x_test = mnist_test.test.images[:test_size] batch_y_test = mnist_test.test.labels[:test_size]
embedding_var = tf.Variable(tf.zeros([test_size, 1024]), name='embedding') assignment = embedding_var.assign(fc1)
config = projector.ProjectorConfig() embedding = config.embeddings.add() embedding.tensor_name = embedding_var.name embedding.metadata_path = os.path.join(projector_path,'metadata.tsv') embedding.sprite.image_path = os.path.join(projector_path, 'mnistdigits.png') embedding.sprite.single_image_dim.extend([28,28])
# 初始化变量 init = tf.global_variables_initializer()
# 'Saver' op to save and restore all the variables saver = tf.train.Saver() merged_summary_op = tf.summary.merge_all()
# 运行计算图 with tf.Session() as sess: sess.run(init) # Restore model weights from previously saved model prev_model = tf.train.get_checkpoint_state(checkpoint_dir) if load_checkpoint: if prev_model: saver.restore(sess, prev_model.model_checkpoint_path) print('Checkpoint found, {}'.format(prev_model)) else: print('No checkpoint found')
summary_writer = tf.summary.FileWriter(logs_path, graph=tf.get_default_graph()) projector.visualize_embeddings(summary_writer, config) start_time = time.time() # 开始训练 for epoch in range(total_epoch): batch_x, batch_y = mnist.train.next_batch(batch_size) # reshapeX = np.reshape(batch_x, [-1, 28, 28, 1]) # 开始反向传播算法 sess.run(optimizer, feed_dict={x: batch_x, y: batch_y, is_training: True}) if epoch % display_step == 0: # 计算损失和精度 cost, acc, summary = sess.run([loss, accuracy, merged_summary_op], feed_dict={x: batch_x, y: batch_y, is_training: False}) elapsed_time = time.time() - start_time start_time = time.time() print('epoch {}, training accuracy: {:.4f}, loss: {:.5f}, time: {}' .format(epoch, acc, cost, elapsed_time)) summary_writer.add_summary(summary, epoch) if epoch % save_step == 0: # 保存训练的结果 sess.run(assignment, feed_dict={x: mnist.test.images[:test_size], y: mnist.test.labels[:test_size], is_training: False}) checkpoint_path = os.path.join(checkpoint_dir, checkpoint_name) save_path = saver.save(sess, checkpoint_path) print("Model saved in file: {}".format(save_path))
# 保存结果 saver.save(sess, os.path.join(logs_path, "model.ckpt"), 1) # 创建可视化的图片 to_visualise = batch_x_test to_visualise = vector_to_matrix_mnist(to_visualise) to_visualise = invert_grayscale(to_visualise) sprite_image = create_sprite_image(to_visualise) # 保存可视化的图片 plt.imsave(path_sprites, sprite_image, cmap='gray') # 写文件 with open(path_metadata, 'w') as f: f.write("Index\tLabel\n") for index, label in enumerate(batch_y_test): f.write("%d\t%d\n" % (index, label))
print("训练完成")

训练过程还是很快的。最后再看看t-SNE:图片

6. 最后看看运行中GPU的情况

这个可直接通过之前下载的GPU-Z软件查看:图片


继续滑动看下一个
Garden001
向上滑动看下一个