papousek commited on
Commit
340bf1c
·
verified ·
1 Parent(s): 7796ed0

Upload folder using huggingface_hub

Browse files
Files changed (6) hide show
  1. README.md +130 -0
  2. classes.txt +6 -0
  3. config.json +20 -0
  4. model.optimized.tflite +3 -0
  5. model.tflite +3 -0
  6. model.traced.pt +3 -0
README.md ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ tags:
4
+ - vision
5
+ - image-classification
6
+ ---
7
+
8
+ # Kindwise Router Classifier
9
+
10
+ This model classifies images based on their content, acting as a "router" to
11
+ direct requests to the correct Kindwise service. It automatically detects
12
+ whether an image contains human, insect, mushroom, or plant.
13
+
14
+ Th model is intended to be the first step in an image processing pipeline.
15
+ Instead of having each specialized service (e.g., insect, plant classification)
16
+ analyze every image, this model quickly determines the image's category. This
17
+ reduces latency and optimizes system resources.
18
+
19
+ ## Technical Details and Formats
20
+
21
+ The model is available in two optimized formats for easy deployment:
22
+ - **TorchScript**: Optimized for production environments and server-side applications where performance and low latency are critical.
23
+ - **TensorFlow Lite**: Perfect for mobile devices and edge computing, where efficiency and minimal model size are key.
24
+
25
+ ## Usage
26
+
27
+ Here is how to use this model to classify an image into one of the basic classes:
28
+
29
+ ### PyTorch
30
+
31
+ ```python
32
+ from huggingface_hub import hf_hub_download
33
+ import cv2
34
+ import numpy as np
35
+ import PIL.Image
36
+ import torch
37
+ import torchvision
38
+
39
+ DEVICE_NAME = 'cuda:0'
40
+ MODEL_PATH = hf_hub_download('kindwise/router.base', 'model.traced.pt')
41
+ CLASSES_PATH = hf_hub_download('kindwise/router.base', 'classes.txt')
42
+ IMAGE_PATH = '/tmp/photo.jpg'
43
+
44
+ with open(CLASSES_PATH) as f:
45
+ CLASSES = [line.strip() for line in f]
46
+ MODEL = torch.jit.load(MODEL_PATH).eval().to(DEVICE_NAME)
47
+
48
+
49
+ def resize_crop(image_data: np.ndarray, target_size: int = 480) -> np.ndarray | None:
50
+ height, width, _ = image_data.shape
51
+ # Determine the size of the square crop
52
+ crop_size = min(height, width)
53
+ # Calculate coordinates for center crop
54
+ start_x = (width - crop_size) // 2
55
+ start_y = (height - crop_size) // 2
56
+ # Perform center crop
57
+ cropped_img = image_data[
58
+ start_y : start_y + crop_size,
59
+ start_x : start_x + crop_size
60
+ ]
61
+ # Resize cropped image to target size
62
+ return cv2.resize(
63
+ cropped_img,
64
+ (target_size, target_size),
65
+ interpolation=cv2.INTER_AREA,
66
+ )
67
+
68
+ with torch.no_grad():
69
+ image_array = np.array(PIL.Image.open(IMAGE_PATH))
70
+ image_array_resized = resize_crop(image_array)
71
+ image_tensor = torchvision.transforms.functional.to_tensor(image_array_resized).to(DEVICE_NAME)
72
+ prediction = MODEL(image_tensor.unsqueeze(0)).squeeze(0).cpu().numpy()
73
+ for i in (-prediction).argsort():
74
+ print(f'{CLASSES[i]:>10}: {100 * prediction[i]:.1f}%')
75
+ ```
76
+
77
+ Output:
78
+ ```
79
+ plant: 91.3%
80
+ unhealthy_plant: 53.3%
81
+ crop: 16.2%
82
+ insect: 0.4%
83
+ human: 0.1%
84
+ mushroom: 0.0%
85
+ ```
86
+
87
+ ###
88
+
89
+ ### TensorFlow Lite
90
+
91
+ ```python
92
+ from huggingface_hub import hf_hub_download
93
+ import numpy as np
94
+ import tensorflow as tf
95
+
96
+ MODEL_PATH = hf_hub_download('kindwise/router.base', 'model.tflite') # or model.optimized.tflite
97
+ CLASSES_PATH = hf_hub_download('kindwise/router.base', 'classes.txt')
98
+
99
+ with open(CLASSES_PATH) as f:
100
+ CLASSES = [line.strip() for line in f]
101
+ INTERPRETER = tf.lite.Interpreter(model_path=MODEL_PATH)
102
+ INTERPRETER.allocate_tensors()
103
+
104
+ image_array_resized = ... # see the previous example
105
+ tf_input = np.expand_dims( # add batch dimension
106
+ (image_array_resized / 255).astype(np.float32), # image values in [0..1]
107
+ 0,
108
+ )
109
+ input_details = INTERPRETER.get_input_details()
110
+ output_details = INTERPRETER.get_output_details()
111
+ INTERPRETER.set_tensor(
112
+ input_details[0]['index'],
113
+ tf_input,
114
+ )
115
+ INTERPRETER.invoke()
116
+ logits = INTERPRETER.get_tensor(output_details[0]['index'])[0]
117
+ prediction = tf.nn.sigmoid(logits).numpy()
118
+ for i in (-prediction).argsort():
119
+ print(f'{CLASSES[i]:>10}: {100 * prediction[i]:.1f}%')
120
+ ```
121
+
122
+ Output:
123
+ ```
124
+ plant: 91.3%
125
+ unhealthy_plant: 53.3%
126
+ crop: 16.2%
127
+ insect: 0.4%
128
+ human: 0.1%
129
+ mushroom: 0.0%
130
+ ```
classes.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ crop
2
+ human
3
+ insect
4
+ mushroom
5
+ plant
6
+ unhealthy_plant
config.json ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architecture": "caformer_b36",
3
+ "id2label": {
4
+ "0": "crop",
5
+ "1": "human",
6
+ "2": "insect",
7
+ "3": "mushroom",
8
+ "4": "plant"
9
+ "6": "unhealthy_plant",
10
+ },
11
+ "label2id": {
12
+ "crop": 0,
13
+ "human": 1,
14
+ "insect": 2,
15
+ "mushroom": 3,
16
+ "plant": 4,
17
+ "unhealthy_plant": 5
18
+ },
19
+ "image_size": 480
20
+ }
model.optimized.tflite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:883540e848437343e7334177d253549e2b53f4748f4a07cf7b65338e4ff104d1
3
+ size 96167688
model.tflite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d5bd4bd5aac65c587b2c9e5c93dce063562a50dca40fe5f31358a869a2b485e2
3
+ size 374264068
model.traced.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6165ee37ac6936a85678f3e1ab62e484c2fc8d1a4a79e13d35c8282e25e327f6
3
+ size 374752635