def validation_score(model_path, type, save=False, debugging=False):

    model = i3d(input_shape=(configs.LENGTH, configs.IMG_HEIGHT,
                             configs.IMG_WIDTH, configs.CHANNELS),
                weights_path=model_path)

    # read the steering labels and image path
    df_truth = pd.read_csv(
        '/home/neil/dataset/speedchallenge/data/validation.csv').values

    e_sum = 0
    inputs = []
    predictions = []

    if type == 'rgb':

        start_time = time.time()

        for i in range(configs.LENGTH):
            file = configs.TRAIN_DIR + str(df_truth[i][1])
            img = helper.load_image(file)
            inputs.append(img)

        # Run through all images
        for i in range(configs.LENGTH, len(df_truth)):

            p = configs.TRAIN_DIR + str(df_truth[i][1])
            img = helper.load_image(p)
            inputs.pop(0)
            inputs.append(img)
            prediction = model.model.predict(np.array([np.asarray(inputs)
                                                       ]))[0][0]
            actual_speed = df_truth[i][2]
            e_sum += (actual_speed - prediction)**2

            predictions.append(prediction)

            if len(predictions) % 1000 == 0:
                print('.')

    elif type == 'rgb-flow':

        print('Started')

        start_time = time.time()

        previous = helper.load_image(configs.TRAIN_DIR + str(df_truth[0][1]))

        for i in range(1, configs.LENGTH + 1):

            img = helper.load_image(configs.TRAIN_DIR + str(df_truth[i][1]))
            rgbImg = helper.optical_flow_rgb(previous=previous, current=img)
            if debugging:
                fig = plt.figure(figsize=(3, 1))
                fig.add_subplot(1, 3, 1)
                plt.imshow(rgbImg)
                fig.add_subplot(1, 3, 2)
                plt.imshow(previous)
                fig.add_subplot(1, 3, 3)
                plt.imshow(img)
                plt.show()

            previous = img
            inputs.append(rgbImg)

        previous = helper.load_image(configs.TRAIN_DIR +
                                     str(df_truth[configs.LENGTH + 1][1]))

        for i in range(configs.LENGTH + 2, len(df_truth)):

            img = helper.load_image(configs.TRAIN_DIR + str(df_truth[i][1]))
            rgb_flow = helper.optical_flow_rgb(previous, img)
            if debugging:
                plt.imshow(rgb_flow)
                plt.show()
            inputs.pop(0)
            inputs.append(rgb_flow)
            input_array = np.array([np.asarray(inputs)])
            prediction = model.model.predict(input_array)[0][0]
            actual_steers = df_truth[i][2]
            e = (actual_steers - prediction)**2
            e_sum += e

            predictions.append(prediction)

            if len(predictions) % 1000 == 0:
                print('.')

    elif type == 'flow':

        print('Started')

        start_time = time.time()

        previous = helper.load_image(configs.TRAIN_DIR + str(df_truth[0][1]))

        for i in range(1, configs.LENGTH + 1):

            img = helper.load_image(configs.TRAIN_DIR + str(df_truth[i][1]))
            flow = helper.optical_flow(previous=previous, current=img)
            inputs.append(flow)

        previous = helper.load_image(configs.TRAIN_DIR +
                                     str(df_truth[configs.LENGTH + 1][1]))

        for i in range(configs.LENGTH + 2, len(df_truth)):

            img = helper.load_image(configs.TRAIN_DIR + str(df_truth[i][1]))
            flow = helper.optical_flow(previous, img)
            inputs.pop(0)
            inputs.append(flow)
            prediction = model.model.predict(np.array([np.asarray(inputs)
                                                       ]))[0][0]
            actual_steers = df_truth[i][2]
            e_sum += (actual_steers - prediction)**2

            predictions.append(prediction)

            if len(predictions) % 1000 == 0:
                print('.')
    else:
        raise Exception('Sorry, the model type is not recognized')

    print("time per step: %s seconds" %
          ((time.time() - start_time) / len(predictions)))

    if save:
        print("Writing predictions...")
        pd.DataFrame({
            "steering_angle": predictions
        }).to_csv('./result.csv', index=False, header=True)
        print("Done!")

    return e_sum / len(predictions)
