Eugenia-Tate-Duke commited on
Commit
43d68f7
Β·
1 Parent(s): ada3289

Update to use improved baseline model

Browse files
Files changed (4) hide show
  1. README.md +217 -11
  2. app.py +2 -1
  3. models/augmented_model.pt +1 -1
  4. models/baseline_model.pt +1 -1
README.md CHANGED
@@ -1,14 +1,220 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
- title: Robust Doc Classifier
3
- emoji: πŸ“ˆ
4
- colorFrom: purple
5
- colorTo: yellow
6
- sdk: gradio
7
- sdk_version: 6.15.1
8
- python_version: '3.13'
9
- app_file: app.py
10
- pinned: false
11
- short_description: 'Document Classifier '
12
  ---
13
 
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Robust Document Classifier
2
+
3
+ A computer vision prototype for robust document image classification using transfer learning and data augmentation.
4
+
5
+ This project was developed for Mini Hackathon #1 in AIPI 540: Deep Learning Applications at Duke University.
6
+
7
+ ## Problem Statement
8
+
9
+ Business documents are often uploaded under imperfect real-world conditions. Images may be blurry, rotated, shadowed, poorly cropped, distorted, or captured from mobile devices at unusual angles.
10
+
11
+ The goal of this project is to build a document classification system that can better generalize to noisy and imperfect document images using:
12
+
13
+ - Transfer learning
14
+ - Domain-specific data augmentation
15
+ - Robustness-focused evaluation
16
+
17
+ Rather than maximizing raw accuracy, this project focuses on improving model robustness under real-world variation.
18
+
19
+ ---
20
+
21
+ # Project Overview
22
+
23
+ This prototype classifies document images into several document categories:
24
+
25
+ - Letter
26
+ - Form
27
+ - Email
28
+ - Handwritten
29
+ - Advertisement
30
+ - Invoice
31
+
32
+ The system uses a pretrained MobileNetV2 convolutional neural network and fine-tunes a custom classifier head for document classification.
33
+
34
+ ---
35
+
36
+ # Transfer Learning Approach
37
+
38
+ The project uses MobileNetV2 pretrained on ImageNet as the feature extractor.
39
+
40
+ Transfer learning steps:
41
+
42
+ 1. Load pretrained MobileNetV2 weights
43
+ 2. Freeze most pretrained backbone layers
44
+ 3. Unfreeze the final MobileNetV2 blocks for domain adaptation
45
+ 4. Replace the final classification layer
46
+ 5. Fine-tune the classifier head on document images
47
+
48
+ This approach allowed the pretrained model to better adapt from natural images to document-layout classification while still benefiting from ImageNet feature learning.
49
+ ---
50
+
51
+ # Data Augmentation Strategy
52
+
53
+ The project applies realistic augmentations designed to simulate common document upload issues:
54
+
55
+ - Random rotation
56
+ - Perspective distortion
57
+ - Random resized crop
58
+ - Brightness and contrast jitter
59
+ - Gaussian blur
60
+
61
+ These augmentations were selected to improve robustness to:
62
+
63
+ - Mobile phone captures
64
+ - Poor scans
65
+ - Cropped documents
66
+ - Lighting variation
67
+ - Low-quality images
68
+
69
+ ---
70
+
71
+ # Dataset
72
+
73
+ Dataset used:
74
+ - RVL-CDIP Mini Dataset (subset) https://adamharley.com/rvl-cdip/
75
+
76
+ The project uses a reduced subset of the RVL-CDIP document image dataset for rapid experimentation during the hackathon.
77
+
78
+ ---
79
+
80
+ # Model Architecture
81
+
82
+ - Backbone: MobileNetV2
83
+ - Framework: PyTorch
84
+ - Input size: 224x224
85
+ - Loss function: CrossEntropyLoss
86
+ - Optimizer: Adam
87
+ - Transfer learning with partial backbone fine-tuning
88
+
89
+ ---
90
+
91
+ # Results
92
+
93
+ Two transfer learning models were evaluated:
94
+
95
+ 1. Baseline model: 76.2% accuracy
96
+ 2. Augmented model: 67.5% accuracy
97
+
98
+ * Transfer learning significantly improved document classification performance.
99
+ * The baseline model achieved the strongest overall performance.
100
+ * The augmented model demonstrated some robustness improvements but aggressive augmentations reduced performance for several visually subtle document classes.
101
+ * Visually similar classes such as invoices, forms, and letters remained challenging due to overlapping layout structures.
102
+
103
+ Per-Class Observations
104
+
105
+ Strongest classes:
106
+
107
+ * Email
108
+ * Handwritten
109
+ * Advertisement
110
+
111
+ Most difficult classes:
112
+
113
+ * Form
114
+ * Invoice
115
+
116
+ The baseline model achieved:
117
+
118
+ * 94.7% recall for emails
119
+ * 100% recall for handwritten documents
120
+ * 68% recall for invoices
121
+
122
+ One important observation from this project is that pure CNN-based document classifiers rely heavily on layout and visual structure rather than semantic text understanding.
123
+
124
+ Although the word β€œINVOICE” may visibly appear in a document image, the model does not explicitly read text because OCR was not used in this prototype.
125
+
126
+ This explains why visually similar business documents can still be confused despite strong overall performance.
127
+
128
+ ---
129
+
130
+ # Project Structure
131
+
132
+ ```text
133
+ robust-doc-classifier/
134
+ β”œβ”€β”€ app.py
135
+ β”œβ”€β”€ train.py
136
+ β”œβ”€β”€ evaluate.py
137
+ β”œβ”€β”€ requirements.txt
138
+ β”œβ”€β”€ README.md
139
+ β”œβ”€β”€ src/
140
+ β”œβ”€β”€ models/
141
+ └── figures/
142
+ ```
143
+
144
+ ---
145
+
146
+ # Running the Project
147
+
148
+ ## Install dependencies
149
+
150
+ ```bash
151
+ pip install -r requirements.txt
152
+ ```
153
+
154
+ ## Train baseline model
155
+
156
+ ```bash
157
+ python train.py --epochs 10 --max-per-class 150
158
+ ```
159
+
160
+ ## Train augmented model
161
+
162
+ ```bash
163
+ python train.py --augmented --epochs 10 --max-per-class 150
164
+ ```
165
+
166
+ ## Evaluate model
167
+
168
+ ```bash
169
+ python evaluate.py --model-path models/baseline_model.pt --name baseline
170
+ ```
171
+
172
+ ## Run Gradio app locally
173
+
174
+ ```bash
175
+ python app.py
176
+ ```
177
+
178
  ---
