public Graph BuildGraph() { var graph = new Graph().as_default(); tf_with(tf.name_scope("define_input"), scope => { input_data = tf.placeholder(dtype: tf.float32, name: "input_data"); label_sbbox = tf.placeholder(dtype: tf.float32, name: "label_sbbox"); label_mbbox = tf.placeholder(dtype: tf.float32, name: "label_mbbox"); label_lbbox = tf.placeholder(dtype: tf.float32, name: "label_lbbox"); true_sbboxes = tf.placeholder(dtype: tf.float32, name: "sbboxes"); true_mbboxes = tf.placeholder(dtype: tf.float32, name: "mbboxes"); true_lbboxes = tf.placeholder(dtype: tf.float32, name: "lbboxes"); trainable = tf.placeholder(dtype: tf.@bool, name: "training"); }); tf_with(tf.name_scope("define_loss"), scope => { model = new YOLOv3(cfg, input_data, trainable); }); tf_with(tf.name_scope("define_weight_decay"), scope => { var moving_ave = tf.train.ExponentialMovingAverage(moving_ave_decay).apply((RefVariable[])tf.trainable_variables()); }); return(graph); }
public Graph BuildGraph() { var graph = new Graph().as_default(); tf_with(tf.name_scope("define_input"), scope => { input_data = tf.placeholder(dtype: tf.float32, name: "input_data"); label_sbbox = tf.placeholder(dtype: tf.float32, name: "label_sbbox"); label_mbbox = tf.placeholder(dtype: tf.float32, name: "label_mbbox"); label_lbbox = tf.placeholder(dtype: tf.float32, name: "label_lbbox"); true_sbboxes = tf.placeholder(dtype: tf.float32, name: "sbboxes"); true_mbboxes = tf.placeholder(dtype: tf.float32, name: "mbboxes"); true_lbboxes = tf.placeholder(dtype: tf.float32, name: "lbboxes"); trainable = tf.placeholder(dtype: tf.@bool, name: "training"); }); tf_with(tf.name_scope("define_loss"), scope => { model = new YOLOv3(cfg, input_data, trainable); }); return(graph); }
public Graph BuildGraph() { var graph = new Graph().as_default(); tf_with(tf.name_scope("define_input"), scope => { input_data = tf.placeholder(dtype: tf.float32, name: "input_data"); label_sbbox = tf.placeholder(dtype: tf.float32, name: "label_sbbox"); label_mbbox = tf.placeholder(dtype: tf.float32, name: "label_mbbox"); label_lbbox = tf.placeholder(dtype: tf.float32, name: "label_lbbox"); true_sbboxes = tf.placeholder(dtype: tf.float32, name: "sbboxes"); true_mbboxes = tf.placeholder(dtype: tf.float32, name: "mbboxes"); true_lbboxes = tf.placeholder(dtype: tf.float32, name: "lbboxes"); trainable = tf.placeholder(dtype: tf.@bool, name: "training"); }); tf_with(tf.name_scope("define_loss"), scope => { model = new YOLOv3(cfg, input_data, trainable); net_var = tf.global_variables(); (giou_loss, conf_loss, prob_loss) = model.compute_loss( label_sbbox, label_mbbox, label_lbbox, true_sbboxes, true_mbboxes, true_lbboxes); loss = giou_loss + conf_loss + prob_loss; }); Tensor global_step_update = null; tf_with(tf.name_scope("learn_rate"), scope => { global_step = tf.Variable(1.0, dtype: tf.float64, trainable: false, name: "global_step"); var warmup_steps = tf.constant(warmup_periods * steps_per_period, dtype: tf.float64, name: "warmup_steps"); var train_steps = tf.constant((first_stage_epochs + second_stage_epochs) * steps_per_period, dtype: tf.float64, name: "train_steps"); learn_rate = tf.cond( pred: global_step < warmup_steps, true_fn: delegate { return(global_step / warmup_steps * learn_rate_init); }, false_fn: delegate { return(learn_rate_end + 0.5 * (learn_rate_init - learn_rate_end) * (1 + tf.cos( (global_step - warmup_steps) / (train_steps - warmup_steps) * Math.PI))); } ); global_step_update = tf.assign_add(global_step, 1.0f); }); Operation moving_ave = null; tf_with(tf.name_scope("define_weight_decay"), scope => { var emv = tf.train.ExponentialMovingAverage(moving_ave_decay); var vars = tf.trainable_variables().Select(x => (RefVariable)x).ToArray(); moving_ave = emv.apply(vars); }); tf_with(tf.name_scope("define_first_stage_train"), scope => { }); return(graph); }
public Graph BuildGraph() { var graph = new Graph().as_default(); tf_with(tf.name_scope("define_input"), scope => { input_data = tf.placeholder(dtype: tf.float32, name: "input_data"); label_sbbox = tf.placeholder(dtype: tf.float32, name: "label_sbbox"); label_mbbox = tf.placeholder(dtype: tf.float32, name: "label_mbbox"); label_lbbox = tf.placeholder(dtype: tf.float32, name: "label_lbbox"); true_sbboxes = tf.placeholder(dtype: tf.float32, name: "sbboxes"); true_mbboxes = tf.placeholder(dtype: tf.float32, name: "mbboxes"); true_lbboxes = tf.placeholder(dtype: tf.float32, name: "lbboxes"); trainable = tf.placeholder(dtype: tf.@bool, name: "training"); }); tf_with(tf.name_scope("define_loss"), scope => { model = new YOLOv3(cfg, input_data, trainable); net_var = tf.global_variables(); (giou_loss, conf_loss, prob_loss) = model.compute_loss( label_sbbox, label_mbbox, label_lbbox, true_sbboxes, true_mbboxes, true_lbboxes); loss = giou_loss + conf_loss + prob_loss; }); Tensor global_step_update = null; tf_with(tf.name_scope("learn_rate"), scope => { global_step = tf.Variable(1.0, dtype: tf.float64, trainable: false, name: "global_step"); var warmup_steps = tf.constant(warmup_periods * steps_per_period, dtype: tf.float64, name: "warmup_steps"); var train_steps = tf.constant((first_stage_epochs + second_stage_epochs) * steps_per_period, dtype: tf.float64, name: "train_steps"); learn_rate = tf.cond( pred: global_step < warmup_steps, true_fn: delegate { return(global_step / warmup_steps * learn_rate_init); }, false_fn: delegate { return(learn_rate_end + 0.5 * (learn_rate_init - learn_rate_end) * (1 + tf.cos( (global_step - warmup_steps) / (train_steps - warmup_steps) * Math.PI))); } ); global_step_update = tf.assign_add(global_step, 1.0f); }); Operation moving_ave = null; tf_with(tf.name_scope("define_weight_decay"), scope => { var emv = tf.train.ExponentialMovingAverage(moving_ave_decay); var vars = tf.trainable_variables().Select(x => (RefVariable)x).ToArray(); moving_ave = emv.apply(vars); }); tf_with(tf.name_scope("define_first_stage_train"), scope => { first_stage_trainable_var_list = new List <RefVariable>(); foreach (var var in tf.trainable_variables()) { var var_name = var.op.name; var var_name_mess = var_name.Split('/'); if (new[] { "conv_sbbox", "conv_mbbox", "conv_lbbox" }.Contains(var_name_mess[0])) { first_stage_trainable_var_list.Add(var as RefVariable); } } var adam = tf.train.AdamOptimizer(learn_rate); var first_stage_optimizer = adam.minimize(loss, var_list: first_stage_trainable_var_list); tf_with(tf.control_dependencies(tf.get_collection <Operation>(tf.GraphKeys.UPDATE_OPS).ToArray()), delegate { tf_with(tf.control_dependencies(new ITensorOrOperation[] { first_stage_optimizer, global_step_update }), delegate { tf_with(tf.control_dependencies(new[] { moving_ave }), delegate { train_op_with_frozen_variables = tf.no_op(); }); }); }); }); tf_with(tf.name_scope("define_second_stage_train"), delegate { var second_stage_trainable_var_list = tf.trainable_variables().Select(x => x as RefVariable).ToList(); var adam = tf.train.AdamOptimizer(learn_rate); var second_stage_optimizer = adam.minimize(loss, var_list: second_stage_trainable_var_list); tf_with(tf.control_dependencies(tf.get_collection <Operation>(tf.GraphKeys.UPDATE_OPS).ToArray()), delegate { tf_with(tf.control_dependencies(new ITensorOrOperation[] { second_stage_optimizer, global_step_update }), delegate { tf_with(tf.control_dependencies(new[] { moving_ave }), delegate { train_op_with_all_variables = tf.no_op(); }); }); }); }); tf_with(tf.name_scope("loader_and_saver"), delegate { loader = tf.train.Saver(net_var); saver = tf.train.Saver(tf.global_variables(), max_to_keep: 10); }); tf_with(tf.name_scope("summary"), delegate { tf.summary.scalar("learn_rate", learn_rate); tf.summary.scalar("giou_loss", giou_loss); tf.summary.scalar("conf_loss", conf_loss); tf.summary.scalar("prob_loss", prob_loss); tf.summary.scalar("total_loss", loss); }); return(graph); }