Ejemplo n.º 1
0
    def __init__(self, model_path):

        net_type = 'mb1-ssd'
        label_path = os.path.join(os.path.dirname(model_path), 'voc-model-labels.txt')
        self.class_names = [name.strip() for name in open(label_path).readlines()]
        class_length = len(self.class_names)
        if net_type == 'vgg16-ssd':
            net = create_vgg_ssd(class_length, is_test=True)
        elif net_type == 'mb1-ssd':
            net = create_mobilenetv1_ssd(class_length, is_test=True)
        elif net_type == 'mb1-ssd-lite':
            net = create_mobilenetv1_ssd_lite(class_length, is_test=True)
        else:
            print("The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite.")
            sys.exit(1)
    
        if net_type == 'vgg16-ssd':
            predictor = create_vgg_ssd_predictor(net, candidate_size=200)
        elif net_type == 'mb1-ssd':
            predictor = create_mobilenetv1_ssd_predictor(net, candidate_size=200)
        elif net_type == 'mb1-ssd-lite':
            predictor = create_mobilenetv1_ssd_lite_predictor(net, candidate_size=200)
        else:
            print("The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite.")
            sys.exit(1)
            
        net.load(model_path)
        self.predictor = predictor
Ejemplo n.º 2
0
    def net_initilize(self, net_type, model_path, class_names):
        if net_type == 'vgg16-ssd':
            net = create_vgg_ssd(len(class_names), is_test=True)
        elif net_type == 'mb1-ssd':
            net = create_mobilenetv1_ssd(len(class_names), is_test=True)
        elif net_type == 'mb1-ssd-lite':
            net = create_mobilenetv1_ssd_lite(len(class_names), is_test=True)
        else:
            print(
                "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
            )
            sys.exit(1)

        if net_type == 'vgg16-ssd':
            predictor = create_vgg_ssd_predictor(net, candidate_size=200)
        elif net_type == 'mb1-ssd':
            predictor = create_mobilenetv1_ssd_predictor(net,
                                                         candidate_size=200)
        elif net_type == 'mb1-ssd-lite':
            predictor = create_mobilenetv1_ssd_lite_predictor(
                net, candidate_size=200)
        else:
            print(
                "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
            )
            sys.exit(1)

        self.net = net
        self.net.load(model_path)
        self.pdc = predictor
Ejemplo n.º 3
0
 def __init__(self):
     model_path = './models/mb1-ssd-Epoch-1290-Loss-2.35230020682017.pth'
     label_path = './models/voc-model-labels.txt'
     self.class_names = [name.strip() for name in open(label_path).readlines()]
     net = create_mobilenetv1_ssd(len(self.class_names), is_test=True)
     net.load(model_path)
     self.predictor = create_mobilenetv1_ssd_predictor(net, candidate_size=200)
     self.alert_sum = 0
Ejemplo n.º 4
0
	def __init__(self):

		model = "v1"
		self.prob_threshold = 0.85
		self.cv_bridge = CvBridge() 
		self.labels = ['background' , # always index 0
				'person','palm']
		self.objects = []
		if model == "v2_lite":
			self.network = create_mobilenetv2_ssd_lite(len(self.labels), is_test=True) 
		elif model == "v1":
			self.network = create_mobilenetv1_ssd(len(self.labels), is_test=True) 
		elif model == "v1_lite":
			self.network = create_mobilenetv1_ssd_lite(len(self.labels), is_test=True) 

		model_path = '/home/arg_ws3/pytorch-ssd/models/argbot_person_palm_new_new/mb1-ssd-Epoch-749-Loss-1.8576.pth'
		state_dict = torch.load(os.path.join(model_path))
		self.network.load_state_dict(state_dict)
		DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
		self.network.to(DEVICE)
		if model == "v2_lite":
			self.predictor = create_mobilenetv2_ssd_lite_predictor(self.network, candidate_size=200, device = DEVICE)
		elif model == "v1_lite":
			self.predictor = create_mobilenetv1_ssd_lite_predictor(self.network, candidate_size=200, device = DEVICE)
		elif model == "v1":	
			self.predictor = create_mobilenetv1_ssd_predictor(self.network, candidate_size=200, device = DEVICE)

		#### Publisher
		self.origin = rospy.Publisher('/input', bb_input, queue_size=1)
		self.image_pub = rospy.Publisher("/predict_img", Image, queue_size = 1)
		self.pub_tracking = rospy.Publisher("/tracking_point", Marker, queue_size = 1)
		self.pub_point_array = rospy.Publisher("/person_point_array", MarkerArray, queue_size = 1)

		self.kf_x = KalmanFilter(dim_x=2, dim_z=1)
		self.kf_y = KalmanFilter(dim_x=2, dim_z=1)
		self.path = []
		self.person_list = []
		self.cv_depthimage = None
		self.start_tracking = False
		self.frame_id = 'camera_link'
		self.t_old = None 
		self.t_now = None

		info_msg = rospy.wait_for_message('/camera/color/camera_info', CameraInfo, timeout=None)
		self.fx = info_msg.P[0]
		self.fy = info_msg.P[5]
		self.cx = info_msg.P[2]
		self.cy = info_msg.P[6]

		### msg filter 
		self.is_compressed = False

		image_sub = message_filters.Subscriber('/camera/color/image_raw', Image)
		depth_sub = message_filters.Subscriber('/camera/aligned_depth_to_color/image_raw', Image)
		ts = message_filters.TimeSynchronizer([image_sub, depth_sub], 10)
		ts.registerCallback(self.callback)
