开发者社区> taro_秋刀鱼> 正文

阿里云DSW实例运行LeNet Sample

简介: LeNet,它是最早发布的卷积神经网络之一,因其在计算机视觉任务中的高效性能而受到广泛关注。 这个模型是由AT&T贝尔实验室的研究员Yann LeCun在1989年提出的(并以其命名),目的是识别图像中的手写数字。本文主要演示在阿里云PAI DSW实例中使用GPU资源运行LetNet网络。
+关注继续查看

Step By Step

1、创建GPU实例
2、查看使用GPU卡情况
3、LetNet Code运行示例
4、显存释放问题


一、创建GPU实例

图片.png

二、查看使用GPU卡情况
!nvidia-smi

图片.png

  • 查看tf版本及devices信息
import tensorflow as tf
print(tf.__version__)
tf.config.list_physical_devices()

图片.png

三、LetNet Code运行示例
  • 3.1 Code Sample
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow import keras

# 数据预处理函数
def preprocess(x, y):
    x = tf.cast(x, dtype=tf.float32) / 255.
    x = tf.reshape(x, [-1, 32, 32, 1])
    y = tf.one_hot(y, depth=10)  # one_hot 编码
    return x, y


# 加载数据集
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()

# 样本图像周围补0(上下左右均补2个0),将28*28的图像转成32*32的图像
paddings = tf.constant([[0, 0], [2, 2], [2, 2]])
x_train = tf.pad(x_train, paddings)
x_test = tf.pad(x_test, paddings)

train_db = tf.data.Dataset.from_tensor_slices((x_train, y_train))
train_db = train_db.shuffle(10000)  # 打乱训练集样本
train_db = train_db.batch(128)
train_db = train_db.map(preprocess)

test_db = tf.data.Dataset.from_tensor_slices((x_test, y_test))
test_db = test_db.shuffle(10000)  # 打乱测试集样本
test_db = test_db.batch(128)
test_db = test_db.map(preprocess)

batch = 32

# 创建模型
model = keras.Sequential([
    # 卷积层1
    keras.layers.Conv2D(6, 5),  # 使用6个5*5的卷积核对单通道32*32的图片进行卷积,结果得到6个28*28的特征图
    keras.layers.MaxPooling2D(pool_size=2, strides=2),  # 对28*28的特征图进行2*2最大池化,得到14*14的特征图
    keras.layers.ReLU(),  # ReLU激活函数
    # 卷积层2
    keras.layers.Conv2D(16, 5),  # 使用16个5*5的卷积核对6通道14*14的图片进行卷积,结果得到16个10*10的特征图
    keras.layers.MaxPooling2D(pool_size=2, strides=2),  # 对10*10的特征图进行2*2最大池化,得到5*5的特征图
    keras.layers.ReLU(),  # ReLU激活函数
    # 卷积层3
    keras.layers.Conv2D(120, 5),  # 使用120个5*5的卷积核对16通道5*5的图片进行卷积,结果得到120个1*1的特征图
    keras.layers.ReLU(),  # ReLU激活函数
    # 将 (None, 1, 1, 120) 的下采样图片拉伸成 (None, 120) 的形状
    keras.layers.Flatten(),
    # 全连接层1
    keras.layers.Dense(84, activation='relu'),  # 120*84
    # 全连接层2
    keras.layers.Dense(10, activation='softmax')  # 84*10
])
model.build(input_shape=(batch, 32, 32, 1))
model.summary()

model.compile(optimizer=keras.optimizers.Adam(), loss=keras.losses.CategoricalCrossentropy(), metrics=['accuracy'])
# 训练
history = model.fit(train_db, epochs=50)

