File size: 2,522 Bytes
09b3ff6
 
 
 
 
 
 
 
 
9b98bc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
---
license: mit
language:
- en
metrics:
- accuracy
base_model:
- timm/mobilenetv3_large_100.ra_in1k
pipeline_tag: image-classification
---
# Model Card for Model ID

<!-- Provide a quick summary of what the model is/does. -->

A fine-tuned model for classifying invoices, credit card and bank cheque, trained using MobileNetV3_Large using Google Colab.

### Model Description

<!-- Provide a longer summary of what this model is. -->

- **Developed by:** Calvin
- **Model type:** Image Classification
- **License:** MIT
- **Finetuned from model:** timm/mobilenetv3_large_100.ra_in1k

'''
  from tensorflow.keras.models import load_model
  import cv2
  from tensorflow.keras.preprocessing.image import img_to_array
  import tensorflow as tf
  import numpy as np
  from google.colab import files, drive
  import matplotlib.pyplot as plt
  
  # Define image size
  IMG_SIZE = 224
  
  # Set confidence threshold
  CONFIDENCE_THRESHOLD = 0.5
  
  drive.mount('/content/drive')
  
  # Load the finetuned model
  finetuned_model = load_model('/content/drive/MyDrive/models/finetuned_mobilenetv3large_model_v4.keras')
  
  # Upload image using google.colab.files
  uploaded = files.upload()
  
  # Process the uploaded image
  for fn in uploaded.keys():
      # Read the image
      img_path = fn
      img = cv2.imread(img_path)
  
      # Resize the image (using tf.image.resize)
      img = tf.image.resize(img, [IMG_SIZE, IMG_SIZE])
  
      # Convert image to array
      img_array = img_to_array(img)
  
      # Normalize pixel values
      img_array = img_array / 255.0
  
      # Add a dimension (batch size)
      img_array = tf.expand_dims(img_array, 0)
  
      # Perform inference using the finetuned model
      prediction = finetuned_model.predict(img_array)
  
      # Get probabilities for all classes
      class_probabilities = prediction[0]
  
      # Display results
      print(f'{fn}:')
  
      max_prob = np.max(class_probabilities)
      if max_prob > CONFIDENCE_THRESHOLD:
          for i, prob in enumerate(class_probabilities):
              if i == 0:
                  class_name = 'Bank Cheque'
              elif i == 1:
                  class_name = 'Credit Card'
              elif i == 2:
                  class_name = 'Invoice'
              else:
                  class_name = 'Others'
              print(f' {class_name}: ({prob:.4f})')
      else:
          print('Others')
  
      # Display the image
      plt.imshow(img.numpy().astype(np.uint8))
      plt.title(fn)
      plt.show()