Ejemplo n.º 5
0
    def __init__(self):
        model = "v1"
        self.prob_threshold = 0.85
        self.cv_bridge = CvBridge()
        self.num_points = 8000
        self.labels = [
            'background',  # always index 0
            'person',
            'palm'
        ]
        self.objects = []
        if model == "v2_lite":
            self.network = create_mobilenetv2_ssd_lite(len(self.labels),
                                                       is_test=True)
        elif model == "v1":
            self.network = create_mobilenetv1_ssd(len(self.labels),
                                                  is_test=True)
        elif model == "v1_lite":
            self.network = create_mobilenetv1_ssd_lite(len(self.labels),
                                                       is_test=True)

        model_path = '/home/arg_ws3/pytorch-ssd/models/argbot_person_palm/mb1-ssd-Epoch-10-Loss-3.1767.pth'
        state_dict = torch.load(os.path.join(model_path))
        self.network.load_state_dict(state_dict)
        DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        self.network.to(DEVICE)
        if model == "v2_lite":
            self.predictor = create_mobilenetv2_ssd_lite_predictor(
                self.network, candidate_size=200, device=DEVICE)
        elif model == "v1_lite":
            self.predictor = create_mobilenetv1_ssd_lite_predictor(
                self.network, candidate_size=200, device=DEVICE)
        elif model == "v1":
            self.predictor = create_mobilenetv1_ssd_predictor(
                self.network, candidate_size=200, device=DEVICE)

        #### Publisher
        self.origin = rospy.Publisher('/input', bb_input, queue_size=1)
        self.image_pub = rospy.Publisher("/predict_img", Image, queue_size=1)
        self.mask_pub = rospy.Publisher("/predict_mask", Image, queue_size=1)

        ### msg filter
        self.is_compressed = False

        video_mode = False
        if video_mode:
            image_sub = rospy.Subscriber('/camera/color/image_raw', Image,
                                         self.video_callback)
        else:
            image_sub = message_filters.Subscriber('/camera/color/image_raw',
                                                   Image)
            depth_sub = message_filters.Subscriber(
                '/camera/aligned_depth_to_color/image_raw', Image)
            ts = message_filters.TimeSynchronizer([image_sub, depth_sub], 10)
            ts.registerCallback(self.callback)
    def __init__(self):
        model = "v1"
        self.prob_threshold = 0.65
        r = rospkg.RosPack()
        self.path = r.get_path('ssd_mobile_lite')
        self.cv_bridge = CvBridge()
        self.num_points = 8000
        self.labels = [
            'background',  # always index 0
            'duckie_car',
            'house',
            'broken',
            'duck'
        ]
        self.objects = []
        if model == "v2_lite":
            self.network = create_mobilenetv2_ssd_lite(len(self.labels),
                                                       is_test=True)
            model_dir = "/home/nvidia"
            model_name = "model1.pth"
        elif model == "v1":
            self.network = create_mobilenetv1_ssd(len(self.labels),
                                                  is_test=True)
            model_name = "/models/model.pth"

        elif model == "v1_lite":
            self.network = create_mobilenetv1_ssd_lite(len(self.labels),
                                                       is_test=True)
            model_name = "/models/model.pth"

        state_dict = torch.load(self.path + model_name)
        self.network.load_state_dict(state_dict)
        DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        self.network.to(DEVICE)
        if model == "v2_lite":
            self.predictor = create_mobilenetv2_ssd_lite_predictor(
                self.network, candidate_size=200, device=DEVICE)
        elif model == "v1_lite":
            self.predictor = create_mobilenetv1_ssd_lite_predictor(
                self.network, candidate_size=200, device=DEVICE)
        elif model == "v1":
            self.predictor = create_mobilenetv1_ssd_predictor(
                self.network, candidate_size=200, device=DEVICE)
        print("finish load model")
        #### Publisher
        self.image_pub = rospy.Publisher("/predict_img", Image, queue_size=1)

        ### msg filter
        self.is_compressed = True

        image_sub = rospy.Subscriber('/camera/color/image_raw/compressed',
                                     CompressedImage, self.callback)
Ejemplo n.º 7
0
    def get_model(self):
        device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

        net_type = 'mb1-ssd'  # sys.argv[1]
        model_path = './models/mobilenet-v1-ssd-mp-0_675.pth'  # sys.argv[2]
        label_path = './models/voc-model-labels.txt'  # sys.argv[3]

        class_names = [name.strip() for name in open(label_path).readlines()]

        net = create_mobilenetv1_ssd(len(class_names), is_test=True)
        net.load(model_path)
        predictor = create_mobilenetv1_ssd_predictor(net,
                                                     candidate_size=200,
                                                     device=device)
        return predictor
Ejemplo n.º 8
0
        def __init__(self, mode):

            self.config = patch_config.patch_configs[mode](
            )  # select the mode for the patch

            # load cfg file (.yaml) and override default cfg options in lib_ssd.utils.config_parse
            # cfg_from_file(self.config.cfgfile_ssds)
            # self.cfgfile_ssds = cfg

            self.device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
            print(torch.cuda.device_count())

            #self.darknet_model = Darknet(self.config.cfgfile)
            #self.darknet_model.load_weights(self.config.weightfile)

            #self.mbntv2_ssdlite_model, self.priorbox = create_model(self.cfgfile_ssds.MODEL) # COCO
            #self.priors = Variable(self.priorbox.forward(), volatile=True) # num_priors = grid x grid x num_anchors

            self.mbntv1_ssd_model = create_mobilenetv1_ssd(21,
                                                           is_test=True)  # VOC
            self.mbntv1_ssd_model.load(self.config.ssdmbntv1_model_path)

            if use_cuda:
                #self.darknet_model = self.darknet_model.eval().to(self.device)  # Why eval? test!
                self.mbntv1_ssd_model = self.mbntv1_ssd_model.eval().to(
                    self.device)
                self.patch_applier = PatchApplier().to(self.device)
                self.patch_transformer = PatchTransformer().to(self.device)
                #self.prob_extractor = MaxProbExtractor(0, 80, self.config).to(self.device)
                self.score_extractor_ssd = ssd_feature_output_manage(
                    15, 21, self.config).to(
                        self.device
                    )  # 15 is person class in VOC (with 21 elements)
                self.nps_calculator = NPSCalculator(self.config.printfile,
                                                    self.config.patch_size).to(
                                                        self.device)
                self.total_variation = TotalVariation().to(self.device)
            else:
                #self.darknet_model = self.darknet_model.eval()  # Why eval? test!
                self.mbntv1_ssd_model = self.mbntv1_ssd_model.eval()
                self.patch_applier = PatchApplier()
                self.patch_transformer = PatchTransformer()
                #self.prob_extractor = MaxProbExtractor(0, 80, self.config)
                self.score_extractor_ssd = ssd_feature_output_manage(
                    15, 21, self.config).to(self.device)
                self.nps_calculator = NPSCalculator(self.config.printfile,
                                                    self.config.patch_size)
                self.total_variation = TotalVariation()
