def build(self): # instanciate a graph with all but the last node self.g = Graph() self.vertices = self.g.add_vertex(self.num_real_pixels - 1) # insert sink with edges to it self.t = self.g.add_vertex() for x in range(self.shape_frame[0]): for y in range(self.shape_frame[1]): # add to sink #e = self.g.add_edge(self.g.vertex(pixel2id(self.shape_frame, self.num_frames -1, x, y)), self.t) # add edges from sink e = self.g.add_edge( self.t, self.g.vertex( pixel2id(self.shape_frame, self.num_frames - 1, x, y))) # add edges from all vertices to the ones in the next frame for v in self.g.vertices(): # is vertext not on last frame if int(v) + self.shape_frame[0] * self.shape_frame[ 1] < self.num_real_pixels - 1: frame_num, x, y = id2pixel(int(v), self.shape_frame) self.add_edges2pixel(frame_num, x, y) # print status, once a frame is connected if int(v) % (self.shape_frame[0] * self.shape_frame[1]) == 0 and int(v) > 0: print('Frame ' + str(int(v) // (self.pixel_per_frame)) + ' connected.')
def update_costs(self, mask_f, mask_l): paths = self.path_generator(mask_f) # paths = self.all_paths_generator() for path, v_x, v_y in paths: # obtain x,y value for first pixel on path _, p_x, p_y = id2pixel(int(path[0].source()), self.shape_frame) # if path started in object if np.all(mask_f[p_x, p_y] == 1): # depending on whether path lands in BB or not, # update edge costs on path and loss length = len(path) if np.all(mask_l[v_x, v_y] == 1): f = 0 # distance from last frame for e in path: """ # update with scaled factor f += 1 # increase factor with which we update the costs of an edge self.g.ep.cost[e] -= 2*f/length """ # udated with constant factor self.g.ep.cost[e] -= 1 else: f = 0 for e in path: """ # update with scaled factor f +=1 self.g.ep.cost[e] += 2*f/length """ # update with constant factor self.g.ep.cost[e] += 1 # if path started outside object elif np.all(mask_f[p_x, p_y] == 0): # depending on whether path lands in BB or not, # update edge costs on path and loss length = len(path) if np.all(mask_l[v_x, v_y] == 0): f = 0 # distance from last frame for e in path: """ # update with scaled factor f += 1 # increase factor with which we update the costs of an edge self.g.ep.cost[e] += 2*f/length """ # udated with constant factor self.g.ep.cost[e] += 1 else: f = 0 for e in path: """ # update with scaled factor f +=1 self.g.ep.cost[e] -= 2*f/length """ # update with constant factor self.g.ep.cost[e] -= 1 else: print('Weird Ground Truth, should be 0 or 1.')
def all_paths_generator(self): # iterate over entire frame for x in range(len(mask_f)): for y in range(len(mask_f[0, :])): # vertex that iterates along the path v = self.g.vertex(pixel2id(self.shape_frame, 0, x, y)) # while we have not reached the first node (t), go to # predecessor and remember the edges we passed path = [] while self.pred_map[v] != self.num_real_pixels - 1: path.append(self.g.edge(self.pred_map[v], v)) v = self.pred_map[v] # obatin coodinates to pixel on last frame _, v_x, v_y = id2pixel(int(v), self.shape_frame) yield path, v_x, v_y
def update_costs(g, pred_map, frame_0, frame_l): loss = 0 hit = 0 not_hit = 0 # iterate over entire frame for x in range(len(frame_0)): for y in range(len(frame_0[0, :])): # only paths from pixels in BB matter if np.all(frame_0[x, y] == 1): # vertex that iterates along the path v = g.vertex(pixel2id(frame_0.shape, 0, x, y)) # while we have not reached the first node (t), go to # predecessor and remember the edges we passed path = [] while pred_map[v] != g.num_vertices() - 1: path.append(g.edge(pred_map[v], v)) v = pred_map[v] # obatin coodinates to pixel on last frame _, v_x, v_y = id2pixel(int(v), frame_l.shape) # depedning on whether path lands in BB or not, # update edge costs on path and loss length = len(path) if np.all(frame_l[v_x, v_y] == 1): f = 0 hit += 1 loss -= 1 for e in path: f += 1 factor = f / length g.ep.cost[e] -= 2 * f / length else: f = 0 not_hit += 1 loss += 1 for e in path: f += 1 g.ep.cost[e] += 2 * f / length return loss, hit, not_hit
g = Graph() vertices = g.add_vertex(n_pixels) """ for frame_num in range(frames.n_frames-1): print('Edges to frame ' +str(frame_num)+ ' are being added') for x in range(frames.dims[0]): for y in range(frames.dims[1]): add_edges2pixel(g, frames.dims, frame_num, x , y , window) """ # for all vertices, add edges to next frame for v in g.vertices(): if int(v) + frames.dims[0] * frames.dims[1] < n_pixels: frame_num, x, y = id2pixel(int(v), frames.dims) add_edges2pixel(g, frames.dims, frame_num, x, y, window) if int(v) % (frames.dims[0] * frames.dims[1]) == 0: print('Frame ' + str(int(v) // (frames.dims[0] * frames.dims[1])) + ' connected.') #graph_draw(g) # print out neighbours for a test pixel #for v in g.vertex(pixel2id(frames.dims,1, 1,2)).out_neighbors(): #< print(id2pixel(int(v), frames.dims)) # create edge property for edge costs eprop = g.new_edge_property("double") g.ep.cost = eprop # equivalent to g.vertex_properties["foo"] = vprop