コード例 #1
0
    def drive(self,
              ip='127.0.0.1',
              port=50007,
              model='cnn_18.pkl',
              crop_start=126,
              crop_end=226):
        print("Loading model: %s" % model)
        cnn = CNNDriver()
        # Model file trained with gpu need to be remaped on CPU
        if self.__device.type == 'cpu':
            cnn.load_state_dict(torch.load(model, map_location='cpu'))
        else:
            cnn.load_state_dict(torch.load(model))
        cnn.eval()
        cnn = cnn.to(self.__device)

        transformations = transforms.Compose([transforms.ToTensor()])
        comm = game_communication.GameTelemetry(ip, port)
        comm.connect()

        # Run until Crtl-C
        try:
            list_records = []
            degrees = 0
            while True:
                # Sleep for 50ms
                time.sleep(0.05)

                # Get telemetry and image
                telemetry = comm.get_game_data()
                cam_img = comm.get_image()

                # Skip entire record if image is invalid
                if (cam_img is None) or (telemetry is None):
                    continue

                start = time.time()
                # Resize image to the format expected by the model
                cam_img_res = (scipy.misc.imresize(
                    np.array(cam_img)[crop_start:crop_end], [66, 200]))
                torch_tensor = transformations(cam_img_res)
                cam_img_res = torch_tensor.unsqueeze(0)
                cam_img_res = cam_img_res.to(self.__device)

                # Get steering angle from model
                degrees = cnn(cam_img_res)

                # Convert Variable to numpy
                degrees = float(degrees.data.cpu().numpy())
                end = time.time()
                elapsed_seconds = float("%.2f" % (end - start))
                print('Elapsed time:', elapsed_seconds, 'angle:', degrees)

                # Send command to game here...
                commands = [degrees, 0.5]
                comm.send_command(commands)

        except KeyboardInterrupt:
            pass
コード例 #2
0
def game_pilot(ip, port, model_path, gpu, crop_start=126, crop_end=226):

    # Set enviroment variable to set the GPU to use
    if gpu != -1:
        os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu)
    else:
        print('Set tensorflow on CPU')
        os.environ["CUDA_VISIBLE_DEVICES"] = ""

    # Build model and get references to placeholders
    driving_model = model.DrivingModel(training_mode=False)
    model_in = driving_model.input
    model_out = driving_model.output
    model_drop = driving_model.dropout_control
    train_mode = driving_model.train_mode

    # Load tensorflow model
    print("Loading model: %s" % model_path)
    sess = tf.InteractiveSession()
    saver = tf.train.Saver()
    saver.restore(sess, model_path)

    print(ip)
    print(port)

    comm = game_communication.GameTelemetry(ip, port)
    comm.connect()

    # Run until Crtl-C
    try:
        list_records = []
        while True:
            # Get telemetry and image
            telemetry = comm.get_game_data()
            cam_img = comm.get_image()

            # Skip entire record if image is invalid
            if (cam_img is None) or (telemetry is None):
                continue

            # Sleep for 50ms
            time.sleep(0.05)

            # Resize image to the format expected by the model
            cam_img_res = scipy.misc.imresize(np.array(cam_img)[crop_start:crop_end], [66, 200]) / 255.0

            # Get steering angle from tensorflow model (Also convert from rad to degree), dropout 0 at test time.
            degrees = model_out.eval(feed_dict={model_in: [cam_img_res], model_drop: 0.0, train_mode: False})[0][0]
            print(degrees)

            # Send command to game here...
            commands = [degrees, 0.5]
            comm.send_command(commands)

    except KeyboardInterrupt:
        pass
コード例 #3
0
def connect_and_create_dataset(ip, port):
    print(ip)
    print(port)

    comm = game_communication.GameTelemetry(ip, port)
    comm.connect()

    # Run until Crtl-C
    img_index = 0
    try:
        list_records = []
        while True:
            # Get telemetry and image
            telemetry = comm.get_game_data()
            cam_img = comm.get_image()

            # Skip entire record if image is invalid
            if (cam_img is None) or (telemetry is None):
                continue

            # Sleep for 30ms
            time.sleep(0.03)

            img_index += 1

            # Add elements on the list until it's bigger than 20 elements
            if len(list_records) < 20:
                list_records.append(GameRecord(img_index, cam_img, telemetry))
            else:
                # From the main thread add item on the queue
                if not list_game_state.full():
                    #print("Add elements to assync queue")
                    list_game_state.put(list_records)

                list_records = []
                list_records.append(GameRecord(img_index, cam_img, telemetry))

    except KeyboardInterrupt:
        pass
コード例 #4
0
ファイル: drive_game.py プロジェクト: swalpa/LearnPytorch
def game_pilot(ip, port, model_path, gpu, crop_start=126, crop_end=226):
    device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
    # Set enviroment variable to set the GPU to use

    # Load model
    # https://stackoverflow.com/questions/42703500/best-way-to-save-a-trained-model-in-pytroch
    print("Loading model: %s" % model_path)
    cnn = CNNDriver()
    # Model file trained with gpu need to be remaped on CPU
    if device.type == 'cpu':
        cnn.load_state_dict(torch.load(model_path, map_location='cpu'))
    else:
        cnn.load_state_dict(torch.load(model_path))
    cnn.eval()
    cnn = cnn.to(device)

    #transformations = transforms.Compose([
    #transforms.ToTensor(), transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])])
    transformations = transforms.Compose([transforms.ToTensor()])

    print(ip)
    print(port)

    comm = game_communication.GameTelemetry(ip, port)
    comm.connect()

    # Run until Crtl-C
    try:
        list_records = []
        degrees = 0
        while True:
            # Sleep for 50ms
            time.sleep(0.05)

            # Get telemetry and image
            telemetry = comm.get_game_data()
            cam_img = comm.get_image()

            # Skip entire record if image is invalid
            if (cam_img is None) or (telemetry is None):
                continue

            start = time.time()
            # Resize image to the format expected by the model
            cam_img_res = (scipy.misc.imresize(
                np.array(cam_img)[crop_start:crop_end], [66, 200]))
            #cam_img_res = cam_img_res.transpose([2, 0, 1]).astype(np.float32)
            #cam_img_res = np.expand_dims(cam_img_res, axis=0)
            #torch_tensor = torch.from_numpy(cam_img_res).unsqueeze(0)
            #torch_tensor = trfNorm(torch_tensor)
            torch_tensor = transformations(cam_img_res)
            cam_img_res = torch_tensor.unsqueeze(0)
            cam_img_res = cam_img_res.to(device)

            # Get steering angle from model
            degrees = cnn(cam_img_res)

            # Convert Variable to numpy
            degrees = float(degrees.data.cpu().numpy())
            #time.sleep(0.55)
            end = time.time()
            elapsed_seconds = float("%.2f" % (end - start))
            print('Elapsed time:', elapsed_seconds, 'angle:', degrees)

            # Send command to game here...
            commands = [degrees, 0.5]
            comm.send_command(commands)

    except KeyboardInterrupt:
        pass