Python detectron.utils.c2.NamedCudaScope() Examples
The following are 30
code examples of detectron.utils.c2.NamedCudaScope().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
detectron.utils.c2
, or try the search function
.
Example #1
Source File: optimizer.py From Detectron-Cascade-RCNN with Apache License 2.0 | 6 votes |
def build_data_parallel_model(model, single_gpu_build_func): """Build a data parallel model given a function that builds the model on a single GPU. """ if model.only_build_forward_pass: single_gpu_build_func(model) elif model.train: all_loss_gradients = _build_forward_graph(model, single_gpu_build_func) # Add backward pass on all GPUs model.AddGradientOperators(all_loss_gradients) if cfg.NUM_GPUS > 1: _add_allreduce_graph(model) for gpu_id in range(cfg.NUM_GPUS): # After allreduce, all GPUs perform SGD updates on their identical # params and gradients in parallel with c2_utils.NamedCudaScope(gpu_id): add_single_gpu_param_update_ops(model, gpu_id) else: # Test-time network operates on single GPU # Test-time parallelism is implemented through multiprocessing with c2_utils.NamedCudaScope(model.target_gpu_id): single_gpu_build_func(model)
Example #2
Source File: optimizer.py From Detectron-DA-Faster-RCNN with Apache License 2.0 | 6 votes |
def build_data_parallel_model(model, single_gpu_build_func): """Build a data parallel model given a function that builds the model on a single GPU. """ if model.only_build_forward_pass: single_gpu_build_func(model) elif model.train: all_loss_gradients = _build_forward_graph(model, single_gpu_build_func) # Add backward pass on all GPUs model.AddGradientOperators(all_loss_gradients) if cfg.NUM_GPUS > 1: _add_allreduce_graph(model) for gpu_id in range(cfg.NUM_GPUS): # After allreduce, all GPUs perform SGD updates on their identical # params and gradients in parallel with c2_utils.NamedCudaScope(gpu_id): add_single_gpu_param_update_ops(model, gpu_id) else: # Test-time network operates on single GPU # Test-time parallelism is implemented through multiprocessing with c2_utils.NamedCudaScope(model.target_gpu_id): single_gpu_build_func(model)
Example #3
Source File: optimizer.py From CBNet with Apache License 2.0 | 6 votes |
def build_data_parallel_model(model, single_gpu_build_func): """Build a data parallel model given a function that builds the model on a single GPU. """ if model.only_build_forward_pass: single_gpu_build_func(model) elif model.train: all_loss_gradients = _build_forward_graph(model, single_gpu_build_func) # Add backward pass on all GPUs model.AddGradientOperators(all_loss_gradients) if cfg.NUM_GPUS > 1: _add_allreduce_graph(model) for gpu_id in range(cfg.NUM_GPUS): # After allreduce, all GPUs perform SGD updates on their identical # params and gradients in parallel with c2_utils.NamedCudaScope(gpu_id): add_single_gpu_param_update_ops(model, gpu_id) else: # Test-time network operates on single GPU # Test-time parallelism is implemented through multiprocessing with c2_utils.NamedCudaScope(model.target_gpu_id): single_gpu_build_func(model)
Example #4
Source File: optimizer.py From Detectron with Apache License 2.0 | 6 votes |
def build_data_parallel_model(model, single_gpu_build_func): """Build a data parallel model given a function that builds the model on a single GPU. """ if model.only_build_forward_pass: single_gpu_build_func(model) elif model.train: all_loss_gradients = _build_forward_graph(model, single_gpu_build_func) # Add backward pass on all GPUs model.AddGradientOperators(all_loss_gradients) if cfg.NUM_GPUS > 1: _add_allreduce_graph(model) for gpu_id in range(cfg.NUM_GPUS): # After allreduce, all GPUs perform SGD updates on their identical # params and gradients in parallel with c2_utils.NamedCudaScope(gpu_id): add_single_gpu_param_update_ops(model, gpu_id) else: # Test-time network operates on single GPU # Test-time parallelism is implemented through multiprocessing with c2_utils.NamedCudaScope(model.target_gpu_id): single_gpu_build_func(model)
Example #5
Source File: optimizer.py From Clustered-Object-Detection-in-Aerial-Image with Apache License 2.0 | 6 votes |
def build_data_parallel_model(model, single_gpu_build_func): """Build a data parallel model given a function that builds the model on a single GPU. """ if model.only_build_forward_pass: single_gpu_build_func(model) elif model.train: all_loss_gradients = _build_forward_graph(model, single_gpu_build_func) # Add backward pass on all GPUs model.AddGradientOperators(all_loss_gradients) if cfg.NUM_GPUS > 1: _add_allreduce_graph(model) for gpu_id in range(cfg.NUM_GPUS): # After allreduce, all GPUs perform SGD updates on their identical # params and gradients in parallel with c2_utils.NamedCudaScope(gpu_id): add_single_gpu_param_update_ops(model, gpu_id) else: # Test-time network operates on single GPU # Test-time parallelism is implemented through multiprocessing with c2_utils.NamedCudaScope(model.target_gpu_id): single_gpu_build_func(model)
Example #6
Source File: rpn_generator.py From Detectron with Apache License 2.0 | 5 votes |
def generate_proposals_on_roidb( model, roidb, start_ind=None, end_ind=None, total_num_images=None, gpu_id=0, ): """Generate RPN proposals on all images in an imdb.""" _t = Timer() num_images = len(roidb) roidb_boxes = [[] for _ in range(num_images)] roidb_scores = [[] for _ in range(num_images)] roidb_ids = [[] for _ in range(num_images)] if start_ind is None: start_ind = 0 end_ind = num_images total_num_images = num_images for i in range(num_images): roidb_ids[i] = roidb[i]['id'] im = cv2.imread(roidb[i]['image']) with c2_utils.NamedCudaScope(gpu_id): _t.tic() roidb_boxes[i], roidb_scores[i] = im_proposals(model, im) _t.toc() if i % 10 == 0: ave_time = _t.average_time eta_seconds = ave_time * (num_images - i - 1) eta = str(datetime.timedelta(seconds=int(eta_seconds))) logger.info( ( 'rpn_generate: range [{:d}, {:d}] of {:d}: ' '{:d}/{:d} {:.3f}s (eta: {})' ).format( start_ind + 1, end_ind, total_num_images, start_ind + i + 1, start_ind + num_images, ave_time, eta ) ) return roidb_boxes, roidb_scores, roidb_ids
Example #7
Source File: rpn_generator.py From Detectron-Cascade-RCNN with Apache License 2.0 | 5 votes |
def generate_proposals_on_roidb( model, roidb, start_ind=None, end_ind=None, total_num_images=None, gpu_id=0, ): """Generate RPN proposals on all images in an imdb.""" _t = Timer() num_images = len(roidb) roidb_boxes = [[] for _ in range(num_images)] roidb_scores = [[] for _ in range(num_images)] roidb_ids = [[] for _ in range(num_images)] if start_ind is None: start_ind = 0 end_ind = num_images total_num_images = num_images for i in range(num_images): roidb_ids[i] = roidb[i]['id'] im = cv2.imread(roidb[i]['image']) with c2_utils.NamedCudaScope(gpu_id): _t.tic() roidb_boxes[i], roidb_scores[i] = im_proposals(model, im) _t.toc() if i % 10 == 0: ave_time = _t.average_time eta_seconds = ave_time * (num_images - i - 1) eta = str(datetime.timedelta(seconds=int(eta_seconds))) logger.info( ( 'rpn_generate: range [{:d}, {:d}] of {:d}: ' '{:d}/{:d} {:.3f}s (eta: {})' ).format( start_ind + 1, end_ind, total_num_images, start_ind + i + 1, start_ind + num_images, ave_time, eta ) ) return roidb_boxes, roidb_scores, roidb_ids
Example #8
Source File: optimizer.py From CBNet with Apache License 2.0 | 5 votes |
def _build_forward_graph(model, single_gpu_build_func): """Construct the forward graph on each GPU.""" all_loss_gradients = {} # Will include loss gradients from all GPUs # Build the model on each GPU with correct name and device scoping for gpu_id in range(cfg.NUM_GPUS): with c2_utils.NamedCudaScope(gpu_id): all_loss_gradients.update(single_gpu_build_func(model)) return all_loss_gradients
Example #9
Source File: infer.py From Detectron-Cascade-RCNN with Apache License 2.0 | 5 votes |
def get_rpn_box_proposals(im, args): cfg.immutable(False) merge_cfg_from_file(args.rpn_cfg) cfg.NUM_GPUS = 1 cfg.MODEL.RPN_ONLY = True cfg.TEST.RPN_PRE_NMS_TOP_N = 10000 cfg.TEST.RPN_POST_NMS_TOP_N = 2000 assert_and_infer_cfg(cache_urls=False) model = model_engine.initialize_model_from_cfg(args.rpn_pkl) with c2_utils.NamedCudaScope(0): boxes, scores = rpn_engine.im_proposals(model, im) return boxes, scores
Example #10
Source File: loader.py From Detectron with Apache License 2.0 | 5 votes |
def create_enqueue_blobs(self): blob_names = self.get_output_names() enqueue_blob_names = [ '{}_enqueue_{}'.format(b, self._loader_id) for b in blob_names ] for gpu_id in range(self._num_gpus): with c2_utils.NamedCudaScope(gpu_id): for blob in enqueue_blob_names: workspace.CreateBlob(core.ScopedName(blob)) return enqueue_blob_names
Example #11
Source File: optimizer.py From Detectron with Apache License 2.0 | 5 votes |
def _build_forward_graph(model, single_gpu_build_func): """Construct the forward graph on each GPU.""" all_loss_gradients = {} # Will include loss gradients from all GPUs # Build the model on each GPU with correct name and device scoping for gpu_id in range(cfg.NUM_GPUS): with c2_utils.NamedCudaScope(gpu_id): all_loss_gradients.update(single_gpu_build_func(model)) return all_loss_gradients
Example #12
Source File: model_builder.py From Detectron with Apache License 2.0 | 5 votes |
def add_training_inputs(model, roidb=None): """Create network input ops and blobs used for training. To be called *after* model_builder.create(). """ # Implementation notes: # Typically, one would create the input ops and then the rest of the net. # However, creating the input ops depends on loading the dataset, which # can take a few minutes for COCO. # We prefer to avoid waiting so debugging can fail fast. # Thus, we create the net *without input ops* prior to loading the # dataset, and then add the input ops after loading the dataset. # Since we defer input op creation, we need to do a little bit of surgery # to place the input ops at the start of the network op list. assert model.train, 'Training inputs can only be added to a trainable model' if roidb is not None: # To make debugging easier you can set cfg.DATA_LOADER.NUM_THREADS = 1 model.roi_data_loader = RoIDataLoader( roidb, num_loaders=cfg.DATA_LOADER.NUM_THREADS, minibatch_queue_size=cfg.DATA_LOADER.MINIBATCH_QUEUE_SIZE, blobs_queue_capacity=cfg.DATA_LOADER.BLOBS_QUEUE_CAPACITY ) orig_num_op = len(model.net._net.op) blob_names = roi_data_minibatch.get_minibatch_blob_names(is_training=True) for gpu_id in range(cfg.NUM_GPUS): with c2_utils.NamedCudaScope(gpu_id): for blob_name in blob_names: workspace.CreateBlob(core.ScopedName(blob_name)) model.net.DequeueBlobs( model.roi_data_loader._blobs_queue_name, blob_names ) # A little op surgery to move input ops to the start of the net diff = len(model.net._net.op) - orig_num_op new_op = model.net._net.op[-diff:] + model.net._net.op[:-diff] del model.net._net.op[:] model.net._net.op.extend(new_op)
Example #13
Source File: model_builder.py From Detectron-Cascade-RCNN with Apache License 2.0 | 5 votes |
def add_training_inputs(model, roidb=None): """Create network input ops and blobs used for training. To be called *after* model_builder.create(). """ # Implementation notes: # Typically, one would create the input ops and then the rest of the net. # However, creating the input ops depends on loading the dataset, which # can take a few minutes for COCO. # We prefer to avoid waiting so debugging can fail fast. # Thus, we create the net *without input ops* prior to loading the # dataset, and then add the input ops after loading the dataset. # Since we defer input op creation, we need to do a little bit of surgery # to place the input ops at the start of the network op list. assert model.train, 'Training inputs can only be added to a trainable model' if roidb is not None: # To make debugging easier you can set cfg.DATA_LOADER.NUM_THREADS = 1 model.roi_data_loader = RoIDataLoader( roidb, num_loaders=cfg.DATA_LOADER.NUM_THREADS, minibatch_queue_size=cfg.DATA_LOADER.MINIBATCH_QUEUE_SIZE, blobs_queue_capacity=cfg.DATA_LOADER.BLOBS_QUEUE_CAPACITY ) orig_num_op = len(model.net._net.op) blob_names = roi_data_minibatch.get_minibatch_blob_names(is_training=True) for gpu_id in range(cfg.NUM_GPUS): with c2_utils.NamedCudaScope(gpu_id): for blob_name in blob_names: workspace.CreateBlob(core.ScopedName(blob_name)) model.net.DequeueBlobs( model.roi_data_loader._blobs_queue_name, blob_names ) # A little op surgery to move input ops to the start of the net diff = len(model.net._net.op) - orig_num_op new_op = model.net._net.op[-diff:] + model.net._net.op[:-diff] del model.net._net.op[:] model.net._net.op.extend(new_op)
Example #14
Source File: loader.py From CBNet with Apache License 2.0 | 5 votes |
def create_enqueue_blobs(self): blob_names = self.get_output_names() enqueue_blob_names = [ '{}_enqueue_{}'.format(b, self._loader_id) for b in blob_names ] for gpu_id in range(self._num_gpus): with c2_utils.NamedCudaScope(gpu_id): for blob in enqueue_blob_names: workspace.CreateBlob(core.ScopedName(blob)) return enqueue_blob_names
Example #15
Source File: infer.py From Detectron with Apache License 2.0 | 5 votes |
def get_rpn_box_proposals(im, args): cfg.immutable(False) merge_cfg_from_file(args.rpn_cfg) cfg.NUM_GPUS = 1 cfg.MODEL.RPN_ONLY = True cfg.TEST.RPN_PRE_NMS_TOP_N = 10000 cfg.TEST.RPN_POST_NMS_TOP_N = 2000 assert_and_infer_cfg(cache_urls=False) model = model_engine.initialize_model_from_cfg(args.rpn_pkl) with c2_utils.NamedCudaScope(0): boxes, scores = rpn_engine.im_proposals(model, im) return boxes, scores
Example #16
Source File: loader.py From Detectron-DA-Faster-RCNN with Apache License 2.0 | 5 votes |
def create_enqueue_blobs(self): blob_names = self.get_output_names() enqueue_blob_names = [ '{}_enqueue_{}'.format(b, self._loader_id) for b in blob_names ] for gpu_id in range(self._num_gpus): with c2_utils.NamedCudaScope(gpu_id): for blob in enqueue_blob_names: workspace.CreateBlob(core.ScopedName(blob)) return enqueue_blob_names
Example #17
Source File: optimizer.py From Detectron-DA-Faster-RCNN with Apache License 2.0 | 5 votes |
def _build_forward_graph(model, single_gpu_build_func): """Construct the forward graph on each GPU.""" all_loss_gradients = {} # Will include loss gradients from all GPUs # Build the model on each GPU with correct name and device scoping for gpu_id in range(cfg.NUM_GPUS): with c2_utils.NamedCudaScope(gpu_id): all_loss_gradients.update(single_gpu_build_func(model)) return all_loss_gradients
Example #18
Source File: model_builder.py From Detectron-DA-Faster-RCNN with Apache License 2.0 | 5 votes |
def add_training_inputs(model, source_roidb=None, target_roidb=None): """Create network input ops and blobs used for training. To be called *after* model_builder.create(). """ # Implementation notes: # Typically, one would create the input ops and then the rest of the net. # However, creating the input ops depends on loading the dataset, which # can take a few minutes for COCO. # We prefer to avoid waiting so debugging can fail fast. # Thus, we create the net *without input ops* prior to loading the # dataset, and then add the input ops after loading the dataset. # Since we defer input op creation, we need to do a little bit of surgery # to place the input ops at the start of the network op list. assert model.train, 'Training inputs can only be added to a trainable model' if source_roidb is not None: # To make debugging easier you can set cfg.DATA_LOADER.NUM_THREADS = 1 model.roi_data_loader = RoIDataLoader( source_roidb=source_roidb, target_roidb=target_roidb, num_loaders=cfg.DATA_LOADER.NUM_THREADS, minibatch_queue_size=cfg.DATA_LOADER.MINIBATCH_QUEUE_SIZE, blobs_queue_capacity=cfg.DATA_LOADER.BLOBS_QUEUE_CAPACITY ) orig_num_op = len(model.net._net.op) blob_names = roi_data_minibatch.get_minibatch_blob_names(is_training=True) for gpu_id in range(cfg.NUM_GPUS): with c2_utils.NamedCudaScope(gpu_id): for blob_name in blob_names: workspace.CreateBlob(core.ScopedName(blob_name)) model.net.DequeueBlobs( model.roi_data_loader._blobs_queue_name, blob_names ) # A little op surgery to move input ops to the start of the net diff = len(model.net._net.op) - orig_num_op new_op = model.net._net.op[-diff:] + model.net._net.op[:-diff] del model.net._net.op[:] model.net._net.op.extend(new_op)
Example #19
Source File: rpn_generator.py From Detectron-DA-Faster-RCNN with Apache License 2.0 | 5 votes |
def generate_proposals_on_roidb( model, roidb, start_ind=None, end_ind=None, total_num_images=None, gpu_id=0, ): """Generate RPN proposals on all images in an imdb.""" _t = Timer() num_images = len(roidb) roidb_boxes = [[] for _ in range(num_images)] roidb_scores = [[] for _ in range(num_images)] roidb_ids = [[] for _ in range(num_images)] if start_ind is None: start_ind = 0 end_ind = num_images total_num_images = num_images for i in range(num_images): roidb_ids[i] = roidb[i]['id'] im = cv2.imread(roidb[i]['image']) with c2_utils.NamedCudaScope(gpu_id): _t.tic() roidb_boxes[i], roidb_scores[i] = im_proposals(model, im) _t.toc() if i % 10 == 0: ave_time = _t.average_time eta_seconds = ave_time * (num_images - i - 1) eta = str(datetime.timedelta(seconds=int(eta_seconds))) logger.info( ( 'rpn_generate: range [{:d}, {:d}] of {:d}: ' '{:d}/{:d} {:.3f}s (eta: {})' ).format( start_ind + 1, end_ind, total_num_images, start_ind + i + 1, start_ind + num_images, ave_time, eta ) ) return roidb_boxes, roidb_scores, roidb_ids
Example #20
Source File: infer.py From Detectron-DA-Faster-RCNN with Apache License 2.0 | 5 votes |
def get_rpn_box_proposals(im, args): cfg.immutable(False) merge_cfg_from_file(args.rpn_cfg) cfg.NUM_GPUS = 1 cfg.MODEL.RPN_ONLY = True cfg.TEST.RPN_PRE_NMS_TOP_N = 10000 cfg.TEST.RPN_POST_NMS_TOP_N = 2000 assert_and_infer_cfg(cache_urls=False) model = model_engine.initialize_model_from_cfg(args.rpn_pkl) with c2_utils.NamedCudaScope(0): boxes, scores = rpn_engine.im_proposals(model, im) return boxes, scores
Example #21
Source File: loader.py From Clustered-Object-Detection-in-Aerial-Image with Apache License 2.0 | 5 votes |
def create_enqueue_blobs(self): blob_names = self.get_output_names() enqueue_blob_names = [ '{}_enqueue_{}'.format(b, self._loader_id) for b in blob_names ] for gpu_id in range(self._num_gpus): with c2_utils.NamedCudaScope(gpu_id): for blob in enqueue_blob_names: workspace.CreateBlob(core.ScopedName(blob)) return enqueue_blob_names
Example #22
Source File: loader.py From KL-Loss with Apache License 2.0 | 5 votes |
def create_enqueue_blobs(self): blob_names = self.get_output_names() enqueue_blob_names = [ '{}_enqueue_{}'.format(b, self._loader_id) for b in blob_names ] for gpu_id in range(self._num_gpus): with c2_utils.NamedCudaScope(gpu_id): for blob in enqueue_blob_names: workspace.CreateBlob(core.ScopedName(blob)) return enqueue_blob_names
Example #23
Source File: optimizer.py From KL-Loss with Apache License 2.0 | 5 votes |
def _build_forward_graph(model, single_gpu_build_func): """Construct the forward graph on each GPU.""" all_loss_gradients = {} # Will include loss gradients from all GPUs # Build the model on each GPU with correct name and device scoping for gpu_id in range(cfg.NUM_GPUS): with c2_utils.NamedCudaScope(gpu_id): all_loss_gradients.update(single_gpu_build_func(model)) return all_loss_gradients
Example #24
Source File: model_builder.py From KL-Loss with Apache License 2.0 | 5 votes |
def add_training_inputs(model, roidb=None): """Create network input ops and blobs used for training. To be called *after* model_builder.create(). """ # Implementation notes: # Typically, one would create the input ops and then the rest of the net. # However, creating the input ops depends on loading the dataset, which # can take a few minutes for COCO. # We prefer to avoid waiting so debugging can fail fast. # Thus, we create the net *without input ops* prior to loading the # dataset, and then add the input ops after loading the dataset. # Since we defer input op creation, we need to do a little bit of surgery # to place the input ops at the start of the network op list. assert model.train, 'Training inputs can only be added to a trainable model' if roidb is not None: # To make debugging easier you can set cfg.DATA_LOADER.NUM_THREADS = 1 model.roi_data_loader = RoIDataLoader( roidb, num_loaders=cfg.DATA_LOADER.NUM_THREADS, minibatch_queue_size=cfg.DATA_LOADER.MINIBATCH_QUEUE_SIZE, blobs_queue_capacity=cfg.DATA_LOADER.BLOBS_QUEUE_CAPACITY ) orig_num_op = len(model.net._net.op) blob_names = roi_data_minibatch.get_minibatch_blob_names(is_training=True) for gpu_id in range(cfg.NUM_GPUS): with c2_utils.NamedCudaScope(gpu_id): for blob_name in blob_names: workspace.CreateBlob(core.ScopedName(blob_name)) model.net.DequeueBlobs( model.roi_data_loader._blobs_queue_name, blob_names ) # A little op surgery to move input ops to the start of the net diff = len(model.net._net.op) - orig_num_op new_op = model.net._net.op[-diff:] + model.net._net.op[:-diff] del model.net._net.op[:] model.net._net.op.extend(new_op)
Example #25
Source File: rpn_generator.py From KL-Loss with Apache License 2.0 | 5 votes |
def generate_proposals_on_roidb( model, roidb, start_ind=None, end_ind=None, total_num_images=None, gpu_id=0, ): """Generate RPN proposals on all images in an imdb.""" _t = Timer() num_images = len(roidb) roidb_boxes = [[] for _ in range(num_images)] roidb_scores = [[] for _ in range(num_images)] roidb_ids = [[] for _ in range(num_images)] if start_ind is None: start_ind = 0 end_ind = num_images total_num_images = num_images for i in range(num_images): roidb_ids[i] = roidb[i]['id'] im = cv2.imread(roidb[i]['image']) with c2_utils.NamedCudaScope(gpu_id): _t.tic() roidb_boxes[i], roidb_scores[i] = im_proposals(model, im) _t.toc() if i % 10 == 0: ave_time = _t.average_time eta_seconds = ave_time * (num_images - i - 1) eta = str(datetime.timedelta(seconds=int(eta_seconds))) logger.info( ( 'rpn_generate: range [{:d}, {:d}] of {:d}: ' '{:d}/{:d} {:.3f}s (eta: {})' ).format( start_ind + 1, end_ind, total_num_images, start_ind + i + 1, start_ind + num_images, ave_time, eta ) ) return roidb_boxes, roidb_scores, roidb_ids
Example #26
Source File: infer.py From CBNet with Apache License 2.0 | 5 votes |
def get_rpn_box_proposals(im, args): cfg.immutable(False) merge_cfg_from_file(args.rpn_cfg) cfg.NUM_GPUS = 1 cfg.MODEL.RPN_ONLY = True cfg.TEST.RPN_PRE_NMS_TOP_N = 10000 cfg.TEST.RPN_POST_NMS_TOP_N = 2000 assert_and_infer_cfg(cache_urls=False) model = model_engine.initialize_model_from_cfg(args.rpn_pkl) with c2_utils.NamedCudaScope(0): boxes, scores = rpn_engine.im_proposals(model, im) return boxes, scores
Example #27
Source File: infer.py From KL-Loss with Apache License 2.0 | 5 votes |
def get_rpn_box_proposals(im, args): cfg.immutable(False) merge_cfg_from_file(args.rpn_cfg) cfg.NUM_GPUS = 1 cfg.MODEL.RPN_ONLY = True cfg.TEST.RPN_PRE_NMS_TOP_N = 10000 cfg.TEST.RPN_POST_NMS_TOP_N = 2000 assert_and_infer_cfg(cache_urls=False) model = model_engine.initialize_model_from_cfg(args.rpn_pkl) with c2_utils.NamedCudaScope(0): boxes, scores = rpn_engine.im_proposals(model, im) return boxes, scores
Example #28
Source File: optimizer.py From Clustered-Object-Detection-in-Aerial-Image with Apache License 2.0 | 5 votes |
def _build_forward_graph(model, single_gpu_build_func): """Construct the forward graph on each GPU.""" all_loss_gradients = {} # Will include loss gradients from all GPUs # Build the model on each GPU with correct name and device scoping for gpu_id in range(cfg.NUM_GPUS): with c2_utils.NamedCudaScope(gpu_id): all_loss_gradients.update(single_gpu_build_func(model)) return all_loss_gradients
Example #29
Source File: model_builder.py From Clustered-Object-Detection-in-Aerial-Image with Apache License 2.0 | 5 votes |
def add_training_inputs(model, roidb=None): """Create network input ops and blobs used for training. To be called *after* model_builder.create(). """ # Implementation notes: # Typically, one would create the input ops and then the rest of the net. # However, creating the input ops depends on loading the dataset, which # can take a few minutes for COCO. # We prefer to avoid waiting so debugging can fail fast. # Thus, we create the net *without input ops* prior to loading the # dataset, and then add the input ops after loading the dataset. # Since we defer input op creation, we need to do a little bit of surgery # to place the input ops at the start of the network op list. assert model.train, 'Training inputs can only be added to a trainable model' if roidb is not None: # To make debugging easier you can set cfg.DATA_LOADER.NUM_THREADS = 1 model.roi_data_loader = RoIDataLoader( roidb, num_loaders=cfg.DATA_LOADER.NUM_THREADS, minibatch_queue_size=cfg.DATA_LOADER.MINIBATCH_QUEUE_SIZE, blobs_queue_capacity=cfg.DATA_LOADER.BLOBS_QUEUE_CAPACITY ) orig_num_op = len(model.net._net.op) blob_names = roi_data_minibatch.get_minibatch_blob_names(is_training=True) for gpu_id in range(cfg.NUM_GPUS): with c2_utils.NamedCudaScope(gpu_id): for blob_name in blob_names: workspace.CreateBlob(core.ScopedName(blob_name)) model.net.DequeueBlobs( model.roi_data_loader._blobs_queue_name, blob_names ) # A little op surgery to move input ops to the start of the net diff = len(model.net._net.op) - orig_num_op new_op = model.net._net.op[-diff:] + model.net._net.op[:-diff] del model.net._net.op[:] model.net._net.op.extend(new_op)
Example #30
Source File: rpn_generator.py From Clustered-Object-Detection-in-Aerial-Image with Apache License 2.0 | 5 votes |
def generate_proposals_on_roidb( model, roidb, start_ind=None, end_ind=None, total_num_images=None, gpu_id=0, ): """Generate RPN proposals on all images in an imdb.""" _t = Timer() num_images = len(roidb) roidb_boxes = [[] for _ in range(num_images)] roidb_scores = [[] for _ in range(num_images)] roidb_ids = [[] for _ in range(num_images)] if start_ind is None: start_ind = 0 end_ind = num_images total_num_images = num_images for i in range(num_images): roidb_ids[i] = roidb[i]['id'] im = cv2.imread(roidb[i]['image']) with c2_utils.NamedCudaScope(gpu_id): _t.tic() roidb_boxes[i], roidb_scores[i] = im_proposals(model, im) _t.toc() if i % 10 == 0: ave_time = _t.average_time eta_seconds = ave_time * (num_images - i - 1) eta = str(datetime.timedelta(seconds=int(eta_seconds))) logger.info( ( 'rpn_generate: range [{:d}, {:d}] of {:d}: ' '{:d}/{:d} {:.3f}s (eta: {})' ).format( start_ind + 1, end_ind, total_num_images, start_ind + i + 1, start_ind + num_images, ave_time, eta ) ) return roidb_boxes, roidb_scores, roidb_ids