Skip to content

Commit

Permalink
Merge branch 'carlasim' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
AlirezaMorsali committed Oct 20, 2022
2 parents e152296 + 83508d2 commit 850d1f8
Show file tree
Hide file tree
Showing 39 changed files with 923 additions and 866 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import pygame
import os
import sys
from dotenv import load_dotenv

load_dotenv()
os.environ["IAI_MOCK_API"] = "0"
os.environ["IAI_DEV"] = "1"
os.environ["IAI_DEV_URL"] = "http://localhost:8888"
if os.environ.get("IAI_DEV", False):
sys.path.append("../../")
sys.path.append("../")
import invertedai as iai
from invertedai import CarlaEnv, CarlaSimulationConfig
from simulators import CarlaEnv, CarlaSimulationConfig
import argparse


parser = argparse.ArgumentParser(description="Simulation Parameters.")
parser.add_argument("-n", "--scene_name", type=str, default="CARLA:Town03:Roundabout")
parser.add_argument("-n", "--scene_name", type=str, default="carla:Town03:Roundabout")
parser.add_argument("-c", "--agent_count", type=int, default=8)
parser.add_argument("-l", "--episode_length", type=int, default=30)
parser.add_argument("-e", "--ego_spawn_point", default="demo")
Expand Down Expand Up @@ -44,27 +45,28 @@
location=args.scene_name,
agent_count=args.agent_count,
)
initial_states = response["states"][0]
initial_states = response.agent_states
sim = CarlaEnv(
cfg=carla_cfg,
initial_states=initial_states,
ego_spawn_point=args.ego_spawn_point,
spectator_transform=args.spectator_transform,
)
states, recurrent_states, dimensions = sim.reset()
clock = pygame.time.Clock()

agent_states, recurrent_states, agent_attributes = sim.reset()
clock = pygame.time.Clock()

for i in range(carla_cfg.episode_length * carla_cfg.fps):

response = iai.drive(
agent_attributes=[dimensions],
states=[states],
recurrent_states=[recurrent_states],
agent_attributes=agent_attributes,
agent_states=agent_states,
recurrent_states=recurrent_states,
location=args.scene_name,
steps=1,
traffic_states_id=response["traffic_states_id"],
)
states, recurrent_states, dimensions = sim.step(npcs=response, ego="autopilot")
agent_states, recurrent_states, agent_attributes = sim.step(
npcs=response, ego="autopilot"
)

clock.tick_busy_loop(carla_cfg.fps)

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import pygame
import os
import sys
import numpy as np
from dotenv import load_dotenv

load_dotenv()
os.environ["IAI_MOCK_API"] = "0"
os.environ["IAI_DEV"] = "1"
os.environ["IAI_DEV_URL"] = "http://localhost:8888"
if os.environ.get("IAI_DEV", False):
sys.path.append("../../")
sys.path.append("../")
import invertedai as iai
from invertedai.simulators import CarlaEnv, CarlaSimulationConfig
from simulators import CarlaEnv, CarlaSimulationConfig
import argparse
from tqdm import tqdm


parser = argparse.ArgumentParser(description="Simulation Parameters.")
parser.add_argument("-n", "--scene_name", type=str, default="CARLA:Town03:Roundabout")
parser.add_argument("-c", "--agent_count", type=int, default=10)
parser.add_argument("-l", "--episode_length", type=int, default=20)
parser.add_argument("-c", "--agent_count", type=int, default=8)
parser.add_argument("-l", "--episode_length", type=int, default=30)
parser.add_argument("-e", "--ego_spawn_point", default="demo")
parser.add_argument("-s", "--spectator_transform", default=None)
parser.add_argument("-i", "--initial_states", default=None)
Expand All @@ -40,38 +41,36 @@
npc_population_interval=args.npc_population_interval,
max_cars_in_map=args.max_cars_in_map,
)

iai.add_apikey("")
response = iai.initialize(
location=args.scene_name,
agent_count=args.agent_count,
)

initial_states = response["states"][0]
initial_states = response.agent_states
sim = CarlaEnv(
cfg=carla_cfg,
initial_states=initial_states,
ego_spawn_point=args.ego_spawn_point,
spectator_transform=args.spectator_transform,
)
clock = pygame.time.Clock()

