JakeWatson commited on
Commit
3a5ef11
·
verified ·
1 Parent(s): 123f321

Upload README.md

Browse files
Files changed (1) hide show
  1. README.md +264 -0
README.md ADDED
@@ -0,0 +1,264 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Udacity Car Simulator Dataset
2
+
3
+ A preprocessed end-to-end driving dataset collected from the [Udacity Self-Driving Car Simulator](https://github.com/udacity/self-driving-car-sim), intended for training a CNN to predict steering angles from camera input.
4
+
5
+ Collected via manual mouse driving in the simulator and processed into NumPy arrays for fast loading during training. This dataset is used by the [University of Brighton RO501 Coursework project](https://github.com/Jake1402/University-of-Brighton-RO501-Coursework).
6
+
7
+ ---
8
+
9
+ ## Dataset Structure
10
+
11
+ The dataset consists of four `.npy` files:
12
+
13
+ | File | Shape | Dtype | Description |
14
+ |------|-------|-------|-------------|
15
+ | `center.npy` | `(N, 3, 160, 80)` | `float32` | Centre camera frames |
16
+ | `left.npy` | `(N, 3, 160, 80)` | `float32` | Left camera frames |
17
+ | `right.npy` | `(N, 3, 160, 80)` | `float32` | Right camera frames |
18
+ | `steer.npy` | `(M,)` | `float32` | Smoothed steering angles |
19
+
20
+ Where:
21
+ - **N** ≈ 27,000 — number of image frames per camera view
22
+ - **M** ≈ 54,000 — number of steering samples (M = 2 × N)
23
+ - Images are in **channel-first** format: `(batch, channels, height, width)`
24
+ - Color space is **HSV**, pixel values normalised to **[0, 1]**
25
+ - Steering values are in the range **[-1, 1]** (negative = left, positive = right)
26
+
27
+ > **Why M = 2 × N?** The raw simulator log records one row per frame with all three camera paths. During preprocessing, images are sampled every other row (`[::2]`) to reduce redundancy, while the steering signal is kept at full resolution and smoothed. See [Alignment](#aligning-images-and-steering) below for how to pair them up.
28
+
29
+ ---
30
+
31
+ ## Preprocessing Pipeline
32
+
33
+ The raw data starts as a `driving_log.csv` produced by the simulator with columns:
34
+
35
+ ```
36
+ C_img, L_img, R_img, Steer, Accel, Deccel, Velocity
37
+ ```
38
+
39
+ Each image goes through the following transformations before being saved:
40
+
41
+ 1. **Crop** — rows `60:135` are kept (the top of the frame contains no useful driving information)
42
+ 2. **Resize** — to `160 × 80` pixels
43
+ 3. **Color space** — converted from BGR to HSV
44
+ 4. **Normalise** — divided by 255 to give values in `[0, 1]`
45
+ 5. **Reshape** — to channel-first `(3, 160, 80)`
46
+
47
+ The steering column is smoothed with a **moving average (window = 3)** to remove sudden jerks from manual driving.
48
+
49
+ ---
50
+
51
+ ## Loading the Dataset
52
+
53
+ ### Option 1: Download files directly
54
+
55
+ Download all four `.npy` files into a local folder (e.g. `npysets/`):
56
+
57
+ ```bash
58
+ mkdir npysets
59
+ huggingface-cli download JakeWatson/UdacityCarSimDataset --local-dir npysets
60
+ ```
61
+
62
+ Or use the `datasets` library:
63
+
64
+ ```python
65
+ from huggingface_hub import snapshot_download
66
+
67
+ snapshot_download(
68
+ repo_id="JakeWatson/UdacityCarSimDataset",
69
+ local_dir="npysets",
70
+ allow_patterns=["*.npy"]
71
+ )
72
+ ```
73
+
74
+ ### Option 2: Load with NumPy
75
+
76
+ ```python
77
+ import numpy as np
78
+
79
+ center = np.load("npysets/center.npy") # (N, 3, 160, 80) float32
80
+ left = np.load("npysets/left.npy") # (N, 3, 160, 80) float32
81
+ right = np.load("npysets/right.npy") # (N, 3, 160, 80) float32
82
+ steer = np.load("npysets/steer.npy") # (M,) float32
83
+
84
+ print(center.shape) # (27xxx, 3, 160, 80)
85
+ print(steer.shape) # (54xxx,)
86
+ ```
87
+
88
+ ---
89
+
90
+ ## Aligning Images and Steering
91
+
92
+ Because images were sampled at half the rate of steering, you need to subsample both arrays to align them. The companion project uses:
93
+
94
+ - **Images:** every 3rd frame → `arr[::3]`
95
+ - **Steering:** every 6th value → `arr[::6]`
96
+
97
+ This gives matching lengths for each camera view:
98
+
99
+ ```python
100
+ X_center = np.load("npysets/center.npy")[::3] # (N/3, 3, 160, 80)
101
+ X_left = np.load("npysets/left.npy")[::3] # (N/3, 3, 160, 80)
102
+ X_right = np.load("npysets/right.npy")[::3] # (N/3, 3, 160, 80)
103
+ y = np.load("npysets/steer.npy")[::6] # (N/3,)
104
+
105
+ assert X_center.shape[0] == y.shape[0]
106
+ ```
107
+
108
+ ---
109
+
110
+ ## Building a Training Set
111
+
112
+ The standard approach (used in the companion project) concatenates all three camera views and offsets the steering label for left/right views to act as data augmentation:
113
+
114
+ ```python
115
+ import numpy as np
116
+ import torch
117
+ from torch.utils.data import TensorDataset, DataLoader
118
+
119
+ def build_dataset(path="npysets", split=0.95, balance=(31, 550)):
120
+ # Load and align
121
+ X_center = np.load(f"{path}/center.npy")[::3]
122
+ X_left = np.load(f"{path}/left.npy")[::3]
123
+ X_right = np.load(f"{path}/right.npy")[::3]
124
+ y = np.load(f"{path}/steer.npy")[::6]
125
+
126
+ # Concatenate views; offset steering for left/right cameras
127
+ X = np.concatenate([X_center, X_left, X_right], axis=0)
128
+ y = np.clip(np.concatenate([y, y + 0.35, y - 0.35]), -1.0, 1.0)
129
+
130
+ # Balance by steering frequency (optional but recommended)
131
+ bins, max_per_bin = balance
132
+ bin_edges = np.histogram_bin_edges(y, bins=bins)
133
+ bin_ids = np.digitize(y, bin_edges) - 1
134
+ keep = []
135
+ for b in range(bins):
136
+ idx = np.where(bin_ids == b)[0]
137
+ if len(idx) > max_per_bin:
138
+ idx = np.random.choice(idx, max_per_bin, replace=False)
139
+ keep.append(idx)
140
+ keep = np.concatenate(keep)
141
+ X, y = X[keep], y[keep]
142
+
143
+ # Shuffle and split
144
+ perm = np.random.permutation(X.shape[0])
145
+ X, y = X[perm], y[perm]
146
+ split_idx = int(X.shape[0] * split)
147
+
148
+ train_ds = TensorDataset(
149
+ torch.from_numpy(X[:split_idx].astype(np.float32)),
150
+ torch.from_numpy(y[:split_idx].astype(np.float32))
151
+ )
152
+ test_ds = TensorDataset(
153
+ torch.from_numpy(X[split_idx:].astype(np.float32)),
154
+ torch.from_numpy(y[split_idx:].astype(np.float32))
155
+ )
156
+ return train_ds, test_ds
157
+
158
+ train_ds, test_ds = build_dataset()
159
+
160
+ # Create DataLoaders
161
+ train_loader = DataLoader(train_ds, batch_size=32, shuffle=True)
162
+ test_loader = DataLoader(test_ds, batch_size=32, shuffle=False)
163
+ ```
164
+
165
+ ### Why the ±0.35 offset?
166
+
167
+ The left and right cameras see the road shifted relative to the centre camera. Adding a constant to their steering labels teaches the model that "if I'm looking from the left camera, I need to steer slightly more right to stay centred." This is a form of view-based data augmentation that improves generalisation.
168
+
169
+ ---
170
+
171
+ ## Model Input/Output
172
+
173
+ The companion CNN (called **UBAC**) expects:
174
+
175
+ - **Input:** `(batch_size, 3, 160, 80)` — float32 tensor, HSV, normalised [0, 1]
176
+ - **Output:** `(batch_size, 1)` — steering angle in `[-1, 1]` (Tanh activation)
177
+
178
+ ```python
179
+ import torch
180
+ from EffecientRes import ConvolutionalAgent # from the companion repo
181
+
182
+ model = ConvolutionalAgent(action_space=1)
183
+ x = torch.randn(4, 3, 160, 80) # batch of 4 images
184
+ steering = model(x) # shape: (4, 1)
185
+ ```
186
+
187
+ ---
188
+
189
+ ## Quick Start (Full Example)
190
+
191
+ ```python
192
+ import numpy as np
193
+ import torch
194
+ from torch.utils.data import TensorDataset, DataLoader
195
+ from torch import nn
196
+
197
+ # 1. Load data
198
+ center = np.load("npysets/center.npy")[::3]
199
+ left = np.load("npysets/left.npy")[::3]
200
+ right = np.load("npysets/right.npy")[::3]
201
+ steer = np.load("npysets/steer.npy")[::6]
202
+
203
+ # 2. Build augmented dataset
204
+ X = np.concatenate([center, left, right], axis=0).astype(np.float32)
205
+ y = np.clip(np.concatenate([steer, steer + 0.35, steer - 0.35]), -1, 1).astype(np.float32)
206
+
207
+ # 3. Shuffle & split
208
+ perm = np.random.permutation(X.shape[0])
209
+ X, y = X[perm], y[perm]
210
+ n_train = int(0.95 * X.shape[0])
211
+
212
+ train_ds = TensorDataset(torch.from_numpy(X[:n_train]), torch.from_numpy(y[:n_train]))
213
+ test_ds = TensorDataset(torch.from_numpy(X[n_train:]), torch.from_numpy(y[n_train:]))
214
+
215
+ train_loader = DataLoader(train_ds, batch_size=32, shuffle=True)
216
+ test_loader = DataLoader(test_ds, batch_size=32)
217
+
218
+ # 4. Define model (from companion repo)
219
+ # from EffecientRes import ConvolutionalAgent
220
+ # model = ConvolutionalAgent(action_space=1).cuda()
221
+
222
+ # 5. Training loop
223
+ # optimizer = torch.optim.AdamW(model.parameters(), lr=0.0007)
224
+ # criterion = nn.SmoothL1Loss()
225
+ # for epoch in range(20):
226
+ # for X_batch, y_batch in train_loader:
227
+ # X_batch, y_batch = X_batch.cuda(), y_batch.cuda()
228
+ # pred = model(X_batch).squeeze()
229
+ # loss = criterion(pred, y_batch.squeeze())
230
+ # optimizer.zero_grad()
231
+ # loss.backward()
232
+ # torch.nn.utils.clip_grad_norm_(model.parameters(), 1.5)
233
+ # optimizer.step()
234
+ ```
235
+
236
+ ---
237
+
238
+ ## Tips
239
+
240
+ - **Dataset size:** The full dataset is ~12.2 GB. If you're low on disk space, you can subsample further (e.g. `[::6]` for images and `[::12]` for steering) to halve the working set.
241
+ - **Balancing matters:** The raw data is heavily skewed toward near-zero steering (driving straight). Without frequency balancing, the model will learn to just hold the wheel.
242
+ - **GPU recommended:** Training is fast on a GPU (~minutes per epoch). CPU training works but is slow.
243
+ - **Minimum data:** Aim for at least ~50k raw frames (≈ 27k after sampling) for good results.
244
+
245
+ ---
246
+
247
+ ## Citation
248
+
249
+ If you use this dataset in your work, please cite the original project:
250
+
251
+ ```bibtex
252
+ @misc{watson2025udacity,
253
+ author = {Watson, Jake},
254
+ title = {Self-Driving Car Agent — University of Brighton RO501 Coursework},
255
+ year = {2025},
256
+ url = {https://github.com/Jake1402/University-of-Brighton-RO501-Coursework}
257
+ }
258
+ ```
259
+
260
+ ---
261
+
262
+ ## License
263
+
264
+ This dataset is provided for educational purposes. The images were generated by the Udacity Self-Driving Car Simulator and do not contain any real-world or personal data.