179
+
180
+ # Deployment
181
+
182
+ The project is deployed using Hugging Face Spaces with Gradio.
183
+
184
+ Live Demo:
185
+ - https://huggingface.co/spaces/Eugenia-Tate-Duke/robust-doc-classifier
186
+
 
 
187
  ---
188
 
189
+ # Future Improvements
190
+
191
+ Potential future improvements include:
192
+
193
+ * OCR integration for text-aware classification
194
+ * Vision Transformer (ViT) architectures
195
+ * Multimodal document understanding
196
+ * Larger and more balanced datasets
197
+ * More targeted augmentation strategies
198
+ * Fine-tuning additional backbone layers
199
+
200
+ ---
201
+
202
+ # Technologies Used
203
+
204
+ - Python
205
+ - PyTorch
206
+ - Torchvision
207
+ - Hugging Face Datasets
208
+ - Gradio
209
+ - Scikit-learn
210
+ - Matplotlib
211
+ - MobileNetV2 Transfer Learning
212
+
213
+ ---
214
+
215
+ # Author
216
+
217
+ Eugenia Tate
218
+
219
+ AIPI 540 β€” Deep Learning Applications
220
+ Duke University
app.py CHANGED
@@ -8,7 +8,8 @@ from src.config import SELECTED_CLASSES
8
  from src.transforms import get_baseline_transforms
9
 
10
 
11
- MODEL_PATH = "models/augmented_model.pt"
 
12
 
13
  # recreate the same model used during training
14
  def build_model(num_classes):
 
8
  from src.transforms import get_baseline_transforms
9
 
10
 
11
+ # MODEL_PATH = "models/augmented_model.pt"
12
+ MODEL_PATH = "models/baseline_model.pt"
13
 
14
  # recreate the same model used during training
15
  def build_model(num_classes):
models/augmented_model.pt CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:4047af5501be5e6105fae443a78fb3960c9b235e1da253b986c9478775be58d2
3
  size 9169035
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:873de4da151e06875d026ca9e1c2d240754cfbeacb1d03b0dbd47444b9620ceb
3
  size 9169035
models/baseline_model.pt CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:9b6fe3029f4d3483b85ca37a6574493e07158195477adf15e6ec1f2a88dcfa41
3
  size 9168715
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:eb63fdc2f93f97b12bc08aa0f53c113f3079bf8ccaa2638a1df8df13fae114a4
3
  size 9168715