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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| import tensorflow as tf import os os.environ['CUDA_VISIBLE_DEVICES']='0,1,2' gpus=[0, 1, 2]
def average_gradients(tower_grads): average_grads = [] for grad_and_vars in zip(*tower_grads): grads = [] for g, _ in grad_and_vars: expanded_g = tf.expand_dims(g, 0) grads.append(expanded_g) grad = tf.concat(axis=0, values=grads) grad = tf.reduce_mean(grad, 0) v = grad_and_vars[0][1] grad_and_var = (grad, v) average_grads.append(grad_and_var) return average_grads
def train_mult(): input_image_size = 224 inputs = tf.placeholder(shape=[None, input_image_size, input_image_size, 3], dtype=tf.float32, name='inputs') shape_label = tf.placeholder(shape=[None], dtype=tf.int64, name='shape_label') color_label = tf.placeholder(shape=[None], dtype=tf.int64, name='color_label') global_step = tf.Variable(0, name='global_step', trainable=False) optimizer = tf.train.AdamOptimizer(learning_rate=0.001)
tower_grads = [] tower_loss = [] images_splits = tf.split(inputs, num_or_size_splits=len(gpus), axis=0) color_label_splits = tf.split(color_label, num_or_size_splits=len(gpus), axis=0) shape_label_splits = tf.split(shape_label, num_or_size_splits=len(gpus), axis=0)
for i in range(len(gpus)): with tf.device('/gpu:%d' % i): with tf.variable_scope('my_net', reuse=tf.AUTO_REUSE): logits_shape, logits_color= my_net(images_splits[i])
color_loss = compute_color_loss(logits_color, color_label_splits[i]) tf.summary.scalar('color_loss_gpu%d' % i, color_loss) shape_loss = compute_shape_loss(logits_shape, shape_label_splits[i]) tf.summary.scalar('shape_loss_gpu%d' % i, shape_loss)
sum_loss = color_loss + shape_loss tf.summary.scalar('sum_loss_gpu%d' % i, sum_loss)
grads = optimizer.compute_gradients(sum_loss, var_list=tf.trainable_variables()) tower_grads.append([x for x in grads if x[0] is not None])
tower_loss.append(sum_loss)
avg_tower_loss = tf.reduce_mean(tower_loss, axis=0) tf.summary.scalar('avg_tower_loss', avg_tower_loss) grads_avg = average_gradients(tower_grads)
merged_summay = tf.summary.merge_all() update = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update): train_op = optimizer.apply_gradients(grads_avg, global_step)
|