def _predict(self, img_msg, label_msg):
     # convert image
     bridge = cv_bridge.CvBridge()
     input_image = bridge.imgmsg_to_cv2(img_msg, 'rgb8')
     input_label = bridge.imgmsg_to_cv2(label_msg)
     # predict
     region_imgs = []
     for l in np.unique(input_label):
         if l == 0:  # bg_label
             continue
         mask = (input_label == l)
         region = jsk_recognition_utils.bounding_rect_of_mask(
             input_image, mask)
         region_imgs.append(region)
     y_proba = self.estimator.predict(region_imgs)
     target_names = np.array(jsk_apc2015_common.get_object_list())
     y_pred = np.argmax(y_proba, axis=-1)
     label_proba = [p[i] for p, i in zip(y_proba, y_pred)]
     # prepare message
     res = ClassificationResult()
     res.header = img_msg.header
     res.labels = y_pred
     res.label_names = target_names[y_pred]
     res.label_proba = label_proba
     res.probabilities = y_proba.reshape(-1)
     res.classifier = '<jsk_2015_05_baxter_apc.ColorHistogramFeatures>'
     res.target_names = target_names
     self._pub.publish(res)
    def _apply(self, bof_msg, ch_msg):
        target_names = bof_msg.target_names
        assert target_names == ch_msg.target_names

        N_label = len(target_names)
        bof_proba = np.array(bof_msg.probabilities).reshape((-1, N_label))
        ch_proba = np.array(ch_msg.probabilities).reshape((-1, N_label))
        bof_weight = np.array([self.weight[n]['bof'] for n in target_names])
        ch_weight = np.array([self.weight[n]['color'] for n in target_names])
        y_proba = (bof_weight * bof_proba) + (ch_weight * ch_proba)

        # verification result for debug
        y_pred = np.argmax(y_proba, axis=-1)
        target_names = np.array(target_names)
        label_proba = [p[i] for p, i in zip(y_proba, y_pred)]
        # compose msg
        msg = ClassificationResult()
        msg.header = bof_msg.header
        msg.labels = y_pred
        msg.label_names = target_names[y_pred]
        msg.label_proba = label_proba
        msg.probabilities = y_proba.reshape(-1)
        msg.classifier = '<jsk_2015_05_baxter_apc.BoostObjectRecognition>'
        msg.target_names = target_names
        self.pub.publish(msg)
    def _apply(self, bof_msg, ch_msg):
        target_names = bof_msg.target_names
        assert target_names == ch_msg.target_names

        N_label = len(target_names)
        bof_proba = np.array(bof_msg.probabilities).reshape((-1, N_label))
        ch_proba = np.array(ch_msg.probabilities).reshape((-1, N_label))
        bof_weight = np.array([self.weight[n]['bof'] for n in target_names])
        ch_weight = np.array([self.weight[n]['color'] for n in target_names])
        y_proba = (bof_weight * bof_proba) + (ch_weight * ch_proba)

        # verification result for debug
        y_pred = np.argmax(y_proba, axis=-1)
        target_names = np.array(target_names)
        label_proba = [p[i] for p, i in zip(y_proba, y_pred)]
        # compose msg
        msg = ClassificationResult()
        msg.header = bof_msg.header
        msg.labels = y_pred
        msg.label_names = target_names[y_pred]
        msg.label_proba = label_proba
        msg.probabilities = y_proba.reshape(-1)
        msg.classifier = '<jsk_2015_05_baxter_apc.BoostObjectRecognition>'
        msg.target_names = target_names
        self.pub.publish(msg)
 def _cb(self, imgmsg):
     bridge = cv_bridge.CvBridge()
     img = bridge.imgmsg_to_cv2(imgmsg)
     label, proba = self._classify(img)
     msg = ClassificationResult()
     msg.header = imgmsg.header
     msg.labels = [label]
     msg.label_names = [self.target_names[label]]
     msg.label_proba = [proba[label]]
     msg.probabilities = proba
     msg.classifier = self.classifier_name
     msg.target_names = self.target_names
     self.pub.publish(msg)
 def _cb(self, imgmsg):
     bridge = cv_bridge.CvBridge()
     img = bridge.imgmsg_to_cv2(imgmsg)
     label, proba = self._classify(img)
     msg = ClassificationResult()
     msg.header = imgmsg.header
     msg.labels = [label]
     msg.label_names = [self.target_names[label]]
     msg.label_proba = [proba[label]]
     msg.probabilities = proba
     msg.classifier = self.classifier_name
     msg.target_names = self.target_names
     self.pub.publish(msg)
Esempio n. 6
0
    def callback(self, imgmsg, mask_msg):
        bridge = cv_bridge.CvBridge()
        img = bridge.imgmsg_to_cv2(imgmsg, desired_encoding='bgr8')
        mask = bridge.imgmsg_to_cv2(mask_msg, desired_encoding='mono8')
        if mask.ndim == 3:
            mask = np.squeeze(mask, axis=2)
        mask = mask >= 127  # uint8 -> bool

        img = img.astype(np.float64)
        img[mask] -= self.mean[mask]
        img[~mask] = 0

        img = img.transpose(2, 0, 1)
        img = img.astype(np.float32)
        x_data = np.asarray([img])
        if self.gpu >= 0:
            x_data = cuda.to_gpu(x_data)
        x = chainer.Variable(x_data)
        y = self.model(x)

        feat = cuda.to_cpu(y.data)
        feat = feat.squeeze(axis=(2, 3))
        X_query = feat

        y_pred_proba = self.knn.predict_proba(X_query)
        y_pred = np.argmax(y_pred_proba, axis=1)

        classes = self.knn.classes_
        target_names = self.target_names[classes]

        msg = ClassificationResult()
        msg.header = imgmsg.header
        msg.labels = y_pred.tolist()
        msg.label_names = target_names[y_pred].tolist()
        msg.label_proba = y_pred_proba[:, y_pred].flatten().tolist()
        msg.probabilities = y_pred_proba.flatten().tolist()
        msg.target_names = target_names.tolist()
        self.pub.publish(msg)
    def callback(self, imgmsg, mask_msg):
        bridge = cv_bridge.CvBridge()
        img = bridge.imgmsg_to_cv2(imgmsg, desired_encoding='bgr8')
        mask = bridge.imgmsg_to_cv2(mask_msg, desired_encoding='mono8')
        if mask.ndim == 3:
            mask = np.squeeze(mask, axis=2)
        mask = mask >= 127  # uint8 -> bool

        img = img.astype(np.float64)
        img[mask] -= self.mean[mask]
        img[~mask] = 0

        img = img.transpose(2, 0, 1)
        img = img.astype(np.float32)
        x_data = np.asarray([img])
        if self.gpu >= 0:
            x_data = cuda.to_gpu(x_data)
        x = chainer.Variable(x_data)
        y = self.model(x)

        feat = cuda.to_cpu(y.data)
        feat = feat.squeeze(axis=(2, 3))
        X_query = feat

        y_pred_proba = self.knn.predict_proba(X_query)
        y_pred = np.argmax(y_pred_proba, axis=1)

        classes = self.knn.classes_
        target_names = self.target_names[classes]

        msg = ClassificationResult()
        msg.header = imgmsg.header
        msg.labels = y_pred.tolist()
        msg.label_names = target_names[y_pred].tolist()
        msg.label_proba = y_pred_proba[:, y_pred].flatten().tolist()
        msg.probabilities = y_pred_proba.flatten().tolist()
        msg.target_names = target_names.tolist()
        self.pub.publish(msg)