Views
No views yet
1import tensorflow as tf
2from PIL import Image
3import numpy as np
4import json
5
6import tensorflow as tf
7from huggingface_hub import snapshot_download
8
9# Download the entire model directory
10model_dir = snapshot_download(repo_id="eligapris/agroeye",
11 local_dir="path/to/model")
12
13# Load the model
14model = tf.saved_model.load('path/to/model')
15
16# Now you can use the model for inference
17
18# Load and preprocess the image
19img = Image.open('/path/to/image.jpg')
20img = img.resize((300, 300 * img.size[1] // img.size[0]))
21img_array = np.array(img)[None]
22
23# Make prediction
24inp = tensorflow.constant(img_array, dtype='float32')
25prediction = model(inp)[0].numpy()
26
27# Load class names
28with open('path/to/model/classes.json', 'r') as f:
29 class_names = json.load(f)
30
31# Get the predicted class
32predicted_class = list(class_names.keys())[prediction.argmax()]
33print(f"Predicted class: {predicted_class}")1import tensorflow as tf
2from PIL import Image
3import numpy as np
4import json
5
6import tensorflow as tf
7from huggingface_hub import snapshot_download
8
9# Download the entire model directory
10model_dir = snapshot_download(repo_id="eligapris/agroeye",
11 local_dir="path/to/model")
12
13# Load the model
14model = tf.saved_model.load('path/to/model')
15
16# Now you can use the model for inference
17
18# Load and preprocess the image
19img = Image.open('/path/to/image.jpg')
20img = img.resize((300, 300 * img.size[1] // img.size[0]))
21img_array = np.array(img)[None]
22
23# Make prediction
24inp = tensorflow.constant(img_array, dtype='float32')
25prediction = model(inp)[0].numpy()
26
27# Load class names and details
28with open('model/classes_detailed.json', 'r') as f:
29 data = json.load(f)
30
31class_names = data['classes']
32class_details = data['details']
33
34# Get the predicted class
35predicted_class = list(class_names.keys())[prediction.argmax()]
36predicted_class_label = class_names[predicted_class]
37
38print(f"Predicted class: {predicted_class} (Label: {predicted_class_label})")
39
40# Print detailed information about the predicted class
41if predicted_class in class_details:
42 details = class_details[predicted_class]
43 print("\nDetailed Information:")
44 for key, value in details.items():
45 if isinstance(value, list):
46 print(f"{key.capitalize()}:")
47 for item in value:
48 print(f" - {item}")
49 else:
50 print(f"{key.capitalize()}: {value}")
51
52# Print general notes
53print("\nGeneral Notes:")
54for note in data['general_notes']:
55 print(f"- {note}")https://colab.research.google.com/drive/13-S-obR6MZDDP5kgj6ytsbFiNKzzfXbp