Skip to content

Commit e8d2b26

Browse files
committed
Merge minor updates.
2 parents fb94195 + f56b3ee commit e8d2b26

4 files changed

Lines changed: 28 additions & 6 deletions

File tree

episodes/01-xrays.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ import os
8686
# If your dataset is compressed, unzip with:
8787
# !unzip chest_xrays.zip
8888

89-
# Define folders containing images
90-
data_path = os.path.join("chest_xrays")
89+
# Define folders containing images (assume we are in Desktop/src/ml-xray)
90+
data_path = "chest_xrays"
9191
effusion_path = os.path.join(data_path, "effusion", "*.png")
9292
normal_path = os.path.join(data_path, "normal", "*.png")
9393

@@ -104,6 +104,10 @@ Number of cases with pleural effusion: 350
104104
Number of normal cases: 350
105105
```
106106

107+
Note that `glob()` makes a list of the directory contents in
108+
an unspecified order. If you want consistent results, you should
109+
use `sort(effusion_list)`. This is less important
110+
for our case, since we are going to shuffle these later.
107111

108112

109113
:::::::::::::::::::::::::::::::::::::::: keypoints

episodes/02-visualisation.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ print(image.shape)
116116
```
117117

118118
Here we see that the image has 3 dimensions. The first dimension is height (512 pixels) and the second is width (also 512 pixels).
119-
The presence of a third dimension indicates that we are looking at a color image ("RGB", or Red, Green, Blue).
119+
The presence of a third dimension indicates that we are looking at a color image. The last "RGB" index holds separate Red, Green, and Blue channels. For these images, however, R=G=B, so it doesn't appear to have color.
120120

121121
For more detail on image representation in Python, take a look at the [Data Carpentry course on Image Processing with Python](https://datacarpentry.org/image-processing/). The following image is reproduced from the [section on Image Representation](https://datacarpentry.org/image-processing/03-skimage-images/index.html).
122122

@@ -171,6 +171,15 @@ dataset = dataset_effusion + dataset_normal
171171
labels = np.concatenate([label_effusion, label_normal])
172172
```
173173

174+
:::::::::::::::::::::::::::::::::::::::: Performance Optimization
175+
176+
Pro Tip: the code above loads all 700 images at once. In real-world
177+
deep learning, we use Data Loaders (like torch.util.data.DataLoader) to
178+
load images from disk on-the-fly during training. This avoids the memory
179+
bottleneck of loading the entire dataset into RAM.
180+
181+
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
182+
174183
### Downsampling
175184

176185
X-ray images are often high resolution, which can be useful for detailed clinical interpretation. However, for training a machine learning model, especially in an educational or prototype setting, using smaller images can reduce:
@@ -204,6 +213,10 @@ for i in range(len(dataset)):
204213
dataset[i] = (dataset[i] - np.mean(dataset[i])) / np.std(dataset[i])
205214
```
206215

216+
Note that this standardizes each image individually, so that
217+
the image mean is now zero for *every* image. If we had standardized the entire
218+
dataset, there would still be variations in `dataset[i].mean()`.
219+
207220
### Reshaping
208221

209222
Finally, we'll convert our dataset from a list to an array. We are expecting it to be (700, 256, 256), representing 700 images (350 effusion and 350 normal), each with dimensions 256×256.
@@ -247,6 +260,9 @@ plt.imshow(dataset[idx], cmap='gray', vmin=min(vals), vmax=max(vals))
247260

248261
![](fig/final_example_image.png){alt='Example greyscale image' width="400px"}
249262

263+
Note that vmin and vmax are used because our images are no longer on a 0-255 scale,
264+
but have values centered around zero. Without specifying these, matplotlib
265+
auto-scales.
250266

251267

252268
:::::::::::::::::::::::::::::::::::::::: keypoints

episodes/05-train_eval.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ for epoch in range(epochs):
121121
val_losses.append(val_loss)
122122
val_accs.append(val_acc)
123123

124-
if val_loss << best best_val_loss:
124+
if val_loss < best_val_loss:
125125
best_val_loss = val_loss
126126
torch.save(model.state_dict(), 'best_model.pt')
127127

learners/setup.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ in Linux, or [Git Bash][gitbash] on Windows, execute the following command:
3333
cd ~/Desktop
3434
mkdir src
3535
cd src
36-
uv init ml-xray
36+
uv init --native-tls ml-xray
3737
cd ~/Desktop/src/ml-xray
3838
```
3939

@@ -52,12 +52,14 @@ create your project:
5252
cd /D %userprofile%\Desktop
5353
mkdir src
5454
cd src
55-
uv init ml-xray
55+
uv init --native-tls ml-xray
5656
cd /D %userprofile%\Desktop\src\ml-xray
5757
```
5858

5959
:::::::::::::::::::::::::
6060

61+
Note the `--native-tls` option may be necessary in some environments.
62+
6163
## Download Project Package Dependencies
6264

6365
The major strength of Python is the number of libraries available.

0 commit comments

Comments
 (0)