使用多块GPU进行训练 1.slim.arg_scope(对于同等类型使用相同操作) 2.tf.name_scope(定义名字的范围) 3.tf.get_variable_scope().reuse_variable(参数的复用) 4.tf.py_func(构造函数)
1. slim.arg_scope(函数, 传参) # 对于同类的函数操作,都传入相同的参数
from tensorflow.contrib import slim as slim
import tensorflow as tf @slim.add_arg_scope # 进行修饰操作
def fun1(a=0, b=0):
return a + b with slim.arg_scope([fun1], a=2): x = fun1(b=2)
print(x)
# 4
2. tf.name_scope('tower') 对范围内的操作都添加上该操作名
import tensorflow as tf
with tf.name_scope('tower'):
op = tf.constant(1)
print(op.name)
with tf.name_scope('tower_1'):
op = tf.constant(1)
print(op.name)
# tower/Const:0
# tower_1/Const:0
3. tf.get_variable_scope.reuse_variable() 对于相同的操作传入使用同一个网络结构,不需要再创建新值
import tensorflow as tf
import vgg inputs_1 = tf.random_normal([10, 224, 224, 3])
input_2 = tf.random_normal([10, 224, 224, 3]) with tf.variable_scope('vgg_16'):
net, end_points = vgg.vgg_16(inputs_1, 100, False)
tf.get_variable_scope().reuse_variables() # 对上述的参数进行重复使用,相当于是reuse=True
net_, end_points_ = vgg.vgg_16(input_2, 100, False) with tf.Session() as sess:
print('no error')
4. tf.py_func(func, [x], [tf.int32]) 与lamda配套使用,相当于是将参数传入到func中
参数说明:func表示定义的函数, [x] 表示传入的参数,[tf.int32]表示返回的类型
import numpy as np
import tensorflow as tf def func(x):
return x+20 softmax_idx = np.arange(10)
softmax_idx = map(lambda x:tf.py_func(func, [x], [tf.int32]), softmax_idx)
sess = tf.Session()
for softmax_ides in softmax_idx:
print(sess.run(softmax_ides))
使用多块GPU进行训练,主要是通过tf.device('/gpu' + str(i)) 来实现的,通过对参数
tf.variable_scope(tf.get_variable_scope) as var_scope:定义参数范围
reuse = False if i== 0 else True 来定义参数是否重复使用,对于第一次创建网络,reuse=False,后续的网络使用reuse=True,表示不需要重新构建网络
这里同样使用tf.get_variable_scope().reuse_variable()来进行参数的复用
第一步:
第一步:使用tf.data.Dataset.range() 来构造数据队列
第二步:使用softmax_data.map(lamda x:tf.pyfunc( _parse_function, [x], [tf.string, tf.int32])) # 将数据集进行传入
第三步:使用softmax_data.map(tensor_from_slice) # 将数据转换为slice格式进行输出
第四步:softmax_data.batch(single_size_batch*args.num_gpus) # 创建一个batch的数据集
第五步:softmax_data.make_initializable_iterator() # 构造迭代器
第六步: softmax_data_element = softmax_iterator.get_next_batch() 获得一个batch的数据
第七步:softmax_data_element[0].set_shape() 进行维度的设置
第八步:使用tf.spilt(softmax_data_element[0], args.num_gpus) 将数据进行切分,构造出batch_image_list 和 batch_label_list
第二步:
第一步:循环 args.gpu_num
第二步:使用with tf.device('/gpu' + str(i)) # 将训练放在gpu上
第三步:使用with tf.name_scope('tower_' + str(i)) # 定义名字的变量防止冲突
第四步:with slim.arg_scope([slim.mode_variable, slim.variable], '/cpu0') # 将变量放在CPU上进行训练
第五步:使用tf.variable_scope(tf.get_variable_scope) as var_scope 定义变量的范围
第六步: 使用tf.get_variable_scope().reuse_scope() 进行变量的复用
第三步:
第一步: 将添加到tower_loss的loss求出均值
第二步:求取梯度,建立训练train_op
import tensorflow as tf
from tensorflow import data as tf_data
import random
import numpy as np
from tensorflow.contrib import slim as slim def _sample_people_softmax(x):
global softmax_ind
if softmax_ind >= dataset_size:
softmax_ind = 0
random.shuffle(indices)
true_num_batch = min(single_batch_size*args.num_gpus, dataset_size - softmax_ind) sample_paths = image_list[softmax_ind:softmax_ind + true_num_batch]
sample_labels = label_list[softmax_ind:softmax_ind+true_num_batch] softmax_ind += true_num_batch return np.array(sample_paths), np.array(sample_labels, dtype=np.int32) def _parse_function(filename, label):
file_content = tf.read_file(filename)
image = tf.image.decode_image(file_content)
print(image.shape) if args.random_crop:
print('use random crop')
image = tf.random_crop(image, [args.image_size, args.image_size, 3])
else:
print('Not use random crop')
image = tf.image.resize(image, [args.image_height, args.image_width, 3])
if args.random_flip:
image = tf.image.random_flip_left_right(image) image.set_shape((args.image_height, args.image_width, 3)) if debug:
image = tf.cast(image, tf.float32) else:
image = tf.cast(image, tf.float32)
image = tf.subtract(image, 127.5)
image = tf.div(image, 128) return image, label learning_rate = 0.01
opt = tf.train.AdamOptimizer(learning_rate) def _from_tensor_slices(tensor_x, tensor_y):
return tf_data.Dataset.from_tensor_slices((tensor_x, tensor_y)) with tf.device('/cpu:0'): softmax_data = tf_data.Dataset.range(args.epoch_size*args.max_nrof_epochs*100)
softmax_data = softmax_data.map(lambda x: tf.py_func(_sample_people_softmax, [x], [tf.string, tf.float32]))
softmax_data = softmax_data.flat_map(_from_tensor_slices)
softmax_data = softmax_data.map(_parse_function)
softmax_data = softmax_data.batch(args.num_gpus*single_batch_size)
softmax_iterator = softmax_data.make_initializable_iterator()
softmax_data_element = softmax_iterator.get_next()
softmax_data_element[0].set_shape((args.num_gpus*single_batch_size, args.image_height, args.image_width, 3))
sotfmax_data_element[1].set_shape((args.num_gpus*single_batch_size))
batch_image_split = tf.split(softmax_data_element[0], args.num_gpus)
batch_label_split = tf.split(softmax_data_element[1], args.num_gpus) tower_loss = []
for i in range(args.num_gpus):
with tf.device('/gpu:' + str(i)):
with tf.name_scope('tower_' + str(i)) as scope: # 对参数进行命名防止出现变量冲突
with slim.arg_scope([slim.model_variable, slim.variable], device='/cpu:0'): # 将变量和全局变量放在cpu上
with tf.variable_scope(tf.get_variable_scope()) as var_scope: # 设置参数变量的范围
reuse = False if i==0 else True
''' '''
tower_loss.append(loss) # 将loss添加到tower_loss里面
tf.get_variable_scope().reuse_variables() # 将参数变量进行重复的使用,相当于是reuse = True tower_loss = tf.reduce_mean(loss) # 求得损失值
# 进行损失值的train_op更新的构造
grads = opt.compute_gradients(tower_loss, tf.trainable_variables(), colocate_gradients_with_ops=True)
grad_opt = opt.apply_gradients(grads) # 计算梯度
update_op = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_op):
train_op = tf.group(grad_opt)
sess = tf.Session()
sess.run(softmax_iterator.initializer)
使用多块GPU进行训练 1.slim.arg_scope(对于同等类型使用相同操作) 2.tf.name_scope(定义名字的范围) 3.tf.get_variable_scope().reuse_variable(参数的复用) 4.tf.py_func(构造函数)的更多相关文章
- TensorFlow指定使用GPU 多块gpu
持续监控GPU使用情况命令: $ watch -n 10 nvidia-smi1一.指定使用某个显卡如果机器中有多块GPU,tensorflow会默认吃掉所有能用的显存, 如果实验室多人公用一台服务器 ...
- tensorflow 13:多gpu 并行训练
多卡训练模式: 进行深度学习模型训练的时候,一般使用GPU来进行加速,当训练样本只有百万级别的时候,单卡GPU通常就能满足我们的需求,但是当训练样本量达到上千万,上亿级别之后,单卡训练耗时很长,这个时 ...
- 在GPU上训练数据
在GPU上训练数据 模型搬到GPU上 数据搬到GPU上 损失函数计算搬到GPU上
- tf.contrib.slim arg_scope
缘由 最近一直在看深度学习的代码,又一次看到了slim.arg_scope()的嵌套使用,具体代码如下: with slim.arg_scope( [slim.conv2d, slim.separab ...
- 深度学习原理与框架-猫狗图像识别-卷积神经网络(代码) 1.cv2.resize(图片压缩) 2..get_shape()[1:4].num_elements(获得最后三维度之和) 3.saver.save(训练参数的保存) 4.tf.train.import_meta_graph(加载模型结构) 5.saver.restore(训练参数载入)
1.cv2.resize(image, (image_size, image_size), 0, 0, cv2.INTER_LINEAR) 参数说明:image表示输入图片,image_size表示变 ...
- slim.arg_scope()的使用
[https://blog.csdn.net/u013921430 转载] slim是一种轻量级的tensorflow库,可以使模型的构建,训练,测试都变得更加简单.在slim库中对很多常用的函数进行 ...
- C++ 为什么拷贝构造函数参数必须为引用?赋值构造函数参数也必须为引用吗?
之前写拷贝构造函数的时候,以为参数为引用,不为值传递,仅仅是为了减少一次内存拷贝.然而今天看到一篇文章发现自己对拷贝构造的参数理解有误. 参数为引用,不为值传递是为了防止拷贝构造函数的无限递归,最终导 ...
- slim.arg_scope中python技巧
slim.arg_scope函数说明如下: Stores the default arguments for the given set of list_ops. For usage, please ...
- 目标检测算法SSD在window环境下GPU配置训练自己的数据集
由于最近想试一下牛掰的目标检测算法SSD.于是乎,自己做了几千张数据(实际只有几百张,利用数据扩充算法比如镜像,噪声,切割,旋转等扩充到了几千张,其实还是很不够).于是在网上找了相关的介绍,自己处理数 ...
随机推荐
- vue中监听数据变化 watch
今天做项目的时候,子组件中数据(原本固定的数据)需要父组件动态传入,如果一开始初始化用到的数据.但当时还没有获取到,初始化结束就不会更新数据了.只有监听这两个属性,再重新执行初始化. 1.watch是 ...
- Mybatis分页-利用Mybatis Generator插件生成基于数据库方言的分页语句,统计记录总数 (转)
众所周知,Mybatis本身没有提供基于数据库方言的分页功能,而是基于JDBC的游标分页,很容易出现性能问题.网上有很多分页的解决方案,不外乎是基于Mybatis本机的插件机制,通过拦截Sql做分页. ...
- 普元 BPS表结构参考
BPS表结构 BPS默认采用数据库方式对业务流程的定义以及运行期的流程实例.活动.工作项等进行持久化存储.主要包括BPS流程流转相关的定义.实例.工作项.参与者相关的数据表以及和实际业务流程控制相关的 ...
- A*算法实现(图形化表示)——C++描述
概要 A*算法是一种启发式寻路算法,BFS是一种盲目的无目标的搜索算法,相比于BFS,A*算法根据适应度构建优先队列,根据适应度值可以很好的向目标点移动,具体详情,请看搜索相关文档,我在只是实现了在无 ...
- poj1830 开关问题[高斯消元]
其实第一反应是双向BFS或者meet in middle,$2^{14}$的搜索量,多测,应该是可以过的,但是无奈双向BFS我只写过一题,已经不会写了. 发现灯的操作情况顺序不影响结果,因为操作相当于 ...
- jvm的几篇文章
https://www.cnblogs.com/xrq730/category/731395.html
- idea中使用JRebel插件
首先 等待下载 点击Restart idea (重启idea) 然后等待idea 进行激活 记得勾选下面的那个复选框 激活url:http://139.199.89.239:1008/88414687 ...
- 再提供一种解决Nginx文件类型错误解析漏洞的方法
[文章作者:张宴 本文版本:v1.2 最后修改:2010.05.24 转载请注明原文链接:http://blog.zyan.cc/nginx_0day/] 注:2010年5月23日14:00前阅读本文 ...
- 利用rand7() 产生rand10()(腾讯)
题目1:已知rand7() 可以产生 1~7 的7个数(均匀概率),利用rand7() 产生rand10() 1~10(均匀概率) int rand10() { int temp; int te ...
- vuex , 简单入(liao)门(jie)
vuex什么 ? 官方的说法就是 vuex是专门为vue.js应用程序开发的 状态管理模式 .并采用集中式存储 , 管理应用的所有组件的状态 ,并以相同的规则保证状态以一种可预估的方式发生变化. 自己 ...