Вот ключевые моменты моего Настройка: < /p>
Модель: retinanet с основой Resnet50. DataSet). < /li>
Источник модели: Tensorflow Model Garden (официальная реализация сетчатки). < /li>
< /ul>
Это то, что у меня есть Пробовал до сих пор: < /h1>
Код: Выделить всё
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
from object_detection.utils import visualization_utils
# Load the model
model = tf.saved_model.load('exported/model/path')
# Define the input image size and threshold
input_image_size = (256, 256) # Image size to match the model's input size
min_score_thresh = 0.1 # Lower the threshold to see more boxes
# Load the image
image_path = 'image.jpg' # Example image path
image = tf.image.decode_jpeg(tf.io.read_file(image_path))
plt.figure(figsize=(10, 10)) # Adjust figure size for a single image
# Preprocess the image for input
image_resized = tf.image.resize(image, input_image_size)
image_resized = tf.expand_dims(image_resized, axis=0) # Add batch dimension
image_resized = tf.cast(image_resized, dtype=tf.uint8) # Convert to uint8 type for visualization
image_np = image_resized[0].numpy() # Convert tensor to numpy array for visualization
# Use the model's 'serving_default' signature to make predictions
infer = model.signatures['serving_default'] # Access the serving signature for inference
result = infer(image_resized) # Run inference
# Extract detection boxes, classes, and scores
detection_boxes = result['detection_boxes'][0].numpy()
detection_classes = result['detection_classes'][0].numpy().astype(int)
detection_scores = result['detection_scores'][0].numpy()
# Debugging: Check the shapes and content of the results
print("Detection boxes shape:", detection_boxes.shape)
print("Detection scores:", detection_scores)
print("Detection classes:", detection_classes)
# Filter detections based on the score threshold
valid_boxes = []
valid_scores = []
valid_classes = []
category_index = {
1: {'id': 1, 'name': 'potato_leaf_early_blight'},
2: {'id': 2, 'name': 'potato_leaf_healthy'},
3: {'id': 3, 'name': 'potato_leaf_late_blight'},
# Add more class mappings as per your dataset
}
for i, score in enumerate(detection_scores):
if score > min_score_thresh: # Include more detections by lowering the threshold
valid_boxes.append(detection_boxes[i])
valid_scores.append(score)
valid_classes.append(detection_classes[i])
# Debugging: Check the valid detections
print("Valid boxes:", valid_boxes)
print("Valid scores:", valid_scores)
print("Valid classes:", valid_classes)
# Visualize the bounding boxes on the image (for debugging and visualization)
visualization_utils.visualize_boxes_and_labels_on_image_array(
image_np,
np.array(valid_boxes),
np.array(valid_classes),
np.array(valid_scores),
category_index=category_index, # Define category_index with class labels
use_normalized_coordinates=False,
max_boxes_to_draw=200,
min_score_thresh=min_score_thresh,
agnostic_mode=False,
instance_masks=None,
line_thickness=4 # Adjust the line thickness for the boxes
)
# Display the image with bounding boxes
plt.imshow(image_np)
plt.axis('off') # Turn off axis for clean visualization
# Show the image with bounding boxes
plt.show()
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
from object_detection.utils import visualization_utils
# Load the model
model = tf.saved_model.load('exported/model/path')
# Define the input image size and threshold
input_image_size = (256, 256) # Image size to match the model's input size
min_score_thresh = 0.1 # Lower the threshold to see more boxes
# Load the image
image_path = 'image.jpg' # Example image path
image = tf.image.decode_jpeg(tf.io.read_file(image_path))
plt.figure(figsize=(10, 10)) # Adjust figure size for a single image
# Preprocess the image for input
image_resized = tf.image.resize(image, input_image_size)
image_resized = tf.expand_dims(image_resized, axis=0) # Add batch dimension
image_resized = tf.cast(image_resized, dtype=tf.uint8) # Convert to uint8 type for visualization
image_np = image_resized[0].numpy() # Convert tensor to numpy array for visualization
# Use the model's 'serving_default' signature to make predictions
infer = model.signatures['serving_default'] # Access the serving signature for inference
result = infer(image_resized) # Run inference
# Extract detection boxes, classes, and scores
detection_boxes = result['detection_boxes'][0].numpy()
detection_classes = result['detection_classes'][0].numpy().astype(int)
detection_scores = result['detection_scores'][0].numpy()
# Debugging: Check the shapes and content of the results
print("Detection boxes shape:", detection_boxes.shape)
print("Detection scores:", detection_scores)
print("Detection classes:", detection_classes)
# Filter detections based on the score threshold
valid_boxes = []
valid_scores = []
valid_classes = []
category_index = {
1: {'id': 1, 'name': 'potato_leaf_early_blight'},
2: {'id': 2, 'name': 'potato_leaf_healthy'},
3: {'id': 3, 'name': 'potato_leaf_late_blight'},
# Add more class mappings as per your dataset
}
for i, score in enumerate(detection_scores):
if score > min_score_thresh: # Include more detections by lowering the threshold
valid_boxes.append(detection_boxes[i])
valid_scores.append(score)
valid_classes.append(detection_classes[i])
# Debugging: Check the valid detections
print("Valid boxes:", valid_boxes)
print("Valid scores:", valid_scores)
print("Valid classes:", valid_classes)
# Visualize the bounding boxes on the image (for debugging and visualization)
visualization_utils.visualize_boxes_and_labels_on_image_array(
image_np,
np.array(valid_boxes),
np.array(valid_classes),
np.array(valid_scores),
category_index=category_index, # Define category_index with class labels
use_normalized_coordinates=False,
max_boxes_to_draw=200,
min_score_thresh=min_score_thresh,
agnostic_mode=False,
instance_masks=None,
line_thickness=4 # Adjust the line thickness for the boxes
)
# Display the image with bounding boxes
plt.imshow(image_np)
plt.axis('off') # Turn off axis for clean visualization
# Show the image with bounding boxes
plt.show()
Detection boxes shape: (100, 4)
Detection scores: [ 0.96933 0.10999 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0
-0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0
-0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0
-0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0]
Detection classes: [1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
< /code>
Я хочу получить коробки, оценки, метки и прогнозируемое изображение.
Это файлы с экспортированной моделью < /p>
Подробнее здесь: https://stackoverflow.com/questions/792 ... et50-model