Exemplo n.º 2
0
def test_loop(model_path, model_type):
    """
    for visualizing acceleration models with the comma AI
    validation dataset. The speed is initialized before
    acceleration prediction starts.

    :param model_path: the path of the trained Keras model
    :param model_type: the type of model, rgb, flow or rgb-flow
    :return: None
    """

    print(colored('Preparing', 'blue'))

    model = Inception3D(weights_path=model_path,
                        input_shape=(configs.LENGTH, configs.IMG_HEIGHT,
                                     configs.IMG_WIDTH, 2))

    # read the steering labels and image path
    files = os.listdir(configs.TEST_DIR)

    inputs = []
    starting_index = 1000
    end_index = 1000

    if model_type == 'rgb':

        for i in range(starting_index, starting_index + configs.LENGTH):
            img = helper.load_image(configs.TEST_DIR + "frame" + str(i) +
                                    ".jpg")
            inputs.append(img)

        print(colored('Started', 'blue'))

        # Run through all images
        for i in range(starting_index + configs.LENGTH + 1,
                       len(files) - 1 - end_index):

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    break

            img = helper.load_image(configs.TEST_DIR + "frame" + str(i) +
                                    ".jpg",
                                    resize=False)
            in_frame = cv2.resize(img, (configs.IMG_WIDTH, configs.IMG_HEIGHT))
            inputs.pop(0)
            inputs.append(in_frame)
            prediction = model.model.predict(np.array([inputs]))[0][0]

            pygame_loop(label=0, prediction=prediction, img=img)

    elif model_type == 'flow':

        previous = helper.load_image(configs.TEST_DIR + "frame" +
                                     str(starting_index) + ".jpg")

        for i in range(starting_index, starting_index + configs.LENGTH):

            img = helper.load_image(configs.TEST_DIR + "frame" + str(i) +
                                    ".jpg")
            in_frame = cv2.resize(img, (configs.IMG_WIDTH, configs.IMG_HEIGHT))
            flow = helper.optical_flow(previous=previous, current=in_frame)
            inputs.append(flow)

        previous = helper.load_image(configs.TEST_DIR + "frame" +
                                     str(starting_index + configs.LENGTH) +
                                     ".jpg")

        for i in range(starting_index + configs.LENGTH + 1,
                       len(files) - 1 - end_index):

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    break

            img = helper.load_image(configs.TEST_DIR + "frame" + str(i) +
                                    ".jpg",
                                    resize=False)
            in_frame = cv2.resize(img, (configs.IMG_WIDTH, configs.IMG_HEIGHT))
            flow = helper.optical_flow(previous, in_frame)
            inputs.pop(0)
            inputs.append(flow)
            input_array = np.array([np.asarray(inputs)])
            prediction = model.model.predict(input_array)[0][0]

            pygame_loop(label=0, prediction=prediction, img=img)

    else:
        raise Exception('Sorry, the model type is not recognized')
Exemplo n.º 3
0
def test_loop(results_paths):
    """
    for visualizing the model with the comma AI
    test dataset. The ds doesn't contain training labels.

    :param model_path: the path of the trained Keras model
    :param model_type: the type of model, rgb, flow or rgb-flow
    :return: None
    """

    print(colored('Preparing', 'blue'))

    # read the steering labels and image path
    files = os.listdir(configs.TEST_DIR)

    inputs = []
    starting_index = 8000
    end_index = 1000

    for i in range(starting_index, starting_index + configs.LENGTH):
        img = helper.load_image(configs.TEST_DIR + "frame" + str(i) + ".jpg")
        inputs.append(img)

    print(colored('Started', 'blue'))

    # Run through all images
    for i in range(starting_index + configs.LENGTH + 1,
                   len(files) - 1 - end_index):

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                break

        img = helper.load_image(configs.TEST_DIR + "frame" + str(i) + ".jpg",
                                resize=False)
        in_frame = cv2.resize(img, (configs.IMG_WIDTH, configs.IMG_HEIGHT))
        inputs.pop(0)
        inputs.append(in_frame)

        pygame_loop(prediction=prediction, img=img)


