def __init__(self, parent):
        self.states = {
            "model_ready": False,
            "model_source": None,
            "layers_changed": False,
            "training_data_ready": False,
            "training_data_files": [],
            "test_data_ready": False,
            "test_data_files": [],
            "settings_locked": False,
        }

        self.gui_handle = parent
        self.status_widgets = []
        self.model_data = {"loaded": False}
        self.keras_handle = kp.MachineLearning()
示例#2
0
    def trigger_support_model(self, data, feature_map, trigger_options):
        threshold = trigger_options["auto_threshold"]

        if not self.trigger_variables.get("initialized", False):
            if not hasattr(self, 'keras_process'):
                self.kp = keras_p.MachineLearning()
            else:
                self.kp.clear_model()
            trigger_model, t_message = self.kp.load_model("model_trigger.npy")

            if not trigger_model["loaded"]:
                print(t_message)
                return True, None, t_message
            else:
                print("model loaded")

            config = trigger_model["sensor_config"]
            feature_list = trigger_model["feature_list"]
            frame_settings = trigger_model["frame_settings"]
            if frame_settings["collection_mode"] == "support_model":
                print("Not supported, changing to continuous")
                frame_settings["collection_mode"] = "continuous"
                frame_settings["rolling"] = True

            self.trigger_process = feat_p.FeatureProcessing(config)
            self.trigger_process.set_feature_list(feature_list)
            self.trigger_process.set_frame_settings(frame_settings)

            self.trigger_variables["initialized"] = True

        ml_frame_data = self.trigger_process.feature_extraction(data)
        feature_map = ml_frame_data["current_frame"]["feature_map"]
        complete = ml_frame_data["current_frame"]["frame_complete"]

        score = 0
        detected = False
        if complete and feature_map is not None:
            self.kp.clear_session()
            trigger = self.kp.predict(feature_map)[0]
            self.kp.clear_session()
            score = trigger["label_predictions"]["centered"][0]
            if score > threshold:
                detected = True

        return detected, score, ""
