def put(self, key=config.CONFIG_SINGLETON_KEY): model_key = ndb.Key(config.Configuration, key) old_model = model_key.get() if not old_model: raise NotFound('{} with key {} does not exist'.format('Configuration', key)) # the history mechanism doesn't work unless we make a copy. So a put is always a clone, never # an actual update. model = config.Configuration(**old_model.to_dict()) model.key = model_key model.configuration = request.get_json(force=True) self.validate(model) date = None if config.getSettingJson(config.ALLOW_NONPROD_REQUESTS, False): date = request.headers.get('x-pretend-date', None) if date is not None: date = parse_date(date) client_id = app_util.get_oauth_id() config.store(model, date=date, client_id=client_id) return model.configuration
plt.plot(accuracy_check_iterations, allAccuracyTrain, 'b-') plt.plot(accuracy_check_iterations, allAccuracyTest, 'ro') plt.plot(accuracy_check_iterations, allAccuracyTest, 'r-') plt.plot(accuracy_check_iterations, sliding_window_graph, 'g-') axes = plt.gca() axes.set_ylim([0, 1000.05]) plt.title("training (blue), test (red), avg " + str(round(sliding_window_graph[-1], 5)) + " / " + str(len(sliding_window))) plt.xlabel('iteration') plt.ylabel('diff squared') plt.savefig(os.path.join(output_path, "progress.png")) # Save the model. save_path = saver.save(sess, os.path.join(output_path, "model.ckpt")) config.store('last_tf_model', save_path) # put the print after writing everything so it indicates things have been written. debug_iteration = format(iteration, '^10') debug_loss = format(results_loss, '^16') debug_regularizers = format(results_regularizers, '^16') #Format sliding window debug_sliding_window = generate_color_text(prev_sliding_window, sliding_window_graph[-1]) debug_sliding_window = format(debug_sliding_window, '^25') prev_sliding_window = sliding_window_graph[-1] # Format steering regression accuracy debug_sqr_acc = generate_color_text(prev_squared_diff, results_squared_diff)
def post(self, key=config.CONFIG_SINGLETON_KEY): model = config.Configuration(key=ndb.Key(config.Configuration, key), configuration=request.get_json(force=True)) self.validate(model) config.store(model) return model.configuration
def main(): global last_odometer_reset # Init some vars.. old_steering = 0 old_throttle = 0 old_aux1 = 0 steering = 90 throttle = 90 aux1 = 0 frame_count = 0 session_full_path = '' last_switch = 0 button_arduino_out = 0 currently_running = False override_autonomous_control = False train_on_this_image = True vel = 0.0 last_odo = 0 last_millis = 0.0 # Check for insomnia if platform.system() == "Darwin": check_for_insomnia() # Setup ports. port_in, port_out = setup_serial_and_reset_arduinos() # Setup tensorflow sess, net_model = setup_tensorflow() # Start the clock. drive_start_time = time.time() print 'Awaiting switch flip..' while True: loop_start_time = time.time() # Switch was just flipped. if last_switch != button_arduino_out: last_switch = button_arduino_out # See if the car started up with the switch already flipped. if time.time() - drive_start_time < 1: print 'Error: start switch in the wrong position.' sys.exit() if button_arduino_out == 1: currently_running = True print '%s: Switch flipped.' % frame_count last_odometer_reset = odometer_ticks if we_are_recording and (not we_are_autonomous): session_full_path = make_data_folder('~/training-images') print 'STARTING TO RECORD.' print 'Folder: %s' % session_full_path config.store('last_record_dir', session_full_path) elif we_are_recording and we_are_autonomous: session_full_path = make_data_folder('~/tf-driving-images') print 'DRIVING AUTONOMOUSLY and STARTING TO RECORD' print 'Folder: %s' % session_full_path else: print("DRIVING AUTONOMOUSLY (not recording).") else: print("%s: Switch flipped. Recording stopped." % frame_count) currently_running = False # Read input data from arduinos. new_steering, new_throttle, new_aux1, button_arduino_in, button_arduino_out = ( process_input(port_in, port_out)) if new_steering != None: steering = new_steering if new_throttle != None: throttle = new_throttle if new_aux1 != None: aux1 = new_aux1 # Check to see if we should stop the car via the RC during TF control. # But also provide a way to re-engage autonomous control after an override. if we_are_autonomous and currently_running: if (steering > 130 or steering < 50) and throttle > 130: if not override_autonomous_control: print '%s: Detected RC override: stopping.' % frame_count override_autonomous_control = True if abs(aux1 - old_aux1) > 400 and override_autonomous_control: old_aux1 = aux1 print '%s: Detected RC input: re-engaging autonomous control.' % frame_count override_autonomous_control = False # Check to see if we should reset the odometer via aux1 during manual # driving. This is Button E on the RC transmitter. # The values will swing from ~1100 to ~1900. if abs(aux1 - old_aux1) > 400: old_aux1 = aux1 print '%s: Resetting the odometer.' % frame_count last_odometer_reset = odometer_ticks # Overwrite steering with neural net output in autonomous mode. # This seems to take about 10ms. if we_are_autonomous and currently_running: # Calculate velocity from odometer. Gets weird when stopped. if odometer_ticks != last_odo and milliseconds > last_millis: vel = (float(odometer_ticks) - last_odo) / (milliseconds - last_millis) if last_millis == 0 and last_odo == 0: vel = 0 if odometer_ticks - last_odo > 50 or last_odo >= odometer_ticks: vel = 0 last_odo = odometer_ticks last_millis = milliseconds # Read a frame from the camera. frame = camera_stream.read() steering, throttle = do_tensorflow( sess, net_model, frame, odometer_ticks - last_odometer_reset, vel) # steering, throttle = do_tensor_flow(frame, odometer_ticks - last_odometer_reset, vel) if we_are_recording and currently_running: # TODO(matt): also record vel in filename for tf? # Read a frame from the camera. frame = camera_stream.read() # Save image with car data in filename. cv2.imwrite( "%s/" % session_full_path + "frame_" + str(frame_count).zfill(5) + "_thr_" + str(throttle) + "_ste_" + str(steering) + "_mil_" + str(milliseconds) + "_odo_" + str(odometer_ticks - last_odometer_reset).zfill(5) + ".png", frame) else: frame = camera_stream.read() cv2.imwrite('/tmp/test.png', frame) if override_autonomous_control: # Full brake and neutral steering. throttle, steering = 0, 90 # Send output data to arduinos. process_output(old_steering, old_throttle, steering, throttle, port_out) old_steering = steering old_throttle = throttle # Attempt to go at 30 fps. In reality, we could go slower if something hiccups. seconds = time.time() - loop_start_time while seconds < 1 / 30.: time.sleep(0.001) seconds = time.time() - loop_start_time frame_count += 1
# plt.plot(accuracy_check_iterations, allAccuracyTrain, 'bo') # plt.plot(accuracy_check_iterations, allAccuracyTrain, 'b-') # plt.plot(accuracy_check_iterations, allAccuracyTest, 'ro') # plt.plot(accuracy_check_iterations, allAccuracyTest, 'r-') # plt.plot(accuracy_check_iterations, sliding_window_graph, 'g-') # axes = plt.gca() # axes.set_ylim([0, 1000.05]) # plt.title("training (blue), test (red), avg " + str(round(sliding_window_graph[-1], 5)) + " / " + str( # len(sliding_window))) # plt.xlabel('iteration') # plt.ylabel('diff squared') # plt.savefig(os.path.join(output_path, "progress.png")) # Save the model. save_path = saver.save(sess, os.path.join(output_path, "model.ckpt")) config.store('last_tf_model', save_path) copyfile(os.path.join(output_path, "debug.html"), os.path.join(output_path, "debug_best.html")) print("Saved: " + str(cool_score)) # put the print after writing everything so it indicates things have been written. debug_iteration = format(iteration, '^10') debug_loss = format(results_loss, '^16') debug_regularizers = format(results_regularizers, '^16') # Format sliding window debug_sliding_window = Colorize(prev_sliding_window, sliding_window_graph[-1], '^24') prev_sliding_window = sliding_window_graph[-1] # Format steering regression accuracy debug_sqr_acc = Colorize(prev_squared_diff, results_squared_diff, '^28') prev_squared_diff = results_squared_diff
def switch_cams(self): self._switch_cams() self.config['switch_cameras_on_start'] = not self.config.get( 'switch_cameras_on_start', False) config.store(self.config)
def main(): global last_odometer_reset # Init some vars.. telemetry = [] old_steering = 0 old_throttle = 0 old_aux1 = 0 steering = 90 throttle = 90 aux1 = 0 frame_count = 0 session_full_path = '' last_switch = 0 button_arduino_out = 0 currently_running = False override_autonomous_control = False train_on_this_image = True vel = 0.0 last_odo_queue = [0] * (config.odo_delta + 1) last_millis_queue = [0] * (config.odo_delta + 1) # Check for insomnia if platform.system() == "Darwin": check_for_insomnia() # Setup ports. port_in, port_out, imu_port = setup_serial_and_reset_arduinos() # Setup tensorflow sess, net_model = setup_tensorflow() # Start the clock. drive_start_time = time.time() print 'Awaiting switch flip..' if we_are_autonomous: print("Warning, we are intending to drive with tensorflow") session_full_path = make_data_folder('~/training-images') while True: loop_start_time = time.time() # Switch was just flipped. if last_switch != button_arduino_out: last_switch = button_arduino_out # resets stuck_override status last_time_when_nonzero_velocity = -1 stuck_override_start = -1 stuck_override_active = False # See if the car started up with the switch already flipped. # if time.time() - drive_start_time < 1: # print 'Error: start switch in the wrong position.' # sys.exit() if button_arduino_out == 1: currently_running = True print '%s: Switch flipped.' % frame_count last_odometer_reset = odometer_ticks if we_are_recording and (not we_are_autonomous): print 'STARTING TO RECORD.' print 'Folder: %s' % session_full_path config.store('last_record_dir', session_full_path) elif we_are_recording and we_are_autonomous: session_full_path = make_data_folder('~/tf-driving-images') print 'DRIVING AUTONOMOUSLY and STARTING TO RECORD' print 'Folder: %s' % session_full_path else: print("DRIVING AUTONOMOUSLY (not recording).") else: print("%s: Switch flipped. Recording stopped." % frame_count) override_autonomous_control = False currently_running = False # Read input data from arduinos. new_steering, new_throttle, new_aux1, button_arduino_in, button_arduino_out = ( process_input(port_in, port_out)) if new_steering != None: steering = new_steering if new_throttle != None: throttle = new_throttle if new_aux1 != None: aux1 = new_aux1 #telemetry = process_imu(imu_port) # print("Throttle: {}".format(new_throttle)) # Check to see if we should stop the car via the RC during TF control. # But also provide a way to re-engage autonomous control after an override. if we_are_autonomous and currently_running: if (steering > 130 or steering < 50) and throttle > 130: if not override_autonomous_control: print '%s: Detected RC override: stopping.' % frame_count override_autonomous_control = True if abs(aux1 - old_aux1) > 400 and override_autonomous_control: old_aux1 = aux1 print '%s: Detected RC input: re-engaging autonomous control.' % frame_count center_esc(port_out) override_autonomous_control = False # Check to see if we should reset the odometer via aux1 during manual # driving. This is Button E on the RC transmitter. # The values will swing from ~1100 to ~1900. if abs(aux1 - old_aux1) > 400: old_aux1 = aux1 print '%s: Resetting the odometer.' % frame_count last_odometer_reset = odometer_ticks # Overwrite steering with neural net output in autonomous mode. # This seems to take about 10ms. if we_are_autonomous and currently_running: # Calculate velocity from odometer. Gets weird when stopped. last_odo = last_odo_queue[-config.odo_delta] last_millis = last_millis_queue[-config.odo_delta] vel = 0 if odometer_ticks != last_odo and milliseconds > last_millis: if milliseconds - last_millis == 0: vel = 0 else: vel = (float(odometer_ticks) - last_odo) / (milliseconds - last_millis) if last_millis == 0 and last_odo == 0: vel = 0 if last_odo >= odometer_ticks: vel = 0 # append to circular queue last_odo_queue.append(odometer_ticks) last_millis_queue.append(milliseconds) if len(last_odo_queue) > config.odo_delta: last_odo_queue = last_odo_queue[1:] if len(last_millis_queue) > config.odo_delta: last_millis_queue = last_millis_queue[1:] # Read a frame from the camera. frame = camera_stream.read() steering, throttle = do_tensorflow(sess, net_model, frame, odometer_ticks - last_odometer_reset, vel) if ((frame_count % 25) == 0) and (vel != 0): # Simulate dropped radio frames from rc #throttle = 0 pass # Stuck: throttle override # If the car is stuck for more than 2 s, override and full throttle. if config.stuck_override: # TODO config.py global last_time_when_nonzero_velocity, stuck_override_start, stuck_override_active stt = time.time() if vel > 0.0: last_time_when_nonzero_velocity = stt #print throttle,stuck_override_active,stt - stuck_override_start,stt - last_time_when_nonzero_velocity if stuck_override_active: throttle = 115 if stt - stuck_override_start > 1.0: # How long to force throttle for: 1.0s stuck_override_active = False else: if stt - last_time_when_nonzero_velocity > 1.0: # this is true at the beginning. stuck_override_active = True stuck_override_start = stt last_time_when_nonzero_velocity = stt # steering, throttle = do_tensor_flow(frame, odometer_ticks - last_odometer_reset, vel) global override_random_error if we_are_recording and currently_running and config.use_override_random_error: steering = override_random_error.tick(loop_start_time, steering) if we_are_recording and currently_running and override_random_error.state != 'override': # TODO(matt): also record vel in filename for tf? # Read a frame from the camera. frame = camera_stream.read() # Save image with car data in filename. cv2.imwrite("%s/" % session_full_path + "frame_" + str(frame_count).zfill(5) + "_thr_" + str(throttle) + "_ste_" + str(steering) + "_mil_" + str(milliseconds) + "_odo_" + str(odometer_ticks - last_odometer_reset).zfill(5) + ".png", frame) else: frame = camera_stream.read() cv2.imwrite('/tmp/test.png', frame) #if telemetry is not None: # frames = [str(frame_count).zfill(5)] # telemetry = frames + telemetry + [throttle, steering] # data_logger.log_data(telemetry) if override_autonomous_control: # Full brake and neutral steering. throttle, steering = 0, 90 #print("Sending kill command to car") stop_car(steering, throttle, port_out) else: # Send output data to arduinos. process_output(old_steering, old_throttle, steering, throttle, port_out) old_steering = steering old_throttle = throttle # Attempt to go at 30 fps. In reality, we could go slower if something hiccups. seconds = time.time() - loop_start_time while seconds < 1 / 30.: time.sleep(0.001) seconds = time.time() - loop_start_time frame_count += 1
def set_brightness(self, value): value = min(max(value, 0), 8) config.brightness = value self.update_brightness() config.store()
def main(): # Init some vars.. telemetry = [] old_steering = 0 old_throttle = 0 old_aux1 = 0 steering = 90 throttle = 90 aux1 = 0 frame_count = 0 session_full_path = '' last_switch = 0 # set to 1 for auto-switch button_arduino_out = 0 currently_running = False override_autonomous_control = False train_on_this_image = True vel = 0.0 # Check for insomnia #if platform.system() == "Darwin": # check_for_insomnia() # Setup ports. port_in, port_out, imu_port = setup_serial_and_reset_arduinos() # Setup tensorflow sess, net_model = setup_tensorflow() # Start the clock. print 'Awaiting switch flip..' if we_are_autonomous: print("Warning, we are intending to drive with tensorflow") session_full_path = make_data_folder('./training-images') # This block is copied from below, and is a temporary hack to make recording auto-start currently_running = True print '%s: Switch flipped.' % frame_count if we_are_recording and (not we_are_autonomous): print 'STARTING TO RECORD.' print 'Folder: %s' % session_full_path config.store('last_record_dir', session_full_path) elif we_are_recording and we_are_autonomous: session_full_path = make_data_folder('~/tf-driving-images') print 'DRIVING AUTONOMOUSLY and STARTING TO RECORD' print 'Folder: %s' % session_full_path else: print("DRIVING AUTONOMOUSLY (not recording).") # Endhack while True: loop_start_time = time.time() # Switch was just flipped. if last_switch != button_arduino_out: last_switch = button_arduino_out if button_arduino_out == 1: currently_running = True print '%s: Switch flipped.' % frame_count if we_are_recording and (not we_are_autonomous): print 'STARTING TO RECORD.' print 'Folder: %s' % session_full_path config.store('last_record_dir', session_full_path) elif we_are_recording and we_are_autonomous: session_full_path = make_data_folder('~/tf-driving-images') print 'DRIVING AUTONOMOUSLY and STARTING TO RECORD' print 'Folder: %s' % session_full_path else: print("DRIVING AUTONOMOUSLY (not recording).") else: print("%s: Switch flipped. Recording stopped." % frame_count) override_autonomous_control = False currently_running = False # Read input data from arduinos. # new_steering, new_throttle, new_aux1, button_arduino_in, button_arduino_out = ( # process_input(port_in, port_out)) # if new_steering != None: # steering = new_steering # if new_throttle != None: # throttle = new_throttle # if new_aux1 != None: # aux1 = new_aux1 # Check to see if we should stop the car via the RC during TF control. # But also provide a way to re-engage autonomous control after an override. if we_are_autonomous and currently_running: if (steering > 130 or steering < 50) and throttle > 130: if not override_autonomous_control: print '%s: Detected RC override: stopping.' % frame_count override_autonomous_control = True if abs(aux1 - old_aux1) > 400 and override_autonomous_control: old_aux1 = aux1 print '%s: Detected RC input: re-engaging autonomous control.' % frame_count center_esc(port_out) override_autonomous_control = False # Overwrite steering with neural net output in autonomous mode. # This seems to take about 10ms. if we_are_autonomous and currently_running: # Read a frame from the camera. frame = camera_stream.read() # Hard code odo_ticks for pinball purposes odo_ticks = 0 steering, throttle = do_tensorflow(sess, net_model, frame, odo_ticks, vel) if ((frame_count % 25) == 0) and (vel != 0): # Simulate dropped radio frames from rc #throttle = 0 pass if we_are_recording and currently_running: # TODO(matt): also record vel in filename for tf? # Read a frame from the camera. frame = camera_stream.read() # Save image with car data in filename. cv2.imwrite( "%s/" % session_full_path + "frame_" + str(frame_count).zfill(5) + "_thr_" + str(throttle) + "_ste_" + str(steering) + "_mil_" + str(milliseconds) + ".png", frame) else: frame = camera_stream.read() cv2.imwrite('/tmp/test.png', frame) if telemetry is not None: frames = [str(frame_count).zfill(5)] telemetry = frames + telemetry data_logger.log_data(telemetry) if override_autonomous_control: # Full brake and neutral steering. throttle, steering = 0, 90 #print("Sending kill command to car") stop_car(steering, throttle, port_out) else: # Send output data to arduinos. # process_output(old_steering, old_throttle, steering, throttle, port_out) old_steering = steering old_throttle = throttle # Attempt to go at 30 fps. In reality, we could go slower if something hiccups. seconds = time.time() - loop_start_time while seconds < 1 / 30.: time.sleep(0.001) seconds = time.time() - loop_start_time frame_count += 1