Ejemplo n.º 9
0
def net_select(model_type, class_names):
    if model_type == 'vgg16-ssd':
        return create_vgg_ssd(len(class_names), is_test=True)
    elif model_type == 'mb1-ssd':
        return create_mobilenetv1_ssd(len(class_names), is_test=True)
    elif model_type == 'mb1-ssd-lite':
        return create_mobilenetv1_ssd_lite(len(class_names), is_test=True)
    elif model_type == 'mb2-ssd-lite':
        return create_mobilenetv2_ssd_lite(len(class_names), is_test=True)
    elif model_type == 'sq-ssd-lite':
        return create_squeezenet_ssd_lite(len(class_names), is_test=True)
    else:
        print(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        sys.exit(1)
Ejemplo n.º 10
0
 def _create_network(self, net_type):
     if net_type == 'vgg16-ssd':
         return create_vgg_ssd(len(self.class_names), is_test=True)
     elif net_type == 'mb1-ssd':
         return create_mobilenetv1_ssd(len(self.class_names), is_test=True)
     elif net_type == 'mb1-ssd-lite':
         return create_mobilenetv1_ssd_lite(len(self.class_names),
                                            is_test=True)
     elif net_type == 'mb2-ssd-lite':
         return create_mobilenetv2_ssd_lite(len(self.class_names),
                                            is_test=True)
     elif net_type == 'sq-ssd-lite':
         return create_squeezenet_ssd_lite(len(self.class_names),
                                           is_test=True)
     else:
         raise RuntimeError(
             "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
         )
def select_net(model_path, net_type, num_classes):
    '''
    选择模型
    :param model_path: 模型路径
    :param net_type: 模型类型
    :param num_classes: label个数,label=0,是背景
    :return:
    '''
    if net_type == 'vgg16-ssd':
        net = create_vgg_ssd(num_classes, is_test=True)
    elif net_type == 'mb1-ssd':
        net = create_mobilenetv1_ssd(num_classes, is_test=True)
    elif net_type == 'mb1-ssd-lite':
        net = create_mobilenetv1_ssd_lite(num_classes, is_test=True)
    elif net_type == 'mb2-ssd-lite':
        net = create_mobilenetv2_ssd_lite(num_classes,
                                          is_test=True,
                                          device=device)
    elif net_type == 'sq-ssd-lite':
        net = create_squeezenet_ssd_lite(num_classes, is_test=True)
    else:
        print(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        sys.exit(1)
    net.load(model_path)
    if net_type == 'vgg16-ssd':
        predictor = create_vgg_ssd_predictor(net, candidate_size=200)
    elif net_type == 'mb1-ssd':
        predictor = create_mobilenetv1_ssd_predictor(net, candidate_size=200)
    elif net_type == 'mb1-ssd-lite':
        predictor = create_mobilenetv1_ssd_lite_predictor(net,
                                                          candidate_size=200)
    elif net_type == 'mb2-ssd-lite':
        predictor = create_mobilenetv2_ssd_lite_predictor(net,
                                                          candidate_size=200,
                                                          device=device)
    elif net_type == 'sq-ssd-lite':
        predictor = create_squeezenet_ssd_lite_predictor(net,
                                                         candidate_size=200)
    else:
        predictor = create_vgg_ssd_predictor(net, candidate_size=200)
    return predictor
	def __init__(self):

		model = "v1"
		r = rospkg.RosPack()
		path = r.get_path('ssd_mobile_lite')
		model_name = "Epoch-630-Loss-0.4744.pth"
		self.prob_threshold = 0.5
		self.cv_bridge = CvBridge() 

		self.labels = ['BACKGROUND', 'backpack']
		if model == "v2_lite":
			self.network = create_mobilenetv2_ssd_lite(len(self.labels), is_test=True) 
		elif model == "v1":
			self.network = create_mobilenetv1_ssd(len(self.labels), is_test=True) 	
		elif model == "v1_lite":
			self.network = create_mobilenetv1_ssd_lite(len(self.labels), is_test=True) 

		state_dict = torch.load(os.path.join(path, "weights/", model_name))
		self.network.load_state_dict(state_dict)
		DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
		self.network.to(DEVICE)
		if model == "v2_lite":
			self.predictor = create_mobilenetv2_ssd_lite_predictor(self.network, candidate_size=200, device = DEVICE)
		elif model == "v1_lite":
			self.predictor = create_mobilenetv1_ssd_lite_predictor(self.network, candidate_size=200, device = DEVICE)
		elif model == "v1":	
			self.predictor = create_mobilenetv1_ssd_predictor(self.network, candidate_size=200, device = DEVICE)

		## Publisher
		self.image_pub = rospy.Publisher("camera/predict_img/", Image, queue_size=1)
		self.BoundingBoxes_pub = rospy.Publisher("BoundingBoxes/", BoundingBoxes, queue_size = 1)

		## msg filter 
		self.depth_sub = message_filters.Subscriber(
			"camera/aligned_depth_to_color/image_raw", Image)
		self.image_sub = message_filters.Subscriber("camera/color/image_raw", Image)
		self.ts = message_filters.ApproximateTimeSynchronizer(
			[self.image_sub, self.depth_sub], 5, 5)
		self.ts.registerCallback(self.img_cb)

		print("Start Predicting image")
    def __init__(self):
        model = "v1"
        self.prob_threshold = 0.65
        self.cv_bridge = CvBridge() 
        self.num_points = 8000
        self.labels = ['background' , # always index 0
            'duckie_car','house','broken','duck']
        self.objects = []
        if model == "v2_lite":
            self.network = create_mobilenetv2_ssd_lite(len(self.labels), is_test=True) 
            model_dir = "/home/nvidia"
            model_name = "model1.pth"
        elif model == "v1":
            self.network = create_mobilenetv1_ssd(len(self.labels), is_test=True) 
            model_dir = "/home/nvidia"
            model_name = "model.pth"    
        elif model == "v1_lite":
            self.network = create_mobilenetv1_ssd_lite(len(self.labels), is_test=True) 
            model_dir = "/home/nvidia/"
            model_name = "model.pth"

        state_dict = torch.load(os.path.join(model_dir, model_name))
        self.network.load_state_dict(state_dict)
        DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        self.network.to(DEVICE)
        if model == "v2_lite":
            self.predictor = create_mobilenetv2_ssd_lite_predictor(self.network, candidate_size=200, device = DEVICE)
        elif model == "v1_lite":
            self.predictor = create_mobilenetv1_ssd_lite_predictor(self.network, candidate_size=200, device = DEVICE)
        elif model == "v1": 
            self.predictor = create_mobilenetv1_ssd_predictor(self.network, candidate_size=200, device = DEVICE)

        #### Publisher
        self.origin = rospy.Publisher('/input', bb_input, queue_size=1) 
        self.image_pub = rospy.Publisher("/predict_img", Image, queue_size = 1)
        self.mask_pub = rospy.Publisher("/predict_mask", Image, queue_size = 1)
        self.position = rospy.Publisher("/position", Point32, queue_size = 1)
        ### msg filter 
        self.is_compressed = False

        image_sub = rospy.Subscriber('/camera/color/image_raw', Image, self.callback)
Ejemplo n.º 14
0
def load_model(args):
    device = torch.device(args.device)
    print("Loading model")
    if (args.model == 'yolo'):
        # Initialize model
        model = Darknet(args.cfg, img_size=416)
        # Load weights
        attempt_download(args.weights)
        if args.weights.endswith('.pt'):  # pytorch format
            model.load_state_dict(
                torch.load(args.weights, map_location=device)['model'])
        else:  # darknet format
            load_darknet_weights(model, args.weights)
    elif (args.model == 'mobilenetv1_ssd'):
        model = create_mobilenetv1_ssd(len(voc_class_names), is_test=True)
        model.load('./saved_models/mobilenet-v1-ssd-mp-0_675.pth')
    else:
        model = detection.__dict__[args.model](num_classes=91,
                                               pretrained=args.pretrained)

    return model
Ejemplo n.º 15
0
def main():
    print('cuda device count: ', torch.cuda.device_count())
    DEVICE = 'cuda:0'
    class_names = [
        name.strip()
        for name in open('models/voc-model-labels.txt').readlines()
    ]

    image = torch.ones(1, 3, 300, 300).to(DEVICE)

    net = create_mobilenetv1_ssd(len(class_names), is_test=True)
    net.load('models/mobilenet-v1-ssd-mp-0_675.pth')
    net = net.to(DEVICE)

    net = net.eval()
    scores, boxes = net(image)
    print(net(image))
    print("Input shape ", image.shape)
    print("Scores shape ", scores.shape)
    print("Boxes shape ", boxes.shape)

    export_as_weights(net)
Ejemplo n.º 16
0
def prepare_predictor(net_type, model_path, label_path):
    class_names = [name.strip() for name in open(label_path).readlines()]
    num_classes = len(class_names)
    if net_type == 'vgg16-ssd':
        net = create_vgg_ssd(num_classes, is_test=True)
    elif net_type == 'mb1-ssd':
        net = create_mobilenetv1_ssd(num_classes, is_test=True)
    elif net_type == 'mb1-ssd-lite':
        net = create_mobilenetv1_ssd_lite(num_classes, is_test=True)
    elif net_type == 'mb2-ssd-lite':
        net = create_mobilenetv2_ssd_lite(num_classes, is_test=True)
    elif net_type == 'sq-ssd-lite':
        net = create_squeezenet_ssd_lite(num_classes, is_test=True)
    else:
        raise ValueError(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )

    net.load(model_path)

    if net_type == 'vgg16-ssd':
        predictor = create_vgg_ssd_predictor(net, candidate_size=200)
    elif net_type == 'mb1-ssd':
        predictor = create_mobilenetv1_ssd_predictor(net, candidate_size=200)
    elif net_type == 'mb1-ssd-lite':
        predictor = create_mobilenetv1_ssd_lite_predictor(net,
                                                          candidate_size=200)
    elif net_type == 'mb2-ssd-lite':
        predictor = create_mobilenetv2_ssd_lite_predictor(net,
                                                          candidate_size=200)
    elif net_type == 'sq-ssd-lite':
        predictor = create_squeezenet_ssd_lite_predictor(net,
                                                         candidate_size=200)
    else:
        raise ValueError(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )

    return class_names, predictor
Ejemplo n.º 17
0
def main(args):
    net_type = args.net_type
    img_folder = args.img_folder
    model_path = args.weights_path
    label_path = args.label_path
    class_names = [name.strip() for name in open(label_path).readlines()]
    out_path = args.out_path
    if not os.path.exists(out_path):
        os.mkdir(out_path)

    num_gpus = torch.cuda.device_count()
    device = 'cuda' if num_gpus else 'cpu'

    if net_type == 'vgg16-ssd':
        net = create_vgg_ssd(len(class_names), is_test=True)
    elif net_type == 'mb1-ssd':
        net = create_mobilenetv1_ssd(len(class_names), is_test=True)
    elif net_type == 'mb1-ssd-lite':
        net = create_mobilenetv1_ssd_lite(len(class_names), is_test=True)
    elif net_type == 'mb2-ssd-lite':
        net = create_mobilenetv2_ssd_lite(len(class_names), is_test=True)
    elif net_type == 'sq-ssd-lite':
        net = create_squeezenet_ssd_lite(len(class_names), is_test=True)
    #elif net_type == 'mb3-ssd-lite':
    #    net = create_mobilenetv3_ssd_lite(len(class_names), is_test=True)
    else:
        print(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        sys.exit(1)

    net.load(model_path)

    if net_type == 'vgg16-ssd':
        predictor = create_vgg_ssd_predictor(net,
                                             candidate_size=20,
                                             device=device)
    elif net_type == 'mb1-ssd':
        predictor = create_mobilenetv1_ssd_predictor(net,
                                                     candidate_size=20,
                                                     device=device)
    elif net_type == 'mb1-ssd-lite':
        predictor = create_mobilenetv1_ssd_lite_predictor(net,
                                                          candidate_size=20,
                                                          device=device)
    elif net_type == 'mb2-ssd-lite':
        predictor = create_mobilenetv2_ssd_lite_predictor(net,
                                                          candidate_size=20,
                                                          device=device)
    elif net_type == 'sq-ssd-lite':
        predictor = create_squeezenet_ssd_lite_predictor(net,
                                                         candidate_size=20,
                                                         device=device)
    #elif net_type == 'mb3-ssd-lite':
    #    predictor = create_mobilenetv3_ssd_lite_predictor(net, candidate_size=10)
    else:
        print(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        sys.exit(1)

    timer = Timer()

    img_names = glob.glob(img_folder + os.sep + '*.jpg')
    #result_csv=os.path.join(out_path,'rest_result.csv')
    if len(img_names) == 0:
        print('No imagesfound in {}'.format(img_folder))
        exit(-1)

    for img_name in img_names:
        image = cv2.imread(img_name)

        timer.start()
        boxes, labels, probs = predictor.predict(image, 10, 0.3)
        interval = timer.end()

        print('Time: {:.2f}s, Detect Objects: {:d}.'.format(
            interval, labels.size(0)))

        label_text = []
        for i in range(boxes.size(0)):
            box = boxes[i, :]
            label = f"{class_names[labels[i]]}: {probs[i]:.2f}"
            label_text.append(label)
            cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]),
                          (255, 255, 0), 4)
            cv2.putText(image, label, (box[0] + 20, box[1] + 40),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 255), 2)

        if args.store_result:
            new_name = '{}/{}'.format(out_path, img_name.split('/')[-1])
            cv2.imwrite(new_name, image)
            if not label_text:
                result_label = 'empty'
            else:
                result_label = label_text[0]
            with open(os.path.join(out_path, 'rest_result.csv'),
                      'a+') as result_writer:
                result_writer.write(
                    img_name.split('/')[-1] + ',' + result_label + '\n')

        cv2.imshow('result', image)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cv2.destroyAllWindows()
