def make_model(input, result, mask, reuse=False, use_slim=False): input = tf.reshape(input, [batch_size, xdim, ydim, zdim, n_pos + 1]) result = tf.reshape(result, [batch_size, xdim, ydim, zdim, n_pos]) mask = tf.reshape(mask, [batch_size, xdim, ydim, zdim, n_pos]) voxel_list, head_net = model(input, n_pos, reuse, use_slim) # define loss losses = [] stage_losses = [] for _, voxel in enumerate(voxel_list): loss = tf.nn.l2_loss((voxel.outputs - result) * mask) losses.append(loss) stage_losses.append(loss / batch_size) last_voxel = head_net.outputs l2_loss = 0.0 for p in tl.layers.get_variables_with_name('W_', True, True): l2_loss += tf.contrib.layers.l2_regularizer(weight_decay_factor)(p) head_loss = tf.reduce_sum(losses) / batch_size + l2_loss head_net.input = input # base_net input head_net.last_voxel = last_voxel # base_net output head_net.mask = mask head_net.voxels = result # GT head_net.stage_losses = stage_losses head_net.l2_loss = l2_loss return head_net, head_loss
def make_model(input, results, is_train=True, reuse=False): confs = results[:, :, :, :n_pos] pafs = results[:, :, :, n_pos:] cnn, b1_list, b2_list, base_net = model(input, n_pos, is_train, reuse) # define loss losses = [] stage_losses = [] for _, (l1, l2) in enumerate(zip(b1_list, b2_list)): loss_l1 = tf.nn.l2_loss(l1.outputs - confs) loss_l2 = tf.nn.l2_loss(l2.outputs - pafs) losses.append(tf.reduce_mean([loss_l1, loss_l2])) stage_losses.append(loss_l1 / batch_size) stage_losses.append(loss_l2 / batch_size) last_conf = b1_list[-1].outputs last_paf = b2_list[-1].outputs l2_loss = 0.0 for p in tl.layers.get_variables_with_name('kernel', True, True): l2_loss += tf.contrib.layers.l2_regularizer(weight_decay_factor)(p) base_loss = tf.reduce_sum(losses) / batch_size + l2_loss base_net.cnn = cnn base_net.input = input # base_net input base_net.last_conf = last_conf # base_net output base_net.last_paf = last_paf # base_net output base_net.confs = confs # GT base_net.pafs = pafs # GT base_net.stage_losses = stage_losses base_net.l2_loss = l2_loss return base_net, base_loss
def make_model(img, results, mask, is_train=True, train_bn=True, reuse=False): confs = results[:, :, :, :n_pos] pafs = results[:, :, :, n_pos:] m1 = tf_repeat(mask, [1, 1, 1, n_pos]) m2 = tf_repeat(mask, [1, 1, 1, n_pos * 2]) cnn, b1_list, b2_list, net = model(img, n_pos, m1, m2, is_train, train_bn, reuse) # define loss losses = [] last_losses_b1 = [] last_losses_b2 = [] stage_losses = [] for idx, (b1, b2) in enumerate(zip(b1_list, b2_list)): loss_b1 = tf.nn.l2_loss((b1.outputs - confs) * m1) loss_b2 = tf.nn.l2_loss((b2.outputs - pafs) * m2) losses.append(tf.reduce_mean([loss_b1, loss_b2])) stage_losses.append(loss_b1 / config.TRAIN.batch_size) stage_losses.append(loss_b2 / config.TRAIN.batch_size) last_conf = b1_list[-1].outputs last_paf = b2_list[-1].outputs last_losses_b1.append(loss_b1) last_losses_b2.append(loss_b2) l2_loss = 0.0 for p in tl.layers.get_variables_with_name('kernel', True, True): l2_loss += tf.contrib.layers.l2_regularizer(weight_decay_factor)(p) total_loss = tf.reduce_sum(losses) / config.TRAIN.batch_size + l2_loss log_tensors = { 'total_loss': total_loss, 'stage_losses': stage_losses, 'l2_loss': l2_loss } net.cnn = cnn net.img = img # net input net.last_conf = last_conf # net output net.last_paf = last_paf # net output net.confs = confs # GT net.pafs = pafs # GT net.m1 = m1 # mask1, GT net.m2 = m2 # mask2, GT net.stage_losses = stage_losses net.l2_loss = l2_loss return net, total_loss, log_tensors
def make_model(input, result, mask, reuse=False, use_slim=False): input = tf.reshape(input, [batch_size, xdim, ydim, zdim, n_pos + 1]) result = tf.reshape(result, [batch_size, n_pos, 3]) mask = tf.reshape(mask, [batch_size, n_pos]) grid = tf.meshgrid(tf.range(0.0, xdim), tf.range(0.0, ydim), tf.range(0.0, zdim), indexing='ij') grid = tf.tile(tf.expand_dims(grid, -1), [1, 1, 1, 1, n_pos]) voxel_list, head_net = model(input, n_pos, reuse, use_slim) # define loss losses = [] stage_losses = [] for _, voxel in enumerate(voxel_list): loss = 0.0 for idx in range(0, batch_size): one_voxel = voxel.outputs[idx, :, :, :, :] one_pred = tf.exp(one_voxel) / tf.exp( tf.reduce_max(one_voxel, [0, 1, 2])) # 防止数值溢出 one_pred = one_pred / tf.reduce_sum(one_pred, [0, 1, 2]) one_pred = tf.reduce_sum(one_pred * grid, [1, 2, 3]) pred_loss = tf.nn.l2_loss( (one_pred - tf.transpose(result[idx, :, :])) * mask[idx, :]) norm_loss = tf.nn.l2_loss(one_voxel * mask[idx, :]) loss += pred_loss * 0.1 + norm_loss * 0.01 losses.append(loss) stage_losses.append(loss / batch_size) last_voxel = head_net.outputs l2_loss = 0.0 for p in tl.layers.get_variables_with_name('W_', True, True): l2_loss += tf.contrib.layers.l2_regularizer(weight_decay_factor)(p) head_loss = tf.reduce_sum(losses) / batch_size + l2_loss head_net.input = input # base_net input head_net.last_voxel = last_voxel # base_net output head_net.mask = mask head_net.voxels = result # GT head_net.stage_losses = stage_losses head_net.l2_loss = l2_loss return head_net, head_loss
def make_model(input, result, mask, reuse=False, use_slim=False): input = tf.reshape(input, [batch_size, xdim, ydim, zdim, n_pos + 1]) result = tf.reshape(result, [batch_size, n_pos, 3]) mask = tf.reshape(mask, [batch_size, n_pos]) # # ori # grid = tf.meshgrid(tf.range(0.0, xdim), tf.range(0.0, ydim), tf.range(0.0, zdim), indexing='ij') # mid and not norm (0) grid = tf.meshgrid(tf.range(-xdim / 2 + 0.5, xdim / 2), tf.range(-ydim / 2 + 0.5, ydim / 2), tf.range(-zdim / 2 + 0.5, zdim / 2), indexing='ij') # # mid and norm (1) # grid = tf.meshgrid(tf.range(-1.0, 1.0, 2.0/xdim), tf.range(-1.0, 1.0, 2.0/ydim), tf.range(-1.0, 1.0, 2.0/zdim), indexing='ij') grid = tf.tile(tf.expand_dims(grid, -1), [1, 1, 1, 1, n_pos]) voxel_list, head_net = model(input, n_pos, reuse, use_slim) # define loss mean_losses = [] var_losses = [] stage_losses = [] for _, voxel in enumerate(voxel_list): loss = 0.0 mean_loss = 0.0 var_loss = 0.0 for idx in range(batch_size): one_mask = mask[idx, :] one_voxel = voxel.outputs[idx, :, :, :, :] mean, variance = tf.nn.moments(one_voxel, [0, 1, 2]) mean_loss += tf.nn.l2_loss(mean * one_mask) var_loss += tf.nn.l2_loss( tf.nn.relu((variance - sigma * sigma) * one_mask)) one_pred = tf.exp(one_voxel) / tf.exp( tf.reduce_max(one_voxel, [0, 1, 2])) # 防止数值溢出 one_pred = one_pred / tf.reduce_sum(one_pred, [0, 1, 2]) one_pred = tf.reduce_sum(one_pred * grid, [1, 2, 3]) # # ori # one_result = tf.transpose(result[idx,:,:]) # mid and not norm (0) one_result = tf.transpose(result[idx, :, :]) - 31.5 # # mid and norm (1) # one_result = (tf.transpose(result[idx,:,:]) - 31.5) / 31.5 loss += tf.nn.l2_loss((one_pred - one_result) * one_mask) mean_losses.append(mean_loss / batch_size) var_losses.append(var_loss / batch_size) stage_losses.append(loss / batch_size) l2_loss = 0.0 for p in tl.layers.get_variables_with_name('W_', True, True): l2_loss += tf.contrib.layers.l2_regularizer(weight_decay_factor)(p) head_loss = tf.reduce_sum(mean_losses) + tf.reduce_sum( var_losses) + tf.reduce_sum(stage_losses) + l2_loss head_net.input = input # base_net input head_net.last_voxel = head_net.outputs # base_net output head_net.mask = mask head_net.voxels = result # GT head_net.stage_losses = stage_losses head_net.mean_losses = mean_losses head_net.var_losses = var_losses head_net.l2_loss = l2_loss return head_net, head_loss