start_loc=start_loc) extension_vertex = paths[path_idx].pop() path_input, path_detect_target = model_utils.make_path_input( paths[path_idx], extension_vertex, SEGMENT_LENGTH, detect_mode=DETECT_MODE, window_size=WINDOW_SIZE) batch_extension_vertices.append(extension_vertex) batch_inputs.append(path_input) plt.imshow(path_input[:, :, :3]) plt.show() batch_detect_targets.append(path_detect_target) targets = model_utils.compute_targets_by_best( paths[path_idx], extension_vertex, SEGMENT_LENGTH) angle_targets, action_targets = action_to_vector(targets) batch_angle_targets[i, :] = angle_targets batch_action_targets[i, :] = action_targets times['prepare'] += time.time() - stage_time stage_time = time.time() # train model feed_dict = { m.is_training: True, m.inputs: batch_inputs, m.angle_targets: batch_angle_targets, m.action_targets: batch_action_targets, m.detect_targets: batch_detect_targets, m.learning_rate: epoch_to_learning_rate(outer_it),
def eval(paths, m, session, max_path_length=MAX_PATH_LENGTH, segment_length=SEGMENT_LENGTH, save=False, compute_targets=True, max_batch_size=model.BATCH_SIZE, window_size=WINDOW_SIZE, verbose=True, threshold_override=None, cache_m6=None, cache_m6_old=None, cache_m6_new=None): global vis_counter angle_losses = [] detect_losses = [] losses = [] path_lengths = {path_idx: 0 for path_idx in range(len(paths))} last_time = None big_time = None last_extended = False for len_it in range(99999999): if len_it % 500 == 0 and verbose: print('it {}'.format(len_it)) big_time = time.time() path_indices = [] extension_vertices = [] for path_idx in range(len(paths)): if path_lengths[path_idx] >= max_path_length: continue extension_vertex = paths[path_idx].pop() if extension_vertex is None: continue path_indices.append(path_idx) path_lengths[path_idx] += 1 extension_vertices.append(extension_vertex) if len(path_indices) >= max_batch_size: break if len(path_indices) == 0: break batch_inputs = [] batch_detect_targets = [] batch_angle_targets = numpy.zeros((len(path_indices), 64), 'float32') inputs_per_path = 1 for i in range(len(path_indices)): path_idx = path_indices[i] path_input, path_detect_target = model_utils.make_path_input( paths[path_idx], extension_vertices[i], segment_length, window_size=window_size) if type(path_input) == list: batch_inputs.extend([x[:, :, 0:3] for x in path_input]) inputs_per_path = len(path_input) #batch_inputs.append(numpy.concatenate([x[:, :, 0:3] for x in path_input], axis=2)) else: batch_inputs.append(path_input[:, :, 0:3]) #batch_detect_targets.append(path_detect_target) batch_detect_targets.append( numpy.zeros((64, 64, 1), dtype='float32')) if compute_targets: angle_targets, _ = model_utils.compute_targets_by_best( paths[path_idx], extension_vertices[i], segment_length) batch_angle_targets[i, :] = angle_targets # run model if M6: angle_loss, detect_loss, loss = 0.0, 0.0, 0.0 if cache_m6 is not None: p = extension_vertices[0].point.sub( paths[0].tile_data['rect'].start).scale(0.25) batch_angle_outputs = numpy.array([cache_m6[p.x, p.y, :]], dtype='float32') else: pre_outputs = session.run(m.outputs, feed_dict={ m.is_training: False, m.inputs: batch_inputs, }) batch_angle_outputs = pre_outputs[:, window_size // 8, window_size // 8, :] else: feed_dict = { m.is_training: False, m.inputs: batch_inputs, m.angle_targets: [ x for x in batch_angle_targets for _ in range(inputs_per_path) ], m.detect_targets: [ x for x in batch_detect_targets for _ in range(inputs_per_path) ], } if ANGLE_ONEHOT: feed_dict[m.angle_onehot] = model_utils.get_angle_onehot( ANGLE_ONEHOT) batch_angle_outputs, angle_loss, detect_loss, loss = session.run( [m.angle_outputs, m.angle_loss, m.detect_loss, m.loss], feed_dict=feed_dict) if inputs_per_path > 1: actual_outputs = numpy.zeros((len(path_indices), 64), 'float32') for i in range(len(path_indices)): actual_outputs[ i, :] = batch_angle_outputs[i * inputs_per_path:(i + 1) * inputs_per_path, :].max(axis=0) batch_angle_outputs = actual_outputs angle_losses.append(angle_loss) losses.append(loss) if (save is True and len_it % 1 == 0 and vector_to_action( extension_vertices[0], cache_m6_new[p.x, p.y, :], 0.3).max() > 0) or (save == 'extends' and last_extended): fname = '/home/ubuntu/data/{}_'.format(vis_counter) vis_counter += 1 save_angle_targets = batch_angle_targets[0, :] if not compute_targets: save_angle_targets = None #if numpy.max(batch_angle_outputs[0, :]) > 0.1: # batch_angle_outputs[0, :] *= 1.0 / numpy.max(batch_angle_outputs[0, :]) #model_utils.make_path_input(paths[path_indices[0]], extension_vertices[0], segment_length, fname=fname, angle_targets=save_angle_targets, angle_outputs=batch_angle_outputs[0, :], window_size=window_size) old_outputs = numpy.array([cache_m6_old[p.x, p.y, :]], dtype='float32') new_outputs = numpy.array([cache_m6_new[p.x, p.y, :]], dtype='float32') model_utils.make_path_input(paths[path_indices[0]], extension_vertices[0], segment_length, fname=fname + 'old_', angle_targets=save_angle_targets, angle_outputs=old_outputs[0, :], window_size=window_size) model_utils.make_path_input(paths[path_indices[0]], extension_vertices[0], segment_length, fname=fname + 'new_', angle_targets=save_angle_targets, angle_outputs=new_outputs[0, :], window_size=window_size) with open(fname + 'meta.txt', 'w') as f: f.write('max angle output: {}\n'.format( batch_angle_outputs[0, :].max())) for i in range(len(path_indices)): path_idx = path_indices[i] if len(extension_vertices[i].out_edges) >= 2: threshold = THRESHOLD_BRANCH else: threshold = THRESHOLD_FOLLOW if threshold_override is not None: threshold = threshold_override x = vector_to_action(extension_vertices[i], batch_angle_outputs[i, :], threshold) last_extended = x.max() > 0 paths[path_idx].push(extension_vertices[i], x, segment_length, training=False, branch_threshold=0.01, follow_threshold=0.01, point_reconnect=False) if save: paths[0].graph.save('out.graph') return numpy.mean(angle_losses), numpy.mean(detect_losses), numpy.mean( losses), len_it
def eval(paths, m, session, starting_points_list, max_path_length=MAX_PATH_LENGTH, segment_length=SEGMENT_LENGTH, save=False, follow_targets=False, compute_targets=True, max_batch_size=model.BATCH_SIZE, window_size=WINDOW_SIZE, verbose=True, threshold_override=False): angle_losses = [] detect_losses = [] stop_losses = [] losses = [] accuracies = [] path_lengths = {path_idx: 0 for path_idx in range(len(paths))} last_time = None big_time = None for len_it in range(99999999): if len_it % 1000 == 0 and verbose: print('it {}'.format(len_it)) big_time = time.time() path_indices = [] extension_vertices = [] # get next road vertex for path_idx in range(len(paths)): # if total number of paths is greater than the maximum, then pass if path_lengths[path_idx] >= max_path_length: continue extension_vertex = paths[path_idx].pop() # if the next extension_vertex is none, then stop # in the final iter, extension vertex is none, them path_indices is none, # the program will stop if extension_vertex is None: if len(starting_points_list) > 0: # debug here to see the structure if extension_vertex st_pt = starting_points_list.pop() print("change starting point:{},{}".format( st_pt[0] - 4096, st_pt[1] - 4096)) start_point = geom.Point(st_pt[0] - 4096, st_pt[1] - 4096) extension_vertex = graph.Vertex(0, start_point) else: continue path_indices.append(path_idx) # sum the path_length path_lengths[path_idx] += 1 extension_vertices.append(extension_vertex) if len(path_indices) >= max_batch_size: break if len(path_indices) == 0: break batch_inputs = [] batch_detect_targets = [] batch_angle_targets = numpy.zeros((len(path_indices), 64), 'float32') batch_stop_targets = numpy.zeros((len(path_indices), 2), 'float32') for i in range(len(path_indices)): path_idx = path_indices[i] # 256*256*5 64*64*1 path_input, path_detect_target = model_utils.make_path_input( paths[path_idx], extension_vertices[i], segment_length, window_size=window_size) batch_inputs.append(path_input) batch_detect_targets.append(path_detect_target) if compute_targets: targets = model_utils.compute_targets_by_best( paths[path_idx], extension_vertices[i], segment_length) angle_targets, stop_targets = action_to_vector(targets) batch_angle_targets[i, :] = angle_targets batch_stop_targets[i, :] = stop_targets feed_dict = { m.is_training: False, m.inputs: batch_inputs, m.angle_targets: batch_angle_targets, m.action_targets: batch_stop_targets, m.detect_targets: batch_detect_targets, } batch_angle_outputs, batch_stop_outputs, batch_detect_outputs, angle_loss, detect_loss, stop_loss, loss = session.run( [ m.angle_outputs, m.action_outputs, m.detect_outputs, m.angle_loss, m.detect_loss, m.action_loss, m.loss ], feed_dict=feed_dict) angle_losses.append(angle_loss) detect_losses.append(detect_loss) stop_losses.append(stop_loss) losses.append(loss) batch_angle_outputs, batch_stop_outputs = fix_outputs( batch_angle_outputs, batch_stop_outputs) # whether save result if save and len_it % 1 == 0: fname = '/data/temp/{}_'.format(len_it) save_angle_targets = batch_angle_targets[0, :] if not compute_targets: save_angle_targets = None model_utils.make_path_input( paths[path_indices[0]], extension_vertices[0], segment_length, fname=fname, angle_targets=save_angle_targets, angle_outputs=batch_angle_outputs[0, :], detect_output=batch_detect_outputs[0, :, :, 0], window_size=window_size) for i in range(len(path_indices)): path_idx = path_indices[i] if len(extension_vertices[i].out_edges) >= 2: threshold = THRESHOLD_BRANCH mode = 'branch' else: threshold = THRESHOLD_FOLLOW mode = 'follow' if threshold_override: threshold = threshold_override if follow_targets == True: x = vector_to_action(batch_angle_targets[i, :], batch_stop_targets[i, :], threshold=threshold) elif follow_targets == 'partial': # (a) always use stop_targets instead of stop_outputs # (b) if we are far away from graph, use angle_targets, otherwise use angle_outputs extension_vertex = batch_extension_vertices[i] if extension_vertex.edge_pos is None or extension_vertex.edge_pos.point( ).distance(extension_vertex.point) > SEGMENT_LENGTH * 2: x = vector_to_action(batch_angle_targets[i, :], batch_stop_targets[i, :], threshold=threshold) else: x = vector_to_action(batch_angle_outputs[i, :], batch_stop_targets[i, :], threshold=threshold) elif follow_targets == 'npartial': # always move if gt says to move if batch_stop_outputs[i, 0] > threshold: x = vector_to_action(batch_angle_outputs[i, :], batch_stop_outputs[i, :], threshold=threshold) else: x = vector_to_action(batch_angle_outputs[i, :], batch_stop_targets[i, :], threshold=threshold) elif follow_targets == False: # 64, all other positions are 0 only the walk direction is p x = vector_to_action(batch_angle_outputs[i, :], batch_stop_outputs[i, :], threshold=threshold) else: raise Exception( 'invalid FOLLOW_TARGETS setting {}'.format(follow_targets)) paths[path_idx].push(extension_vertices[i], x, segment_length, training=False, branch_threshold=0.01, follow_threshold=0.01) # score accuracy accuracy = score_accuracy(batch_stop_targets[i, :], batch_angle_targets[i, :], batch_stop_outputs[i, :], batch_angle_outputs[i, :], threshold) accuracies.append(accuracy) if save: paths[0].graph.save('out.graph') return numpy.mean(angle_losses), numpy.mean(detect_losses), numpy.mean( stop_losses), numpy.mean(losses), len_it, numpy.mean(accuracies)
def eval(paths, m, session, max_path_length=MAX_PATH_LENGTH, segment_length=SEGMENT_LENGTH, save=False, follow_targets=False, compute_targets=True, max_batch_size=model.BATCH_SIZE, window_size=WINDOW_SIZE, verbose=True, threshold_override=False): angle_losses = [] detect_losses = [] stop_losses = [] losses = [] accuracies = [] path_lengths = {path_idx: 0 for path_idx in xrange(len(paths))} last_time = None big_time = None for len_it in xrange(99999999): if len_it % 500 == 0 and verbose: print 'it {}'.format(len_it) big_time = time.time() path_indices = [] extension_vertices = [] for path_idx in xrange(len(paths)): if path_lengths[path_idx] >= max_path_length: continue extension_vertex = paths[path_idx].pop() if extension_vertex is None: continue path_indices.append(path_idx) path_lengths[path_idx] += 1 extension_vertices.append(extension_vertex) if len(path_indices) >= max_batch_size: break if len(path_indices) == 0: break batch_inputs = [] batch_detect_targets = [] batch_angle_targets = numpy.zeros((len(path_indices), 64), 'float32') batch_stop_targets = numpy.zeros((len(path_indices), 2), 'float32') for i in xrange(len(path_indices)): path_idx = path_indices[i] path_input, path_detect_target = model_utils.make_path_input(paths[path_idx], extension_vertices[i], segment_length, window_size=window_size) batch_inputs.append(path_input) batch_detect_targets.append(path_detect_target) if compute_targets: targets = model_utils.compute_targets_by_best(paths[path_idx], extension_vertices[i], segment_length) angle_targets, stop_targets = action_to_vector(targets) batch_angle_targets[i, :] = angle_targets batch_stop_targets[i, :] = stop_targets feed_dict = { m.is_training: False, m.inputs: batch_inputs, m.angle_targets: batch_angle_targets, m.action_targets: batch_stop_targets, m.detect_targets: batch_detect_targets, } batch_angle_outputs, batch_stop_outputs, batch_detect_outputs, angle_loss, detect_loss, stop_loss, loss = session.run([m.angle_outputs, m.action_outputs, m.detect_outputs, m.angle_loss, m.detect_loss, m.action_loss, m.loss], feed_dict=feed_dict) angle_losses.append(angle_loss) detect_losses.append(detect_loss) stop_losses.append(stop_loss) losses.append(loss) batch_angle_outputs, batch_stop_outputs = fix_outputs(batch_angle_outputs, batch_stop_outputs) if save and len_it % 1 == 0: fname = '/home/ubuntu/data/{}_'.format(len_it) save_angle_targets = batch_angle_targets[0, :] if not compute_targets: save_angle_targets = None model_utils.make_path_input(paths[path_indices[0]], extension_vertices[0], segment_length, fname=fname, angle_targets=save_angle_targets, angle_outputs=batch_angle_outputs[0, :], detect_output=batch_detect_outputs[0, :, :, 0], window_size=window_size) for i in xrange(len(path_indices)): path_idx = path_indices[i] if len(extension_vertices[i].out_edges) >= 2: threshold = THRESHOLD_BRANCH mode = 'branch' else: threshold = THRESHOLD_FOLLOW mode = 'follow' if threshold_override: threshold = threshold_override if follow_targets == True: x = vector_to_action(batch_angle_targets[i, :], batch_stop_targets[i, :], threshold=threshold) elif follow_targets == 'partial': # (a) always use stop_targets instead of stop_outputs # (b) if we are far away from graph, use angle_targets, otherwise use angle_outputs extension_vertex = batch_extension_vertices[i] if extension_vertex.edge_pos is None or extension_vertex.edge_pos.point().distance(extension_vertex.point) > SEGMENT_LENGTH * 2: x = vector_to_action(batch_angle_targets[i, :], batch_stop_targets[i, :], threshold=threshold) else: x = vector_to_action(batch_angle_outputs[i, :], batch_stop_targets[i, :], threshold=threshold) elif follow_targets == 'npartial': # always move if gt says to move if batch_stop_outputs[i, 0] > threshold: x = vector_to_action(batch_angle_outputs[i, :], batch_stop_outputs[i, :], threshold=threshold) else: x = vector_to_action(batch_angle_outputs[i, :], batch_stop_targets[i, :], threshold=threshold) elif follow_targets == False: x = vector_to_action(batch_angle_outputs[i, :], batch_stop_outputs[i, :], threshold=threshold) else: raise Exception('invalid FOLLOW_TARGETS setting {}'.format(follow_targets)) paths[path_idx].push(extension_vertices[i], x, segment_length, training=False, branch_threshold=0.01, follow_threshold=0.01) # score accuracy accuracy = score_accuracy(batch_stop_targets[i, :], batch_angle_targets[i, :], batch_stop_outputs[i, :], batch_angle_outputs[i, :], threshold) accuracies.append(accuracy) if save: paths[0].graph.save('out.graph') return numpy.mean(angle_losses), numpy.mean(detect_losses), numpy.mean(stop_losses), numpy.mean(losses), len_it, numpy.mean(accuracies)
batch_action_targets = numpy.zeros((model.BATCH_SIZE, 2), 'float32') for i in xrange(len(path_indices)): path_idx = path_indices[i] extension_vertex = paths[path_idx].pop() if extension_vertex is None or len(paths[path_idx].graph.vertices) >= MAX_PATH_LENGTH: start_loc = random.choice(subtiles[path_idx]['starting_locations']) paths[path_idx] = model_utils.Path(subtiles[path_idx]['gc'], subtiles[path_idx], start_loc=start_loc) extension_vertex = paths[path_idx].pop() path_input, path_detect_target = model_utils.make_path_input(paths[path_idx], extension_vertex, SEGMENT_LENGTH, detect_mode=DETECT_MODE, window_size=WINDOW_SIZE) batch_extension_vertices.append(extension_vertex) batch_inputs.append(path_input) batch_detect_targets.append(path_detect_target) targets = model_utils.compute_targets_by_best(paths[path_idx], extension_vertex, SEGMENT_LENGTH) angle_targets, action_targets = action_to_vector(targets) batch_angle_targets[i, :] = angle_targets batch_action_targets[i, :] = action_targets times['prepare'] += time.time() - stage_time stage_time = time.time() # train model feed_dict = { m.is_training: True, m.inputs: batch_inputs, m.angle_targets: batch_angle_targets, m.action_targets: batch_action_targets, m.detect_targets: batch_detect_targets, m.learning_rate: epoch_to_learning_rate(outer_it),