def train(tubs, new_model_path, base_model_path=None, train_split=.08, batch_size=64): """ use the specified data in tub_names to train an artifical neural network saves the output trained model as model_name """ X_keys = ['cam/image_array'] y_keys = ['user/angle', 'user/throttle'] new_model_path = os.path.expanduser(new_model_path) kl = KerasLinear() if base_model_path is not None: base_model_path = os.path.expanduser(base_model_path) kl.load(base_model_path) print('tubs') tubgroup = TubGroup(tubs) train_gen, val_gen = tubgroup.get_train_val_gen(X_keys, y_keys, batch_size=batch_size, train_frac=train_split) total_records = len(tubgroup.df) total_train = int(total_records * train_split) total_val = total_records - total_train print('train: %d, validation: %d' % (total_train, total_val)) steps_per_epoch = total_train // batch_size print('steps_per_epoch', steps_per_epoch) kl.train(train_gen, val_gen, saved_model_path=new_model_path, steps=steps_per_epoch, train_split=train_split)
def train(cfg, tub_names, new_model_path, base_model_path=None): """ use the specified data in tub_names to train an artifical neural network saves the output trained model as model_name """ X_keys = ['cam/image_array'] y_keys = ['user/angle', 'user/throttle'] new_model_path = os.path.expanduser(new_model_path) kl = KerasLinear() if base_model_path is not None: base_model_path = os.path.expanduser(base_model_path) kl.load(base_model_path) print('tub_names', tub_names) if not tub_names: tub_names = os.path.join(cfg.DATA_PATH, '*') tubgroup = TubGroup(tub_names) train_gen, val_gen = tubgroup.get_train_val_gen( X_keys, y_keys, batch_size=cfg.BATCH_SIZE, train_frac=cfg.TRAIN_TEST_SPLIT) total_records = len(tubgroup.df) total_train = int(total_records * cfg.TRAIN_TEST_SPLIT) total_val = total_records - total_train print('train: %d, validation: %d' % (total_train, total_val)) steps_per_epoch = total_train // cfg.BATCH_SIZE print('steps_per_epoch', steps_per_epoch) kl.train(train_gen, val_gen, saved_model_path=new_model_path, steps=steps_per_epoch, train_split=cfg.TRAIN_TEST_SPLIT)
def drive(cfg, model_path=None, use_chaos=False): """ """ V = dk.vehicle.Vehicle() clock = Timestamp() V.add(clock, outputs=['timestamp']) cam = PiCamera(resolution=cfg.CAMERA_RESOLUTION) V.add(cam, outputs=['cam/image_array'], threaded=True) ctr = LocalWebController(use_chaos=use_chaos) V.add(ctr, inputs=['cam/image_array'], outputs=['user/angle', 'user/throttle', 'user/mode', 'recording'], threaded=True) # See if we should even run the pilot module. # This is only needed because the part run_condition only accepts boolean pilot_condition_part = MakeRunConditionBoolean() V.add(pilot_condition_part, inputs=['user/mode'], outputs=['run_pilot']) # Run the pilot if the mode is not user. kl = KerasLinear() if model_path: kl.load(model_path) V.add(kl, inputs=['cam/image_array'], outputs=['pilot/angle', 'pilot/throttle'], run_condition='run_pilot') state_controller = StateController() V.add(state_controller, inputs=[ 'user/mode', 'user/angle', 'user/throttle', 'pilot/angle', 'pilot/throttle' ], outputs=['angle', 'throttle']) steering_controller = PCA9685(cfg.STEERING_CHANNEL) steering = PWMSteering(controller=steering_controller, left_pulse=cfg.STEERING_LEFT_PWM, right_pulse=cfg.STEERING_RIGHT_PWM) throttle_controller = PCA9685(cfg.THROTTLE_CHANNEL) throttle = PWMThrottle(controller=throttle_controller, max_pulse=cfg.THROTTLE_FORWARD_PWM, zero_pulse=cfg.THROTTLE_STOPPED_PWM, min_pulse=cfg.THROTTLE_REVERSE_PWM) V.add(steering, inputs=['angle']) V.add(throttle, inputs=['throttle']) # add tub to save data inputs = [ 'cam/image_array', 'user/angle', 'user/throttle', 'user/mode', 'timestamp' ] types = ['image_array', 'float', 'float', 'str', 'str'] # single tub tub = TubWriter(path=cfg.TUB_PATH, inputs=inputs, types=types) V.add(tub, inputs=inputs, run_condition='recording') # run the vehicle V.start(rate_hz=cfg.DRIVE_LOOP_HZ)
def drive(cfg, model_path=None, use_chaos=False): """ """ V = dk.vehicle.Vehicle() clock = Timestamp() V.add(clock, outputs=['timestamp']) cam = PiCamera(resolution=cfg.CAMERA_RESOLUTION) V.add(cam, outputs=['cam/image_array'], threaded=True) """ ctr = LocalWebController(use_chaos=use_chaos) V.add(ctr, inputs=['cam/image_array'], outputs=['user/angle', 'user/throttle', 'user/mode', 'recording'], threaded=True) """ # then replace your current controller with... ctl = BluetoothGameController() V.add(ctl, inputs=['cam/image_array'], outputs=['user/angle', 'user/throttle', 'user/mode', 'recording'], threaded=True) # See if we should even run the pilot module. # This is only needed because the part run_condition only accepts boolean pilot_condition_part = MakeRunConditionBoolean() V.add(pilot_condition_part, inputs=['user/mode'], outputs=['run_pilot']) # Run the pilot if the mode is not user. kl = KerasLinear() if model_path: kl.load(model_path) V.add(kl, inputs=['cam/image_array'], outputs=['pilot/angle', 'pilot/throttle'], run_condition='run_pilot') state_controller = StateController() V.add(state_controller, inputs=[ 'user/mode', 'user/angle', 'user/throttle', 'pilot/angle', 'pilot/throttle' ], outputs=['angle', 'throttle']) sombrero = Sombrero(steering_channel=cfg.STEERING_CHANNEL, steering_left_pwm=cfg.STEERING_LEFT_PWM, steering_right_pwm=cfg.STEERING_RIGHT_PWM, throttle_channel=cfg.THROTTLE_CHANNEL, throttle_forward_pwm=cfg.THROTTLE_FORWARD_PWM, throttle_stop_pwm=cfg.THROTTLE_STOPPED_PWM, throttle_reverse_pwm=cfg.THROTTLE_REVERSE_PWM) V.add(sombrero, inputs=['angle', 'throttle']) # add tub to save data inputs = [ 'cam/image_array', 'user/angle', 'user/throttle', 'user/mode', 'timestamp' ] types = ['image_array', 'float', 'float', 'str', 'str'] # single tub tub = TubWriter(path=cfg.TUB_PATH, inputs=inputs, types=types) V.add(tub, inputs=inputs, run_condition='recording') # run the vehicle V.start(rate_hz=cfg.DRIVE_LOOP_HZ, max_loop_count=cfg.MAX_LOOPS)
def test_linear(): kl = KerasLinear() assert kl.model is not None
def test_linear_with_model(): kc = KerasLinear(default_linear()) assert kc.model is not None