def load_model(model_file): torch.set_default_tensor_type('torch.cuda.FloatTensor') set_cfg('yolact_plus_resnet50_config') net = Yolact() net.load_weights(model_file) net.eval() return net
def __init__(self, trained_model: str, save_json=True, output_dir=None, output_name="detection", output_num=5): """ YOLACT 初始化,参数: - save_json 是否将计算结果保存为json文件 - output_dir 当上个参数为True时,这个参数表示将json文件保存到的位置 - output_name 保存的json文件名 - output_num # ? 目测是要输出的类别个数 """ # step 0 初始化变量 self.save_json = save_json # NOTE 卧槽还有这种用法,学习了 self.detections = None self.output_num = output_num # step 1 如果指定了要生成json文件,就创建上面的Detection类对象 if self.save_json and output_dir is not None: self.detections = Detections(output_dir, output_name) # step 2 初始化YOLACT网络 with torch.no_grad(): set_cfg("yolact_base_config") torch.cuda.set_device(1) cudnn.benchmark = True cudnn.fastest = True torch.set_default_tensor_type('torch.cuda.FloatTensor') self.net = Yolact() # TODO 这里的权值是需要进行修改的 # self.net.load_weights('./weights/yolact_base_54_800000.pth') self.net.load_weights(trained_model) self.net.eval() self.net = self.net.cuda() print("load model complete")
def __init__(self, id): # self.cap = cv2.VideoCapture(id) self.cap = WebcamVideoStream(src=id).start() self.width = 1280 #640# self.height = 720 #360# self.display_lincomb = False self.crop = True self.score_threshold = 0.15 self.top_k = 30 self.display_masks = True self.display_fps = False self.display_text = True self.display_bboxes = True self.display_scores = False self.fast_nms = True self.cross_class_nms = True self.config = 'yolact_plus_base_config' print('Config specified. Parsed %s from the file name.\n' % self.config) set_cfg(self.config) print('Loading model...', end='') self.trained_model = 'weights/yolact_plus_base_54_800000.pth' self.model = Yolact() self.model.load_weights(self.trained_model) self.model.detect.use_fast_nms = self.fast_nms self.model.detect.use_cross_class_nms = self.cross_class_nms self.model.eval() self.model = self.model.to(device, non_blocking=True) print(' Done.') self.model_path = SavePath.from_str(self.trained_model)
def __init__(self, model_pth, output_num=5): self.output_num = output_num with torch.no_grad(): set_cfg("yolact_base_config") torch.cuda.set_device(0) cudnn.benchmark = True cudnn.fastest = True torch.set_default_tensor_type('torch.cuda.FloatTensor') self.net = Yolact() self.net.load_weights(model_pth) self.net.eval() self.net = self.net.cuda() print("load model complete")
def __init__( self, weights='./crow_vision_yolact/data/yolact/weights/weights_yolact_kuka_17/crow_base_35_457142.pth', config=None, batchsize=1, top_k=25, score_threshold=0.1, display_text=True, display_bboxes=True, display_masks=True, display_scores=True): self.score_threshold = score_threshold self.top_k = top_k self.batchsize = batchsize # initialize a yolact net for inference ## YOLACT setup # setup config if config is not None: if '.obj' in config: with open(config, 'rb') as f: config = dill.load(f) set_cfg(config) self.class_names_tuple = get_class_names_tuple() parse_args([ '--top_k=' + str(top_k), '--score_threshold=' + str(score_threshold), '--display_text=' + str(display_text), '--display_bboxes=' + str(display_bboxes), '--display_masks=' + str(display_masks), '--display_scores=' + str(display_scores), ]) # CUDA setup for yolact torch.backends.cudnn.fastest = True torch.set_default_tensor_type('torch.cuda.FloatTensor') #YOLACT net itself with torch.no_grad(): net = Yolact().cuda(torch.cuda.current_device()) net.load_weights(weights) net.eval() net.detect.use_fast_nms = True net.detect.use_cross_class_nms = False self.net = net print("YOLACT network available as self.net") #for debug,benchmark self.duration = 0.0
def __init__( self, weight_path='C:/Users/user/yolact_notes/weights/yolact_darknet53_249_2000.pth', save_path='C:/Users/user/yolact_notes/pear_output'): set_cfg('pear_config') self.save_path = save_path self.weight_path = weight_path self.net = Yolact() self.net.load_weights(self.weight_path) self.net.eval() self.net = self.net.cuda() print('model loaded...') self.net.detect.cross_class_nms = True self.net.detect.use_fast_nms = True
def __init__(self): parse_args(self) self.args.config = 'yolact_edge_mobilenetv2_config' set_cfg(self.args.config) self.args.trained_model = '/home/ht/catkin_ws/src/instance_segmentation/scripts/weights/yolact_edge_mobilenetv2_124_10000.pth' self.args.top_k = 10 self.args.score_threshold = 0.3 self.args.trt_batch_size = 3 self.args.disable_tensorrt = False self.args.use_fp16_tensorrt = False self.args.use_tensorrt_safe_mode = True self.args.cuda = True self.args.fast_nms = True self.args.display_masks = True self.args.display_bboxes = True self.args.display_text = True self.args.display_scores = True self.args.display_linecomb = False self.args.fast_eval = False self.args.deterministic = False self.args.no_crop = False self.args.crop = True self.args.calib_images = '/home/ht/catkin_ws/src/instance_segmentation/scripts/data/coco/calib_images' setup_logger(logging_level=logging.INFO) self.logger = logging.getLogger('yolact.eval') self.color_cache = defaultdict(lambda: {}) with torch.no_grad(): cudnn.benchmark = True cudnn.fastest = True torch.set_default_tensor_type('torch.cuda.FloatTensor') self.logger.info('Loading model...') self.net = Yolact(training=False) if self.args.trained_model is not None: self.net.load_weights(self.args.trained_model, args=self.args) else: self.logger.warning('No weights loaded!') self.net.eval() self.logger.info('Model loaded.') convert_to_tensorrt(self.net, cfg, self.args, transform=BaseTransform())
def init_model(transform): args = parse_args() if args.config is not None: print(args.config) set_cfg(args.config) cfg.mask_proto_debug = False if args.trained_model == 'interrupt': args.trained_model = SavePath.get_interrupt('weights/') elif args.trained_model == 'latest': args.trained_model = SavePath.get_latest('weights/', cfg.name) if args.config is None: model_path = SavePath.from_str(args.trained_model) # TODO: Bad practice? Probably want to do a name lookup instead. args.config = model_path.model_name + '_config' print('Config not specified. Parsed %s from the file name.\n' % args.config) set_cfg(args.config) if args.detect: cfg.eval_mask_branch = False if args.dataset is not None: set_dataset(args.dataset) with torch.no_grad(): if args.cuda: cudnn.fastest = True torch.set_default_tensor_type('torch.cuda.FloatTensor') else: torch.set_default_tensor_type('torch.FloatTensor') print('Loading model...', end='') net = Yolact() net.load_weights(args.trained_model) net.eval() print(' Done.') net = net.cuda() net = CustomDataParallel(net).cuda() transform = torch.nn.DataParallel(FastBaseTransform()).cuda() return net, args
def __init__(self, opts): #concat the two files to one file # if not os.path.isfile('weights/yolact_resnet50_54_800000.pth'): # script = "cat weights/a* > weights/yolact_resnet50_54_800000.pth" # call(script, shell=True) set_cfg('yolact_resnet50_config') cudnn.benchmark = True cudnn.fastest = True torch.set_default_tensor_type('torch.cuda.FloatTensor') self.net = Yolact() self.net.load_weights(opts['checkpoint']) print("done.") self.net.eval() self.net = self.net.cuda() self.net.detect.use_fast_nms = True cfg.mask_proto_debug = False self.color_cache = defaultdict(lambda: {}) self.threshold = opts['threshold']
def convert_to_onnx_with_hydra(cfg: DictConfig): # create folder for onnx createFolderOnnx(cfg) # set cfg set_cfg(cfg.onnx.yolact_cfg) model = Yolact() model.load_weights(cfg.onnx.model_ckpt_path) model.eval() model = model.cpu() dummy_input = torch.rand( (cfg.onnx.model_batch_size, cfg.onnx.model_channel_input, cfg.onnx.model_height_input, cfg.onnx.model_width_input)) torch.onnx.export(model, dummy_input, cfg.onnx.model_onnx_path, verbose=cfg.onnx.verbose, opset_version=cfg.onnx.opset_version)
def main(args): rospy.init_node('yolact_ros') rospack = rospkg.RosPack() yolact_path = rospack.get_path('yolact_ros') model_path_str = yolact_path + "/scripts/yolact/weights/yolact_base_54_800000.pth" model_path = SavePath.from_str(model_path_str) set_cfg(model_path.model_name + '_config') with torch.no_grad(): results_path_str = yolact_path + "/scripts/yolact/results" if not os.path.exists(results_path_str): os.makedirs(results_path_str) cudnn.benchmark = True cudnn.fastest = True torch.set_default_tensor_type('torch.cuda.FloatTensor') print('Loading model...', end='') net = Yolact() net.load_weights(model_path_str) net.eval() print(' Done.') net = net.cuda() net.detect.use_fast_nms = True cfg.mask_proto_debug = False ic = image_converter(net) try: rospy.spin() except KeyboardInterrupt: print("Shutting down") cv2.destroyAllWindows()
def detect(): img_path = '/home/user/dataset/pear/train/JPEGImages' save_path = '/home/user/pear_output' weight_path = '/home/user/caoliwei/yolact/weights/20200901/yolact_darknet53_1176_20000.pth' set_cfg('pear_config') with torch.no_grad(): torch.cuda.set_device(0) ###### # If the input image size is constant, this make things faster (hence why we can use it in a video setting). # cudnn.benchmark = True # cudnn.fastest = True torch.set_default_tensor_type('torch.cuda.FloatTensor') ###### net = Yolact() net.load_weights(weight_path) net.eval() net = net.cuda() print('model loaded...') net.detect.cross_class_nms = True net.detect.use_fast_nms = True cfg.mask_proto_debug = False if not os.path.exists(save_path): os.mkdir(save_path) img_names = [ name for name in os.listdir(img_path) if name.endswith('.jpg') or name.endswith('.png') ] #for img_name in tqdm(img_names): for img_name in img_names: img = cv2.imread(os.path.join(img_path, img_name)) img = torch.from_numpy(img).cuda().float() img = FastBaseTransform()(img.unsqueeze(0)) start = time.time() preds = net(img) print('clw: image_name: %s, inference time use %.3fs' % (img_name, time.time() - start)) # inference time use 0.023s, 550x550 # start = time.time() h, w = img.shape[2:] result = postprocess( preds, w, h, crop_masks=True, score_threshold=0.3) # classes, scores, boxes, masks 按照score排序 # top_k = 10 # classes, scores, boxes, masks = [x[:top_k].cpu().numpy() for x in result] # clw note TODO: 是否有必要只取top_k个? # print('clw: postprocess time use %.3fs' % (time.time() - start)) # 0.001s ### 顺序遍历result[0],找到第一个是0的值,也就是梨,也就拿到了相应的mask # start = time.time() bFindPear = False for i, cls_id in enumerate(result[0]): if cls_id == 0 and not bFindPear: pear_mask = result[3][i].cpu().numpy() bFindPear = True # 从梨的mask中提取轮廓 pear_outline = get_outline_from_mask(pear_mask, w, h) # print('pear_mask.sum:', pear_mask.sum()) # 124250.0 # print('pear_outline.sum:', pear_outline.sum()) # 34335.0 # print('clw: outline extract time use %.3fs' % (time.time() - start)) # 0.001s roundness = compute_roundness(pear_outline) ### result.append(roundness)
def __init__(self, nn, input_device): """ Initialisation function """ print('Loading model...') self.nn = nn if self.nn == 'yolact': print("Selected NN: Yolact") # Yoloact imports sys.path.append('../nn/yolact/') from yolact import Yolact from data import cfg, set_cfg, set_dataset import torch import torch.backends.cudnn as cudnn set_cfg("yolact_resnet50_config") #set_cfg("yolact_resnet50_config") cfg.eval_mask_branch = True cfg.mask_proto_debug = False cfg.rescore_bbox = True self.net = Yolact() self.net.load_weights("../weights/yolact_resnet50_54_800000.pth") #self.net.load_weights("../weights/yolact_resnet50_54_800000.pth") self.net.eval() cudnn.fastest = True torch.set_default_tensor_type('torch.cuda.FloatTensor') self.net = self.net.cuda() elif self.nn == 'yolact++': print("Selected NN: Yolact++") # Yoloact imports sys.path.append('../nn/yolact/') from yolact import Yolact from data import cfg, set_cfg, set_dataset import torch import torch.backends.cudnn as cudnn set_cfg("yolact_plus_resnet50_config") #set_cfg("yolact_resnet50_config") cfg.eval_mask_branch = True cfg.mask_proto_debug = False cfg.rescore_bbox = True self.net = Yolact() self.net.load_weights("../weights/yolact_plus_resnet50_54_800000.pth") #self.net.load_weights("../weights/yolact_resnet50_54_800000.pth") self.net.eval() cudnn.fastest = True torch.set_default_tensor_type('torch.cuda.FloatTensor') self.net = self.net.cuda() elif self.nn == 'yolact_edge': print("Selected NN: Yolact_edge") #Yoloact_edge imports sys.path.append('../nn/yolact_edge') from yolact import Yolact from data import cfg, set_cfg, set_dataset import torch import torch.backends.cudnn as cudnn set_cfg("yolact_edge_resnet50_config") cfg.eval_mask_branch = True cfg.mask_proto_debug = False cfg.rescore_bbox = True self.net = Yolact() self.net.load_weights("../weights/yolact_edge_resnet50_54_800000.pth") self.net.eval() cudnn.fastest = True torch.set_default_tensor_type('torch.cuda.FloatTensor') self.net = self.net.cuda() elif self.nn == 'mrcnn': print("Selected NN: Mask-RCNN") # Keras import keras from keras.models import Model from keras import backend as K K.common.set_image_dim_ordering('tf') # Mask-RCNN sys.path.append('../nn/Mask_RCNN/') from mrcnn import config from mrcnn import utils from mrcnn import model as modellib from inference_config import InferenceConfig self.config = InferenceConfig() self.model = modellib.MaskRCNN( mode="inference", model_dir="../weights/",#"../nn/Mask_RCNN/mrcnn/", config=self.config) # Load weights trained on MS-COCO self.model.load_weights("../weights/mask_rcnn_coco.h5", by_name=True) else: print("no nn defined") self.bridge = CvBridge() self._max_inactive_frames = 10 # Maximum nb of frames before destruction self.next_object_id = 0 # ID for next object self.objects_dict = {} # Detected objects dictionary self.var_init = 0 self.cam_pos_qat = np.array([[0.,0.,0.],[0.,0.,0.,1.]]) self.cam_pos = np.array([[0.,0.,0.],[0.,0.,0.]]) self.dilatation = 1 self.score_threshold = 0.1 self.max_number_observation = 5 self.human_threshold = 0.01 self.object_threshold = 0.3 self.iou_threshold = 0.9 self.selected_classes = [0, 56, 67] self.masked_id = [] #if input_device == 'xtion': # self.human_threshold = 0.1 # self.iou_threshold = 0.3 self.depth_image_pub = rospy.Publisher( "/camera/depth_registered/masked_image_raw", Image,queue_size=1) self.dynamic_depth_image_pub = rospy.Publisher( "/camera/depth_registered/dynamic_masked_image_raw", Image,queue_size=1) self.frame = [] self.depth_frame = [] self.msg_header = std_msgs.msg.Header() self.depth_msg_header = std_msgs.msg.Header() # Class names COCO dataset self.class_names = [ 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']
from utils import timer from utils.functions import SavePath from layers.output_utils import postprocess import pycocotools from data import cfg, set_cfg, set_dataset import numpy as np import torch import torch.backends.cudnn as cudnn from torch.autograd import Variable from collections import defaultdict import matplotlib.pyplot as plt import cv2 set_cfg('yolact_resnet50_config') cudnn.benchmark = True cudnn.fastest = True torch.set_default_tensor_type('torch.cuda.FloatTensor') net = Yolact() net.load_weights('weights/yolact_resnet50_54_800000.pth') net.eval() net = net.cuda() net.detect.use_fast_nms = True cfg.mask_proto_debug = False path = "cat.jpg" frame = torch.from_numpy(cv2.imread(path)).cuda().float() batch = FastBaseTransform()(frame.unsqueeze(0)) print(batch.shape)
def main(argv=None): """ Parses the parameters or, if None, sys.argv and starts prediction mode. :param argv: the command-line parameters to parse (list of strings) :type: argv: list """ parser = argparse.ArgumentParser(description='YOLACT Prediction') parser.add_argument('--model', required=True, type=str, help='The trained model to use (.pth file).') parser.add_argument('--config', default="external_config", help='The name of the configuration to use.') parser.add_argument( '--top_k', default=5, type=int, help='Further restrict the number of predictions (eg objects) to parse' ) parser.add_argument( '--score_threshold', default=0, type=float, help= 'Detections with a score under this threshold will not be considered.') parser.add_argument( '--fast_nms', action="store_false", help='Whether to use a faster, but not entirely correct version of NMS.' ) parser.add_argument('--cross_class_nms', action="store_true", help='Whether compute NMS cross-class or per-class.') parser.add_argument( '--prediction_in', default=None, type=str, required=True, help='The directory in which to look for images for processing.') parser.add_argument('--prediction_out', default=None, type=str, required=True, help='The directory to store the results in.') parser.add_argument( '--prediction_tmp', default=None, type=str, required=False, help= 'The directory to store the results in first, before moving them to the actual output directory.' ) parser.add_argument( '--continuous', action="store_true", help= 'Whether to continuously poll the input directory or exit once all initial images have been processed.' ) parser.add_argument( '--delete_input', action="store_true", help= 'Whether to delete the input images rather than moving them to the output directory.' ) parser.add_argument( '--output_polygons', action='store_true', help= 'Whether to masks are predicted and polygons should be output in the ROIS CSV files', required=False, default=False) parser.add_argument( '--fit_bbox_to_polygon', action='store_true', help= 'When outputting polygons whether to fit the bounding box to the polygon', required=False, default=False) parser.add_argument( '--bbox_as_fallback', default=-1.0, type=float, help= 'When outputting polygons the bbox can be used as fallback polygon. This happens if the ratio ' + 'between the surrounding bbox of the polygon and the bbox is smaller than the specified value. ' + 'Turned off if < 0.', required=False) parser.add_argument( '--mask_threshold', type=float, help='The threshold (0-1) to use for determining the contour of a mask', required=False, default=0.1) parser.add_argument( '--mask_nth', type=int, help='To speed polygon detection up, use every nth row and column only', required=False, default=1) parser.add_argument( '--output_minrect', action='store_true', help= 'When outputting polygons whether to store the minimal rectangle around the objects in the CSV files as well', required=False, default=False) parser.add_argument( '--view_margin', default=2, type=int, required=False, help= 'The number of pixels to use as margin around the masks when determining the polygon' ) parser.add_argument( '--fully_connected', default='high', choices=['high', 'low'], required=False, help= 'When determining polygons, whether regions of high or low values should be fully-connected at isthmuses' ) parser.add_argument( '--output_width_height', action='store_true', help= "Whether to output x/y/w/h instead of x0/y0/x1/y1 in the ROI CSV files", required=False, default=False) parser.add_argument( '--scale', type=float, help= 'The scale factor to apply to the image (0-1) before processing. Output will be in original dimension space.', required=False, default=1.0) parser.add_argument( '--debayer', default="", type=str, help='The OpenCV2 debayering method to use, eg "COLOR_BAYER_BG2BGR"', required=False) parser.add_argument( '--output_mask_image', action='store_true', default=False, help= "Whether to output a mask image (PNG) when predictions generate masks (independent of outputting polygons)", required=False) parsed = parser.parse_args(args=argv) if parsed.fit_bbox_to_polygon and (parsed.bbox_as_fallback >= 0): raise Exception( "Options --fit_bbox_to_polygon and --bbox_as_fallback cannot be used together!" ) if (parsed.debayer is not None ) and not (parsed.debayer == "") and not parsed.debayer.startswith("COLOR_BAYER_"): raise Exception( "Expected debayering type to start with COLOR_BAYER_, instead got: " + str(parsed.debayer)) with torch.no_grad(): # initializing cudnn print('Initializing cudnn', end='') cudnn.fastest = True torch.set_default_tensor_type('torch.cuda.FloatTensor') print(' Done.') # load configuration and model print('Loading config %s' % parsed.config, end='') set_cfg(parsed.config) cfg.mask_proto_debug = False print(' Done.') print('Loading model: %s' % parsed.model, end='') net = Yolact() net.load_weights(parsed.model) net.eval() net = net.cuda() net.detect.use_fast_nms = parsed.fast_nms net.detect.use_cross_class_nms = parsed.cross_class_nms print(' Done.') predict(model=net, input_dir=parsed.prediction_in, output_dir=parsed.prediction_out, tmp_dir=parsed.prediction_tmp, top_k=parsed.top_k, score_threshold=parsed.score_threshold, delete_input=parsed.delete_input, output_polygons=parsed.output_polygons, mask_threshold=parsed.mask_threshold, mask_nth=parsed.mask_nth, output_minrect=parsed.output_minrect, view_margin=parsed.view_margin, fully_connected=parsed.fully_connected, fit_bbox_to_polygon=parsed.fit_bbox_to_polygon, output_width_height=parsed.output_width_height, bbox_as_fallback=parsed.bbox_as_fallback, scale=parsed.scale, debayer=parsed.debayer, continuous=parsed.continuous, output_mask_image=parsed.output_mask_image)
if (args.scene.split('_')[0] == 'realsense'): rgb_paths = os.path.join(data_folder, 'rgb/') else: rgb_paths = os.path.join(data_folder, 'color/') rgb_names = get_file_names(rgb_paths) depth_paths = os.path.join(data_folder, 'depth/') depth_names = get_file_names(depth_paths) pose_paths = os.path.join(data_folder, 'pose/') pose_names = get_file_names(pose_paths) if args.config is not None: set_cfg(args.config) if args.trained_model == 'interrupt': args.trained_model = SavePath.get_interrupt('weights/') elif args.trained_model == 'latest': args.trained_model = SavePath.get_latest('weights/', cfg.name) if args.config is None: model_path = SavePath.from_str(args.trained_model) args.config = model_path.model_name + '_config' print('Config not specified. Parsed %s from the file name.\n' % args.config) set_cfg(args.config) if args.detect: cfg.eval_mask_branch = False
args = parse_args() cuda = False if torch.cuda.is_available(): cuda = True weight_path = 'weights/yolact_edge_mobilenetv2_54_800000.pth' image_path = 'images/demo.jpg' result_path = 'results/demo.png' model_path = SavePath.from_str(weight_path) # TODO: Bad practice? Probably want to do a name lookup instead. config = model_path.model_name + '_config' print('Config not specified. Parsed %s from the file name.\n' % config) set_cfg(config) setup_logger(logging_level=logging.INFO) logger = logging.getLogger("yolact.eval") logger.info('Loading model...') model = Yolact(training=False) model.load_weights(weight_path, args=args) model.eval() logger.info('Model loaded.') if cuda: model = model.cuda() logger.info('Predicting with gpu.') else: logger.info('Predicting with cpu.')