Beispiel #1
0
def get_model(time_len=1):
    ch, row, col = camera_format.get_camera_image_dim()

    model = Sequential()
    model.add(
        Lambda(lambda x: x / 127.5 - 1.,
               input_shape=(ch, row, col),
               output_shape=(ch, row, col)))
    model.add(Convolution2D(24, 5, 5, subsample=(2, 2), border_mode="same"))
    model.add(ELU())
    model.add(Convolution2D(36, 5, 5, subsample=(2, 2), border_mode="same"))
    model.add(ELU())
    model.add(Convolution2D(48, 3, 3, subsample=(2, 2), border_mode="same"))
    model.add(ELU())
    model.add(Convolution2D(64, 3, 3, subsample=(2, 2), border_mode="same"))
    model.add(Flatten())
    model.add(Dropout(.2))
    model.add(ELU())
    model.add(Dense(512))
    model.add(Dropout(.5))
    model.add(ELU())
    model.add(Dense(256))
    model.add(ELU())
    model.add(Dense(128))
    model.add(ELU())
    model.add(Dense(1))

    model.compile(optimizer="adam", loss="mse")

    return model
import sys
import numpy as np
import h5py
import json
from keras.models import model_from_json
#import matplotlib.pyplot as plt
import time
import asyncore
import json
import socket
from PIL import Image
import pygame
import camera_format

pygame.init()
ch, row, col = camera_format.get_camera_image_dim()

size = (col * 2, row * 2)
pygame.display.set_caption("sdsandbox data monitor")
screen = pygame.display.set_mode(size, pygame.DOUBLEBUF)
camera_surface = pygame.surface.Surface((col, row), 0, 24).convert()
myfont = pygame.font.SysFont("monospace", 15)


def screen_print(x, y, msg, screen):
    label = myfont.render(msg, 1, (255, 255, 0))
    screen.blit(label, (x, y))


def display_img(img, steering):
    img = img.transpose().swapaxes(0, 1)
Beispiel #3
0
def prepare(drivinglog, drivingimages, outputpath, prefix, activity):
    t = time.ctime(os.path.getmtime(drivinglog))
    t = t.replace(' ', '_')
    t = t.replace(':', '_')
    basename = prefix + t + ".h5"
    outfilename = os.path.join(outputpath, "log", basename)
    infile = open(drivinglog, "r")
    lines = []
    for line in infile:
        lines.append(line)
    infile.close()

    print 'gathering images', drivingimages
    images = glob.glob(drivingimages)
    num_images = len(images)
    num_records = len(lines)

    if (num_images != num_records):
        #use the smaller of the two.
        if num_images < num_records:
            num_records = num_images
        else:
            num_images = num_records
            images = images[:num_images]

    print len(lines), 'steering records'

    logf = h5py.File(outfilename, "w")
    dse = logf.create_dataset("steering_angle", (num_records, ),
                              dtype='float64')
    dse_speed = logf.create_dataset("speed", (num_records, ), dtype='float64')
    iIter = 0
    iLine = 0
    iRecord = 0
    num_lines = len(lines)
    log_ids = []
    while iLine < num_lines and iRecord < num_records:
        tokens = lines[iLine].split(',')
        iLine += 1
        iRecord += 1
        if len(tokens) != 4:
            continue
        log_activity = tokens[1]
        if activity is not None and activity != log_activity:
            continue
        log_ids.append(int(tokens[0]))
        steering = float(tokens[2])
        speed = float(tokens[3])
        dse[iIter] = np.array([steering])
        dse_speed[iIter] = np.array([speed])
        iIter += 1
        if iIter % 1000 == 0:
            print iIter
    if activity is not None:
        print iIter, 'records found w activity', activity
    logf.close()

    print 'done with log'

    outfilename = os.path.join(outputpath, "camera", basename)
    camf = h5py.File(outfilename, "w")
    print num_images, 'images'
    ch, rows, col = camera_format.get_camera_image_dim()
    dse = camf.create_dataset("X", (num_images, ch, rows, col), dtype='uint8')
    images.sort()
    imgs_by_id = {}

    for img_filename in images:
        path, name = os.path.split(img_filename)
        first_zero = name.find('0')
        first_period = name.find('.')
        num_string = name[first_zero:first_period]
        id = int(num_string)
        imgs_by_id[id] = img_filename

    iIter = 0
    for id in log_ids:
        try:
            img_filename = imgs_by_id[id]
            im = Image.open(img_filename).convert('RGB')
            if im.width != col or im.height != rows:
                print 'Aborting! image:', img_filename, 'had the wrong dimension:', im.width, im.height, 'expecting', col, rows
                #stopping because we are likely to see many of these..
                return
            imarr = np.array(im).transpose()
            dse[iIter] = imarr
        except KeyError:
            print 'no image for frame', id
        iIter = iIter + 1
        if iIter % 1000 == 0:
            print iIter
    camf.close()
    print 'done with images'
    if activity is not None:
        print iIter, 'images found w activity', activity