def on_the_truck_cost(cf): state, am = waverly_state_truck() print state.orientation #state.orientation = 3.14/6 cf.initialize_state(state) extractor = Extractor() esdcs = extractor.extractEsdcs("next to the truck.") place_esdc = esdcs[0] place_esdc.type = "PLACE" truck_esdc = place_esdc.l[0] print "place", place_esdc, place_esdc.type print "truck", truck_esdc searcher = BeamSearch(cf) annotation = initial_annotation(state, esdcs[0]) truck = [ state.getGroundableById(o) for o in state.getObjectsSet() if 'flatbed' in state.getGroundableById(o).tags ][0] annotation.setGrounding(truck_esdc, truck) annotation.setGrounding("sequence", [(state, None)]) obj = prism_from_point(0, 0, truck.centroid3d[2] + .2, truck.centroid3d[2] + .5, width=0.5) #axes = p.axes() featureBrowser = crfFeatureWeights.MainWindow() featureBrowser.show() axes = featureBrowser.axes for o in state.getObjectsSet(): drawObject(axes, state.getGroundableById(o)) drawPlaceCostMap(featureBrowser, obj, place_esdc, annotation, searcher, -5, 10, 25, 40, .2) #p.axes().set_aspect('equal') #p.show() state, gggs = annotation_to_ggg_map(annotation) ggg = gggs[place_esdc] #ggg = ggg_from_esdc(esdcs[0]) factor = ggg.esdc_to_factor(place_esdc) print "factor", factor, factor.id print "cache", cf.factor_to_cost factor_to_cost = cf.factor_to_cost[factor] print "cache", factor_to_cost featureBrowser.load(cf.lccrf, factor_to_cost)
def drawObjectPathCostMap(featureBrowser, obj_esdc, event_esdcs, annotation, cf, xmin, xmax, ymin, ymax, step): xstart, ystart = annotation.getGroundings(obj_esdc)[0].centroid2d ax, ay = annotation.agent.centroid2d ath = annotation.agent.path.theta[0] print ax, ay, ath #agent move to pick up object aX, aY = sf.math2d_step_along_line(tp([(ax, ay), (xstart, ystart)]), .1) aTh = ath * na.ones(len(aX)) costs = na.zeros((int((ymax - ymin) / step), int((xmax - xmin) / step))) annotations = [] for i, x in enumerate(na.arange(xmin, xmax, step)): for j, y in enumerate(na.arange(ymin, ymax, step)): X, Y = sf.math2d_step_along_line(tp([(xstart, ystart), (x, y)]), .1) Z = na.ones(len(X)) th = na.zeros(len(X)) timestamps = range(len(X)) path = Path(timestamps, [X, Y, Z, th]) new_annotation = annotation_copy(annotation) atimestamps = range(len(X) + len(aX)) axs = na.append(aX, X) ays = na.append(aY, Y) azs = na.zeros(len(X) + len(aX)) ath = na.append(aTh, th) new_annotation.agent.path = Path(atimestamps, [axs, ays, azs, ath]) obj = new_annotation.getGroundings(obj_esdc)[0] obj.path = path assignPathGroundings(event_esdcs[0], new_annotation) state, gggs = annotation_to_ggg_map(new_annotation) ggg = ggg_from_esdc(new_annotation.esdcs[0]) factor = ggg.esdc_to_factor(event_esdcs[0]) cost, entries = cf.compute_costs([factor], ggg, state_sequence=None) costs[j][i] = math.exp(-1.0 * cost) annotations.append(((x, y), entries)) print i featureBrowser.setCostImage(costs, annotations, xmin, xmax, ymin, ymax)
def drawPlaceCostMap(featureBrowser, physicalObject, esdc, annotation, beam_search, xmin, xmax, ymin, ymax, step): obj = physicalObject Xs = obj.pX - obj.pX[0] Ys = obj.pY - obj.pY[0] zStart = obj.zStart zEnd = obj.zEnd cx, cy = obj.centroid2d() dx, dy = cx - obj.pX[0], cy - obj.pY[0] costs = na.zeros((int((ymax - ymin) / step), int((xmax - xmin) / step))) annotations = [] print dx, dy path = Path([0], [[(xmax - xmin) * 0.5], [(ymax - ymin) * 0.5], [0], [0]]) for i, x in enumerate(na.arange(xmin, xmax, step)): for j, y in enumerate(na.arange(ymin, ymax, step)): prism = Prism([Xs + x - dy, Ys + y - dx], zStart, zEnd) place = Place(prism) new_annotation = annotation_copy(annotation) new_annotation.setGrounding(esdc, place) new_annotation.setGroundingIsCorrect(esdc, True) #new_annotation.agent.setPath(path) state, gggs = annotation_to_ggg_map(new_annotation) #ggg = ggg_from_esdc(new_annotation.esdcs[0]) ggg = gggs[esdc] factor = ggg.esdc_to_factor(esdc) new_evidences = ggg.evidences for phi in factor.nodes_with_type("phi"): new_evidences = new_evidences.set_evidence(phi.id, True) ggg = GGG.from_ggg_and_evidence(ggg, new_evidences) cost, entries = beam_search.cf_obj.costEntry([factor], state, ggg) costs[j][i] = math.exp(-1.0 * cost) annotations.append(((x, y), entries)) #axes.imshow(costs, origin="lower", # extent=(xmin, xmax, ymin, ymax)) featureBrowser.setCostImage(costs, annotations, xmin, xmax, ymin, ymax)
def describe(self, thing_to_describe): results = [] for esdc_structure in self.esdc_structures: for i in range(0, 500): annotation, esdc_candidate = annotation_candidate( esdc_structure, self.esdc_field_to_texts, self.groundings, thing_to_describe) state, esdc_to_ggg = annotation_to_ggg_map(annotation) ggg = esdc_to_ggg[esdc_candidate] new_evidences = ggg.evidences for phi in ggg.nodes_with_name("phi"): new_evidences = new_evidences.set_evidence(phi.id, True) ggg = GGG.from_ggg_and_evidence(ggg, new_evidences) cost, entries = self.cf.costEntry(ggg.factors, state, ggg) #cost, entries = self.cf.costEntry([factor], state, ggg) results.append((cost, annotation)) results.sort() return results
def main(argv): app = basewindow.makeApp() from optparse import OptionParser parser = OptionParser() parser.add_option("--training_filename", dest="training_fname", help="Training Filename", metavar="FILE") (options, args) = parser.parse_args() annotations = annotationIo.load(options.training_fname) annotation = annotations[0] state, esdc_to_ggg = annotation_to_ggg_map(annotation) ggg = esdc_to_ggg[annotation.esdcs[0]] wnd = MainWindow() wnd.show() wnd.load(ggg) app.exec_()
def selectAnnotation(self): annotation = self.annotationModel.selectedAnnotation() self.esdcModel.setData(annotation.esdcs) self.esdcTreeView.expandAll() a_state, gggs = annotation_to_ggg_map(annotation) self.esdcToFvalues = {} self.esdcToFnames = {} for g_index, (esdc, ggg) in enumerate(gggs.iteritems()): print "extracting features", esdc factor_id_to_fvalues, factor_id_to_fnames = self.feature_extractor.extract_features( a_state, ggg, factors=None) for factor_id in factor_id_to_fvalues.keys(): esdc = ggg.factor_id_to_esdc(factor_id) print "loading", esdc #assert not esdc in self.esdcToFvalues self.esdcToFvalues[esdc] = factor_id_to_fvalues[factor_id] self.esdcToFnames[esdc] = factor_id_to_fnames[factor_id] self.draw()
def annotation_to_observations(annotation, fe, force_default_class_value, default_class_value, counter, id_base): observations = [] try: a_state, esdc_to_ggg = annotation_to_ggg_map(annotation) for g_index, (esdc, ggg) in enumerate(esdc_to_ggg.iteritems()): new_evidences = assignPathGroundingsToGGG(a_state, ggg) ggg = GGG.from_ggg_and_evidence(ggg, new_evidences) true_class_value = annotation.isGroundingCorrect(esdc) if true_class_value == None and default_class_value == None: continue elif true_class_value == None: true_class_value = default_class_value if force_default_class_value: labeled_class_value = default_class_value else: labeled_class_value = true_class_value factor = ggg.esdc_to_factor(esdc) if factor != None: phi_node = factor.nodes_with_type("phi")[0] ggg.set_evidence_for_node(phi_node, labeled_class_value) obs = ggg_to_observation(ggg, a_state, fe, esdc, id_base, counter, annotation, true_class_value) if obs != None: observations.append(obs) except: print "problem with", annotation.entireText #print annotation raise return observations
def drawAgentPathCostMap(featureBrowser, event_path_esdcs, annotation, cf, xmin, xmax, ymin, ymax, step): print "y", ymax, ymin xstart, ystart = (xmin + 0.5, (ymax + ymin) / 2.0) #xstart, ystart = 19.658, 14.900 ath = 0 print "start", xstart, ystart, ath costs = na.zeros((int((ymax - ymin) / step), int((xmax - xmin) / step))) state, gggs = annotation_to_ggg_map(annotation) esdc = event_path_esdcs[0] ggg = gggs[esdc] factor = ggg.esdc_to_factor(esdc) node = ggg.node_for_esdc(esdc) print "esdc", esdc print "node", node, node.__class__ for i, x in enumerate(na.arange(xmin, xmax, step)): for j, y in enumerate(na.arange(ymin, ymax, step)): X, Y = sf.math2d_step_along_line(tp([(xstart, ystart), (x, y)]), .1) Z = na.ones(len(X)) fig_xy = na.array([X, Y]) Xst, Yst = fig_xy[:, :-1] Xend, Yend = fig_xy[:, 1:] Theta = na.arctan2(Yend - Yst, Xend - Xst) Theta = list(Theta) Theta.append(Theta[-1]) th = list(Theta) #th = ath*na.ones(len(X)) timestamps = range(len(X)) path = Path(timestamps, [X, Y, Z, th]) pobj = PhysicalObject(Prism.from_point(X[0], Y[0], Z[0], Z[0] + 1), tags=("forklift", ), path=path) ggg.set_evidence_for_node(node, [pobj]) #new_annotation = annotation_copy(annotation) #new_annotation.agent = new_annotation.agent.withPath(path) #if esdc.type == "EVENT": # assignPathGroundings(esdc, new_annotation) #else: #type is path # new_annotation.setGrounding(esdc, new_annotation.agent.path) new_evidences = Evidences.copy(ggg.evidences) for phi in factor.nodes_with_type("phi"): new_evidences[phi.id] = True ggg = GGG.from_ggg_and_evidence(ggg, new_evidences) # print ggg.entry_for_factor(factor) cost, entries = cf.compute_costs([factor], ggg, state_sequence=None) costs[j][i] = math.exp(-1.0 * cost) #annotations.append(((x,y), entries)) print i # break print 'min/max', min(costs.flatten()), max(costs.flatten()) featureBrowser.setCostImage(costs, xmin, xmax, ymin, ymax) return ggg