def input_fn(): all_anchors, num_anchors_list = anchor_creator.get_all_anchors() anchor_encoder_decoder = anchor_manipulator.AnchorEncoder( all_anchors, num_classes=FLAGS.num_classes, allowed_borders=[0.05], positive_threshold=FLAGS.match_threshold, ignore_threshold=FLAGS.neg_threshold, prior_scaling=[0.1, 0.1, 0.2, 0.2]) list_from_batch, _ = dataset_factory.get_dataset( FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.data_dir, image_preprocessing_fn, file_pattern=None, reader=None, batch_size=FLAGS.batch_size, num_readers=FLAGS.num_readers, num_preprocessing_threads=FLAGS.num_preprocessing_threads, num_epochs=FLAGS.train_epochs, anchor_encoder=anchor_encoder_decoder.encode_all_anchors) return list_from_batch[-1], { 'targets': list_from_batch[:-1], 'decode_fn': lambda pred: anchor_encoder_decoder.decode_all_anchors([pred])[0], 'num_anchors_list': num_anchors_list }
def input_fn(): all_anchors, num_anchors_list = anchor_creator.get_all_anchors() anchor_encoder_decoder = anchor_manipulator.AnchorEncoder(all_anchors, num_classes = FLAGS.num_classes, allowed_borders = [0.], positive_threshold = FLAGS.rpn_match_threshold, ignore_threshold = FLAGS.rpn_neg_threshold, prior_scaling=[1., 1., 1., 1.],#[0.1, 0.1, 0.2, 0.2], rpn_fg_thres = FLAGS.match_threshold, rpn_bg_high_thres = FLAGS.neg_threshold_high, rpn_bg_low_thres = FLAGS.neg_threshold_low) list_from_batch, _ = dataset_factory.get_dataset(FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.data_dir, image_preprocessing_fn, file_pattern = None, reader = None, batch_size = FLAGS.batch_size, num_readers = FLAGS.num_readers, num_preprocessing_threads = FLAGS.num_preprocessing_threads, num_epochs = FLAGS.train_epochs, anchor_encoder = anchor_encoder_decoder.encode_all_anchors) #print(list_from_batch[-4], list_from_batch[-3]) return list_from_batch[-1], {'targets': list_from_batch[:-1], 'rpn_decode_fn': lambda pred : anchor_encoder_decoder.decode_all_anchors([pred], squeeze_inner=True)[0], 'head_decode_fn': lambda rois, pred : anchor_encoder_decoder.ext_decode_rois(rois, pred, head_prior_scaling=[1., 1., 1., 1.]), 'rpn_encode_fn': lambda rois : anchor_encoder_decoder.ext_encode_rois(rois, list_from_batch[-4], list_from_batch[-3], FLAGS.roi_one_image, FLAGS.fg_ratio, 0.1, head_prior_scaling=[1., 1., 1., 1.]), 'num_anchors_list': num_anchors_list}
def main(_): print(tf.gfile.Glob('./debug/example_01?.jpg')) if not FLAGS.data_dir: raise ValueError( 'You must supply the dataset directory with --data_dir') tf.logging.set_verbosity(tf.logging.DEBUG) with tf.Graph().as_default(): global_step = slim.create_global_step() #print(tf.gfile.Glob('./debug/example_01?.jpg')) preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name image_preprocessing_fn = lambda image_, shape_, glabels_, gbboxes_: preprocessing_factory.get_preprocessing( preprocessing_name, is_training=True)(image_, glabels_, gbboxes_, out_shape= [FLAGS.train_image_size] * 2, data_format=DATA_FORMAT) anchor_creator = anchor_manipulator.AnchorCreator( [FLAGS.train_image_size] * 2, layers_shapes=[(38, 38), (19, 19), (10, 10)], anchor_scales=[[0.1, 0.2], [0.3, 0.4], [0.5, 0.6]], extra_anchor_scales=[[0.15], [0.35], [0.55]], anchor_ratios=[[2, .5], [2, .5, 3, 1. / 3], [2, .5, 3, 1. / 3]], layer_steps=[8, 16, 32]) all_anchors = anchor_creator.get_all_anchors()[0] # sess = tf.Session() # print(all_anchors) # print(sess.run(all_anchors)) anchor_operator = anchor_manipulator.AnchorEncoder( all_anchors, num_classes=FLAGS.num_classes, ignore_threshold=0., prior_scaling=[0.1, 0.1, 0.2, 0.2]) #anchor_encoder_fn = lambda next_iter, _ = dataset_factory.get_dataset( FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.data_dir, image_preprocessing_fn, file_pattern=None, reader=None, batch_size=FLAGS.batch_size, num_readers=FLAGS.num_readers, num_preprocessing_threads=FLAGS.num_preprocessing_threads, anchor_encoder=anchor_operator.encode_all_anchors) sess = tf.Session() sess.run( tf.group(tf.global_variables_initializer(), tf.local_variables_initializer(), tf.tables_initializer())) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) count = 0 start_time = time.time() try: while not coord.should_stop(): count += 1 _ = sess.run([next_iter]) if count % 10 == 0: time_elapsed = time.time() - start_time print('time: {}'.format(time_elapsed / 10.)) start_time = time.time() except tf.errors.OutOfRangeError: log.info('Queue Done!') finally: pass # Wait for threads to finish. coord.join(threads) sess.close() for i in range(6): list_from_batch = sess.run(next_iter) # imsave('./debug/example_%03d.jpg' % (i,), list_from_batch[0][0]) # imsave('./debug/example_%03d_.jpg' % (i,), list_from_batch[1][0]) image = list_from_batch[-1] shape = list_from_batch[-2] glabels = list_from_batch[:len(all_anchors)] gtargets = list_from_batch[len(all_anchors):2 * len(all_anchors)] gscores = list_from_batch[2 * len(all_anchors):3 * len(all_anchors)] imsave('./debug/example_%03d.jpg' % (i, ), image[0]) print(image.shape, shape.shape, glabels[0].shape, gtargets[0].shape, gscores[0].shape)
def main(_): with tf.Graph().as_default(): image_input = tf.placeholder(tf.uint8, shape=(None, None, 3)) shape_input = tf.placeholder(tf.int32, shape=(2, )) features = common_preprocessing.light_head_preprocess_for_test( image_input, [FLAGS.train_image_size] * 2, data_format=('NCHW' if FLAGS.data_format == 'channels_first' else 'NHWC')) features = tf.expand_dims(features, axis=0) anchor_creator = anchor_manipulator.AnchorCreator( [FLAGS.train_image_size] * 2, layers_shapes=[(30, 30)], anchor_scales=[[0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]], extra_anchor_scales=[[0.1]], anchor_ratios=[[1., 2., .5]], layer_steps=[16]) all_anchors, num_anchors_list = anchor_creator.get_all_anchors() anchor_encoder_decoder = anchor_manipulator.AnchorEncoder( all_anchors, num_classes=FLAGS.num_classes, allowed_borders=None, positive_threshold=None, ignore_threshold=None, prior_scaling=[1., 1., 1., 1.]) with tf.variable_scope(FLAGS.model_scope, default_name=None, values=[features], reuse=tf.AUTO_REUSE): rpn_feat_map, backbone_feat = xception_body.XceptionBody( features, FLAGS.num_classes, is_training=False, data_format=FLAGS.data_format) #rpn_feat_map = tf.Print(rpn_feat_map,[tf.shape(rpn_feat_map), rpn_feat_map,backbone_feat]) rpn_cls_score, rpn_bbox_pred = xception_body.get_rpn( rpn_feat_map, num_anchors_list[0], False, FLAGS.data_format, 'rpn_head') large_sep_feature = xception_body.large_sep_kernel( backbone_feat, 256, 10 * 7 * 7, False, FLAGS.data_format, 'large_sep_feature') if FLAGS.data_format == 'channels_first': rpn_cls_score = tf.transpose(rpn_cls_score, [0, 2, 3, 1]) rpn_bbox_pred = tf.transpose(rpn_bbox_pred, [0, 2, 3, 1]) rpn_cls_score = tf.reshape(rpn_cls_score, [-1, 2]) rpn_object_score = tf.nn.softmax(rpn_cls_score)[:, -1] rpn_object_score = tf.reshape(rpn_object_score, [1, -1]) rpn_location_pred = tf.reshape(rpn_bbox_pred, [1, -1, 4]) rpn_bboxes_pred = anchor_encoder_decoder.decode_all_anchors( [rpn_location_pred], squeeze_inner=True)[0] proposals_bboxes = xception_body.get_proposals( rpn_object_score, rpn_bboxes_pred, None, FLAGS.rpn_pre_nms_top_n, FLAGS.rpn_post_nms_top_n, FLAGS.rpn_nms_thres, FLAGS.rpn_min_size, False, FLAGS.data_format) cls_score, bboxes_reg = xception_body.get_head( large_sep_feature, lambda input_, bboxes_, grid_width_, grid_height_: ps_roi_align( input_, bboxes_, grid_width_, grid_height_, pool_method), 7, 7, None, proposals_bboxes, FLAGS.num_classes, False, False, 0, FLAGS.data_format, 'final_head') head_bboxes_pred = anchor_encoder_decoder.ext_decode_rois( proposals_bboxes, bboxes_reg, head_prior_scaling=[1., 1., 1., 1.]) head_cls_score = tf.reshape(cls_score, [-1, FLAGS.num_classes]) head_cls_score = tf.nn.softmax(head_cls_score) head_bboxes_pred = tf.reshape(head_bboxes_pred, [-1, 4]) with tf.device('/device:CPU:0'): selected_scores, selected_bboxes = eval_helper.tf_bboxes_select( [head_cls_score], [head_bboxes_pred], FLAGS.select_threshold, FLAGS.num_classes, scope='xdet_v2_select') selected_bboxes = eval_helper.bboxes_clip( tf.constant([0., 0., 1., 1.]), selected_bboxes) selected_scores, selected_bboxes = eval_helper.filter_boxes( selected_scores, selected_bboxes, 0.03, shape_input, [FLAGS.train_image_size] * 2, keep_top_k=FLAGS.nms_topk * 2) # Resize bboxes to original image shape. selected_bboxes = eval_helper.bboxes_resize( tf.constant([0., 0., 1., 1.]), selected_bboxes) selected_scores, selected_bboxes = eval_helper.bboxes_sort( selected_scores, selected_bboxes, top_k=FLAGS.nms_topk * 2) # Apply NMS algorithm. selected_scores, selected_bboxes = eval_helper.bboxes_nms_batch( selected_scores, selected_bboxes, nms_threshold=FLAGS.nms_threshold, keep_top_k=FLAGS.nms_topk) labels_list = [] for k, v in selected_scores.items(): labels_list.append(tf.ones_like(v, tf.int32) * k) all_labels = tf.concat(labels_list, axis=0) all_scores = tf.concat(list(selected_scores.values()), axis=0) all_bboxes = tf.concat(list(selected_bboxes.values()), axis=0) saver = tf.train.Saver() with tf.Session() as sess: init = tf.global_variables_initializer() sess.run(init) saver.restore(sess, FLAGS.checkpoint_path) np_image = imread('./demo/test.jpg') labels_, scores_, bboxes_ = sess.run( [all_labels, all_scores, all_bboxes], feed_dict={ image_input: np_image, shape_input: np_image.shape[:-1] }) img_to_draw = draw_toolbox.bboxes_draw_on_img(np_image, labels_, scores_, bboxes_, thickness=2) imsave(os.path.join(FLAGS.debug_dir, 'test_out.jpg'), img_to_draw)