校园视频AI分析预警系统 TesnorFlow

校园视频AI分析预警系统通过分布式TensorFlow模型训练,校园视频AI分析预警系统对学生的行为进行实时监测,当系统检测到学生出现打架、翻墙、倒地、抽烟等异常行为时,校园视频AI分析预警系统将自动发出警报提示相关人员及时采取措施。深度学习应用到实际问题中,一个非常棘手的问题是训练模型时计算量太大。为了加速训练,TensorFlow可以利用GPU或/和分布式计算进行模型训练。TensorFlow可以通过td.device函数来指定运行每个操作的设备,这个设备可以是本设备的CPU或GPU,也可以是远程的某一台设备。TF生成会话的时候,可愿意通过设置tf.log_device_placemaent参数来打印每一个运算的设备。

import tensorflow as tf

a = tf.constant([1.0,2.0,3.0],shape=[3],name='a')
b = tf.constant([1.0,2.0,3.0],shape=[3],name='b')
c= tf.add_n([a,b],name="c")

with tf.Session(config=tf.ConfigProto(log_device_placement = True)) as sess:
    print(sess.run(c))

########
Device mapping: no known devices.
c: (AddN): /job:localhost/replica:0/task:0/device:CPU:0
b: (Const): /job:localhost/replica:0/task:0/device:CPU:0
a: (Const): /job:localhost/replica:0/task:0/device:CPU:0

[2. 4. 6.]

在配置好了GPU环境的TensorFlow中,如果没有明确指明运行设备,TF会优先选择GPU。

import tensorflow as tf

a = tf.constant([1.0,2.0,3.0],shape=[3],name='a')
b = tf.constant([1.0,2.0,3.0],shape=[3],name='b')
c= tf.add_n([a,b],name="c")

with tf.Session(config=tf.ConfigProto(log_device_placement = True)) as sess:
    print(sess.run(c))

########
Device mapping: no known devices.
c: (AddN): /job:localhost/replica:0/task:0/device:GPU:0
b: (Const): /job:localhost/replica:0/task:0/device:GPU:0
a: (Const): /job:localhost/replica:0/task:0/device:GPU:0

[2. 4. 6.]

可以通过tf.device 来制定运行操作的设备。

import tensorflow as tf
with tf.device("/CPU:0"):
    a = tf.constant([1.0,2.0,3.0],shape=[3],name='a')
    b = tf.constant([1.0,2.0,3.0],shape=[3],name='b')
with tf.device("/GPU:0"):
    c= tf.add_n([a,b],name="c")

with tf.Session(config=tf.ConfigProto(log_device_placement = True)) as sess:
    print(sess.run(c))

某些数据类型是不被GPU所支持的。强制指定设备会报错。为了避免解决这个问题。在创建会还时可以指定参数allow_soft_placement 。当allow_soft_placement为True的时候,如果运算无法在GPU上运行,TF会自动将其放在CPU 上运行。

猜你喜欢

转载自blog.csdn.net/KO_159/article/details/131348594