Ejemplo n.º 18
0
    def __init__(self):
        model = "v1_lite"
        self.prob_threshold = 0.85
        self.cv_bridge = CvBridge()
        self.num_points = 8000
        self.labels = [
            'background',  # always index 0
            'bb_extinguisher',
            'bb_drill',
            'bb_backpack'
        ]
        self.objects = []
        if model == "v2_lite":
            self.network = create_mobilenetv2_ssd_lite(len(self.labels),
                                                       is_test=True)
            model_dir = "/home/andyser/code/exercise_ML/pytorch-ssd-mobile/models/subt_v2_lite"
            model_name = "model.pth"
        elif model == "v1":
            self.network = create_mobilenetv1_ssd(len(self.labels),
                                                  is_test=True)
            model_dir = "/home/andyser/code/exercise_ML/pytorch-ssd-mobile/models/subt_v1"
            model_name = "model.pth"
        elif model == "v1_lite":
            self.network = create_mobilenetv1_ssd_lite(len(self.labels),
                                                       is_test=True)
            model_dir = "/home/andyser/code/exercise_ML/pytorch-ssd-mobile/models/subt_v1_lite"
            model_name = "model.pth"

        state_dict = torch.load(os.path.join(model_dir, model_name))
        self.network.load_state_dict(state_dict)
        DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        self.network.to(DEVICE)
        if model == "v2_lite":
            self.predictor = create_mobilenetv2_ssd_lite_predictor(
                self.network, candidate_size=200, device=DEVICE)
        elif model == "v1_lite":
            self.predictor = create_mobilenetv1_ssd_lite_predictor(
                self.network, candidate_size=200, device=DEVICE)
        elif model == "v1":
            self.predictor = create_mobilenetv1_ssd_predictor(
                self.network, candidate_size=200, device=DEVICE)

        #### Publisher
        self.origin = rospy.Publisher('/input', bb_input, queue_size=1)
        self.image_pub = rospy.Publisher("/predict_img", Image, queue_size=1)
        self.mask_pub = rospy.Publisher("/predict_mask", Image, queue_size=1)

        ### msg filter
        self.is_compressed = False

        video_mode = False
        if video_mode:
            image_sub = rospy.Subscriber('/camera/color/image_raw', Image,
                                         self.video_callback)
        else:
            image_sub = message_filters.Subscriber('/camera/color/image_raw',
                                                   Image)
            depth_sub = message_filters.Subscriber(
                '/camera/aligned_depth_to_color/image_raw', Image)
            ts = message_filters.TimeSynchronizer([image_sub, depth_sub], 10)
            ts.registerCallback(self.callback)