# 损失下降曲线
plt.plot(history.history['loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.show()

# 测试
model.evaluate(test_db)
  • 运行结果
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz
32768/29515 [=================================] - 0s 1us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz
26427392/26421880 [==============================] - 1s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz
8192/5148 [===============================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz
4423680/4422102 [==============================] - 0s 0us/step
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (32, 28, 28, 6)           156       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (32, 14, 14, 6)           0         
_________________________________________________________________
re_lu (ReLU)                 (32, 14, 14, 6)           0         
_________________________________________________________________
conv2d_1 (Conv2D)            (32, 10, 10, 16)          2416      
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (32, 5, 5, 16)            0         
_________________________________________________________________
re_lu_1 (ReLU)               (32, 5, 5, 16)            0         
_________________________________________________________________
conv2d_2 (Conv2D)            (32, 1, 1, 120)           48120     
_________________________________________________________________
re_lu_2 (ReLU)               (32, 1, 1, 120)           0         
_________________________________________________________________
flatten (Flatten)            (32, 120)                 0         
_________________________________________________________________
dense (Dense)                (32, 84)                  10164     
_________________________________________________________________
dense_1 (Dense)              (32, 10)                  850       
=================================================================
Total params: 61,706
Trainable params: 61,706
Non-trainable params: 0
_________________________________________________________________
Epoch 1/50
469/469 [==============================] - 2s 4ms/step - loss: 0.6662 - accuracy: 0.7553
Epoch 2/50
469/469 [==============================] - 2s 4ms/step - loss: 0.3988 - accuracy: 0.8569
Epoch 3/50
469/469 [==============================] - 2s 4ms/step - loss: 0.3414 - accuracy: 0.8751
Epoch 4/50
469/469 [==============================] - 2s 4ms/step - loss: 0.3081 - accuracy: 0.8861
Epoch 5/50
469/469 [==============================] - 2s 4ms/step - loss: 0.2888 - accuracy: 0.8938
Epoch 6/50
469/469 [==============================] - 2s 4ms/step - loss: 0.2733 - accuracy: 0.8997
Epoch 7/50
469/469 [==============================] - 2s 4ms/step - loss: 0.2590 - accuracy: 0.9039
Epoch 8/50
469/469 [==============================] - 2s 4ms/step - loss: 0.2464 - accuracy: 0.9078
Epoch 9/50
469/469 [==============================] - 2s 4ms/step - loss: 0.2358 - accuracy: 0.9126
Epoch 10/50
469/469 [==============================] - 2s 4ms/step - loss: 0.2292 - accuracy: 0.9155
Epoch 11/50
469/469 [==============================] - 2s 4ms/step - loss: 0.2175 - accuracy: 0.9190
Epoch 12/50
469/469 [==============================] - 2s 4ms/step - loss: 0.2096 - accuracy: 0.9227
Epoch 13/50
469/469 [==============================] - 2s 4ms/step - loss: 0.2022 - accuracy: 0.9239
Epoch 14/50
469/469 [==============================] - 2s 4ms/step - loss: 0.1929 - accuracy: 0.9284
Epoch 15/50
469/469 [==============================] - 2s 4ms/step - loss: 0.1875 - accuracy: 0.9301
Epoch 16/50
469/469 [==============================] - 2s 4ms/step - loss: 0.1776 - accuracy: 0.9342
Epoch 17/50
469/469 [==============================] - 2s 4ms/step - loss: 0.1737 - accuracy: 0.9349
Epoch 18/50
469/469 [==============================] - 2s 4ms/step - loss: 0.1658 - accuracy: 0.9379
Epoch 19/50
469/469 [==============================] - 2s 4ms/step - loss: 0.1592 - accuracy: 0.9407
Epoch 20/50
469/469 [==============================] - 2s 4ms/step - loss: 0.1515 - accuracy: 0.9438
Epoch 21/50
469/469 [==============================] - 2s 4ms/step - loss: 0.1461 - accuracy: 0.9445
Epoch 22/50
469/469 [==============================] - 2s 4ms/step - loss: 0.1403 - accuracy: 0.9467
Epoch 23/50
469/469 [==============================] - 2s 4ms/step - loss: 0.1340 - accuracy: 0.9487
Epoch 24/50
469/469 [==============================] - 2s 4ms/step - loss: 0.1295 - accuracy: 0.9506
Epoch 25/50
469/469 [==============================] - 2s 4ms/step - loss: 0.1235 - accuracy: 0.9536
Epoch 26/50
469/469 [==============================] - 2s 4ms/step - loss: 0.1189 - accuracy: 0.9545
Epoch 27/50
469/469 [==============================] - 2s 4ms/step - loss: 0.1134 - accuracy: 0.9571
Epoch 28/50
469/469 [==============================] - 2s 4ms/step - loss: 0.1059 - accuracy: 0.9602
Epoch 29/50
469/469 [==============================] - 2s 4ms/step - loss: 0.1028 - accuracy: 0.9615
Epoch 30/50
469/469 [==============================] - 2s 4ms/step - loss: 0.1006 - accuracy: 0.9616
Epoch 31/50
469/469 [==============================] - 2s 4ms/step - loss: 0.0936 - accuracy: 0.9644
Epoch 32/50
469/469 [==============================] - 2s 4ms/step - loss: 0.0892 - accuracy: 0.9663
Epoch 33/50
469/469 [==============================] - 2s 4ms/step - loss: 0.0846 - accuracy: 0.9683
Epoch 34/50
469/469 [==============================] - 2s 4ms/step - loss: 0.0807 - accuracy: 0.9692
Epoch 35/50
469/469 [==============================] - 2s 4ms/step - loss: 0.0756 - accuracy: 0.9711
Epoch 36/50
469/469 [==============================] - 2s 4ms/step - loss: 0.0763 - accuracy: 0.9718
Epoch 37/50
469/469 [==============================] - 2s 4ms/step - loss: 0.0700 - accuracy: 0.9732
Epoch 38/50
469/469 [==============================] - 2s 4ms/step - loss: 0.0687 - accuracy: 0.9748
Epoch 39/50
469/469 [==============================] - 2s 4ms/step - loss: 0.0648 - accuracy: 0.9754
Epoch 40/50
469/469 [==============================] - 2s 4ms/step - loss: 0.0611 - accuracy: 0.9771
Epoch 41/50
469/469 [==============================] - 2s 4ms/step - loss: 0.0602 - accuracy: 0.9774
Epoch 42/50
469/469 [==============================] - 2s 4ms/step - loss: 0.0564 - accuracy: 0.9793
Epoch 43/50
469/469 [==============================] - 2s 4ms/step - loss: 0.0533 - accuracy: 0.9807
Epoch 44/50
469/469 [==============================] - 2s 4ms/step - loss: 0.0531 - accuracy: 0.9803
Epoch 45/50
469/469 [==============================] - 2s 4ms/step - loss: 0.0499 - accuracy: 0.9809
Epoch 46/50
469/469 [==============================] - 2s 4ms/step - loss: 0.0452 - accuracy: 0.9827
Epoch 47/50
469/469 [==============================] - 2s 4ms/step - loss: 0.0433 - accuracy: 0.9847
Epoch 48/50
469/469 [==============================] - 2s 4ms/step - loss: 0.0439 - accuracy: 0.9838
Epoch 49/50
469/469 [==============================] - 2s 4ms/step - loss: 0.0367 - accuracy: 0.9864
Epoch 50/50
469/469 [==============================] - 2s 4ms/step - loss: 0.0415 - accuracy: 0.9847

79/79 [==============================] - 0s 3ms/step - loss: 0.5499 - accuracy: 0.9044
[0.5498712658882141, 0.9043999910354614]

图片.png

四、显存释放问题
  • 4.1 问题现象

图片.png

  • 4.2 Terminal fuser安装
sudo apt-get update
sudo apt-get install  psmisc

图片.png

  • 4.3 查看僵尸进程
fuser -v /dev/nvidia*

图片.png

  • 4.4 查看具体这个进程调用GPU的情况
pmap -d PID
  • 4.5 强行关掉所有当前并未执行的僵尸进程
sudo kill -9 PID

图片.png

更多参考

解决GPU显存未释放问题
Ubuntu:fuser command not found
TF Implementing LeNet-5 in TensorFlow 2.0

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,阿里云优惠总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系.
27966 0
一个简单的Servlet注册实例
   我们在利用Servlet进行web开发的时候 ,运行的不是 JSP页面 而实际上是一个  Java类的对象  。这个类的对象从 javax.sevlet.http.HttpServlet ;这个类是基于HTTP的 Servlet类 我们自己写的 Servlet应该从 这个类派生而来 . 通过我们自己编写的类 以及对web.xml的部署  ,进行web的流程控制 ,实现用户与服务器的交互 。
642 0
阿里云服务器端口号设置
阿里云服务器初级使用者可能面临的问题之一. 使用tomcat或者其他服务器软件设置端口号后,比如 一些不是默认的, mysql的 3306, mssql的1433,有时候打不开网页, 原因是没有在ecs安全组去设置这个端口号. 解决: 点击ecs下网络和安全下的安全组 在弹出的安全组中,如果没有就新建安全组,然后点击配置规则 最后如上图点击添加...或快速创建.   have fun!  将编程看作是一门艺术,而不单单是个技术。
20101 0
+关注
taro_秋刀鱼
博客园主页:https://home.cnblogs.com/u/taro/
244
文章
13
问答
来源圈子
更多
文章排行榜
最热
最新
相关电子书
更多
JS零基础入门教程(上册)
立即下载
性能优化方法论
立即下载
手把手学习日志服务SLS,云启实验室实战指南
立即下载