tfgo icon indicating copy to clipboard operation
tfgo copied to clipboard

tfgo Object detection

Open mineroot opened this issue 1 year ago • 0 comments

Hello, noob problem here

What i have:

  • a trained and exported model for object detection, trained by this script model_main_tf2.py and exported by this script exporter_main_v2.py

  • python code for inference (i found this snippet on stackowerflow), and it seems works well with my model

import numpy as np
import tensorflow as tf
from PIL import Image
import matplotlib.pyplot as plt
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as viz_utils

detect_fn = tf.saved_model.load("exported-models/my_model/saved_model")
print(detect_fn)
PATH_TO_LABELS = 'annotations/label_map.pbtxt'
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
IMAGE_PATHS = ["img.png"]
def load_image_into_numpy_array(path):
    return np.array(Image.open(path))

for image_path in IMAGE_PATHS:
    print('Running inference for {}... '.format(image_path), end='')
    image_np = load_image_into_numpy_array(image_path)
    input_tensor = tf.convert_to_tensor(image_np)
    input_tensor = input_tensor[tf.newaxis, ...]
    detections = detect_fn(input_tensor)
    num_detections = int(detections.pop('num_detections'))
    detections = {key: value[0, :num_detections].numpy()
                   for key, value in detections.items()}
    detections['num_detections'] = num_detections
    detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
    image_np_with_detections = image_np.copy()
    viz_utils.visualize_boxes_and_labels_on_image_array(
          image_np_with_detections,
          detections['detection_boxes'],
          detections['detection_classes'],
          detections['detection_scores'],
          category_index,
          use_normalized_coordinates=True,
          max_boxes_to_draw=200,
          min_score_thresh=.6,
          agnostic_mode=False)
    plt.figure(figsize=(20, 20))
    plt.imshow(image_np_with_detections)
    # graph.png contains img.png and detected object within rectangle and label
    plt.savefig("graph.png")
  • golang snippet for inference whitch loads same model, but it's not working, this code panics with op detection_boxes not found
func main() {
	model := tg.LoadModel("saved_model", []string{"serve"}, nil)

	imageBytes, err := os.ReadFile("img.png")
	if err != nil {
		log.Fatal(err)
	}

	tensor, err := tf.NewTensor(imageBytes)
	if err != nil {
		log.Fatal(err)
	}

	results := model.Exec([]tf.Output{
		model.Op("detection_boxes", 0),
		model.Op("detection_scores", 0),
		model.Op("detection_classes", 0),
		model.Op("num_detections", 0),
	}, map[tf.Output]*tf.Tensor{
		model.Op("image_tensor", 0): tensor,
	})
	if err != nil {
		log.Fatal(err)
	}

	//TODO
	fmt.Print(results)
}

Indeed, there are no such operations in operations

model, _ := tf.LoadSavedModel("saved_model", []string{"serve"}, nil)
operations := model.Graph.Operations() // no 'detection_boxes' and others

So, whats wrong with my Go code, or maybe it's my model issue?

mineroot avatar Jun 21 '23 14:06 mineroot