Ejemplo n.º 19
0
def main(args):
    net_type = args.net_type
    model_path = args.weights_path
    label_path = args.label_path
    class_names = [name.strip() for name in open(label_path).readlines()]
    num_classes = len(class_names)

    if args.live:
        cap = cv2.VideoCapture(0)
        cap.set(3, 640)
        cap.set(4, 480)
    else:
        cap = cv2.VideoCapture(args.video_path)

    Fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V')
    writer = cv2.VideoWriter('result.mp4',
                             fourcc=Fourcc,
                             fps=15,
                             frameSize=(640, 480))

    num_gpus = torch.cuda.device_count()
    device = 'cuda' if num_gpus else 'cpu'

    if net_type == 'vgg16-ssd':
        net = create_vgg_ssd(len(class_names), is_test=True)
    elif net_type == 'mb1-ssd':
        net = create_mobilenetv1_ssd(len(class_names), is_test=True)
    elif net_type == 'mb1-ssd-lite':
        net = create_mobilenetv1_ssd_lite(len(class_names), is_test=True)
    elif net_type == 'mb2-ssd-lite':
        net = create_mobilenetv2_ssd_lite(len(class_names), is_test=True)
    elif net_type == 'sq-ssd-lite':
        net = create_squeezenet_ssd_lite(len(class_names), is_test=True)
    #elif net_type == 'mb3-ssd-lite':
    #    net = create_mobilenetv3_ssd_lite(len(class_names), is_test=True)
    else:
        print(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        sys.exit(1)

    net.load(model_path)

    if net_type == 'vgg16-ssd':
        predictor = create_vgg_ssd_predictor(net,
                                             candidate_size=20,
                                             device=device)
    elif net_type == 'mb1-ssd':
        predictor = create_mobilenetv1_ssd_predictor(net,
                                                     candidate_size=20,
                                                     device=device)
    elif net_type == 'mb1-ssd-lite':
        predictor = create_mobilenetv1_ssd_lite_predictor(net,
                                                          candidate_size=20,
                                                          device=device)
    elif net_type == 'mb2-ssd-lite':
        predictor = create_mobilenetv2_ssd_lite_predictor(net,
                                                          candidate_size=20,
                                                          device=device)
    elif net_type == 'sq-ssd-lite':
        predictor = create_squeezenet_ssd_lite_predictor(net,
                                                         candidate_size=20,
                                                         device=device)
    #elif net_type == 'mb3-ssd-lite':
    #    predictor = create_mobilenetv3_ssd_lite_predictor(net, candidate_size=10)
    else:
        print(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        sys.exit(1)

    timer = Timer()

    while True:
        _, orig_image = cap.read()
        if orig_image is None:
            print('END')
            break

        image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB)
        timer.start()
        boxes, labels, probs = predictor.predict(image, 10, 0.4)
        interval = timer.end()
        print('Time: {:.2f}s, Detect Objects: {:d}.'.format(
            interval, labels.size(0)))
        for i in range(boxes.size(0)):
            box = boxes[i, :]
            label = f"{class_names[labels[i]]}: {probs[i]:.2f}"
            cv2.rectangle(orig_image, (box[0], box[1]), (box[2], box[3]),
                          (255, 255, 0), 4)

            cv2.putText(
                orig_image,
                label,
                (box[0] + 20, box[1] + 40),
                cv2.FONT_HERSHEY_SIMPLEX,
                1,  # font scale
                (255, 0, 255),
                2)  # line type
        writer.write(orig_image)
        cv2.imshow('annotated', orig_image)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cap.release()
    writer.release()
    cv2.destroyAllWindows()
    if args.out_video:
        shutil.move('result.mp4', args.out_video)
    else:
        os.remove('result.mp4')
Ejemplo n.º 20
0
import sys

if len(sys.argv) < 5:
    print(
        'Usage: python run_ssd_example.py <net type>  <model path> <image path>'
    )
    sys.exit(0)
net_type = sys.argv[1]
model_path = sys.argv[2]
label_path = sys.argv[3]
image_path = sys.argv[4]

class_names = [name.strip() for name in open(label_path).readlines()]
num_classes = len(class_names)
if net_type == "mobilenet-v1-ssd":
    net = create_mobilenetv1_ssd(num_classes, is_test=True)
else:
    net = create_vgg_ssd(num_classes, is_test=True)
net.load(model_path)
if net_type == "mobilenet-v1-ssd":
    predictor = create_mobilenetv1_ssd_predictor(net, candidate_size=200)
else:
    predictor = create_vgg_ssd_predictor(net, candidate_size=200)

orig_image = cv2.imread(image_path)
image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB)
boxes, labels, probs = predictor.predict(image, 10, 0.4)

