Passionate about building machine learning models. Currently exploring transformer architectures and I like to share insights on X (Twitter). Always learning! :)
Handling class imbalance was tough—some species had few samples, leading to overfitting. Used weighted loss functions and oversampling to mitigate. And simple models and even transfer learning models weren't performing upto the mark so had to use ensemble learning to train on the data. Deployment on Hugging Face also required optimizing model size for faster inference.
Then improved it with transfer learning using Xception.
Preprocessed images with resizing and normalization, achieving ~96% accuracy on a Kaggle dataset.
Code Snippet
# Using data augmentation
train_data_generator = ImageDataGenerator(
rescale=1/255., # Normalize the pixel values
rotation_range=30, # Randomly rotate the images by up to 30 degrees
width_shift_range=0.2, # Randomly shift images horizontally by up to 20%
height_shift_range=0.2, # Randomly shift images vertically by up to 20%
shear_range=0.2, # Apply shear transformations
zoom_range=0.2, # Randomly zoom in/out by up to 20%
horizontal_flip=True # Randomly flip images horizontally
)
# Load augmented training data
train_data_augmented = train_data_generator.flow_from_directory(train_dir, batch_size=32, class_mode='binary', target_size=Image_size)
# Model 1: A CNN model training on augmented data
model_1 = Sequential([
Conv2D(32, (3, 3), input_shape=(224, 224, 3), activation='relu'),
MaxPooling2D(pool_size=(2, 2)),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D(pool_size=(2, 2)),
Conv2D(128, (3, 3), activation='relu'),
MaxPooling2D(pool_size=(2, 2)),
Flatten(),
Dense(128, activation='relu'),
Dense(1, activation='sigmoid')
])
# Compile the model
model_1.compile(loss = 'binary_crossentropy', optimizer = Adam(), metrics = ['accuracy'])
# Fit the model with augmented data
history_1 = model_1.fit(train_data_augmented, epochs = 10,validation_data = val_data, callbacks = [checkpoint_callback])
### Trying transfer learning
xception_model = Xception(weights = 'imagenet', include_top = False, input_shape = (224, 224, 3)) # Here the Xception model is trained on Imagenet, which is used as feature extractor
# Include top is False as I am going to another classification head
xception_model.trainable = False # Freezing the base mode to retain pre-trained weights
# Add new classification head
model_2 = Sequential([
xception_model, # The pre-trained xception model
layers.GlobalAveragePooling2D(), # Global average pooling to reduce spatical dimensions
Dense(64, activation = 'relu'), # Dense layer for feature extraction
Dropout(0.5), # Dropping out 50% of neurons
Dense(1, activation = 'sigmoid')
])
# Compile the Model 1
model_2.compile(loss = 'binary_crossentropy', optimizer=Adam(), metrics = ['accuracy'])
# Fit the Model 1 with augmented data
history_2 = model_2.fit(train_data_augmented, epochs=10, validation_data=val_data, callbacks=[checkpoint_callback])
Challenges Faced
Initial model was getting confused and wasn't able to predict dog class. Even though I fixed it but the result wasn't up to the mark so, I had to use Transfer learning. Also faced memory issues with large batch sizes on my GPU.
Created models using Logistic Regression, KNeighborsClassifier, Random Forest classifier on the UCI Heart Disease dataset.
Performed feature engineering (e.g., normalizing cholesterol levels) and analyzed feature importance to identify key predictors like age and chest pain type.
Missing data in the dataset required imputation strategies—tried mean and median, settled on KNN imputation. Tuning hyperparameters took time; grid search was slow on my machine as it was using CPU.