コード例 #1
0
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
コード例 #2
0
best_loss = None

for epoch in xrange(9999):
    start_time = time.time()
    train_losses = []
    for _ in xrange(1024):
        examples = [get_example('train') for _ in xrange(model.BATCH_SIZE)]
        feed_dict = {
            m.is_training: True,
            m.inputs: [example[1] for example in examples],
            m.angle_targets: [example[2] for example in examples],
            m.detect_targets: [example[3] for example in examples],
            m.learning_rate: 1e-5,
        }
        if ANGLE_ONEHOT:
            feed_dict[m.angle_onehot] = model_utils.get_angle_onehot(
                ANGLE_ONEHOT)
        _, angle_loss, detect_loss, loss = session.run(
            [m.optimizer, m.angle_loss, m.detect_loss, m.loss],
            feed_dict=feed_dict)
        train_losses.append((angle_loss, detect_loss, loss))

    train_loss = numpy.mean([l[0] for l in train_losses]), numpy.mean(
        [l[1] for l in train_losses]), numpy.mean([l[2] for l in train_losses])
    train_time = time.time()

    val_losses = []
    for i in xrange(0, len(val_examples), model.BATCH_SIZE):
        examples = val_examples[i:i + model.BATCH_SIZE]
        feed_dict = {
            m.is_training: False,
            m.inputs: [example[1] for example in examples],