def main(): hparams = create_da_full_senet2_hparams() images, class_labels, theta_labels = get_dataset( args.dataset_dir, num_readers=1, num_preprocessing_threads=1, hparams=hparams, shuffle=False, num_epochs=1, is_training=False) with slim.arg_scope(model_arg_scope()): net, end_points = full_senet2(inputs=images, num_classes=num_classes, is_training=False, dropout_keep_prob=1.0, reuse=tf.AUTO_REUSE, scope=hparams.scope, adapt_scope='target_adapt_layer', adapt_dims=hparams.adapt_dims, reduction_ratio=hparams.reduction_ratio) theta_lebels_one_hot = tf.one_hot(theta_labels, depth=18, on_value=1.0, off_value=0.0) theta_acted = tf.reduce_sum(tf.multiply(net, theta_lebels_one_hot), axis=1, name='theta_acted') sig_op = slim.nn.sigmoid(theta_acted) conf = tf.equal( tf.to_int32(tf.greater_equal(sig_op, 0.5)), tf.to_int32(tf.greater_equal(tf.to_float(class_labels), 0.1))) saver = tf.train.Saver() session_config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) session_config.gpu_options.allow_growth = True sess = tf.Session(config=session_config) saver.restore(sess, tf.train.latest_checkpoint(args.checkpoint_dir)) print 'Successfully loading model: {}.'.format( tf.train.latest_checkpoint(args.checkpoint_dir)) sess.run(tf.local_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) num_corrects = 0 num_samples = 0 try: while not coord.should_stop(): num_corrects += int(sess.run(conf)[:]) num_samples += 1 print num_samples except tf.errors.OutOfRangeError: print 'epoch limit reached.' finally: coord.request_stop() coord.join(threads) sess.close() print num_corrects * 1.0 / num_samples
def main(): hparams = create_domain_adapt_se_hparams() images, class_labels, theta_labels = get_dataset( args.dataset_dir, num_readers=1, num_preprocessing_threads=1, hparams=hparams, shuffle=False, num_epochs=1, is_training=False) with slim.arg_scope(model_arg_scope()): net, end_points = se_model(inputs=images, num_classes=num_classes, is_training=False, dropout_keep_prob=1.0, reuse=tf.AUTO_REUSE, scope=hparams.scope, adapt_scope='target_adapt_layer', adapt_dims=hparams.adapt_dims, reduction_ratio=hparams.reduction_ratio) theta_lebels_one_hot = tf.one_hot(theta_labels, depth=18, on_value=1.0, off_value=0.0) theta_acted = tf.reduce_sum(tf.multiply(net, theta_lebels_one_hot), axis=1, name='theta_acted') sig_op = slim.nn.sigmoid(theta_acted) conf = tf.equal( tf.to_int32(tf.greater_equal(sig_op, 0.5)), tf.to_int32(tf.greater_equal(tf.to_float(class_labels), 0.1))) saver = tf.train.Saver() session_config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) session_config.gpu_options.allow_growth = True sess = tf.Session(config=session_config) saver.restore(sess, tf.train.latest_checkpoint(args.checkpoint_dir)) print 'Successfully loading model: {}.'.format( tf.train.latest_checkpoint(args.checkpoint_dir)) sess.run(tf.local_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) num_pos, num_neg = 0.0, 0.0 correct_pos, correct_neg = 0.0, 0.0 try: while not coord.should_stop(): con, label = sess.run([conf, class_labels]) con = int(con[:]) label = label[:] if label: num_neg += 1 correct_neg += con else: num_pos += 1 correct_pos += con except tf.errors.OutOfRangeError: print 'epoch limit reached.' finally: coord.request_stop() coord.join(threads) sess.close() tp = correct_pos / num_pos tn = correct_neg / num_neg fp = (num_pos - correct_pos) / num_pos fn = (num_neg - correct_neg) / num_neg print 'num_pos: {}, num_neg: {}, correct_pos: {}, correct_neg: {}'.format( num_pos, num_neg, correct_pos, correct_neg) print 'tp, tn, fp, fn: {}, {}, {}, {}'.format(tp, tn, fp, fn) print 'precision: {}'.format(tp * 1.0 / (tp + fp)) print 'recall: {}'.format(tp * 1.0 / (tp + fn)) print 'F1 score: {}'.format(2.0 * tp / (2.0 * tp + fp + tn)) print 'accuracy: {}'.format( (correct_neg + correct_pos) / (num_neg + num_pos))
def main(): tf.logging.set_verbosity(tf.logging.INFO) hparams = create_domain_adapt_hparams() for path in [args.train_log_dir]: if not tf.gfile.Exists(path): tf.gfile.MakeDirs(path) hparams_filename = os.path.join(args.train_log_dir, 'hparams.json') with tf.gfile.FastGFile(hparams_filename, 'w') as f: f.write(hparams.to_json()) with tf.Graph().as_default(): with tf.device(tf.train.replica_device_setter(args.task_id)): global_step = tf.train.get_or_create_global_step() images_p_t, class_labels_p_t, theta_labels_p_t = get_dataset( os.path.join(args.target_dir, 'positive'), args.num_readers, args.num_preprocessing_threads, hparams) images_n_t, class_labels_n_t, theta_labels_n_t = get_dataset( os.path.join(args.target_dir, 'negative'), args.num_readers, args.num_preprocessing_threads, hparams) images_t = tf.concat([images_p_t, images_n_t], axis=0) class_labels_t = tf.concat([class_labels_p_t, class_labels_n_t], axis=0) theta_labels_t = tf.concat([theta_labels_p_t, theta_labels_n_t], axis=0) with slim.arg_scope(model_arg_scope()): net_t, end_points_t = model( inputs=images_t, num_classes=num_classes, is_training=True, dropout_keep_prob=hparams.dropout_keep_prob, reuse=tf.AUTO_REUSE, scope=hparams.scope, adapt_scope='adapt_layer', adapt_dims=128) images_p_s, class_labels_p_s, theta_labels_p_s = get_dataset( os.path.join(args.source_dir, 'positive'), args.num_readers, args.num_preprocessing_threads, hparams) images_n_s, class_labels_n_s, theta_labels_n_s = get_dataset( os.path.join(args.source_dir, 'negative'), args.num_readers, args.num_preprocessing_threads, hparams) images_s = tf.concat([images_p_s, images_n_s], axis=0) class_labels_s = tf.concat([class_labels_p_s, class_labels_n_s], axis=0) theta_labels_s = tf.concat([theta_labels_p_s, theta_labels_n_s], axis=0) with slim.arg_scope(model_arg_scope()): net_s, end_points_s = model( inputs=images_s, num_classes=num_classes, is_training=True, dropout_keep_prob=hparams.dropout_keep_prob, reuse=tf.AUTO_REUSE, scope=hparams.scope, adapt_scope='adapt_layer', adapt_dims=128) net = tf.concat([net_t, net_s], axis=0) images = tf.concat([images_t, images_s], axis=0) class_labels = tf.concat([class_labels_t, class_labels_s], axis=0) theta_labels = tf.concat([theta_labels_t, theta_labels_s], axis=0) end_points = {} end_points_t[hparams.scope + '/target_adapt_layer'] = end_points_t[hparams.scope + '/adapt_layer'] end_points_s[hparams.scope + '/source_adapt_layer'] = end_points_s[hparams.scope + '/adapt_layer'] end_points.update(end_points_t) end_points.update(end_points_s) loss, accuracy = create_loss( net, end_points, class_labels, theta_labels, scope=hparams.scope, source_adapt_scope='source_adapt_layer', target_adapt_scope='target_adapt_layer') learning_rate = hparams.learning_rate if hparams.lr_decay_step: learning_rate = tf.train.exponential_decay( hparams.learning_rate, tf.train.get_or_create_global_step(), decay_steps=hparams.lr_decay_step, decay_rate=hparams.lr_decay_rate, staircase=True) tf.summary.scalar('Learning_rate', learning_rate) optimizer = tf.train.GradientDescentOptimizer(learning_rate) train_op = slim.learning.create_train_op(loss, optimizer) add_summary(images, end_points, loss, accuracy, scope='domain_adapt') summary_op = tf.summary.merge_all() variable_map = restore_map( from_adapt_checkpoint=args.from_adapt_checkpoint, scope=hparams.scope, model_name='source_only', checkpoint_exclude_scopes=['adapt_layer', 'fc8']) init_saver = tf.train.Saver(variable_map) def initializer_fn(sess): init_saver.restore( sess, tf.train.latest_checkpoint(args.checkpoint_dir)) tf.logging.info('Successfully load pretrained checkpoint.') init_fn = initializer_fn session_config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) session_config.gpu_options.allow_growth = True saver = tf.train.Saver( keep_checkpoint_every_n_hours=args.save_interval_secs, max_to_keep=200) slim.learning.train(train_op, logdir=args.train_log_dir, master=args.master, global_step=global_step, session_config=session_config, init_fn=init_fn, summary_op=summary_op, number_of_steps=args.num_steps, startup_delay_steps=15, save_summaries_secs=args.save_summaries_steps, saver=saver)
def main(): rospy.init_node(name='grasping', anonymous=True) pnp = BaxterAPI(args.limb, args.hover_distance, args.reach_distance) # pick and place pnp.move_to_start(initial_pose) image_tools = ImageTools(img_id=args.img_id) images_t = tf.placeholder(dtype=tf.float32, shape=[None, 224, 224, 3]) images_t = images_t - [123.68, 116.779, 103.939] with slim.arg_scope(model_arg_scope()): net, end_points = model(inputs=images_t, num_classes=num_classes, is_training=False, dropout_keep_prob=1.0, scope='target_only') # with slim.arg_scope(model_arg_scope()): # net, end_points = model(inputs=images_t, # num_classes=num_classes, # is_training=False, # dropout_keep_prob=1.0, # reuse=None, # scope='domain_adapt', # adapt_scope='target_adapt_layer', # adapt_dims=128) angle_index_t = tf.argmin(net, axis=1) saver = tf.train.Saver() session_config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) session_config.gpu_options.allow_growth = True sess = tf.Session(config=session_config) saver.restore(sess, tf.train.latest_checkpoint(args.checkpoint_dir)) print 'Successfully loading model.' while not rospy.is_shutdown(): flag = 0 while flag != 1: c_x, c_y = utils.get_center_coordinate_from_kinect( args.center_coor, args.limb) d_x = float(c_x) / 1000.0 d_y = float(c_y) / 1000.0 x = origin[0] + d_x y = origin[1] + d_y z = origin[2] flag = pnp.approach( Pose(position=Point(x, y, z), orientation=initial_orientation)) rospy.sleep(0.7) utils.listener(args.topic_name, image_tools) coors, images = image_tools.sampling_image(args.patch_size, args.num_patches) scores = sess.run(net, feed_dict={images_t: images}) patch, coor, new_coors = image_tools.resampling_image( scores, coors, args.patch_size) angle_index, scores = sess.run([angle_index_t, net], feed_dict={images_t: patch}) angle_index = angle_index[0] print angle_index print scores grasp_angle = (angle_index * 10 - 90) * 1.0 / 180 * pi x -= float(coor[0] - 160) * 0.6 / 1000 y -= float(coor[1] - 320) * 0.5 / 1000 z = z pose = Pose(position=Point(x, y, z), orientation=initial_orientation) success = pnp.pick(pose, image_tools, args.output_path, new_coors, coor, args.patch_size, grasp_angle) pnp.move_to_start(initial_pose)