示例#3
0
def main():
    parser = example_utils.ExampleArgumentParser(num_sens=2)
    add_args(parser)
    args = parser.parse_args()

    if args.model_file_name:
        filename = args.model_file_name
    else:
        print("Not implemented!")
        sys.exit(1)

    keras_proc = kp.MachineLearning()
    model_data = keras_proc.load_model(filename)

    print(model_data["message"])

    if not model_data["loaded"]:
        return False

    config = model_data["sensor_config"]
    feature_list = model_data["feature_list"]
    frame_settings = model_data["frame_settings"]

    feature_process = feature_proc.FeatureProcessing(config)
    feature_process.set_feature_list(feature_list)
    feature_process.set_frame_settings(frame_settings)

    example_utils.config_logging(args)

    if args.socket_addr:
        client = SocketClient(args.socket_addr)
    elif args.spi:
        client = SPIClient()
    else:
        port = args.serial_port or example_utils.autodetect_serial_port()
        client = UARTClient(port)

    info = client.setup_session(config)

    interrupt_handler = example_utils.ExampleInterruptHandler()
    print("Press Ctrl-C to end session")

    client.start_streaming()

    init = True
    while not interrupt_handler.got_signal:
        info, sweep = client.get_next()
        if init:
            init = False
            num_sensors, data_len = sweep.shape
            start_x = config.range_interval[0]
            stop_x = config.range_interval[1]
            x_mm = np.linspace(start_x, stop_x, data_len) * 1000
        data = {
            "x_mm": x_mm,
            "iq_data": sweep,
            "env_ampl": abs(sweep),
            "sensor_config": config,
            "num_sensors": num_sensors,
        }

        ml_frame_data = feature_process.feature_extraction(data)
        feature_map = ml_frame_data["current_frame"]["feature_map"]
        complete = ml_frame_data["current_frame"]["frame_complete"]

        if complete and feature_map is not None:
            predict = keras_proc.predict(feature_map)[0]
            prediction_label = predict["prediction"]
            print(prediction_label)

    print("Disconnecting...")
    client.disconnect()
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-s",
                        "--load-train-set",
                        dest="server",
                        help="IP of streaming server",
                        default="127.0.0.1")
    args = parser.parse_args()

    filename = "model_data_speaker_sparse.npy"

    keras_proc = kp.MachineLearning()
    model_data, message = keras_proc.load_model(filename)

    print(message, "\n")

    if not model_data["loaded"]:
        print("Failed to load model!")
        sys.exit(1)

    try:
        client = SocketClient(args.server)
    except Exception:
        print("Failed to connect to server at {}!\n".format(args.server))
        traceback.print_exc()
        sys.exit(1)

    conf_speaker = model_data["sensor_config"]
    conf_speaker.sensor = SENSOR_SPEAKER
    feature_list = model_data["feature_list"]
    feature_list[0]["sensors"] = [SENSOR_SPEAKER[0]]
    feature_list[1]["sensors"] = [SENSOR_SPEAKER[1]]
    frame_settings = model_data["frame_settings"]

    frame_settings["auto_threshold"] = 1.5
    frame_settings["dead_time"] = 30
    frame_settings["auto_offset"] = 15

    frame_settings["collection_mode"] = "auto_feature_based"

    feature_process = feature_proc.FeatureProcessing(conf_speaker)
    feature_process.set_feature_list(feature_list)
    feature_process.set_frame_settings(frame_settings)

    handles = init_demo()
    handles["feature_process"] = feature_process
    handles["keras_proc"] = keras_proc

    # get session config for speaker mode
    info_speaker = client.setup_session(conf_speaker)
    handles["dist_processors"], handles[
        "dist_tags"] = setup_distance_detectors(conf_speaker, info_speaker,
                                                SENSOR_SPEAKER)

    try:
        client.start_session()
        client.stop_session()
    except Exception:
        print("Failed to start session!")
        traceback.print_exc()
        sys.exit(1)

    demo_mode = "speaker"
    if USE_PRESENCE:
        # get session config for presence mode
        demo_mode = "presence"
        conf_presence = presence_detection.get_sensor_config()
        conf_presence.sensor = SENSOR_PRESENCE
        conf_presence.range_interval = PRESENCE_RANGE
        info_presence = client.setup_session(conf_presence)
        handles["presence_processor"] = setup_presence_detector(
            conf_presence, info_presence)

    if USE_PRESENCE:
        start_mode = "presence"
    else:
        start_mode = "speaker"
    print("Starting demo in {}-mode!".format(start_mode))

    interrupt_handler = utils.ExampleInterruptHandler()
    print("Press Ctrl-C to end demo")

    client.start_session()

    while not interrupt_handler.got_signal:
        try:
            info, sweep = client.get_next()

            if demo_mode == "presence":
                new_mode = do_presence_mode(info, sweep, handles)
            else:
                data = {
                    "sweep_data": sweep,
                    "sensor_config": conf_speaker,
                    "session_info": info_speaker,
                }
                new_mode = do_speaker_mode(info, data, handles)

            # switch between presence and speaker mode
            if new_mode != demo_mode:
                demo_mode = new_mode
                handles["led_handle"].double_flash("#000000")
                time.sleep(1)
                client.stop_session()

                if demo_mode == "presence":
                    print("Switching to presence mode!\n")
                    handles["led_handle"].double_flash("#000000")
                    time.sleep(1)
                    handles["idle_counts"] = 0
                    info_presence = client.setup_session(conf_presence)
                    handles["presence_processor"] = setup_presence_detector(
                        conf_presence, info_presence)
                    if handles["play_mode"] == "play":
                        color = "#00ff00"
                    else:
                        color = "#ff0000"
                    handles["led_handle"].set_color(color,
                                                    pos=[3, 4],
                                                    brightness=0.1)
                else:
                    print("Switching to speaker mode!\n")
                    info_speaker = client.setup_session(conf_speaker)
                    handles["led_handle"].set_color("#00ff00",
                                                    pos=[3, 4],
                                                    brightness=0.1)

                client.start_session()
        except Exception:
            traceback.print_exc()
            break

    print("Disconnecting...")
    if handles["play_mode"] == "play":
        handles["lms_handle"].queue.put("PAUSE")
    handles["lms_handle"].stop()
    handles["led_handle"].off()
    handles["led_handle"].exit()
    client.disconnect()