To Implement Transfer Learning for classification using VGG-19 architecture.
To develop a deep learning-based classification model using transfer learning with the VGG-19 architecture to accurately classify images of capacitors into two categories: defected and non-defected. The model will be trained on labeled images, optimized using appropriate loss and optimizer functions, and evaluated using metrics such as confusion matrix, classification report, and performance plots.
This classification system helps automate the quality control process in manufacturing by detecting defective capacitors based on their images, thereby improving efficiency and reducing manual inspection errors. The project leverages pre-trained VGG-19 weights and adapts the final classification layer to suit the specific dataset.
Import the necessary libraries such as PyTorch, Torchvision, and Matplotlib.
Load the dataset and apply preprocessing (resizing, normalization, and augmentation).
Download the pre-trained VGG-19 model from Torchvision models.
Freeze the feature extraction layers of VGG-19.
Modify the final fully connected layer to match the number of dataset classes.
Define the loss function (CrossEntropyLoss) and optimizer (Adam/SGD).
Train the model on the training dataset and validate on the validation set.
Plot Training Loss and Validation Loss vs Iterations.
Evaluate the model on the test dataset.
Generate Confusion Matrix, Classification Report, and test on new sample images.
# Load Pretrained Model and Modify for Transfer Learning
model = models.vgg19(pretrained=True)
# Modify the final fully connected layer to match the dataset classes
in_features=model.classifier[-1].in_features
num_classes = len(train_dataset.classes)
model.classifier[-1] = nn.Linear(in_features, 1)
# Include the Loss function and optimizer
model =CNNClassifier()
criterion =nn.CrossEntropyLoss()
optimizer =optim.Adam(model.parameters(),lr=0.001)
# Train the model
def train_model(model, train_loader, test_loader, num_epochs=5):
train_losses = []
val_losses = []
model.train()
for epoch in range(num_epochs):
running_loss = 0.0
for images, labels in train_loader:
images, labels = images.to(device), labels.to(device)
labels = labels.float().unsqueeze(1) # Ensure proper shape for BCEWithLogitsLoss
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
train_losses.append(running_loss / len(train_loader))
# Compute validation loss
model.eval()
val_loss = 0.0
with torch.no_grad():
for images, labels in test_loader:
images, labels = images.to(device), labels.to(device)
labels = labels.float().unsqueeze(1)
outputs = model(images)
loss = criterion(outputs, labels)
val_loss += loss.item()
val_losses.append(val_loss / len(test_loader))
model.train()
print(f'Epoch [{epoch+1}/{num_epochs}], Train Loss: {train_losses[-1]:.4f}, Validation Loss: {val_losses[-1]:.4f}')
# Plot training and validation loss
print("Name: RANJIT R")
print("Register Number: 212224240131")
plt.figure(figsize=(8, 6))
plt.plot(range(1, num_epochs + 1), train_losses, label='Train Loss', marker='o')
plt.plot(range(1, num_epochs + 1), val_losses, label='Validation Loss', marker='s')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.title('Training and Validation Loss')
plt.legend()
plt.show()
The VGG-19 model was successfully trained and optimized to classify defected and non-defected capacitors.