Exemple #1
0
    def plot_predictions(self, cfg, tub_paths, model_path):
        """
        Plot model predictions for angle and throttle against data from tubs.

        """
        from donkeycar.parts.datastore import TubGroup
        from donkeycar.parts.keras import KerasCategorical

        tg = TubGroup(tub_paths)

        model_path = os.path.expanduser(model_path)
        model = KerasCategorical()
        model.load(model_path)

        gen = tg.get_batch_gen(None, None, batch_size=len(tg.df),shuffle=False, df=tg.df)
        arr = next(gen)

        user_angles = []
        user_throttles = []
        pilot_angles = []
        pilot_throttles = []

        for tub in tg.tubs:
            num_records = tub.get_num_records()
            for iRec in tub.get_index(shuffled=False):
                record = tub.get_record(iRec)

                img = record["cam/image_array"]
                user_angle = float(record["user/angle"])
                user_throttle = float(record["user/throttle"])
                pilot_angle, pilot_throttle = model.run(img)

                user_angles.append(user_angle)
                user_throttles.append(user_throttle)
                pilot_angles.append(pilot_angle)
                pilot_throttles.append(pilot_throttle)

        angles_df = pd.DataFrame({'user_angle': user_angles, 'pilot_angle': pilot_angles})
        throttles_df = pd.DataFrame({'user_throttle': user_throttles, 'pilot_throttle': pilot_throttles})

        fig = plt.figure()

        title = "Model Predictions\nTubs: {}\nModel: {}".format(tub_paths, model_path)
        fig.suptitle(title)

        ax1 = fig.add_subplot(211)
        ax2 = fig.add_subplot(212)

        angles_df.plot(ax=ax1)
        throttles_df.plot(ax=ax2)

        ax1.legend(loc=4)
        ax2.legend(loc=4)

        plt.show()
Exemple #2
0
def drive(model_path=None):
    kl = KerasCategorical()
    if model_path:
        kl.load(model_path)
    print('Model loaded')
    # testImage = Image.open('test.png');
    # testImage = numpy.array(testImage)

    throttle_history = collections.deque(maxlen=10)
    angle_history = collections.deque(maxlen=10)
    for i in range(10):
        throttle_history.append(0)
        angle_history.append(0)
    print(len(throttle_history))
    print(len(angle_history))
    round = 0
    while True:
        with mss.mss() as sct:
            image = captureScreen(sct)
            image = numpy.array(image)
            if (round > 0):
                angle, throttle = kl.run(image, prevImage,
                                         numpy.array(angle_history),
                                         numpy.array(throttle_history))

                # print('Raw throttle', throttle)
                # throttle = min(0.8, max(0.1, throttle * 3000000))

                setThrottle(throttle)
                setSteering(angle)
                #s = input(">>")
                #if len(s) >= 1:
                #    break
                # if (round % 100 == 0):
                # print(throttle, angle)
                throttle_history.append(throttle)
                angle_history.append(angle)

            prevImage = image
            round = round + 1
Exemple #3
0
def playback(cfg, tub_names, model_name=None):
    if not tub_names:
        tub_names = os.path.join(cfg.DATA_PATH, '*')
    tubgroup = TubGroup(tub_names)
    
    if not model_name is None:
        from donkeycar.parts.keras import KerasCategorical
        kl = KerasCategorical()
        kl.load(model_name)
        pilot_angles = []
        pilot_throttles  = []
        
    
    
    print('tub_names', tub_names)
    
    tub_paths = utils.expand_path_arg(tub_names)
    print('TubGroup:tubpaths:', tub_paths)
    user_angles = []
    user_throttles  = []
    
        
    tubs = [Tub(path) for path in tub_paths]
    for tub in tubs:
            num_records = tub.get_num_records()
            print(num_records)
            for iRec in tub.get_index(shuffled=False):
                record = tub.get_record(iRec)
            #record = tubs.get_record(random.randint(1,num_records+1))
                img = record["cam/image_array"]
                user_angle = float(record["user/angle"])
                user_throttle = float(record["user/throttle"])
                user_angles.append(user_angle)
                user_throttles.append(user_throttle)
                if not model_name is None:
                    pilot_angle, pilot_throttle = kl.run(img)
                    pilot_angles.append(pilot_angle)
                    pilot_throttles.append(pilot_throttle)
            
    
    record = tubs[0].get_record(random.randint(1,num_records+1))
    user_angle = float(record["user/angle"])
    user_throttle = float(record["user/throttle"])
    print(img.shape)
    print('-----')
    print(user_angle)
    print(user_throttle)
    plt.figure()
    plt.imshow(img)
    plt.plot([80,80+10*user_throttle*np.cos(user_angle)],[120,120+100*user_throttle*np.sin(user_angle)])
    
    plt.figure()
    plt.plot(user_angles)
    plt.plot(user_throttles)
    
    
    
    
    fig = plt.figure()
    ax1 = plt.subplot2grid((2,2),(0,0))
    record = tubs[0].get_record(1)
    img = record["cam/image_array"]
    imPlot = ax1.imshow(img,animated=True)
    
    
    floorImg = lookAtFloorImg(img)
    print(floorImg)
    ax3 = plt.subplot2grid((2,2),(0,1))
    imPlot2 = ax3.imshow(floorImg,animated=True)
    
    ax2 = plt.subplot2grid((2,2),(1,0), colspan=2)
    line1, = ax2.plot(user_angles)
    line2, = ax2.plot(user_throttles)
    if not model_name is None:
        line4, = ax2.plot(pilot_angles)
        line5, = ax2.plot(pilot_throttles)
    line3, = ax2.plot([0,0],[-1,1])
    
    
    
    def animate(i):
        record = tubs[0].get_record(i)
        img = record["cam/image_array"]
        imPlot.set_array(img)
        imPlot2.set_array(lookAtFloorImg(img))
        line3.set_data([i,i],[-1,1])
        #print(i)
        #sys.stdout.flush()
        return imPlot,


    # Init only required for blitting to give a clean slate.
    def init():
        record = tubs[0].get_record(1)
        img = record["cam/image_array"]
        imPlot.set_array(img)
        line3.set_data([0,0],[-1,1])
        return imPlot,
    
    
    
    ani = animation.FuncAnimation(fig, animate, np.arange(1, tubs[0].get_num_records()),
                              interval=100, blit=False)
                              
    plt.show()