Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions A1-Mousavi-403611252/A1-Mousavi-403611252.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import carla
import random
import time
import os

output_folder = "A1_sensor_data"
os.makedirs(output_folder, exist_ok=True)

client = carla.Client('localhost', 2000)
client.set_timeout(5.0)
world = client.get_world()
blueprint_library = world.get_blueprint_library()

vehicle_bp = random.choice(blueprint_library.filter('vehicle.*'))
spawn_points = world.get_map().get_spawn_points()
spawn_point = random.choice(spawn_points)
vehicle = world.spawn_actor(vehicle_bp, spawn_point)
print("Spawned Vehicle ID:", vehicle.id)
print("Vehicle spawned:", vehicle.type_id)

obstacle_bp = blueprint_library.find('static.prop.streetbarrier') # Example prop
obstacle_transform = carla.Transform(
spawn_point.location + carla.Location(x=5),
spawn_point.rotation
)
obstacle = world.spawn_actor(obstacle_bp, obstacle_transform)
print("Obstacle spawned in front of the vehicle.")



lidar_bp = blueprint_library.find('sensor.lidar.ray_cast')
lidar_bp.set_attribute('channels', '32')
lidar_bp.set_attribute('range', '50')
lidar_sensor = world.spawn_actor(lidar_bp, carla.Transform(), attach_to=vehicle)

def process_lidar_data(point_cloud):
lidar_file = os.path.join(output_folder, "lidar_output.txt")
with open(lidar_file, "a") as file:
file.write(str(point_cloud) + "\n")

lidar_sensor.listen(lambda data: process_lidar_data(data))

radar_bp = blueprint_library.find('sensor.other.radar')
radar_bp.set_attribute('horizontal_fov', '35')
radar_bp.set_attribute('vertical_fov', '20')

radar_sensor = world.spawn_actor(radar_bp, carla.Transform(), attach_to=vehicle)

def process_radar_data(data):
radar_file = os.path.join(output_folder, "radar_output.txt")
with open(radar_file, "a") as file:
file.write(str(data) + "\n")

radar_sensor.listen(lambda data: process_radar_data(data))

collision_bp = blueprint_library.find('sensor.other.collision')

collision_sensor = world.spawn_actor(collision_bp, carla.Transform(), attach_to=vehicle)

def on_collision(event):
collision_file = os.path.join(output_folder, "collision_log.txt")
with open(collision_file, "a") as file:
file.write("Collision detected: " + str(event) + "\n")

collision_sensor.listen(lambda event: on_collision(event))


vehicle.apply_control(carla.VehicleControl(throttle=0.5))
time.sleep(10)
vehicle.apply_control(carla.VehicleControl(throttle=0.0, brake=1.0))

675 changes: 675 additions & 0 deletions A1-Mousavi-403611252/A1_sensor_data/collision_log.txt

Large diffs are not rendered by default.

791 changes: 791 additions & 0 deletions A1-Mousavi-403611252/A1_sensor_data/lidar_output.txt

Large diffs are not rendered by default.

789 changes: 789 additions & 0 deletions A1-Mousavi-403611252/A1_sensor_data/radar_output.txt

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions A1-Mousavi-403611252/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Overview
This assignment involves integrating LiDAR, RADAR, and collision sensors into a CARLA vehicle and saving their output locally. A static obstacle is placed in the car’s path to test sensor readings and collision detection.

# Components Added
LiDAR Sensor: Captures point cloud data.

RADAR Sensor: Detects objects within a certain field of view.

Collision Sensor: Logs collision events during vehicle motion.

# Procedure
Vehicle Initialization: A random vehicle is spawned at a random spawn point in the map.

Obstacle Placement: A static object (street barrier) is placed 5 meters in front of the vehicle.

# Sensor Attachment:

LiDAR with 32 channels and 50m range.

RADAR with horizontal FOV 35° and vertical FOV 20°.

Collision sensor listens for any crash during movement.

Vehicle Motion: The vehicle moves forward for 10 seconds and then stops.

# Output
Sensor outputs are saved in the A1_sensor_data/ folder:

lidar_output.txt: LiDAR point cloud data

radar_output.txt: RADAR object detection readings

collision_log.txt: Any collision event logs
101 changes: 101 additions & 0 deletions A2-Mousavi-403611252/A2-Mousavi-403611252-CNN.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import torch
import torchvision.transforms as transforms
from torchvision.datasets import ImageFolder
from torch.utils.data import DataLoader
import torch.nn as nn
import torch.optim as optim

from sklearn.metrics import confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

transform = transforms.Compose([
transforms.Resize((128, 128)),
transforms.ToTensor()
])

dataset = ImageFolder(root="carla_dataset_a2", transform=transform)
train_size = int(0.8 * len(dataset))
test_size = len(dataset) - train_size
train_dataset, test_dataset = torch.utils.data.random_split(dataset, [train_size, test_size])
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)

import torch.nn as nn
import torch.optim as optim

class WeatherCNN(nn.Module):
def __init__(self):
super(WeatherCNN, self).__init__()
self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1)
self.conv2 = nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1)
self.conv3 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
self.fc1 = nn.Linear(64 * 16 * 16, 128)
self.fc2 = nn.Linear(128, 4)
self.pool = nn.MaxPool2d(2, 2)
self.relu = nn.ReLU()

def forward(self, x):
x = self.pool(self.relu(self.conv1(x)))
x = self.pool(self.relu(self.conv2(x)))
x = self.pool(self.relu(self.conv3(x)))
x = x.view(x.size(0), -1)
x = self.relu(self.fc1(x))
x = self.fc2(x)
return x

