def test_work_order(self, json_file=None): from work_order import ( get_work_order, get_sorted_work_order, get_work_order_msg, ) from common import get_object_list if json_file is None: json_file = os.path.join(pkg_path, 'data/apc-a.json') # for original work order work_order = list(get_work_order(json_file)) self.assertEqual(len(work_order), len('abcdefghijkl')) object_list = get_object_list() for bin_, object_ in work_order: self.assertIn(bin_, 'abcdefghijkl') self.assertIn(object_, object_list) # for sorted work order sorted_work_order = list(get_sorted_work_order(json_file)) self.assertEqual(len(sorted_work_order), len('abcdefghijkl')) for bin_, object_ in sorted_work_order: self.assertIn(bin_, 'abcdefghijkl') self.assertIn(object_, object_list) # for msg msg = get_work_order_msg(json_file) self.assertEqual(len(msg['left'].array), 8) self.assertEqual(len(msg['right'].array), 4) for lr in ('left', 'right'): for order in msg[lr].array: self.assertIn(order.bin, 'abcdefghijkl') self.assertIn(order.object, object_list)
def main(): from common import get_object_list, get_train_imgpaths rospy.init_node('extract_color_histogram') all_objects = get_object_list() color_space_param = rospy.get_param('~color_space', 'lab') if color_space_param == 'rgb': colors = ['red', 'green', 'blue'] elif color_space_param == 'lab': colors = ['l'] else: raise ValueError('Unknown color space') rospy.loginfo('color space: {c}'.format(c=color_space_param)) object_param = rospy.get_param('~object', all_objects) object_nms = object_param.split(',') if len(object_nms) == 1 and object_nms[0] == 'all': object_nms = all_objects rospy.loginfo('objects: {obj}'.format(obj=object_nms)) for object_nm in object_nms: if object_nm not in all_objects: rospy.logwarn('Unknown object, skipping: {}'.format(object_nm)) else: imgpaths = get_train_imgpaths(object_nm) raw_paths, mask_paths = zip(*imgpaths) for color in colors: e = ExtractColorHistogram(object_nm=object_nm, color=color, raw_paths=raw_paths, mask_paths=mask_paths) e.extract_and_save()
def main(): rospy.init_node('extract_sift') obj_names = get_object_list() for obj_name in obj_names: if load_siftdata(obj_name, dry_run=True): continue # already extracted extract_sift(obj_name)
def main(): parser = argparse.ArgumentParser() parser.add_argument('bof_histogram') args = parser.parse_args(sys.argv[1:]) print('loading bof histogram') with gzip.open(args.bof_histogram, 'rb') as f: obj_hists = pickle.load(f) target_names = get_object_list() # create train and test data X ,y = [], [] for i, obj_name in enumerate(target_names): X.append(obj_hists[obj_name]) y += [i] * len(obj_hists[obj_name]) X = np.vstack(X) normalize(X, copy=False) y = np.array(y) X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=np.random.randint(1234)) # train and test lgr = LogisticRegression() print('fitting LogisticRegression') lgr.fit(X_train, y_train) with gzip.open('lgr.pkl.gz', 'wb') as f: pickle.dump(lgr, f) y_pred = lgr.predict(X_test) print('score lgr: {}'.format(accuracy_score(y_test, y_pred))) print(classification_report(y_test, y_pred, target_names=target_names))
def main(): parser = argparse.ArgumentParser() parser.add_argument('bof_histogram') args = parser.parse_args(sys.argv[1:]) print('loading bof histogram') with gzip.open(args.bof_histogram, 'rb') as f: obj_hists = pickle.load(f) target_names = get_object_list() # create train and test data X, y = [], [] for i, obj_name in enumerate(target_names): X.append(obj_hists[obj_name]) y += [i] * len(obj_hists[obj_name]) X = np.vstack(X) normalize(X, copy=False) y = np.array(y) X_train, X_test, y_train, y_test = train_test_split( X, y, random_state=np.random.randint(1234)) # train and test lgr = LogisticRegression() print('fitting LogisticRegression') lgr.fit(X_train, y_train) with gzip.open('lgr.pkl.gz', 'wb') as f: pickle.dump(lgr, f) y_pred = lgr.predict(X_test) print('score lgr: {}'.format(accuracy_score(y_test, y_pred))) print(classification_report(y_test, y_pred, target_names=target_names))
def test_classifier_weight_yaml(self): from common import get_object_list yaml_file = os.path.join(pkg_path, 'data/classifier_weight.yml') with open(yaml_file) as f: weight = yaml.load(f) object_list = get_object_list() for object_, weights in weight.items(): self.assertIn(object_, object_list) for clf, weight in weights.items(): self.assertIn(clf, ['bof', 'color']) self.assert_(0. <= weight <= 1.)
def match(self, obj_names): stamp = rospy.Time.now() while self.query_features.header.stamp < stamp: rospy.sleep(0.3) descs = np.array(self.query_features.descriptors) X = self.bof.transform([descs]) normalize(X, copy=False) object_list = get_object_list() obj_indices = [object_list.index(o) for o in obj_names] obj_probs = self.clf.predict_proba(X)[0][obj_indices] return obj_probs / obj_probs.sum()
def test_bin_contents(self, json_file=None): from bin_contents import get_bin_contents from common import get_object_list if json_file is None: json_file = os.path.join(pkg_path, 'data/apc-a.json') bin_contents = list(get_bin_contents(json_file)) object_list = list(get_object_list()) for bin_, objects in bin_contents: self.assertIn(bin_, 'abcdefghijkl') for object_ in objects: self.assertIn(object_, object_list) self.assertEqual(len(bin_contents), len('abcdefghijkl'))
def match(self, obj_names): stamp = rospy.Time.now() while (self.query_image.header.stamp < stamp) or (self.query_image.height == 0): rospy.sleep(0.3) # convert image bridge = cv_bridge.CvBridge() input_image = bridge.imgmsg_to_cv2(self.query_image, 'rgb8') object_list = get_object_list() obj_indices = [object_list.index(o) for o in obj_names] obj_probs = self.estimator.predict(input_image)[0][obj_indices] return obj_probs / obj_probs.sum()
def spin_once(self): if self.bof_data is None or self.cfeature is None: return stamp, bof_objects_proba = self.bof_data stamp, cfeature_objects_proba = self.cfeature weight = self.weight target_bin = rospy.get_param('target', None) object_list = get_object_list() all_proba = [ (o, (weight[o]['bof'] * bof_objects_proba[o]) + (weight[o]['color'] * cfeature_objects_proba[o]) ) for o in object_list ] # verification result for debug candidates = self.bin_contents.get(target_bin, None) if candidates is None: candidates = object_list matched = sorted(all_proba, key=lambda x: x[1])[-1][0] # compose msg msg = ObjectRecognition() msg.header.stamp = stamp msg.matched = matched msg.probability = dict(all_proba)[matched] / sum(dict(all_proba).values()) msg.candidates = candidates msg.probabilities = np.array([dict(all_proba)[c] for c in candidates]) msg.probabilities /= msg.probabilities.sum() self.pub_debug.publish(msg) # verification result with json target if target_bin is None or target_bin == '': return proba = [ (c, (weight[c]['bof'] * bof_objects_proba[c]) + (weight[c]['color'] * cfeature_objects_proba[c]) ) for c in candidates ] matched = sorted(proba, key=lambda x: x[1])[-1][0] # compose msg msg = ObjectRecognition() msg.header.stamp = stamp msg.matched = matched msg.probability = dict(proba)[matched] / sum(dict(proba).values()) msg.candidates = candidates msg.probabilities = np.array([dict(proba)[c] for c in candidates]) msg.probabilities /= msg.probabilities.sum() self.pub.publish(msg)
def predict_now(self): query_image = self.query_image object_list = get_object_list() probs = self.match(object_list) matched_idx = np.argmax(probs) # prepare message res = ObjectRecognition() res.header.stamp = query_image.header.stamp res.matched = object_list[matched_idx] res.probability = probs[matched_idx] res.candidates = object_list res.probabilities = probs return res
def get_sift_descriptors(n_imgs=None, data_dir=None): objects = get_object_list() obj_descs = [] for obj in objects: descs = load_siftdata(obj_name=obj, return_pos=False, data_dir=data_dir) if descs is None: continue if n_imgs is None: n_imgs = len(descs) p = np.random.randint(0, len(descs), size=n_imgs) descs = np.array(map(lambda x: x.astype('float16'), descs)) obj_descs.append((obj, descs[p])) return obj_descs
def save_result(self, target_obj, probabilities): """Save test result to csv""" object_list = get_object_list() filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../data/test_{m}_matching_result.csv'.format(m=self.matcher)) if not os.path.exists(filename): # initialize csv file with open(filename, 'w') as f: csv_writer = csv.writer(f) csv_writer.writerow(['target_obj', 'is_correct'] + object_list) # save result best_match_obj = object_list[np.argmax(probabilities)] row = [target_obj, best_match_obj==target_obj] + probabilities with open(filename, 'a') as f: csv_writer = csv.writer(f) csv_writer.writerow(row)
def save_result(self, target_obj, probabilities): """Save test result to csv""" object_list = get_object_list() filename = os.path.join( os.path.dirname(os.path.abspath(__file__)), '../data/test_{m}_matching_result.csv'.format(m=self.matcher)) if not os.path.exists(filename): # initialize csv file with open(filename, 'w') as f: csv_writer = csv.writer(f) csv_writer.writerow(['target_obj', 'is_correct'] + object_list) # save result best_match_obj = object_list[np.argmax(probabilities)] row = [target_obj, best_match_obj == target_obj] + probabilities with open(filename, 'a') as f: csv_writer = csv.writer(f) csv_writer.writerow(row)
def match(self, obj_names): stamp = rospy.Time.now() while (self.query_image.header.stamp < stamp) or (self.query_image.height == 0): rospy.sleep(0.3) query_image = self.query_image # convert image bridge = cv_bridge.CvBridge() input_image = bridge.imgmsg_to_cv2(query_image, 'rgb8') hist = self.estimator.color_hist(input_image) self._pub_debug.publish( ColorHistogram(header=query_image.header, histogram=hist)) object_list = get_object_list() obj_indices = [object_list.index(o) for o in obj_names] obj_probs = self.estimator.predict(input_image)[0][obj_indices] return obj_probs / obj_probs.sum()
def predict_now(self): query_features = self.query_features if not len(query_features.descriptors) > 0: return descs = np.array(query_features.descriptors) X = self.bof.transform([descs]) normalize(X, copy=False) self._pub_debug.publish(Histogram(header=query_features.header, histogram=X[0])) object_list = get_object_list() proba = self.clf.predict_proba(X)[0] matched_idx = np.argmax(proba) # prepare message res = ObjectRecognition() res.header.stamp = query_features.header.stamp res.matched = object_list[matched_idx] res.probability = proba[matched_idx] res.candidates = object_list res.probabilities = proba return res
def predict_now(self): query_features = self.query_features if not len(query_features.descriptors) > 0: return descs = np.array(query_features.descriptors) X = self.bof.transform([descs]) normalize(X, copy=False) self._pub_debug.publish( Histogram(header=query_features.header, histogram=X[0])) object_list = get_object_list() proba = self.clf.predict_proba(X)[0] matched_idx = np.argmax(proba) # prepare message res = ObjectRecognition() res.header.stamp = query_features.header.stamp res.matched = object_list[matched_idx] res.probability = proba[matched_idx] res.candidates = object_list res.probabilities = proba return res
def run(self): object_list = get_object_list() already_tested = self.get_already_tested() for target_obj in object_list: if target_obj in already_tested: continue # request to publish image imgpath = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../data/raw_img/{t}.jpg'.format(t=target_obj)) if not os.path.exists(imgpath): continue self.wait_for_service(self.client_of_img) self.client_of_img(string=imgpath) # request to object matcher rospy.sleep(3) rospy.loginfo('target object: {t}'.format(t=target_obj)) self.wait_for_service(self.client_of_matcher) res = self.client_of_matcher(objects=object_list) proba = (np.array(res.probabilities)*100).astype(int) rospy.loginfo('results: {res}'.format(res=proba)) best_match_obj = object_list[np.argmax(res.probabilities)] rospy.loginfo('best match: {best}'.format(best=best_match_obj)) self.save_result(target_obj=target_obj, probabilities=list(proba))
def run(self): object_list = get_object_list() already_tested = self.get_already_tested() for target_obj in object_list: if target_obj in already_tested: continue # request to publish image imgpath = os.path.join( os.path.dirname(os.path.abspath(__file__)), '../data/raw_img/{t}.jpg'.format(t=target_obj)) if not os.path.exists(imgpath): continue self.wait_for_service(self.client_of_img) self.client_of_img(string=imgpath) # request to object matcher rospy.sleep(3) rospy.loginfo('target object: {t}'.format(t=target_obj)) self.wait_for_service(self.client_of_matcher) res = self.client_of_matcher(objects=object_list) proba = (np.array(res.probabilities) * 100).astype(int) rospy.loginfo('results: {res}'.format(res=proba)) best_match_obj = object_list[np.argmax(res.probabilities)] rospy.loginfo('best match: {best}'.format(best=best_match_obj)) self.save_result(target_obj=target_obj, probabilities=list(proba))
# from __future__ import print_function import sys import argparse from termcolor import cprint, colored from common import get_object_list from bin_contents import get_bin_contents from work_order import get_work_order parser = argparse.ArgumentParser('score_calculator') parser.add_argument('json', help='contest interface json') args = parser.parse_args(sys.argv[1:]) OBJECTS = get_object_list() BONUS = { 'munchkin_white_hot_duck_bath_toy': 1, 'stanley_66_052': 3, 'safety_works_safety_glasses': 1, 'rolodex_jumbo_pencil_cup': 2, 'first_years_take_and_toss_straw_cup': 2, 'mark_twain_huckleberry_finn': 3, 'kyjen_squeakin_eggs_plush_puppies': 1, 'kong_sitting_frog_dog_toy': 1, 'kong_air_dog_squeakair_tennis_ball': 1, 'dr_browns_bottle_brush': 2, 'kong_duck_dog_toy': 1, 'laugh_out_loud_joke_book': 3, } SCORING = {
def get_show_master_logs(host_id): return common.get_object_list(db_util.DBUtil().fetchall( cache.Cache().get_host_info(host_id), "show master logs;"))
def __init__(self): SiftMatcher.__init__(self) ObjectMatcher.__init__(self, '/semi/sift_matcher') self.object_list = get_object_list() self.siftdata_cache = {}
from __future__ import print_function import sys import argparse from termcolor import cprint, colored from common import get_object_list from bin_contents import get_bin_contents from work_order import get_work_order parser = argparse.ArgumentParser("score_calculator") parser.add_argument("json", help="contest interface json") args = parser.parse_args(sys.argv[1:]) OBJECTS = get_object_list() BONUS = { "munchkin_white_hot_duck_bath_toy": 1, "stanley_66_052": 3, "safety_works_safety_glasses": 1, "rolodex_jumbo_pencil_cup": 2, "first_years_take_and_toss_straw_cup": 2, "mark_twain_huckleberry_finn": 3, "kyjen_squeakin_eggs_plush_puppies": 1, "kong_sitting_frog_dog_toy": 1, "kong_air_dog_squeakair_tennis_ball": 1, "dr_browns_bottle_brush": 2, "kong_duck_dog_toy": 1, "laugh_out_loud_joke_book": 3, } SCORING = {
def __init__(self): self.file_name = 'rgb' self.object_names = get_object_list() self.cfeatures = [] self.labels = []