carla-simulator / carla

Open-source simulator for autonomous driving research.
http://carla.org
MIT License
10.6k stars 3.41k forks source link

Carla UE4 Server crash #7578

Open rowanabdelaleem opened 1 week ago

rowanabdelaleem commented 1 week ago

CARLA version: 0.9.15 Platform/OS: Ubuntu 22.04 Problem you have experienced: I was trying to train an RL agent using stable-baselines3 but this memory error occurs during the training and the server crash Screenshot 2024-05-05 224539 Also during training this error appears: image

What you expected to happen: I expected to train without crashing the server Steps to reproduce: Other information (documentation you consulted, workarounds you tried):

Blyron commented 1 week ago

We need more context. Which map are you using, Which gpu are you using, which NVIDIA Drivers are you using?

Blyron commented 1 week ago

Before the crash it seems you have tried to close server without closing the client which creates issue.

rowanabdelaleem commented 1 week ago

I`m using town 01
GPU Type: NVIDIA GeForce RTX 4070 Ti Driver Version: 550.54.15 and here is the environment I made:

class CarEnv(gym.Env): SHOW_CAM = SHOW_PREVIEW STEER_AMT = 1.0 im_width = WIDTH im_height = HEIGHT front_camera = None CAMERA_POS_Z = 1.3 CAMERA_POS_X = 1.4 PREFERRED_SPEED = 60 SPEED_THRESHOLD = 2

def __init__(self):
    super(CarEnv, self).__init__()

    self.action_space = spaces.Discrete(9)

    self.observation_space = spaces.Box(low=0.0, high=255.0,
                                        shape=(HEIGHT, WIDTH, N_CHANNELS), dtype=np.uint8)

    self.client = carla.Client("localhost", 2000)
    self.client.set_timeout(4.0)
    self.client.load_world('Town01')
    self.world = self.client.get_world()

    self.settings = self.world.get_settings()
    self.settings.no_rendering_mode = True
    #self.settings.synchronous_mode = False
    self.settings.fixed_delta_seconds = FIXED_DELTA_SECONDS
    self.world.apply_settings(self.settings)
    self.blueprint_library = self.world.get_blueprint_library()
    self.model_3 = self.blueprint_library.filter("model3")[0]

def maintain_speed(self,s):

    if s >= self.PREFERRED_SPEED:
        return 0
    elif s < self.PREFERRED_SPEED - self.SPEED_THRESHOLD:
        return 0.7 # think of it as % of "full gas"
    else:
        return 0.3 # tweak this if the car is way over or under preferred speed 

def reset(self, seed=None, options=None):
    self.collision_hist = []
    self.actor_list = []
    self.spawn_points = self.world.get_map().get_spawn_points()
    #print(type(self.spawn_points[2]))
    #print(type(random.choice(self.spawn_points)))

    self.vehicle = self.world.try_spawn_actor(self.model_3, random.choice(self.spawn_points))

    self.actor_list.append(self.vehicle)
    self.initial_location = self.vehicle.get_location()
    self.sem_cam = self.blueprint_library.find('sensor.camera.semantic_segmentation')
    self.sem_cam.set_attribute("image_size_x", f"{self.im_width}")
    self.sem_cam.set_attribute("image_size_y", f"{self.im_height}")
    self.sem_cam.set_attribute("fov", f"90")

    camera_init_trans = carla.Transform(carla.Location(z=self.CAMERA_POS_Z,x=self.CAMERA_POS_X))
    self.sensor = self.world.spawn_actor(self.sem_cam, camera_init_trans, attach_to=self.vehicle)
    self.actor_list.append(self.sensor)
    self.sensor.listen(lambda data: self.process_img(data))

    self.vehicle.apply_control(carla.VehicleControl(throttle=0.0, brake=0.0))
    time.sleep(2) #not detect a collision when the car spawns/falls from sky.
    # showing camera at the spawn point
    if self.SHOW_CAM:
        cv2.namedWindow('Sem Camera',cv2.WINDOW_AUTOSIZE)
        cv2.imshow('Sem Camera', self.front_camera)
        cv2.waitKey(1)
    colsensor = self.blueprint_library.find("sensor.other.collision")
    self.colsensor = self.world.spawn_actor(colsensor, camera_init_trans, attach_to=self.vehicle)
    self.actor_list.append(self.colsensor)
    self.colsensor.listen(lambda event: self.collision_data(event))

    while self.front_camera is None:
        time.sleep(0.01)#Just in case car takes any longer, because we need to be certain the car is done falling from the sky on spawn.

    self.episode_start = time.time()
    #self.steering_lock = False
    #self.steering_lock_start = None # this is to count time in steering lock and start penalising for long time in steering lock
    self.step_counter = 0
    self.vehicle.apply_control(carla.VehicleControl(throttle=0.0, brake=0.0))

    observation = self.front_camera

    return observation.astype(np.uint8), {}

def step(self, action):
    self.step_counter +=1
    steer = action
    # map steering actions
    if steer ==0:
        steer = - 0.9
    elif steer ==1:
        steer = -0.25
    elif steer ==2:
        steer = -0.1
    elif steer ==3:
        steer = -0.05
    elif steer ==4:
        steer = 0.0 
    elif steer ==5:
        steer = 0.05
    elif steer ==6:
        steer = 0.1
    elif steer ==7:
        steer = 0.25
    elif steer ==8:
        steer = 0.9

    v = self.vehicle.get_velocity()
    kmh = int(3.6 * math.sqrt(v.x**2 + v.y**2 + v.z**2))

    estimated_throttle = self.maintain_speed(kmh)

    self.vehicle.apply_control(carla.VehicleControl(throttle=estimated_throttle, steer=steer, brake = 0.0))

    #distance_travelled = self.initial_location.distance(self.vehicle.get_location())

    # storing camera to return at the end in case the clean-up function destroys it
    cam = self.front_camera
    # showing image
    if self.SHOW_CAM:
        cv2.imshow('Sem Camera', cam)
        cv2.waitKey(1)

    # start defining reward from each step
    reward = 0
    done = False
    #punish for collision
    if len(self.collision_hist) != 0:
        done = True
        reward = reward - 300
        self.cleanup()
    #reward for acceleration
    if kmh < 15:
        reward = reward - 3
    elif kmh <25:
        reward = reward - 2
    elif kmh <50:
        reward = reward - 1
    elif kmh>120:
        reward = reward - 10 #punish for going too fast
    else:
        reward = reward + 1

    # check for episode duration
    if self.episode_start + SECONDS_PER_EPISODE < time.time():
        done = True
        self.cleanup()

    observation = cam

    return observation.astype(np.uint8), reward, done, False, {}

def cleanup(self):
    for sensor in self.world.get_actors().filter('*sensor*'):      
        sensor.destroy()
    for actor in self.world.get_actors().filter('*vehicle*'):

        actor.destroy()

    cv2.destroyAllWindows()

def process_img(self, image):
    image.convert(carla.ColorConverter.CityScapesPalette)
    i = np.array(image.raw_data)
    i = i.reshape((self.im_height, self.im_width, 4))[:, :, :3] # this is to ignore the 4th Alpha channel - up to 3
    self.front_camera = i/255.0

def collision_data(self, event):
    self.collision_hist.append(event)
rowanabdelaleem commented 1 week ago

I tried to run ./CarlaUE4.sh -RenderOffScreen and the server didn`t crash, is that right to stop the render screen ?

Blyron commented 1 week ago

Yes, the RenderScreen used for CARLA server is only debug purpouses. Latest NVIDIA drivers are unstable for Vulkan. We recommend what UE recommend for UE4 which is 4XX