num_classes = len(dataset.classes)
model = WeatherCNN()

criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

num_epochs = 10
for epoch in range(num_epochs):
running_loss = 0.0
for images, labels in train_loader:
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()

print(f"Epoch {epoch+1}, Loss: {running_loss/len(train_loader)}")

correct = 0
total = 0
with torch.no_grad():
for images, labels in test_loader:
outputs = model(images)
_, predicted = torch.max(outputs, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()

print(f"Test Accuracy: {100 * correct / total:.2f}%")

torch.save(model.state_dict(), "weather_cnn_model.pth")
print("Trained model saved as weather_cnn_model.pth")

y_true = []
y_pred = []

with torch.no_grad():
for images, labels in test_loader:
outputs = model(images)
_, predicted = torch.max(outputs, 1)
y_true.extend(labels.numpy())
y_pred.extend(predicted.numpy())

cm = confusion_matrix(y_true, y_pred)
classes = ["day", "night", "rain", "fog"]

plt.figure(figsize=(6,6))
sns.heatmap(cm, annot=True, cmap="Blues", fmt="d", xticklabels=classes, yticklabels=classes)
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.title("Confusion Matrix")
plt.savefig("confusion_matrix.png")
plt.show()
print("Confusion matrix saved as confusion_matrix.png")
79 changes: 79 additions & 0 deletions A2-Mousavi-403611252/A2-Mousavi-403611252-Carla.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import carla
import random
import os
import numpy as np
import cv2
import time

client = carla.Client('localhost', 2000)
client.set_timeout(10.0)
world = client.get_world()

blueprints = world.get_blueprint_library()
vehicle_bp = random.choice(blueprints.filter('vehicle.*'))
spawn_points = world.get_map().get_spawn_points()
spawn_point = random.choice(spawn_points)
vehicle = world.spawn_actor(vehicle_bp, spawn_point)
vehicle.set_autopilot(True)


camera_bp = blueprints.find('sensor.camera.rgb')
camera_bp.set_attribute('image_size_x', '800')
camera_bp.set_attribute('image_size_y', '600')
camera_transform = carla.Transform(carla.Location(x=1.5, z=2.4))
camera = world.spawn_actor(camera_bp, camera_transform, attach_to=vehicle)

output_dir = 'carla_dataset_a2'
os.makedirs(output_dir, exist_ok=True)

def save_image(image, weather_name, img_id):
img_array = np.frombuffer(image.raw_data, dtype=np.uint8)
img_array = np.reshape(img_array, (image.height, image.width, 4))[:, :, :3]
filename = os.path.join(output_dir, f'{weather_name}_{img_id}.png')
cv2.imwrite(filename, img_array)

weathers = {
"day": carla.WeatherParameters.ClearNoon,
"night": carla.WeatherParameters(
cloudiness=0.0,
precipitation=0.5,
sun_altitude_angle=-90.0
) ,
"rain": carla.WeatherParameters.HardRainSunset,
"fog": carla.WeatherParameters(
cloudiness=20.0,
precipitation=0.0,
sun_altitude_angle=20.0,
fog_density=50.0
)
}

try:
img_id = 0
for weather_name, weather in weathers.items():
world.set_weather(weather)
time.sleep(5)

captured_images = [0]

def process_image(image):
""" Callback function to process images """
if captured_images[0] < 50:
save_image(image, weather_name, img_id + captured_images[0])
captured_images[0] += 1
time.sleep(3)
else:
camera.stop()

camera.listen(process_image)

while captured_images[0] < 50:
time.sleep(0.1)

img_id += 50
camera.stop()

finally:
camera.destroy()
vehicle.destroy()

55 changes: 55 additions & 0 deletions A2-Mousavi-403611252/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Overview
In this assignment, a **Convolutional Neural Network (CNN)** is trained to classify weather conditions (Day, Night, Rain, Fog) using images captured in different environments from the **CARLA simulator**.

The project is divided into two main parts:
1. **Data Collection** from CARLA in different weather settings.
2. **CNN Model Training** for weather classification and visualization using a confusion matrix.

---

# Part 1: Data Collection with CARLA

# Features
- A camera is mounted on a moving autopilot vehicle in CARLA.
- Four weather conditions simulated:
- Day
- Night
- Rain
- Fog
- For each condition, **50 images** are captured and saved.

# Output
Captured images are stored in:
`carla_dataset_a2/`
Each image filename follows this pattern:
`<weather_name>_<image_id>.png`

---

# Part 2: CNN Model for Weather Classification

# Features
- A custom CNN architecture is trained on the collected dataset.
- Images are resized to **128x128** and split into **80% training** and **20% testing**.
- The model outputs the **weather label** for a given image.
- After training, a **confusion matrix** visualizes classification performance.

# Results
- Model trained for **10 epochs** using Adam optimizer.
- Final accuracy and loss are printed.
- Confusion matrix is saved as:
`confusion_matrix.png`

# Model Architecture
```plaintext
Conv2D → ReLU → MaxPool
Conv2D → ReLU → MaxPool
Conv2D → ReLU → MaxPool
Flatten → FC (128) → ReLU → FC (4)
```

# Output Files
- `weather_cnn_model.pth`: Trained model weights
- `confusion_matrix.png`: Model performance visualization

---
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added A2-Mousavi-403611252/confusion_matrix.png
Binary file added A2-Mousavi-403611252/weather_cnn_model.pth
Binary file not shown.