You are browsing as a guest. Sign up (or log in) to start making projects!

1h 23m 1s logged

MIRA — Day 3 Creating Model and training on Limited data

What I Built Today

Today I built train_baseline.py, the first complete CNN training pipeline for MIRA. The script loads the dataset, applies data augmentation, trains a 3-layer convolutional neural network, plots the accuracy and loss curves, and saves the trained model to disk. I followed the official TensorFlow image classification tutorial and adjusted it to fit the MIRA dataset structure and 4-class recycling problem.

model = Sequential([
  data_augmentation,
  layers.Rescaling(1./255),
  layers.Conv2D(16, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Conv2D(32, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Conv2D(64, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Dropout(0.2),
  layers.Flatten(),
  layers.Dense(128, activation='relu'),
  layers.Dense(num_classes, name="outputs")
])

Difficulty: Image Stretching

The first training run loaded images incorrectly, the model was receiving stretched and distorted frames because image_dataset_from_directory resizes images to the target dimensions by default, squashing non-square images. Fixed by adding crop_to_aspect_ratio=True, which crops from the center instead of stretching:

train_ds = tf.keras.utils.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="training",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size,
  crop_to_aspect_ratio=True)

Difficulty: Overfitting

Early runs showed training accuracy climbing while validation accuracy stayed flat or dropped ,the model was memorizing the training images rather than learning generalizable features. This happens when the dataset is too small relative to the model’s capacity. Fixed with two countermeasures: data augmentation (RandomFlip, RandomRotation, RandomZoom) to artificially vary the training images, and Dropout(0.2) to prevent individual neurons from becoming too specialized.

Results

val_acc = 0.8235 @ epoch 20
val_loss = 0.4030 @ epoch 20
best val_acc = 0.8824 @ epoch 19
best val_loss = 0.3646 @ epoch 19
dataset = 126 images / 4 classes

The high epoch-to-epoch variance in validation accuracy (swings of ±18%) is happening because of the low validation set containing only 17 images, one wrong prediction equals a 5.9% accuracy shift. Results will stabilize further after expanding the dataset.

Coming from C/C++

Well nothing to say because I never coded anything like this in C/C++ so this one will be the last Coming from C/C++

What’s Next

I will capture 200 images per class (800 total). So my results are actually meaningful and verfiable also in the images I will adjust lighning and more

After the dataset is large enough:
I will Write evaluate.py, load the saved model, run it on the test set, and compute a full classification report: precision, recall, and F1-score per class. So I get a per class breakdown of the real acuraccy of the certain folder each and not just the total median.

Resources Used

Credits

Also wanted to shoutout RUMI where i got insperation for the naming scheme from.

0
2

Comments 0

No comments yet. Be the first!