for i in range(boxes.size(0)):
    box = boxes[i, :]
    cv2.rectangle(orig_image, (box[0], box[1]), (box[2], box[3]),
Ejemplo n.º 21
0
def main(args):
    DEVICE = torch.device(
        "cuda:0" if torch.cuda.is_available() and args.use_cuda else "cpu")
    if not os.path.exists(args.eval_dir):
        os.mkdir(args.eval_dir)
    timer = Timer()
    class_names = [name.strip() for name in open(args.label_file).readlines()]

    # dataset = Folder_image_set()

    # true_case_stat, all_gb_boxes, all_difficult_cases = group_annotation_by_class(dataset)
    if args.net == 'vgg16-ssd':
        net = create_vgg_ssd(len(class_names), is_test=True)
    elif args.net == 'mb1-ssd':
        net = create_mobilenetv1_ssd(len(class_names), is_test=True)
    elif args.net == 'mb1-ssd-lite':
        net = create_mobilenetv1_ssd_lite(len(class_names), is_test=True)
    elif args.net == 'sq-ssd-lite':
        net = create_squeezenet_ssd_lite(len(class_names), is_test=True)
    elif args.net == 'mb2-ssd-lite':
        net = create_mobilenetv2_ssd_lite(len(class_names),
                                          width_mult=args.mb2_width_mult,
                                          is_test=True)
    else:
        logging.fatal(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        parser.print_help(sys.stderr)
        sys.exit(1)

    #train_transform = MatchPrior(config.priors, config.center_variance,
    #                              config.size_variance, 0.5)

    #test_transform = TestTransform(config.image_size, config.image_mean, config.image_std)
    # test_transform = TestTransform(config.image_size, config.image_mean, config.image_std)
    # dataset = FolderDataset("/media/wy_disk/wy_file/Detection/dataset/datasets/ECP_Golden_pattern", transform = test_transform)
    # dataset = FolderDataset("/media/wy_disk/wy_file/Detection/dataset/datasets/ECP_Golden_pattern")
    dataset = FolderDataset("/media/wy_disk/ChenYen/VIRAT/dataset_orgnize/val")

    timer.start("Load Model")
    net.load(args.trained_model)
    net = net.to(DEVICE)
    print('It took {} seconds to load the model.'.format(
        timer.end("Load Model")))

    if args.net == 'vgg16-ssd':
        predictor = create_vgg_ssd_predictor(net,
                                             nms_method=args.nms_method,
                                             device=DEVICE)
    elif args.net == 'mb1-ssd':
        predictor = create_mobilenetv1_ssd_predictor(
            net, nms_method=args.nms_method, device=DEVICE)
    elif args.net == 'mb1-ssd-lite':
        predictor = create_mobilenetv1_ssd_lite_predictor(
            net, nms_method=args.nms_method, device=DEVICE)
    elif args.net == 'sq-ssd-lite':
        predictor = create_squeezenet_ssd_lite_predictor(
            net, nms_method=args.nms_method, device=DEVICE)
    elif args.net == 'mb2-ssd-lite':
        predictor = create_mobilenetv2_ssd_lite_predictor(
            net, nms_method=args.nms_method, device=DEVICE)
    else:
        logging.fatal(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        parser.print_help(sys.stderr)
        sys.exit(1)

    results = []
    eval_path = "eval_results"
    # eval_whole_image(dataset,5, predictor)
    eval_subblock_image(dataset, 5, predictor)
    import pdb
    pdb.set_trace()
    for i in range(len(dataset)):
        print("process image", i)
        timer.start("Load Image")
        import pdb
        pdb.set_trace()
        image = dataset.get_image(i)
        print("Load Image: {:4f} seconds.".format(timer.end("Load Image")))
        timer.start("Predict")
        boxes, labels, probs = predictor.predict(image, 10, 0.5)
        print("Prediction: {:4f} seconds.".format(timer.end("Predict")))
        # indexes = torch.ones(labels.size(0), 1, dtype=torch.float32) * i
        # print("index:p\t{}".format(sum(probs>0.5)))
        # import pdb;pdb.set_trace()
        boxes, labels, probs = boxes.data.numpy(), labels.data.numpy(
        ), probs.data.numpy()
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        for box, _label, _prob in zip(boxes, labels, probs):
            if _prob < 0.7: continue
            print(box)
            box = box.astype(int)
            # import pdb;pdb.set_trace()
            print(box)
            cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]),
                          (255, 255, 0), 4)
            # str(str.split(class_names[_label]," ")[1])
            cv2.putText(
                image,
                dataset.class_names[_label],
                (box[0] + 20, box[1] + 40),
                cv2.FONT_HERSHEY_SIMPLEX,
                1,  # font scale
                (255, 0, 255),
                2)  # line type
        print(boxes.shape[0])
        cv2.imshow('annotated', image)
        # key = cv2.waitKey(0)
        if cv2.waitKey(0) & 0xFF == ord('q'):
            break
Ejemplo n.º 22
0
from vision.ssd.mobilenetv1_ssd import create_mobilenetv1_ssd, create_mobilenetv1_ssd_predictor
from torchsummary import summary

import sys

path = './models/mb1-ssd-Epoch-99-Loss-2.7295520901679993.pth'
net = create_mobilenetv1_ssd(2)
# net.load_state_dict()
#
summary(net, (300, 300, 3))

print(net)
Ejemplo n.º 23
0
    for n in range(N):
        for k in range(K):
            v_mat = np.resize(vh[k, N * d:(N + 1) * d], (1, d))
            H[n, k, :, :] = np.sqrt(s[k]) * torch.from_numpy(v_mat)

    return V, H


timer = Timer()

#pretrained Model
model_path = 'models/BaseModel.pth'
k = 4  #Rank

#Create the network using the architectures.
net = create_mobilenetv1_ssd(2, is_test=True)
net_copy = create_shrinked_mobilenetv1_ssd(2, 4, is_test=True)

#Load the original model
net.load(model_path)

#Extract the parameters of the original model
params = net.state_dict()

print(params.keys())

for layer in net_copy.state_dict().keys():
    print(layer, net_copy.state_dict()[layer].shape)