for episode in range(args.episodes):
states, recurrent_states, dimensions = sim.reset()
for i in range(carla_cfg.episode_length * carla_cfg.fps):

for _ in tqdm(range(args.episodes), position=0):
agent_states, recurrent_states, agent_attributes = sim.reset()
clock = pygame.time.Clock()
for i in tqdm(
range(carla_cfg.episode_length * carla_cfg.fps), position=0, leave=False
):
response = iai.drive(
agent_attributes=[dimensions],
states=[states],
recurrent_states=[recurrent_states],
agent_attributes=agent_attributes,
agent_states=agent_states,
recurrent_states=recurrent_states,
location=args.scene_name,
steps=1,
traffic_states_id=response["traffic_states_id"],
get_infractions=True,
)
print(
f"Collision rate: {100*np.array(response['collision'])[-1, 0, :].mean():.2f}% | "
+ f"Off-road rate: {100*np.array(response['offroad'])[-1, 0, :].mean():.2f}% | "
+ f"Wrong-way rate: {100*np.array(response['wrong_way'])[-1, 0, :].mean():.2f}%"
agent_states, recurrent_states, agent_attributes = sim.step(
npcs=response, ego="autopilot"
)
states, recurrent_states, dimensions = sim.step(npcs=response, ego="autopilot")

clock.tick_busy_loop(carla_cfg.fps)

Expand Down
File renamed without changes
10 changes: 10 additions & 0 deletions Carla_examples/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
carla==0.9.13
jupyterlab==3.4.4
opencv-python==4.6.0.66
ipympl==0.9.2
ipywidgets==8.0.1
matplotlib==3.5.3
imageio==2.20.0
tqdm==4.64.0
ipython==8.4.0
pygame==2.1.2
2 changes: 1 addition & 1 deletion docs/source/pythonapi/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ accessed directly. Below are the key functions of the library, along with some c
```{toctree}
:maxdepth: 1
sdk-simulation
sdk-drive
sdk-initialize
sdk-location-info
sdk-simulation
sdk-common
```

Expand Down
8 changes: 1 addition & 7 deletions docs/source/pythonapi/sdk-common.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@


```{eval-rst}
.. automodule:: invertedai.models
.. automodule:: invertedai.common
:members:
:undoc-members:
:show-inheritance:
```

<!-- .. autoclass:: invertedai.api_resources.TestReturn -->



4 changes: 3 additions & 1 deletion docs/source/pythonapi/sdk-drive.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@


```{eval-rst}
.. autofunction:: invertedai.api_resources.drive
.. automodule:: invertedai.api.drive
:members:
:undoc-members:
```
4 changes: 3 additions & 1 deletion docs/source/pythonapi/sdk-initialize.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@


```{eval-rst}
.. autofunction:: invertedai.api_resources.initialize
.. automodule:: invertedai.api.initialize
:members:
:undoc-members:
```


4 changes: 3 additions & 1 deletion docs/source/pythonapi/sdk-location-info.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@


```{eval-rst}
.. autofunction:: invertedai.api_resources.location_info
.. automodule:: invertedai.api.location
:members:
:undoc-members:
```
6 changes: 4 additions & 2 deletions docs/source/pythonapi/sdk-simulation.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# SIMULATION
# Co-simulation


```{eval-rst}
.. autoclass:: invertedai.simulation.Simulation
.. autoclass:: invertedai.cosimulation.BasicCosimulation
:members:
```

17 changes: 11 additions & 6 deletions docs/source/userguide.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@ specific pages for {ref}`Python SDK`, {ref}`REST API`, {ref}`Getting started`, a

We follow the continuous space, discrete time approach used in most driving simulators. In the current version, the API
only supports the time step of 100 ms, corresponding to 10 frames per second, and expects to run in a synchronous
fashion. In most circumstances, the latency is less than ???, but the API should not be relied upon to perform real-time
simulation. The underlying technology is based on [ITRA]() and was optimized to handle simulations of up to 30 seconds (
300 time steps) with up to 100 agents, contained within an area of roughly 300 meters in diameter.
fashion. The latency of API calls varies with physical location of the client server and its network configuration,
but generally the API should not be relied upon to provide real-time simulation. For optimal resource utilization,
we recommend that you run multiple simulations in parallel, so that one can execute when another is waiting for the
API reply. The technology underlying the API is based on [ITRA](https://arxiv.org/abs/2104.11212) and was optimized to
handle simulations of up to
20 seconds (200 time steps) contained within an area of roughly 300 meters in diameter. The API backend has been
provisioned to accommodate a large number of agents, where the maximum allowed varies per location.

## Programming language support
The core interface is a {ref}`REST API`, that can be called from any programming language. This is a low-level, bare-bones
access mode that offers maximum flexibility to deploy in any environment.
The core interface is a {ref}`REST API`, that can be called from any programming language. This is a low-level,
bare-bones access mode that offers maximum flexibility to deploy in any environment.
For convenience, we also provide a {ref}`Python SDK`, freely available on PyPI with minimal dependencies, which
provides an abstraction layer on top of the REST API. In the future we intend to release a similar library in C++ and
potentially other languages.

## Maps and geofencing
The API operates on a pre-defined collection of maps and currently there is no programmatic way to add additional
locations. For each location there is a map, represented internally in the [Lanelet2](https://github.com/fzi-forschungszentrum-informatik/Lanelet2) format, which specifies
locations. For each location there is a map, represented internally in the
[Lanelet2](https://github.com/fzi-forschungszentrum-informatik/Lanelet2) format, which specifies
lanelets, traffic lights, and a selection of static traffic signs (along with their relationship to specific lanelets).
Each map comes with a canonical Euclidean coordinate frame in meters, which for OSM files is obtained by applying a
specific UTM projector defined by lat/lon, and everything sent across the API is always specified in terms of this
Expand Down
68 changes: 0 additions & 68 deletions examples/Carla_Examples/Carla-Demo-Script-Birdview.py

This file was deleted.

25 changes: 13 additions & 12 deletions examples/Colab-Demo.ipynb

Large diffs are not rendered by default.

25 changes: 6 additions & 19 deletions examples/Demo_Drive.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
#!/usr/bin/env ipython
import os
import sys
from PIL import Image as PImage
import imageio
import numpy as np
import cv2
from tqdm import tqdm
import argparse

os.environ["IAI_MOCK_API"] = "0"
os.environ["IAI_DEV"] = "1"
# os.environ["IAI_DEV_URL"] = "http://localhost:8888"

if os.environ.get("IAI_DEV", False):
sys.path.append("../")
import invertedai as iai

# logger.setLevel(10)

parser = argparse.ArgumentParser(description="Simulation Parameters.")
parser.add_argument("--api_key", type=str, default="")
parser.add_argument("--location", type=str, default="iai:ubc_roundabout")
parser.add_argument("--location", type=str, default="canada:vancouver:ubc_roundabout")
args = parser.parse_args()

iai.add_apikey(args.api_key)
Expand All @@ -31,18 +21,16 @@
file_name = args.location.replace(":", "_")
if response.osm_map is not None:
file_path = f"{file_name}.osm"
with open(file_path, "w") as f:
f.write(response.osm_map[0])
response.osm_map.save_osm_file(file_path)
if response.birdview_image is not None:
file_path = f"{file_name}.jpg"
rendered_map = np.array(response.birdview_image, dtype=np.uint8)
image = cv2.imdecode(rendered_map, cv2.IMREAD_COLOR)
cv2.imwrite(file_path, image)
simulation = iai.Simulation(
response.birdview_image.decode_and_save(file_path)
simulation = iai.BasicCosimulation(
location=args.location,
agent_count=10,
monitor_infractions=True,
render_birdview=True,
ego_agent_mask=[False] * 10,
)
frames = []
pbar = tqdm(range(50))
Expand All @@ -55,8 +43,7 @@
+ f"Wrong-way rate: {100*np.array(wrong_way).mean():.2f}%"
)

birdview = np.array(simulation.birdview, dtype=np.uint8)
image = cv2.imdecode(birdview, cv2.IMREAD_COLOR)
image = simulation.birdview.decode()
frames.append(image)
im = PImage.fromarray(image)
imageio.mimsave("iai-drive.gif", np.array(frames), format="GIF-PIL")

0 comments on commit 850d1f8

Please sign in to comment.