# ==========================================

    previous = helper.load_image(configs.TEST_DIR + "frame" +
                                 str(starting_index) + ".jpg")

    for i in range(starting_index, starting_index + configs.LENGTH):
        img = helper.load_image(configs.TEST_DIR + "frame" + str(i) + ".jpg")
        in_frame = cv2.resize(img, (configs.IMG_WIDTH, configs.IMG_HEIGHT))
        flow = helper.optical_flow(previous=previous, current=in_frame)
        inputs.append(flow)

    previous = helper.load_image(configs.TEST_DIR + "frame" +
                                 str(starting_index + configs.LENGTH) + ".jpg")

    for i in range(starting_index + configs.LENGTH + 1,
                   len(files) - 1 - end_index):

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                break

        img = helper.load_image(configs.TEST_DIR + "frame" + str(i) + ".jpg",
                                resize=False)
        in_frame = cv2.resize(img, (configs.IMG_WIDTH, configs.IMG_HEIGHT))
        flow = helper.optical_flow(previous, in_frame)
        inputs.pop(0)
        inputs.append(flow)
        input_array = np.array([np.asarray(inputs)])
        prediction = model.model.predict(input_array)[0][0]

        pygame_loop(prediction=prediction, img=img)
Exemplo n.º 4
0
def visualize_accel(model_path, label_path, model_type):

    print(colored('Preparing', 'blue'))

    model = Inception3D(weights_path=model_path,
                        input_shape=(configs.LENGTH, configs.IMG_HEIGHT,
                                     configs.IMG_WIDTH, 3))

    # read the steering labels and image path
    labels = pd.read_csv(label_path).values

    inputs = []
    starting_index = 2000
    end_index = 0
    # init_speed = labels[starting_index][2]

    if model_type == 'rgb':

        for i in range(starting_index, starting_index + configs.LENGTH):
            img = helper.load_image(configs.TEST_DIR + "frame" + str(i) +
                                    ".jpg")
            inputs.append(img)

        print(colored('Started', 'blue'))

        # Run through all images
        for i in range(starting_index + configs.LENGTH + 1,
                       len(labels) - 1 - end_index):

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    break

            img = helper.load_image(
                "/home/neil/dataset/speedchallenge/data/train/" +
                str(labels[i][1]),
                resize=False)
            in_frame = cv2.resize(img, (configs.IMG_WIDTH, configs.IMG_HEIGHT))
            inputs.pop(0)
            inputs.append(in_frame)
            pred_accel = model.model.predict(np.array([np.asarray(inputs)
                                                       ]))[0][0]
            label_accel = (labels[i][2] - labels[i - 1][2]) * 20
            pygame_loop(label=label_accel, prediction=pred_accel, img=img)

    elif model_type == 'flow':

        previous = helper.load_image(
            "/home/neil/dataset/speedchallenge/data/train/" +
            str(labels[i][1]))

        for i in range(starting_index, starting_index + configs.LENGTH):
            img = helper.load_image(configs.TEST_DIR + "frame" + str(i) +
                                    ".jpg")
            in_frame = cv2.resize(img, (configs.IMG_WIDTH, configs.IMG_HEIGHT))
            flow = helper.optical_flow(previous=previous, current=in_frame)
            inputs.append(flow)

        previous = helper.load_image(
            "/home/neil/dataset/speedchallenge/data/train/" +
            str(labels[i][1]))

        for i in range(starting_index + configs.LENGTH + 1,
                       len(labels) - 1 - end_index):

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    break

            img = helper.load_image(
                "/home/neil/dataset/speedchallenge/data/train/" +
                str(labels[i][1]))
            in_frame = cv2.resize(img, (configs.IMG_WIDTH, configs.IMG_HEIGHT))
            flow = helper.optical_flow(previous, in_frame)
            inputs.pop(0)
            inputs.append(flow)
            pred_accel = model.model.predict(np.array([np.asarray(inputs)
                                                       ]))[0][0]
            label_accel = (labels[i][2] - labels[i - 1][2]) * 20
            pygame_loop(label=label_accel, prediction=pred_accel, img=img)
    else:
        raise Exception('Sorry, the model type is not recognized')