CalvinYu727
commited on
Commit
•
9b98bc6
1
Parent(s):
09b3ff6
Update README.md
Browse files
README.md
CHANGED
@@ -7,4 +7,88 @@ metrics:
|
|
7 |
base_model:
|
8 |
- timm/mobilenetv3_large_100.ra_in1k
|
9 |
pipeline_tag: image-classification
|
10 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
base_model:
|
8 |
- timm/mobilenetv3_large_100.ra_in1k
|
9 |
pipeline_tag: image-classification
|
10 |
+
---
|
11 |
+
# Model Card for Model ID
|
12 |
+
|
13 |
+
<!-- Provide a quick summary of what the model is/does. -->
|
14 |
+
|
15 |
+
A fine-tuned model for classifying invoices, credit card and bank cheque, trained using MobileNetV3_Large using Google Colab.
|
16 |
+
|
17 |
+
### Model Description
|
18 |
+
|
19 |
+
<!-- Provide a longer summary of what this model is. -->
|
20 |
+
|
21 |
+
- **Developed by:** Calvin
|
22 |
+
- **Model type:** Image Classification
|
23 |
+
- **License:** MIT
|
24 |
+
- **Finetuned from model:** timm/mobilenetv3_large_100.ra_in1k
|
25 |
+
|
26 |
+
'''
|
27 |
+
from tensorflow.keras.models import load_model
|
28 |
+
import cv2
|
29 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
30 |
+
import tensorflow as tf
|
31 |
+
import numpy as np
|
32 |
+
from google.colab import files, drive
|
33 |
+
import matplotlib.pyplot as plt
|
34 |
+
|
35 |
+
# Define image size
|
36 |
+
IMG_SIZE = 224
|
37 |
+
|
38 |
+
# Set confidence threshold
|
39 |
+
CONFIDENCE_THRESHOLD = 0.5
|
40 |
+
|
41 |
+
drive.mount('/content/drive')
|
42 |
+
|
43 |
+
# Load the finetuned model
|
44 |
+
finetuned_model = load_model('/content/drive/MyDrive/models/finetuned_mobilenetv3large_model_v4.keras')
|
45 |
+
|
46 |
+
# Upload image using google.colab.files
|
47 |
+
uploaded = files.upload()
|
48 |
+
|
49 |
+
# Process the uploaded image
|
50 |
+
for fn in uploaded.keys():
|
51 |
+
# Read the image
|
52 |
+
img_path = fn
|
53 |
+
img = cv2.imread(img_path)
|
54 |
+
|
55 |
+
# Resize the image (using tf.image.resize)
|
56 |
+
img = tf.image.resize(img, [IMG_SIZE, IMG_SIZE])
|
57 |
+
|
58 |
+
# Convert image to array
|
59 |
+
img_array = img_to_array(img)
|
60 |
+
|
61 |
+
# Normalize pixel values
|
62 |
+
img_array = img_array / 255.0
|
63 |
+
|
64 |
+
# Add a dimension (batch size)
|
65 |
+
img_array = tf.expand_dims(img_array, 0)
|
66 |
+
|
67 |
+
# Perform inference using the finetuned model
|
68 |
+
prediction = finetuned_model.predict(img_array)
|
69 |
+
|
70 |
+
# Get probabilities for all classes
|
71 |
+
class_probabilities = prediction[0]
|
72 |
+
|
73 |
+
# Display results
|
74 |
+
print(f'{fn}:')
|
75 |
+
|
76 |
+
max_prob = np.max(class_probabilities)
|
77 |
+
if max_prob > CONFIDENCE_THRESHOLD:
|
78 |
+
for i, prob in enumerate(class_probabilities):
|
79 |
+
if i == 0:
|
80 |
+
class_name = 'Bank Cheque'
|
81 |
+
elif i == 1:
|
82 |
+
class_name = 'Credit Card'
|
83 |
+
elif i == 2:
|
84 |
+
class_name = 'Invoice'
|
85 |
+
else:
|
86 |
+
class_name = 'Others'
|
87 |
+
print(f' {class_name}: ({prob:.4f})')
|
88 |
+
else:
|
89 |
+
print('Others')
|
90 |
+
|
91 |
+
# Display the image
|
92 |
+
plt.imshow(img.numpy().astype(np.uint8))
|
93 |
+
plt.title(fn)
|
94 |
+
plt.show()
|