def initialize_RPi_Stuff(): # note: global variables start with a little "g" global g_getter global g_camera global g_steerstats global g_graph global g_image_data global g_stop_event global g_lock g_getter = DataGetter() g_camera = picamera.PiCamera() g_camera.resolution = (128, 96) #final image size g_camera.framerate = 10 #<---- framerate (fps) determines speed of data recording g_steerstats = np.load(g_pi_training_steerstats_file)['arr_0'] model.load_weights(g_pi_training_weights_file) model._make_predict_function() g_graph = tf.get_default_graph() g_image_data = np.zeros((36, 128, 3), dtype=np.uint8) g_stop_event = threading.Event() g_lock = threading.Lock() # dazzle them with Night Rider LED show... for x in range(0, 3): for i in range(0, 6): displayBinaryOnLEDs(2**i) time.sleep(.125) for i in range(5, -1, -1): displayBinaryOnLEDs(2**i) time.sleep(.125) # dazzle them with Night Rider LED show... # turn off all LEDs for initialization turn_OFF_all_LEDs()
def initialize_service(): #initialize the serial port: if the first port fails, we try the other one global g_serial g_serial = 0 try: g_serial = serial.Serial('/dev/ttyACM1') except serial.SerialException: try: g_serial = serial.Serial('/dev/ttyACM0') except serial.SerialException: print('Cannot connect to serial port') #initialize the camera global g_camera g_camera = picamera.PiCamera() g_camera.resolution = (128, 96) g_camera.framerate = 10 #initialize the data collector object global g_collector g_collector = DataCollector(g_serial, "/home/pi/autonomous/data") #initialize the image frame to be shared in autonomous mode global g_image_data g_image_data = np.zeros((36, 128, 3), dtype=np.uint8) #initialize some stuff needed for network thread global g_stop_event g_stop_event = threading.Event() global g_lock g_lock = threading.Lock() #this is the object the camera writes to in autonomous mode global g_getter g_getter = DataGetter() #this stuff sets up the network global g_graph g_graph = tf.get_default_graph() #model.load_weights('weights_2018-02-24_14-00-35_epoch_40.h5') model.load_weights('weights_2018-04-05_02-43-30_epoch_66.h5') model._make_predict_function() global g_steerstats g_steerstats = np.load('steerstats.npz')['arr_0'] global g_ip_thread g_ip_thread = 0
def initialize_service(): #initialize the serial port: if the first port fails, we try the other one global g_serial try: g_serial = serial.Serial('/dev/ttyACM1') except serial.SerialException: try: g_serial = serial.Serial('/dev/ttyACM0') except serial.SerialException: logging.debug("error: cannot connect to serial port") #initialize the camera global g_camera g_camera = picamera.PiCamera() g_camera.resolution = (128, 96) g_camera.framerate = FRAME_RATE #initialize the data collector object global g_collector g_collector = DataCollector(g_serial, COLLECT_DIR) #initialize the image frame to be shared in autonomous mode global g_image_data g_image_data = np.zeros((36, 128, 3), dtype=np.uint8) #initialize some stuff needed for network thread global g_stop_event g_stop_event = threading.Event() global g_lock g_lock = threading.Lock() #this is the object the camera writes to in autonomous mode global g_getter g_getter = DataGetter() #this stuff sets up the network global g_graph g_graph = tf.get_default_graph() #model.load_weights('weights_2018-02-24_14-00-35_epoch_40.h5') model.load_weights(WEIGHTS_FILE) model._make_predict_function() global g_steerstats g_steerstats = np.load(STEERSTATS_FILE)['arr_0'] global g_ip_thread g_ip_thread = 0
for image in imdata[0:data_lengths[i]-args.delay]: #note that we compensate for delay here crop_image=image[row_offset:row_offset+nrows, :] #we have to normalize each image to zero mean, unit variance: image_mean=crop_image.mean() image_std=crop_image.std() training_images[n]=(crop_image-image_mean)/image_std n+=1#this increments for each image, and keeps track of where to put the images in the training_images array i+=1#this increments for each file, and keeps track of where to get the number of data frames from data_lengths #this loads the predefined network architecture from dropout_model.py from dropout_model import model num_epochs=args.epochs#number of epochs to train over save_epochs=args.save_frequency#number of epochs between weight file saves #if the user inputs a weight file for initial state, load it: if args.init_weights!="": model.load_weights(args.init_weights) for n in range(num_epochs): print("starting epoch {0}".format(n)) h=model.fit([training_images], [(steer-steerSampleMean)/steerSampleSTD], batch_size=25, epochs=1, verbose=1, validation_split=0.1, shuffle=True) if n%save_epochs ==0 : savename='%s_%s_epoch_%d.h5'%(args.weight_filename, time_string, n) print("Saving epoch {0} to {1}".format(n, savename)) model.save_weights(savename, overwrite=True) savename='%s_%s_complete.h5'%(args.weight_filename, time_string) model.save_weights(savename, overwrite=True)