Fabric Defect Detection - Explanation & Scope of
Improvement
1) What the Code Does
The provided code implements a basic deep learning pipeline for fabric defect detection and
classification. It uses a pre-trained ResNet-18 (transfer learning) to classify images of fabric into
different defect categories such as 'good', 'oil', 'thread', etc.
• Loads dataset from folders (train/test) with images organized by defect type.
• Applies transformations (resize, tensor conversion) to prepare images.
• Uses ResNet-18 pre-trained on ImageNet and replaces the last layer to match defect classes.
• Trains the model for 5 epochs using cross-entropy loss and Adam optimizer.
• Evaluates the trained model on the test set and prints classification accuracy.
2) How It Works
The workflow follows the typical deep learning pipeline:
• **Data Preparation**: Images are resized to 128x128 and converted to tensors for input into
PyTorch.
• **Model Setup**: ResNet-18 is loaded, and its final fully connected layer is replaced with one
matching the number of fabric defect categories.
• **Training Loop**: The model is trained for a few epochs where it predicts outputs, calculates loss,
backpropagates errors, and updates weights.
• **Evaluation**: The model predicts defect categories on unseen test data, and accuracy is
computed.
3) Scope of Improvement
While the code works as a simple prototype, there are several areas where it can be improved for
better performance and practical deployment:
• **Data Augmentation**: Add random rotations, flips, color jittering to make the model robust.
• **More Epochs & Fine-tuning**: Train longer and fine-tune all ResNet layers instead of only the last
one.
• **Learning Rate Scheduling**: Use learning rate schedulers to optimize convergence.
• **Evaluation Metrics**: Add precision, recall, and F1-score to understand performance beyond
accuracy.
• **Grad-CAM / Visualization**: Visualize what parts of fabric the model focuses on to ensure
explainability.
• **Deployment**: Convert model to ONNX or TorchScript for real-time defect detection in factories.