def get_predictor(cls): ''' load trained model''' with cls.lock: # check if model is already loaded if cls.predictor: return cls.predictor # create a mask r-cnn model mask_rcnn_model = ResNetFPNModel() try: model_dir = os.environ['SM_MODEL_DIR'] except KeyError: model_dir = '/opt/ml/model' try: cls.pretrained_model = os.environ['PRETRAINED_MODEL'] except KeyError: pass # file path to previoulsy trained mask r-cnn model latest_trained_model = "" model_search_path = os.path.join(model_dir, "model-*.index") for model_file in glob.glob(model_search_path): if model_file > latest_trained_model: latest_trained_model = model_file trained_model = latest_trained_model[:-6] print(f'Using model: {trained_model}') # fixed resnet50 backbone weights cfg.BACKBONE.WEIGHTS = os.path.join(cls.pretrained_model) cfg.MODE_FPN = True cfg.MODE_MASK = True cfg.TEST.RESULT_SCORE_THRESH = cfg.TEST.RESULT_SCORE_THRESH_VIS finalize_configs(is_training=False) # Create an inference model # PredictConfig takes a model, input tensors and output tensors input_tensors = mask_rcnn_model.get_inference_tensor_names()[0] output_tensors = mask_rcnn_model.get_inference_tensor_names()[1] cls.predictor = OfflinePredictor( PredictConfig(model=mask_rcnn_model, session_init=get_model_loader(trained_model), input_names=input_tensors, output_names=output_tensors)) return cls.predictor
def init_predictor(): register_coco(cfg.DATA.BASEDIR) MODEL = ResNetFPNModel() finalize_configs(is_training=False) predcfg = PredictConfig( model=MODEL, #session_init=SmartInit("/home/jetson/Documents/trained_model/500000_17/checkpoint"), session_init=SmartInit( "/home/jetson/Documents/trained_model/255000_04.01/checkpoint"), input_names=MODEL.get_inference_tensor_names()[0], output_names=MODEL.get_inference_tensor_names()[1]) predictor = OfflinePredictor(predcfg) return predictor
def evaluate_rcnn(model_name, paper_arxiv_id, cfg_list, model_file): evaluator = COCOEvaluator( root=COCO_ROOT, model_name=model_name, paper_arxiv_id=paper_arxiv_id ) category_id_to_coco_id = { v: k for k, v in COCODetection.COCO_id_to_category_id.items() } cfg.update_config_from_args(cfg_list) # TODO backup/restore config finalize_configs(False) MODEL = ResNetFPNModel() if cfg.MODE_FPN else ResNetC4Model() predcfg = PredictConfig( model=MODEL, session_init=SmartInit(model_file), input_names=MODEL.get_inference_tensor_names()[0], output_names=MODEL.get_inference_tensor_names()[1], ) predictor = OfflinePredictor(predcfg) def xyxy_to_xywh(box): box[2] -= box[0] box[3] -= box[1] return box df = get_eval_dataflow("coco_val2017") df.reset_state() for img, img_id in tqdm.tqdm(df, total=len(df)): results = predict_image(img, predictor) res = [ { "image_id": img_id, "category_id": category_id_to_coco_id.get( int(r.class_id), int(r.class_id) ), "bbox": xyxy_to_xywh([round(float(x), 4) for x in r.box]), "score": round(float(r.score), 3), } for r in results ] evaluator.add(res) if evaluator.cache_exists: break evaluator.save()
parser.add_argument( '--benchmark', action='store_true', help="Benchmark the speed of the model + postprocessing") parser.add_argument( '--config', help="A list of KEY=VALUE to overwrite those defined in config.py", nargs='+') parser.add_argument('--compact', help='Save a model to .pb') parser.add_argument('--serving', help='Save a model to serving file') args = parser.parse_args() if args.config: cfg.update_args(args.config) register_coco(cfg.DATA.BASEDIR) # add COCO datasets to the registry MODEL = ResNetFPNModel() if cfg.MODE_FPN else ResNetC4Model() if not tf.test.is_gpu_available(): from tensorflow.python.framework import test_util assert get_tf_version_tuple() >= (1, 7) and test_util.IsMklEnabled(), \ "Inference requires either GPU support or MKL support!" assert args.load finalize_configs(is_training=False) if args.predict or args.visualize: cfg.TEST.RESULT_SCORE_THRESH = cfg.TEST.RESULT_SCORE_THRESH_VIS if args.visualize: do_visualize(MODEL, args.load) else: predcfg = PredictConfig(
# add green rectangle arround original picture that with failure height, width, channels = img.shape cv2.rectangle(img, (0, 0), (width, height), color=(100, 220, 80), thickness=5) viz = np.concatenate((img, final), axis=1) cv2.imwrite( "/home/jetson/tensorpack/examples/FasterRCNN/static/images/output.png", viz) logger.info("Inference output written to output.png") if __name__ == '__main__': register_coco(cfg.DATA.BASEDIR) MODEL = ResNetFPNModel() finalize_configs(is_training=False) predcfg = PredictConfig( model=MODEL, session_init=SmartInit( "/home/jetson/Documents/trained_model/500000_17/checkpoint"), input_names=MODEL.get_inference_tensor_names()[0], output_names=MODEL.get_inference_tensor_names()[1]) predictor = OfflinePredictor(predcfg) do_predict( predictor, "/home/jetson/tensorpack/examples/FasterRCNN/static/images/original.jpg" ) # this line can be commented out, but the FIRST reference after service start will take longer
factor = 8. / cfg.TRAIN.NUM_GPUS for idx, steps in enumerate(cfg.TRAIN.LR_SCHEDULE[:-1]): mult = 0.1**(idx + 1) lr_schedule.append( (steps * factor // stepnum, cfg.TRAIN.BASE_LR * mult)) logger.info("Warm Up Schedule (steps, value): " + str(warmup_schedule)) logger.info("LR Schedule (epochs, value): " + str(lr_schedule)) train_dataflow = get_train_dataflow() # This is what's commonly referred to as "epochs" total_passes = cfg.TRAIN.LR_SCHEDULE[-1] * 8 / train_dataflow.size() logger.info( "Total passes of the training set is: {:.5g}".format(total_passes)) # Create model and callbacks ... MODEL = ResNetFPNModel() if cfg.MODE_FPN else ResNetC4Model() callbacks = [ PeriodicCallback(ModelSaver(max_to_keep=10, keep_checkpoint_every_n_hours=1), every_k_epochs=20), # linear warmup ScheduledHyperParamSetter('learning_rate', warmup_schedule, interp='linear', step_based=True), ScheduledHyperParamSetter('learning_rate', lr_schedule), GPUMemoryTracker(), HostMemoryTracker(), ThroughputTracker(samples_per_step=cfg.TRAIN.NUM_GPUS), EstimatedTimeLeft(median=True),
def get_predictor(cls): """load trained model""" with cls.lock: # check if model is already loaded if cls.predictor: return cls.predictor # create a mask r-cnn model mask_rcnn_model = ResNetFPNModel() try: model_dir = os.environ["SM_MODEL_DIR"] except KeyError: model_dir = "/opt/ml/model" try: resnet_arch = os.environ["RESNET_ARCH"] except KeyError: resnet_arch = "resnet50" # file path to previoulsy trained mask r-cnn model latest_trained_model = "" model_search_path = os.path.join(model_dir, "model-*.index") for model_file in glob.glob(model_search_path): if model_file > latest_trained_model: latest_trained_model = model_file trained_model = latest_trained_model[:-6] print(f"Using model: {trained_model}") cfg.MODE_FPN = True cfg.MODE_MASK = True if resnet_arch == "resnet101": cfg.BACKBONE.RESNET_NUM_BLOCKS = [3, 4, 23, 3] else: cfg.BACKBONE.RESNET_NUM_BLOCKS = [3, 4, 6, 3] cfg_prefix = "CONFIG__" for key, value in dict(os.environ).items(): if key.startswith(cfg_prefix): attr_name = key[len(cfg_prefix) :] attr_name = attr_name.replace("__", ".") value = eval(value) print(f"update config: {attr_name}={value}") nested_var = cfg attr_list = attr_name.split(".") for attr in attr_list[0:-1]: nested_var = getattr(nested_var, attr) setattr(nested_var, attr_list[-1], value) cfg.TEST.RESULT_SCORE_THRESH = cfg.TEST.RESULT_SCORE_THRESH_VIS cfg.DATA.BASEDIR = "/data" cfg.DATA.TRAIN = "coco_train2017" cfg.DATA.VAL = "coco_val2017" register_coco(cfg.DATA.BASEDIR) finalize_configs(is_training=False) # Create an inference model # PredictConfig takes a model, input tensors and output tensors input_tensors = mask_rcnn_model.get_inference_tensor_names()[0] output_tensors = mask_rcnn_model.get_inference_tensor_names()[1] cls.predictor = OfflinePredictor( PredictConfig( model=mask_rcnn_model, session_init=get_model_loader(trained_model), input_names=input_tensors, output_names=output_tensors, ) ) return cls.predictor
register_coco(cfg.DATA.BASEDIR) # add COCO datasets to the registry register_ic( cfg.DATA.BASEDIR) # add the demo balloon datasets to the registry # Setup logging ... is_horovod = cfg.TRAINER == 'horovod' if is_horovod: hvd.init() if not is_horovod or hvd.rank() == 0: logger.set_logger_dir(args.logdir, 'd') logger.info("Environment Information:\n" + collect_env_info()) finalize_configs(is_training=True) # Create model MODEL = ResNetFPNModel() if cfg.MODE_FPN else ResNetC4Model() # Compute the training schedule from the number of GPUs ... stepnum = cfg.TRAIN.STEPS_PER_EPOCH # warmup is step based, lr is epoch based init_lr = cfg.TRAIN.WARMUP_INIT_LR * min(8. / cfg.TRAIN.NUM_GPUS, 1.) warmup_schedule = [(0, init_lr), (cfg.TRAIN.WARMUP, cfg.TRAIN.BASE_LR)] warmup_end_epoch = cfg.TRAIN.WARMUP * 1. / stepnum lr_schedule = [(int(warmup_end_epoch + 0.5), cfg.TRAIN.BASE_LR)] factor = 8. / cfg.TRAIN.NUM_GPUS for idx, steps in enumerate(cfg.TRAIN.LR_SCHEDULE[:-1]): mult = 0.1**(idx + 1) lr_schedule.append( (steps * factor // stepnum, cfg.TRAIN.BASE_LR * mult)) logger.info("Warm Up Schedule (steps, value): " + str(warmup_schedule))
# add the demo balloon datasets to the registry register_balloon(cfg.DATA.BASEDIR) register_display(cfg.DATA.BASEDIR) # Setup logging ... is_horovod = cfg.TRAINER == 'horovod' if is_horovod: hvd.init() if not is_horovod or hvd.rank() == 0: logger.set_logger_dir(args.logdir, 'd') logger.info("Environment Information:\n" + collect_env_info()) finalize_configs(is_training=True) # Create model MODEL = ResNetFPNModel() if cfg.MODE_FPN else ResNetC4Model() # Compute the training schedule from the number of GPUs ... stepnum = cfg.TRAIN.STEPS_PER_EPOCH # warmup is step based, lr is epoch based init_lr = cfg.TRAIN.WARMUP_INIT_LR * min(8. / cfg.TRAIN.NUM_GPUS, 1.) warmup_schedule = [(0, init_lr), (cfg.TRAIN.WARMUP, cfg.TRAIN.BASE_LR)] warmup_end_epoch = cfg.TRAIN.WARMUP * 1. / stepnum lr_schedule = [(int(warmup_end_epoch + 0.5), cfg.TRAIN.BASE_LR)] factor = 8. / cfg.TRAIN.NUM_GPUS for idx, steps in enumerate(cfg.TRAIN.LR_SCHEDULE[:-1]): mult = 0.1 ** (idx + 1) lr_schedule.append( (steps * factor // stepnum, cfg.TRAIN.BASE_LR * mult)) logger.info("Warm Up Schedule (steps, value): " + str(warmup_schedule))
for dataset in cfg.DATA.VAL #+ cfg.DATA.TRAIN ]) return callbacks_ if __name__ == '__main__': conf_dict = data_conf_gingivitis_only # data_conf_tooth_only data_config = DataConfig(image_data_basedir=None) data_config.pop_from_dict(conf_dict) args, is_horovod = config_setup(data_config=data_config) # Create model MODEL = ResNetFPNModel() if cfg.MODE_FPN else ResNetC4Model() import tensorflow as tf trainable_var_key = tf.GraphKeys.TRAINABLE_VARIABLES all_vars = tf.get_collection(key=trainable_var_key, scope="MLP") all_vars = tf.get_collection(key=trainable_var_key, scope="MLP") # get dataflow train_dataflow = get_train_dataflow() # setup training schedule warmup_schedule, lr_schedule, step_num, max_epoch = setup_training_schedule( train_dataflow) callbacks = create_callbacks(warmup_schedule, lr_schedule,