1. CarENV Class(Environment Setup)
1.1. Purpose: Handles Carla Environment setup and management
1.2. Attributes
1.2.1. SHOW_CAM: Shows camera preview if True.
1.2.2. STEER_AMT: Defines steering amount for discrete actions.
1.2.3. im_width, im_height: image dimensions for camera load
1.2.4. front_camera: Stores camera feed data.
1.3. Methods
1.3.1. __init__(Initialization)
1.3.1.1. Connects to Carla, retrieves blueprints, and initializes the vehicle blueprint.
1.3.2. reset(Environment Reset)
1.3.2.1. Vehicle Setup
1.3.2.1.1. Clears collision history
1.3.2.1.2. Chooses random spawn point and spawns the main vehicle.
1.3.2.2. Sensor Setup
1.3.2.2.1. Adds RGB Camera sensor and sets image dimensions.
1.3.2.2.2. Adds Collision sensor to detect vehicle collisions.
1.3.2.3. Return
1.3.2.3.1. Returns Initial camera view
1.3.2.4. Collision_data
1.3.2.4.1. Appends Collision data to collision_hist.
1.3.2.5. process_img
1.3.2.5.1. Process image data from the camera sensor
1.3.2.6. step
1.3.2.6.1. Action control
1.3.2.6.2. Reward Calculation
1.3.2.6.3. Temination Checks
2. DQN Agent Class
2.1. Attributes
2.1.1. model: Main DQN model
2.1.2. target_model: Target netwoek for stable Q-Learning.
2.1.3. replay_memory: Stores agent experiences for training.
2.1.4. tensorboard: logs training data
2.1.5. target_update_counter: Tracks when to update the target network
2.2. Methods
2.2.1. __init__(Agent Initialization)
2.2.1.1. Load Model: Loads Saved model if available, otherwise creates a new model.
2.2.1.2. Load Replay memory: Loads Saved replay memory if available.
2.2.1.3. Setup Logging: Initializes Tensorboard for logging.
2.2.2. create_model: Constructs the DQN model with Xception as the base.
2.2.2.1. Layers
2.2.2.1.1. Xception feature extractor
2.2.2.1.2. Fully connected output layer with 3 actions (throttle, brake,steer).
2.2.3. update_replay memory: Adds experiences to replay memory
2.2.4. train(Training method)
2.2.4.1. checks if replay memory is large enough for training.
2.2.4.2. Batch Sampling: Samples experiences from replay memory
2.2.4.3. Q-Value Update
2.2.4.3.1. Calculates target Q-values using the target network.
2.2.4.3.2. updates the main model based on the target Q-values
2.2.4.4. Target Network Update: Periodically updates weights of target network
2.2.5. get_qs: Returns Predicted Q-Values for a given state.
2.2.6. train_in_loop: Continously trains the model in a seperate thread.
2.3. replay_memory: Stores agent experiences for training.
3. Initialization & Configuration
3.1. Global settings
3.1.1. Show_Preview: Toggles display of the camera feed.
3.1.2. IM_WIDTH, IM_HEIGHT: SETS image resolution for camera input
3.1.3. SECONDS_PER_EPISODE: Defines the maximum length of each episode
3.1.4. REPLAY_MEMEORY_SIZE: Maximum size of replay memory
3.1.5. MIN_REPLAY_MEMORY_SIZE, MINIBATCH_SIZE: Parameters for memory and training
3.1.6. MODEL_NAME: 'Xception'
3.1.7. EPISODES: Number of traning episodes
3.2. Hyperparameters
3.2.1. Discount: Discount factor for future rewards
3.2.2. epsilon, EPSILON_DECAY, MIN_EPSILON: Controls exploration-exploitation balance.
3.2.3. MEMORY_FRACTION: Sets GPU memory usage limit.
4. ModifiedTensorboard Class
4.1. Purpose: Custom Tensorboard for logging training metrics.
4.2. Attributes
4.2.1. step: Tracks the current step in training
4.2.2. writer: Manages the file writer for Tensorboard
4.3. Methods
4.3.1. update_stats: logs custom metrics.
4.3.2. on_epoch_end: Logs metrics at the end of each epoch
4.3.3. on_train_end: Keeps writer open after training completion.