papousek commited on
Commit
5c0ccbd
·
verified ·
1 Parent(s): c1c88a6

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +66 -3
README.md CHANGED
@@ -1,3 +1,66 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - vision
5
+ - image-classification
6
+ ---
7
+
8
+ # Kindwise Crossroad Model
9
+
10
+ ## The model use it
11
+
12
+ Here is how to use this model to classify an image into one of the basic classes:
13
+
14
+ ```python
15
+ from huggingface_hub import hf_hub_download
16
+ import cv2
17
+ import numpy as np
18
+ import PIL.Image
19
+ import torch
20
+ import torchvision
21
+
22
+ DEVICE_NAME = 'cuda:0'
23
+ MODEL_PATH = hf_hub_download('kindwise/crossroad.tiny', 'model.traced.pt')
24
+ CLASSES_PATH = hf_hub_download('kindwise/crossroad.tiny', 'classes.txt')
25
+ IMAGE_PATH = '/tmp/photo.jpg'
26
+
27
+ with open(CLASSES_PATH) as f:
28
+ CLASSES = [line.strip() for line in f]
29
+ MODEL = torch.jit.load(MODEL_PATH).eval().to(DEVICE_NAME)
30
+
31
+
32
+ def resize_crop(image_data: np.ndarray, target_size: int = 480) -> np.ndarray | None:
33
+ height, width, _ = image_data.shape
34
+ # Determine the size of the square crop
35
+ crop_size = min(height, width)
36
+ # Calculate coordinates for center crop
37
+ start_x = (width - crop_size) // 2
38
+ start_y = (height - crop_size) // 2
39
+ # Perform center crop
40
+ cropped_img = image_data[
41
+ start_y : start_y + crop_size,
42
+ start_x : start_x + crop_size
43
+ ]
44
+ # Resize cropped image to target size
45
+ return cv2.resize(
46
+ cropped_img,
47
+ (target_size, target_size),
48
+ interpolation=cv2.INTER_AREA,
49
+ )
50
+
51
+ with torch.no_grad():
52
+ image_array = np.array(PIL.Image.open(IMAGE_PATH))
53
+ image_array_resized = resize_crop(image_array)
54
+ image_tensor = torchvision.transforms.functional.to_tensor(image_array_resized).to(DEVICE_NAME)
55
+ prediction = MODEL(image_tensor.unsqueeze(0)).squeeze(0).cpu().numpy()
56
+ for i in (-prediction).argsort():
57
+ print(f'{classes[i]:>10}: {100 * prediction[i]:.1f}%')
58
+ ```
59
+
60
+ Output:
61
+ ```
62
+ plant: 96.7%
63
+ human: 0.1%
64
+ insect: 0.1%
65
+ mushroom: 0.0%
66
+ ```