#Select the layer in the original model for computing the Low-Rank Factorization:
layer = 'extras.0.2.weight'
Ejemplo n.º 24
0
def PersonDetector(orig_image, net_type="mb1-ssd"):

    class_names = [name.strip() for name in open(label_path).readlines()]
    num_classes = len(class_names)

    if net_type == 'vgg16-ssd':
        net = create_vgg_ssd(len(class_names), is_test=True)
    elif net_type == 'mb1-ssd':
        net = create_mobilenetv1_ssd(len(class_names), is_test=True)
    elif net_type == 'mb1-ssd-lite':
        net = create_mobilenetv1_ssd_lite(len(class_names), is_test=True)
    elif net_type == 'mb2-ssd-lite':
        net = create_mobilenetv2_ssd_lite(len(class_names), is_test=True)
    elif net_type == 'sq-ssd-lite':
        net = create_squeezenet_ssd_lite(len(class_names), is_test=True)
    else:
        print(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        sys.exit(1)
    net.load(model_path)

    if net_type == 'vgg16-ssd':
        predictor = create_vgg_ssd_predictor(net, candidate_size=200)
    elif net_type == 'mb1-ssd':
        predictor = create_mobilenetv1_ssd_predictor(net, candidate_size=200)
    elif net_type == 'mb1-ssd-lite':
        predictor = create_mobilenetv1_ssd_lite_predictor(net,
                                                          candidate_size=200)
    elif net_type == 'mb2-ssd-lite':
        predictor = create_mobilenetv2_ssd_lite_predictor(net,
                                                          candidate_size=200)
    elif net_type == 'sq-ssd-lite':
        predictor = create_squeezenet_ssd_lite_predictor(net,
                                                         candidate_size=200)
    else:
        print(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        sys.exit(1)

    timer = Timer()
    image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB)
    timer.start()
    boxes, labels, probs = predictor.predict(image, 10, 0.4)
    interval = timer.end()
    print('Time: {:.2f}s, Detect Objects: {:d}.'.format(
        interval, labels.size(0)))
    max_width = -1
    x, y, w, h = None, None, None, None
    for i in range(boxes.size(0)):
        box = boxes[i, :]
        label = f"{class_names[labels[i]]}: {probs[i]:.2f}"
        if (max_width < box[2] - box[0]):
            x, y = box[0], box[1]
            w, h = box[2] - box[0], box[3] - box[1]
            max_width = w

    if (x is not None and y is not None and w is not None and h is not None):
        cv2.rectangle(orig_image, (x, y), (w + x, h + y), (255, 255, 0), 4)

    return (x, y, w, h)


# img = cv2.imread("Image/img.jpg")
# PersonDetector(img)
Ejemplo n.º 25
0
                #Dimension of the rectangle for the bounding box for multi-components sign
                x = 300
                y = 100
                h = 300
                w = 300
            elif (args.bbox == 'small'):
                #Dimension of the rectangle for the bounding box for handshape
                x = 100
                y = 150
                h = 200
                w = 200

        #Detect boundaries of hand using a detection model
        else:
            #Loading detection model
            detect_model = create_mobilenetv1_ssd(2, is_test=True)
            detect_model.load(detection_path)

            # Device configuration
            device = torch.device(
                'cuda:0' if torch.cuda.is_available() else 'cpu')
            if device == 'cpu':
                print("Running on CPU.")
            elif device == 'cuda:0':
                print("Running on GPU.")

            predictor = create_mobilenetv1_ssd_predictor(detect_model,
                                                         candidate_size=200,
                                                         device=device)
            print("Detection Network successfully loaded...")
Ejemplo n.º 26
0
parser.add_argument('--mb2_width_mult', default=1.0, type=float,
                    help='Width Multiplifier for MobilenetV2')
args = parser.parse_args()
DEVICE = torch.device("cuda:0" if torch.cuda.is_available() and args.use_cuda else "cpu")

if __name__ == '__main__':
    eval_path = pathlib.Path(args.eval_dir)
    eval_path.mkdir(exist_ok=True)
    timer = Timer()
    class_names = [name.strip() for name in open(args.label_file).readlines()]

    if args.net == 'vgg16-ssd':
        net = create_vgg_ssd(len(class_names), is_test=True, convert_to_boxes=False)
        config = vgg_ssd_config
    elif args.net == 'mb1-ssd':
        net = create_mobilenetv1_ssd(len(class_names), is_test=True, convert_to_boxes=False)
        config = mobilenetv1_ssd_config
    elif args.net == 'mb1-ssd-lite':
        net = create_mobilenetv1_ssd_lite(len(class_names), is_test=True, convert_to_boxes=False)
        print("Not defined config for mb1 lite")
        exit(-1)
    elif args.net == 'sq-ssd-lite':
        net = create_squeezenet_ssd_lite(len(class_names), is_test=True, convert_to_boxes=False)
        config = squeezenet_ssd_config
    elif args.net == 'mb2-ssd-lite':
        net = create_mobilenetv2_ssd_lite(len(class_names), width_mult=args.mb2_width_mult, is_test=True, convert_to_boxes=False)
        print("Not defined config for mb2 lite")
        exit(-1)
    else:
        logging.fatal("The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite.")
        parser.print_help(sys.stderr)
Ejemplo n.º 27
0
def main(args):
    DEVICE = torch.device("cuda:0" if torch.cuda.is_available()
                          and args['flow_control']['use_cuda'] else "cpu")

    # eval_path = pathlib.Path(args.eval_dir)
    # eval_path.mkdir(exist_ok=True)
    if not os.path.exists(args['flow_control']['eval_dir']):
        os.mkdir(args['flow_control']['eval_dir'])
    timer = Timer()
    class_names = [
        name.strip()
        for name in open(args['flow_control']['label_file']).readlines()
    ]

    _net = args['flow_control']['net']
    _dataset_type = args['flow_control']['dataset_type']

    if _dataset_type == "voc":
        raise NotImplementedError("Not implement error")
        dataset = VOCDataset(args['flow_control']['dataset'], is_test=True)
    elif _dataset_type == 'open_images':
        raise NotImplementedError("Not implement error")
        dataset = OpenImagesDataset(args['flow_control']['dataset'],
                                    dataset_type="test")
    elif _dataset_type == "coco":
        # dataset = CocoDetection("/home/wenyen4desh/datasets/coco/test2017","/home/wenyen4desh/datasets/annotations/image_info_test2017.json")
        #dataset = CocoDetection("../../dataset/datasets/coco/val2017","../../dataset/datasets/coco/annotations/instances_val2017.json")
        # dataset = CocoDetection("/home/wenyen4desh/datasets/coco/train2017","/home/wenyen4desh/datasets/coco/annotations/instances_train2017.json")
        dataset = CocoDetection(args['Datasets']['coco']['val_image_path'],
                                args['Datasets']['coco']['val_anno_path'])
    elif _dataset_type == "ecp":
        dataset = EuroCity_Dataset(args['Datasets']['ecp']['val_image_path'],
                                   args['Datasets']['ecp']['val_anno_path'])
    true_case_stat, all_gb_boxes, all_difficult_cases = group_annotation_by_class(
        dataset)
    if _net == 'vgg16-ssd':
        net = create_vgg_ssd(len(class_names), is_test=True)
    elif _net == 'mb1-ssd':
        net = create_mobilenetv1_ssd(len(class_names), is_test=True)
    elif _net == 'mb1-ssd-lite':
        net = create_mobilenetv1_ssd_lite(len(class_names), is_test=True)
    elif _net == 'sq-ssd-lite':
        net = create_squeezenet_ssd_lite(len(class_names), is_test=True)
    elif _net == 'mb2-ssd-lite':
        net = create_mobilenetv2_ssd_lite(
            len(class_names),
            width_mult=args['flow_control']['mb2_width_mult'],
            is_test=True)
    else:
        logging.fatal(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        parser.print_help(sys.stderr)
        sys.exit(1)

    #train_transform = MatchPrior(config.priors, config.center_variance,
    #                              config.size_variance, 0.5)

    #test_transform = TestTransform(config.image_size, config.image_mean, config.image_std)
    import pdb
    pdb.set_trace()
    ############################## automatically validation ############################################
    timer.start("Load Model")
    net.load(args['flow_control']['trained_model'])
    net = net.to(DEVICE)
    print('It took {} seconds to load the model.'.format(
        timer.end("Load Model")))
    _nms_method = args['flow_control']['nms_method']
    if _net == 'vgg16-ssd':
        predictor = create_vgg_ssd_predictor(net,
                                             nms_method=_nms_method,
                                             device=DEVICE)
    elif _net == 'mb1-ssd':
        predictor = create_mobilenetv1_ssd_predictor(net,
                                                     nms_method=_nms_method,
                                                     device=DEVICE)
    elif _net == 'mb1-ssd-lite':
        predictor = create_mobilenetv1_ssd_lite_predictor(
            net, nms_method=_nms_method, device=DEVICE)
    elif _net == 'sq-ssd-lite':
        predictor = create_squeezenet_ssd_lite_predictor(
            net, nms_method=_nms_method, device=DEVICE)
    elif _net == 'mb2-ssd-lite':
        predictor = create_mobilenetv2_ssd_lite_predictor(
            net, nms_method=_nms_method, device=DEVICE)
    else:
        logging.fatal(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        parser.print_help(sys.stderr)
        sys.exit(1)

    results = []
    # Predict Bounding Box
    for i in range(len(dataset)):
        print("process image {}", i)
        timer.start("Load Image")
        image = dataset.get_image(i)
        print("Load Image: {:4f} seconds.".format(timer.end("Load Image")))
        timer.start("Predict")
        boxes, labels, probs = predictor.predict(image)
        print("Prediction: {:4f} seconds.".format(timer.end("Predict")))
        indexes = torch.ones(labels.size(0), 1, dtype=torch.float32) * i
        results.append(
            torch.cat(
                [
                    indexes.reshape(-1, 1),
                    labels.reshape(-1, 1).float(),
                    probs.reshape(-1, 1),
                    boxes + 1.0  # matlab's indexes start from 1
                ],
                dim=1))
    results = torch.cat(results)

    # Write the result to file
    for class_index, class_name in enumerate(class_names):
        if class_index == 0: continue  # ignore background
        file_name = "det_test_{}.txt".format(class_name)
        prediction_path = os.path.join(args['flow_control']['eval_dir'],
                                       file_name)
        with open(prediction_path, "w") as f:
            sub = results[results[:, 1] == class_index, :]
            for i in range(sub.size(0)):
                prob_box = sub[i, 2:].numpy()
                image_id, _ = dataset.get_annotation(int(sub[i, 0]))
                f.write(
                    str(image_id) + " " + " ".join([str(v)
                                                    for v in prob_box]) + "\n")
                # image_id = dataset.ids[int(sub[i, 0])]
                # print(str(image_id) + " " + " ".join([str(v) for v in prob_box]), file=f)

    aps = []
    prcs = []
    recalls = []
    print("\n\nAverage Precision Per-class:")
    for class_index, class_name in enumerate(class_names):
        if class_index == 0:
            continue
        file_name = "det_test_{}.txt".format(class_name)
        prediction_path = os.path.join(args['flow_control']['eval_dir'],
                                       file_name)
        # [email protected] evaluation method
        ap, precision, recall = compute_average_precision_per_class(
            args, true_case_stat[class_index], all_gb_boxes[class_index],
            all_difficult_cases[class_index], prediction_path,
            args['flow_control']['iou_threshold'],
            args['flow_control']['use_2007_metric'])

        # # COCO eval

        # ap, precision, recall = coco_ap_per_class(
        #     true_case_stat[class_index],
        #     all_gb_boxes[class_index],
        #     all_difficult_cases[class_index],
        #     prediction_path,
        #     args.use_2007_metric
        # )

        aps.append(ap)
        prcs.append(precision)
        recalls.append(recall)
        print("{}: {}".format(class_name, ap))

    print("\nAverage Precision Across All Classes:{}".format(
        sum(aps[0:5]) / len(aps[0:5])))
    print("\nAverage Precision :{}".format(sum(prcs[0:5]) / len(prcs[0:5])))
    print("\nAverage Recall :{}".format(sum(recalls[0:5]) / len(recalls[0:5])))
Ejemplo n.º 28
0
        running_loss += loss.item()
        running_regression_loss += regression_loss.item()
        running_classification_loss += classification_loss.item()
    return running_loss / num, running_regression_loss / num, running_classification_loss / num


if __name__ == '__main__':
    timer = Timer()

    logging.info(args)
    if args.net == 'vgg16-ssd':
        create_net = create_vgg_ssd
        config = vgg_ssd_config
    elif args.net == 'mb1-ssd':
        create_net = lambda num: create_mobilenetv1_ssd(num, alpha=args.mb1_width_mult)
        # create_net = create_mobilenetv1_ssd
        config = mobilenetv1_ssd_config
        config = mobilenetv1_ssd_config
    elif args.net == 'mb1-ssd-lite':
        create_net = create_mobilenetv1_ssd_lite
        config = mobilenetv1_ssd_config
    elif args.net == 'sq-ssd-lite':
        create_net = create_squeezenet_ssd_lite
        config = squeezenet_ssd_config
    elif args.net == 'mb2-ssd-lite':
        create_net = lambda num: create_mobilenetv2_ssd_lite(num, width_mult=args.mb2_width_mult)
        config = mobilenetv1_ssd_config
    else:
        logging.fatal("The net type is wrong.")
        parser.print_help(sys.stderr)
Ejemplo n.º 29
0
from vision.ssd.mobilenet_v2_ssd_lite import create_mobilenetv2_ssd_lite, create_mobilenetv2_ssd_lite_predictor
from vision.utils.misc import Timer
import PySimpleGUI as sg
import cv2
import numpy as np
import sys
import time

net_type = 'mb1-ssd'
model_path = './models/mb1-ssd-Epoch-1290-Loss-2.35230020682017.pth'
label_path = './models/voc-model-labels.txt'

class_names = [name.strip() for name in open(label_path).readlines()]
num_classes = len(class_names)

net = create_mobilenetv1_ssd(len(class_names), is_test=True)
net.load(model_path)
predictor = create_mobilenetv1_ssd_predictor(net, candidate_size=200)

cap = cv2.VideoCapture(0, cv2.CAP_V4L2)  # capture from camera
#cap = cv2.VideoCapture(0)
#cap.set(3, 1080)
#cap.set(4, 940)

sg.theme('DarkAmber')
# define the window layout
layout = [[sg.Image(filename='', key='image')],
          [
              sg.Checkbox('ヘルメット', size=(8, 1), font='Helvetica 14'),
              sg.Checkbox('溶接マスク', size=(8, 1), font='Helvetica 14'),
              sg.Checkbox('作業着', size=(8, 1), font='Helvetica 14'),
Ejemplo n.º 30
0
def imwrite(dataset, net_type, epoch, model_path):
    num_classes = len(dataset.class_names)
    if net_type == 'vgg16-ssd':
        net = create_vgg_ssd(num_classes, is_test=True)
    elif net_type == 'mb1-ssd':
        net = create_mobilenetv1_ssd(num_classes, is_test=True)
    elif net_type == 'mb1-ssd-lite':
        net = create_mobilenetv1_ssd_lite(num_classes, is_test=True)
    elif net_type == 'mb2-ssd-lite':
        net = create_mobilenetv2_ssd_lite(num_classes, is_test=True)
    elif net_type == 'mb3-large-ssd-lite':
        net = create_mobilenetv3_large_ssd_lite(num_classes, is_test=True)
    elif net_type == 'mb3-small-ssd-lite':
        net = create_mobilenetv3_small_ssd_lite(num_classes, is_test=True)
    elif net_type == 'sq-ssd-lite':
        net = create_squeezenet_ssd_lite(num_classes, is_test=True)
    else:
        print(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        sys.exit(1)
    net.load(model_path)

    if net_type == 'vgg16-ssd':
        predictor = create_vgg_ssd_predictor(net, candidate_size=200)
    elif net_type == 'mb1-ssd':
        predictor = create_mobilenetv1_ssd_predictor(net, candidate_size=200)
    elif net_type == 'mb1-ssd-lite':
        predictor = create_mobilenetv1_ssd_lite_predictor(net,
                                                          candidate_size=200)
    elif net_type == 'mb2-ssd-lite' or net_type == "mb3-large-ssd-lite" or net_type == "mb3-small-ssd-lite":
        predictor = create_mobilenetv2_ssd_lite_predictor(net,
                                                          candidate_size=200)
    elif net_type == 'sq-ssd-lite':
        predictor = create_squeezenet_ssd_lite_predictor(net,
                                                         candidate_size=200)
    else:
        predictor = create_vgg_ssd_predictor(net, candidate_size=200)
    for i in range(10):
        image, orig_boxes, labels = dataset[i]
        orig_image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        boxes, labels, probs = predictor.predict(image, 10, 0.4)

        for j in range(boxes.size(0)):  # predict
            box = boxes[j, :]
            box = [int(i) for i in box]
            cv2.rectangle(orig_image, (box[0], box[1]), (box[2], box[3]),
                          (255, 255, 0), 1)
            label = f"{probs[j]:.2f}"
            cv2.putText(
                orig_image,
                label,
                (box[0] + 3, box[1] + 5),
                cv2.FONT_HERSHEY_SIMPLEX,
                1,  # font scale
                (255, 255, 0),
                1)  # line type
        for j in range(orig_boxes.shape[0]):  # ground truth
            box = orig_boxes[j, :]
            cv2.rectangle(orig_image, (box[0], box[1]), (box[2], box[3]),
                          (255, 0, 255), 1)

        path = f"out/{i:02}_{epoch:04}.jpg"
        cv2.imwrite(path, orig_image)