def extract_batch(dataset, config): with tf.device("/cpu:0"): bboxer = PriorBoxGrid(config) data_provider = slim.dataset_data_provider.DatasetDataProvider( dataset, num_readers=2, common_queue_capacity=512, common_queue_min=32) if args.segment: im, bbox, gt, seg = data_provider.get( ['image', 'object/bbox', 'object/label', 'image/segmentation']) else: im, bbox, gt = data_provider.get( ['image', 'object/bbox', 'object/label']) seg = tf.expand_dims(tf.zeros(tf.shape(im)[:2]), 2) im = tf.to_float(im) / 255 bbox = yxyx_to_xywh(tf.clip_by_value(bbox, 0.0, 1.0)) im, bbox, gt, seg = data_augmentation(im, bbox, gt, seg, config) inds, cats, refine = bboxer.encode_gt_tf(bbox, gt) return tf.train.shuffle_batch([im, inds, refine, cats, seg], args.batch_size, 2048, 64, num_threads=4)
def extract_batch(dataset, config): with tf.device("/cpu:0"): bboxer = PriorBoxGrid(config) data_provider = slim.dataset_data_provider.DatasetDataProvider( dataset, num_readers=2, common_queue_capacity=512, common_queue_min=32) if args.instance and args.segment: im, bbox, gt, seg, ins, im_h, im_w = \ data_provider.get(['image', 'object/bbox', 'object/label', 'image/segmentation', 'image/instance', 'image/height', 'image/width']) print("ins: ", ins) print("bbox: ", bbox) print("gt: ", gt) ins_shape = tf.stack([im_h, im_w, args.instance_num], axis=0) ins = tf.reshape(ins, ins_shape) elif args.segment: im, bbox, gt, seg = data_provider.get( ['image', 'object/bbox', 'object/label', 'image/segmentation']) elif args.instance: im, bbox, gt, ins, im_h, im_w = \ data_provider.get(['image', 'object/bbox', 'object/label', 'image/instance', 'image/height', 'image/width']) ins_shape = tf.stack([im_h, im_w, args.instance_num], axis=0) ins = tf.reshape(ins, ins_shape) else: im, bbox, gt = data_provider.get( ['image', 'object/bbox', 'object/label']) seg = tf.expand_dims(tf.zeros(tf.shape(im)[:2]), 2) ins = tf.expand_dims(tf.zeros(tf.shape(im)[:2]), 2) im = tf.to_float(im) / 255 bbox = yxyx_to_xywh(tf.clip_by_value(bbox, 0.0, 1.0)) im, bbox, gt, seg, ins = data_augmentation(im, bbox, gt, seg, ins, config) inds, cats, refine, gt_matches = bboxer.encode_gt_tf(bbox, gt) bbox_pads = args.instance_num - tf.shape(bbox)[0] paddings = [[0, bbox_pads], [0, 0]] bbox = tf.pad(bbox, paddings) bbox = tf.reshape(bbox, (args.instance_num, 4)) return tf.train.shuffle_batch( [im, inds, refine, cats, seg, ins, gt_matches, bbox], args.batch_size, 2048, 64, num_threads=4)
def __init__(self, sess, net, loader, config, no_gt=False, folder=None, profiler=None): self.sess = sess self.net = net self.loader = loader self.config = config self.fm_sizes = self.config['fm_sizes'] self.no_gt = no_gt self.bboxer = PriorBoxGrid(self.config) self.profiler = profiler self.run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) self.run_metadata = tf.RunMetadata() self.build_detector() if folder is not None: self.directory = folder else: self.directory = os.path.join(RESULTS_DIR, args.run_name) if not os.path.exists(self.directory): os.makedirs(self.directory)
def extract_batch(dataset, config): with tf.device("/cpu:0"): bboxer = PriorBoxGrid(config) data_provider = slim.dataset_data_provider.DatasetDataProvider( dataset, num_readers=2, common_queue_capacity=512, common_queue_min=32) if args.segment: im, bbox, gt, seg = data_provider.get(['image', 'object/bbox', 'object/label', 'image/segmentation']) else: im, bbox, gt = data_provider.get(['image', 'object/bbox', 'object/label']) seg = tf.expand_dims(tf.zeros(tf.shape(im)[:2]), 2) im = tf.to_float(im)/255 bbox = yxyx_to_xywh(tf.clip_by_value(bbox, 0.0, 1.0)) im, bbox, gt, seg = data_augmentation(im, bbox, gt, seg, config) inds, cats, refine = bboxer.encode_gt_tf(bbox, gt) return tf.train.shuffle_batch([im, inds, refine, cats, seg], args.batch_size, 2048, 64, num_threads=4)
def __init__(self, sess, net, loader, config, no_gt=False): self.sess = sess self.net = net self.loader = loader self.config = config self.fm_sizes = self.config['fm_sizes'] self.no_gt = no_gt self.bboxer = PriorBoxGrid(self.config) self.build_detector() self.directory = os.path.join(RESULTS_DIR, args.run_name) if not os.path.exists(self.directory): os.makedirs(self.directory)
def extract_batch(dataset, config): with tf.device("/cpu:0"): bboxer = PriorBoxGrid(config) data_provider = slim.dataset_data_provider.DatasetDataProvider( dataset, num_readers=2, common_queue_capacity=512, common_queue_min=32) if args.instance and args.segment: im, bbox, gt, seg, ins, im_h, im_w = \ data_provider.get(['image', 'object/bbox', 'object/label', 'image/segmentation', 'image/instance', 'image/height', 'image/width']) ins_num = tf.cast(tf.shape(bbox)[0], tf.int64) ins_shape = tf.stack([ins_num, im_h, im_w], axis=0) ins = tf.reshape(ins, ins_shape) ins = tf.transpose(ins, [1, 2, 0]) pads = args.instance_num - tf.shape(ins)[-1] ins = tf.pad(ins, [[0, 0], [0, 0], [0, pads]]) # ins_summary = tf.image.resize_images(ins, (300, 300)) # ins_summary = tf.transpose(ins_summary, [2, 0, 1]) # ins_summary = ins_summary[0] # ins_summary = tf.reshape(ins_summary, [300, 300]) elif args.segment: im, bbox, gt, seg = data_provider.get( ['image', 'object/bbox', 'object/label', 'image/segmentation']) elif args.instance: im, bbox, gt, ins, im_h, im_w = \ data_provider.get(['image', 'object/bbox', 'object/label', 'image/instance', 'image/height', 'image/width']) ins_num = tf.cast(tf.shape(bbox)[0], tf.int64) ins_shape = tf.stack([ins_num, im_h, im_w], axis=0) ins = tf.reshape(ins, ins_shape) ins = tf.transpose(ins, [1, 2, 0]) pads = args.instance_num - tf.shape(ins)[-1] ins = tf.pad(ins, [[0, 0], [0, 0], [0, pads]]) seg = tf.expand_dims(tf.zeros(tf.shape(im)[:2]), 2) else: im, bbox, gt = data_provider.get( ['image', 'object/bbox', 'object/label']) seg = tf.expand_dims(tf.zeros(tf.shape(im)[:2]), 2) ins = tf.expand_dims(tf.zeros(tf.shape(im)[:2]), 2) im = tf.to_float(im) / 255 bbox = yxyx_to_xywh(tf.clip_by_value(bbox, 0.0, 1.0)) im, bbox, gt, seg, ins = data_augmentation(im, bbox, gt, seg, ins, config) inds, cats, refine, gt_matches = bboxer.encode_gt_tf(bbox, gt) # ins_summary = ins not_empty_gt_matches = tf.reduce_sum(gt_matches) > 0 gt_matches_idx = tf.reshape(tf.where(gt_matches > 0), (-1, )) # gt_matches_idx, _ = tf.unique(gt_matches_idx) match_roi_info = tf.cond( not_empty_gt_matches, true_fn=lambda: get_match_roi_info(gt_matches_idx, roi_info), false_fn=lambda: tf.zeros((args.instance_num, 5))) gt_matches = tf.gather(gt_matches, gt_matches_idx) gt_matches = tf.cond(not_empty_gt_matches, lambda: gt_matches - 1, lambda: gt_matches) bbox_matches = tf.gather(bbox, gt_matches) need_pad = args.instance_num > tf.shape(bbox_matches)[0] pads = tf.cond(need_pad, lambda: args.instance_num - tf.shape(bbox_matches)[0], lambda: 0) bbox_matches = tf.pad(bbox_matches, [[0, pads], [0, 0]]) bbox_matches = bbox_matches[:args.instance_num, :] # ins = tf.gather(ins, gt_matches) # ins = tf.pad(ins, [[0, pads], [0, 0], [0, 0]]) # ins = ins[:args.instance_num, ...] # if tf.reduce_sum(gt_matches) > 0: # gt_matches_idx = tf.where(gt_matches > 0) # match_roi_info = tf.squeeze(tf.gather(roi_info, gt_matches_idx // 6)) # (?, 5) # # print("gt_matches_idx: ", gt_matches_idx) # # print("roi_info: ", roi_info) # print("match roi info shape: ", match_roi_info.shape) # roi_pads = args.instance_num - tf.shape(match_roi_info)[0] # tf.pad(match_roi_info, [[0, roi_pads], [0, 0]], # mode='CONSTANT', constant_values=0) # match_roi_info = tf.reshape(match_roi_info, (args.instance_num, 5)) # else: # match_roi_info = tf.zeros((args.instance_num, 5)) need_pad = args.instance_num > tf.shape(gt_matches)[0] pads = tf.cond(need_pad, lambda: args.instance_num - tf.shape(gt_matches)[0], lambda: 0) gt_matches = tf.pad(gt_matches, [[0, pads]]) gt_matches = gt_matches[:args.instance_num] ins = extract_matched_gt_instance(bbox_matches, ins, [args.ins_shape, args.ins_shape], gt_matches) ins = tf.reshape(ins, (args.instance_num, args.ins_shape, args.ins_shape)) tf.summary.tensor_summary('tensor/bbox_matches', bbox_matches) tf.summary.tensor_summary('tensor/gt_matches', gt_matches) ins_cats = tf.gather(cats, gt_matches_idx) need_pad = args.instance_num > tf.shape(ins_cats)[0] pads = tf.cond(need_pad, lambda: args.instance_num - tf.shape(ins_cats)[0], lambda: 0) ins_cats = tf.pad(ins_cats, [[0, pads]]) ins_cats = ins_cats[:args.instance_num] ins_cats = tf.reshape(ins_cats, (args.instance_num, )) return tf.train.shuffle_batch( [im, inds, refine, cats, ins_cats, seg, ins, match_roi_info], args.batch_size, 2048, 64, num_threads=4)
def main(argv=None): # pylint: disable=unused-argument net = ResNet depth = 50 loader = VOCLoader('07', 'test') net = net(config=net_config, depth=depth, training=False) num_classes = 21 batch_size = args.batch_size img_size = args.image_size image_ph = tf.placeholder(shape=[1, img_size, img_size, 3], dtype=tf.float32, name='img_ph') net.create_trunk(image_ph) bboxer = PriorBoxGrid(net_config) net.create_multibox_head(num_classes) confidence = tf.nn.softmax(tf.squeeze(net.outputs['confidence'])) location = tf.squeeze(net.outputs['location']) good_bboxes = decode_bboxes(location, bboxer.tiling) detection_list = [] score_list = [] for i in range(1, num_classes): class_mask = tf.greater(confidence[:, i], args.conf_thresh) class_scores = tf.boolean_mask(confidence[:, i], class_mask) class_bboxes = tf.boolean_mask(good_bboxes, class_mask) K = tf.minimum(tf.size(class_scores), args.top_k_nms) _, top_k_inds = tf.nn.top_k(class_scores, K) top_class_scores = tf.gather(class_scores, top_k_inds) top_class_bboxes = tf.gather(class_bboxes, top_k_inds) final_inds = tf.image.non_max_suppression( top_class_bboxes, top_class_scores, max_output_size=50, iou_threshold=args.nms_thresh) final_class_bboxes = tf.gather(top_class_bboxes, final_inds) final_scores = tf.gather(top_class_scores, final_inds) detection_list.append(final_class_bboxes) score_list.append(final_scores) net.create_segmentation_head(num_classes) segmentation = tf.cast( tf.argmax(tf.squeeze(net.outputs['segmentation']), axis=-1), tf.int32) times = [] # im = cv2.imread("/home/lear/kshmelko/testImage.jpg") # im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)/255.0 # im = im.astype(np.float32) with tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=False)) as sess: sess.run(tf.global_variables_initializer()) ckpt_path = train_dir + '/model.ckpt-%i000' % args.ckpt log.debug("Restoring checkpoint %s" % ckpt_path) saver = tf.train.Saver(tf.global_variables()) saver.restore(sess, ckpt_path) for i in range(200): im = loader.load_image(loader.get_filenames()[i]) im = cv2.resize(im, (img_size, img_size)) im = im.reshape((1, img_size, img_size, 3)) st = time.time() sess.run([detection_list, score_list, segmentation], feed_dict={image_ph: im}) et = time.time() if i > 10: times.append(et - st) m = np.mean(times) s = np.std(times) fps = 1 / m log.info("Mean={0:.2f}ms; Std={1:.2f}ms; FPS={2:.1f}".format( m * 1000, s * 1000, fps))