Hood Stand Alone TensorFlow Hub Random Explorations 20211231

From wikidb
Jump to: navigation, search
December 31, 2021
ObjectDecti0on20211231.txt

Just wanted to remember the following page

https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md


from TensorFlow Lite Android, iOS and Raspberry Pi

https://www.tensorflow.org/lite/examples
---

https://www.tensorflow.org/lite/examples/object_detection/overview#get_started

Get started

Download starter model with Metadata

eepp@hood:TFDemo$ mv ~/Downloads/ssd_mobilenet_v1_1_metadata_1.tflite .
eepp@hood:TFDemo$ pwd
/home/eepp/Tensorflow_examples/TFObjectDectionTutorial/TFDemo


epp@hood:TFDemo$ ll
total 4096
drwxrwxr-x 2 eepp eepp    4096 Dec 31 13:52 ./
drwxrwxr-x 4 eepp eepp    4096 Dec 31 13:51 ../
-rw-rw-r-- 1 eepp eepp 4185003 Dec 31 13:49 ssd_mobilenet_v1_1_metadata_1.tflite

------------

TF Hub

number one hit on google
*** https://www.tensorflow.org/hub/tutorials/object_detection

run in google colab

https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/object_detection.ipynb?pli=1#scrollTo=6cPY9Ou4sWs_

https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/object_detection.ipynb#scrollTo=YLWNhjUY1mhg

https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/object_detection.ipynb#scrollTo=DZ18R7dWNyoU


based on
https://github.com/tensorflow/models/blob/master/research/object_detection/utils/visualization_utils.py see jup file

----------------

find model???

example code
https://www.tensorflow.org/hub/tutorials/object_detection

found the following line

module_handle = "https://tfhub.dev/google/faster_rcnn/openimages_v4/inception_resnet_v2/1"


went to 

https://tfhub.dev/s?module-type=image-object-detection

why the "?" in url above

followed ssd_mobilenet_v1


---

extract the code from ipynb
http://10.0.0.9/wiki/index.php/Hood_Ubuntu_Vanilla_TensorFlow_Install_and_Test_20210708#Install_Jupyter

my instructions for gett ing code


eepp@hood:TFDemo$ mv ~/Downloads/object_detection.ipynb pythonCode/
eepp@hood:TFDemo$ cd pythonCode/
eepp@hood:pythonCode$ jupyter nbconvert --to script l03c01 object_detection.ipynb 
[NbConvertApp] WARNING | pattern 'l03c01' matched no files
[NbConvertApp] Converting notebook object_detection.ipynb to script
[NbConvertApp] Writing 9679 bytes to object_detection.py
eepp@hood:pythonCode$ ls
object_detection.ipynb  object_detection.py
eepp@hood:pythonCode$ cp object_detection.py.org
cp: missing destination file operand after 'object_detection.py.org'
Try 'cp --help' for more information.
eepp@hood:pythonCode$ cp object_detection.py object_detection.py.org
e







eepp@hood:pythonCode$ 
eepp@hood:pythonCode$ 
eepp@hood:pythonCode$ 
eepp@hood:pythonCode$ python3 object_detection.py 
2021-12-31 19:47:34.801812: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /home/eepp/image_ws/install/stereo_image_proc/lib:/home/eepp/image_ws/install/realsense_ros/lib:/home/eepp/image_ws/install/realsense_msgs/lib:/home/eepp/image_ws/install/realsense_camera_msgs/lib:/home/eepp/image_ws/install/image_view/lib:/home/eepp/image_ws/install/image_rotate/lib:/home/eepp/image_ws/install/image_proc/lib:/home/eepp/image_ws/install/depth_image_proc/lib:/home/eepp/turtlebot3_ws/install/turtlebot3_msgs/lib:/usr/lib/x86_64-linux-gnu/gazebo-11/plugins:/opt/ros/foxy/opt/yaml_cpp_vendor/lib:/opt/ros/foxy/opt/rviz_ogre_vendor/lib:/opt/ros/foxy/lib/x86_64-linux-gnu:/opt/ros/foxy/lib
2021-12-31 19:47:34.801879: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2.5.0
2021-12-31 19:47:38.330149: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-12-31 19:47:38.333302: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /home/eepp/image_ws/install/stereo_image_proc/lib:/home/eepp/image_ws/install/realsense_ros/lib:/home/eepp/image_ws/install/realsense_msgs/lib:/home/eepp/image_ws/install/realsense_camera_msgs/lib:/home/eepp/image_ws/install/image_view/lib:/home/eepp/image_ws/install/image_rotate/lib:/home/eepp/image_ws/install/image_proc/lib:/home/eepp/image_ws/install/depth_image_proc/lib:/home/eepp/turtlebot3_ws/install/turtlebot3_msgs/lib:/usr/lib/x86_64-linux-gnu/gazebo-11/plugins:/opt/ros/foxy/opt/yaml_cpp_vendor/lib:/opt/ros/foxy/opt/rviz_ogre_vendor/lib:/opt/ros/foxy/lib/x86_64-linux-gnu:/opt/ros/foxy/lib
2021-12-31 19:47:38.333347: W tensorflow/stream_executor/cuda/cuda_driver.cc:326] failed call to cuInit: UNKNOWN ERROR (303)
2021-12-31 19:47:38.333388: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (hood): /proc/driver/nvidia/version does not exist
The following GPU devices are available: 
Image downloaded to /tmp/tmpenuxm37h.jpg.
2021-12-31 19:48:37.105879: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:176] None of the MLIR Optimization Passes are enabled (registered 2)
2021-12-31 19:48:37.126158: I tensorflow/core/platform/profile_utils/cpu_utils.cc:114] CPU Frequency: 1895470000 Hz
Found 100 objects.
Inference time:  52.270376682281494
Image downloaded to /tmp/tmpakhks2xn.jpg.
Found 100 objects.
Inference time:  17.627694845199585
Inference time: 18.38519525527954
Image downloaded to /tmp/tmpwsuhq0q0.jpg.
Found 100 objects.
Inference time:  17.668944120407104
Inference time: 18.37464714050293
Image downloaded to /tmp/tmpe70ovps5.jpg.
Found 100 objects.
Inference time:  17.748656272888184
Inference time: 18.863006114959717
eepp@hood:pythonCode$ 



--------------------------------------------------

passed the stuff from oject_detection.py into the python window

eepp@hood:pythonCode$ 
eepp@hood:pythonCode$ python3
Python 3.8.10 (default, Nov 26 2021, 20:14:08) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
2021-12-31 19:53:56.704824: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /home/eepp/image_ws/install/stereo_image_proc/lib:/home/eepp/image_ws/install/realsense_ros/lib:/home/eepp/image_ws/install/realsense_msgs/lib:/home/eepp/image_ws/install/realsense_camera_msgs/lib:/home/eepp/image_ws/install/image_view/lib:/home/eepp/image_ws/install/image_rotate/lib:/home/eepp/image_ws/install/image_proc/lib:/home/eepp/image_ws/install/depth_image_proc/lib:/home/eepp/turtlebot3_ws/install/turtlebot3_msgs/lib:/usr/lib/x86_64-linux-gnu/gazebo-11/plugins:/opt/ros/foxy/opt/yaml_cpp_vendor/lib:/opt/ros/foxy/opt/rviz_ogre_vendor/lib:/opt/ros/foxy/lib/x86_64-linux-gnu:/opt/ros/foxy/lib
2021-12-31 19:53:56.704869: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
>>> 
>>> import tensorflow_hub as hub
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> # For downloading the image.
>>> import matplotlib.pyplot as plt
>>> import tempfile
>>> from six.moves.urllib.request import urlopen
>>> from six import BytesIO
>>> 
>>> 
>>> 
>>> 
>>> 
>>> # For drawing onto the image.
>>> import numpy as np
>>> from PIL import Image
>>> from PIL import ImageColor
>>> from PIL import ImageDraw
>>> from PIL import ImageFont
>>> from PIL import ImageOps
>>> 
>>> 
>>> 
>>> 
>>> print(tf.__version__)
2.5.0
>>> 
>>> 
>>> 
>>> 
>>> 
>>> # Check available GPU devices.
>>> print("The following GPU devices are available: %s" % tf.test.gpu_device_name())
2021-12-31 19:55:48.230901: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-12-31 19:55:48.231467: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /home/eepp/image_ws/install/stereo_image_proc/lib:/home/eepp/image_ws/install/realsense_ros/lib:/home/eepp/image_ws/install/realsense_msgs/lib:/home/eepp/image_ws/install/realsense_camera_msgs/lib:/home/eepp/image_ws/install/image_view/lib:/home/eepp/image_ws/install/image_rotate/lib:/home/eepp/image_ws/install/image_proc/lib:/home/eepp/image_ws/install/depth_image_proc/lib:/home/eepp/turtlebot3_ws/install/turtlebot3_msgs/lib:/usr/lib/x86_64-linux-gnu/gazebo-11/plugins:/opt/ros/foxy/opt/yaml_cpp_vendor/lib:/opt/ros/foxy/opt/rviz_ogre_vendor/lib:/opt/ros/foxy/lib/x86_64-linux-gnu:/opt/ros/foxy/lib
2021-12-31 19:55:48.231520: W tensorflow/stream_executor/cuda/cuda_driver.cc:326] failed call to cuInit: UNKNOWN ERROR (303)
2021-12-31 19:55:48.231557: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (hood): /proc/driver/nvidia/version does not exist
The following GPU devices are available: 
>>> 
>>> 
>>> 
>>> 
>>> 
>>>
>>> 
>>> 
>>> def display_image(image):
...   fig = plt.figure(figsize=(20, 15))
...   plt.grid(False)
...   plt.imshow(image)
... 
>>> 
>>> 
>>> 
>>> def download_and_resize_image(url, new_width=256, new_height=256,
...                               display=False):
...   _, filename = tempfile.mkstemp(suffix=".jpg")
...   response = urlopen(url)
...   image_data = response.read()
...   image_data = BytesIO(image_data)
...   pil_image = Image.open(image_data)
...   pil_image = ImageOps.fit(pil_image, (new_width, new_height), Image.ANTIALIAS)
...   pil_image_rgb = pil_image.convert("RGB")
...   pil_image_rgb.save(filename, format="JPEG", quality=90)
...   print("Image downloaded to %s." % filename)
...   if display:
...     display_image(pil_image)
...   return filename
... 


The above all worked good. when i got to the next function
the black lines messed this up
removing the blank links fixed issue



>>> 
>>> 
>>> def draw_bounding_box_on_image(image,
...                                ymin,
...                                xmin,
...                                ymax,
...                                xmax,
...                                color,
...                                font,
...                                thickness=4,
...                                display_str_list=()):
...   """Adds a bounding box to an image."""
...   draw = ImageDraw.Draw(image)
...   im_width, im_height = image.size
...   (left, right, top, bottom) = (xmin * im_width, xmax * im_width,
...                                 ymin * im_height, ymax * im_height)
...   draw.line([(left, top), (left, bottom), (right, bottom), (right, top),
...              (left, top)],
...             width=thickness,
...             fill=color)
... 
>>>   # If the total height of the display strings added to the top of the bounding
>>>   # box exceeds the top of the image, stack the strings below the bounding box
>>>   # instead of above.
>>>   display_str_heights = [font.getsize(ds)[1] for ds in display_str_list]
  File "<stdin>", line 1
    display_str_heights = [font.getsize(ds)[1] for ds in display_str_list]
    ^
IndentationError: unexpected indent
>>>   # Each display_str has a top and bottom margin of 0.05x.
>>>   total_display_str_height = (1 + 2 * 0.05) * sum(display_str_heights)
  File "<stdin>", line 1
    total_display_str_height = (1 + 2 * 0.05) * sum(display_str_heights)
    ^
IndentationError: unexpected indent
>>> 
>>>   if top > total_display_str_height:
  File "<stdin>", line 1
    if top > total_display_str_height:
    ^
IndentationError: unexpected indent
>>>     text_bottom = top
  File "<stdin>", line 1
    text_bottom = top
    ^
IndentationError: unexpected indent
>>>   else:
  File "<stdin>", line 1
    else:
    ^
IndentationError: unexpected indent
>>>     text_bottom = top + total_display_str_height
  File "<stdin>", line 1
    text_bottom = top + total_display_str_height
    ^
IndentationError: unexpected indent
>>>   # Reverse list and print from bottom to top.
>>>   for display_str in display_str_list[::-1]:
  File "<stdin>", line 1
    for display_str in display_str_list[::-1]:
    ^
IndentationError: unexpected indent
>>>     text_width, text_height = font.getsize(display_str)
  File "<stdin>", line 1
    text_width, text_height = font.getsize(display_str)
    ^
IndentationError: unexpected indent
>>>     margin = np.ceil(0.05 * text_height)
  File "<stdin>", line 1
    margin = np.ceil(0.05 * text_height)
    ^
IndentationError: unexpected indent
>>>     draw.rectangle([(left, text_bottom - text_height - 2 * margin),
  File "<stdin>", line 1
    draw.rectangle([(left, text_bottom - text_height - 2 * margin),
    ^
IndentationError: unexpected indent
>>>                     (left + text_width, text_bottom)],
  File "<stdin>", line 1
    (left + text_width, text_bottom)],
    ^
IndentationError: unexpected indent
>>>                    fill=color)
  File "<stdin>", line 1
    fill=color)
    ^
IndentationError: unexpected indent
>>>     draw.text((left + margin, text_bottom - text_height - margin),
  File "<stdin>", line 1
    draw.text((left + margin, text_bottom - text_height - margin),
    ^
IndentationError: unexpected indent
>>>               display_str,
  File "<stdin>", line 1
    display_str,
    ^
IndentationError: unexpected indent
>>>               fill="black",
  File "<stdin>", line 1
    fill="black",
    ^
IndentationError: unexpected indent
>>>               font=font)
  File "<stdin>", line 1
    font=font)
    ^
IndentationError: unexpected indent
>>>     text_bottom -= text_height - 2 * margin
  File "<stdin>", line 1
    text_bottom -= text_height - 2 * margin
    ^
IndentationError: unexpected indent
>>> 




>>> def draw_bounding_box_on_image(image,
...                                ymin,
...                                xmin,
...                                ymax,
...                                xmax,
...                                color,
...                                font,
...                                thickness=4,
...                                display_str_list=()):
...   """Adds a bounding box to an image."""
...   draw = ImageDraw.Draw(image)
...   im_width, im_height = image.size
...   (left, right, top, bottom) = (xmin * im_width, xmax * im_width,
...                                 ymin * im_height, ymax * im_height)
...   draw.line([(left, top), (left, bottom), (right, bottom), (right, top),
...              (left, top)],
...             width=thickness,
...             fill=color)
...   # If the total height of the display strings added to the top of the bounding
...   # box exceeds the top of the image, stack the strings below the bounding box
...   # instead of above.
...   display_str_heights = [font.getsize(ds)[1] for ds in display_str_list]
...   # Each display_str has a top and bottom margin of 0.05x.
...   total_display_str_height = (1 + 2 * 0.05) * sum(display_str_heights)
...   if top > total_display_str_height:
...     text_bottom = top
...   else:
...     text_bottom = top + total_display_str_height
...   # Reverse list and print from bottom to top.
...   for display_str in display_str_list[::-1]:
...     text_width, text_height = font.getsize(display_str)
...     margin = np.ceil(0.05 * text_height)
...     draw.rectangle([(left, text_bottom - text_height - 2 * margin),
...                     (left + text_width, text_bottom)],
...                    fill=color)
...     draw.text((left + margin, text_bottom - text_height - margin),
...               display_str,
...               fill="black",
...               font=font)
...     text_bottom -= text_height - 2 * margin
... 
>>> 
>>> 
>>> 
>>> 
>>> def draw_boxes(image, boxes, class_names, scores, max_boxes=10, min_score=0.1):
...   """Overlay labeled boxes on an image with formatted scores and label names."""
...   colors = list(ImageColor.colormap.values())
...   try:
...     font = ImageFont.truetype("/usr/share/fonts/truetype/liberation/LiberationSansNarrow-Regular.ttf",
...                               25)
...   except IOError:
...     print("Font not found, using default font.")
...     font = ImageFont.load_default()
...   for i in range(min(boxes.shape[0], max_boxes)):
...     if scores[i] >= min_score:
...       ymin, xmin, ymax, xmax = tuple(boxes[i])
...       display_str = "{}: {}%".format(class_names[i].decode("ascii"),
...                                      int(100 * scores[i]))
...       color = colors[hash(class_names[i]) % len(colors)]
...       image_pil = Image.fromarray(np.uint8(image)).convert("RGB")
...       draw_bounding_box_on_image(
...           image_pil,
...           ymin,
...           xmin,
...           ymax,
...           xmax,
...           color,
...           font,
...           display_str_list=[display_str])
...       np.copyto(image, np.array(image_pil))
...   return image
...


don't forget these defines

>>> image_url = "https://upload.wikimedia.org/wikipedia/commons/6/60/Naxos_Taverna.jpg"  #@param
>>> downloaded_image_path = download_and_resize_image(image_url, 1280, 856, True)
Image downloaded to /tmp/tmpxsui99xj.jpg.
>>> 
>>> 
>




>>> module_handle = "https://tfhub.dev/google/faster_rcnn/openimages_v4/inception_resnet_v2/1" #@param ["https://tfhub.dev/google/openimages_v4/ssd/mobilenet_v2/1", "https://tfhub.dev/google/faster_rcnn/openimages_v4/inception_resnet_v2/1"]
>>> 
>>> 
>>>> 
>>> def load_img(path):
...   img = tf.io.read_file(path)
...   img = tf.image.decode_jpeg(img, channels=3)
...   return img
... 
>>> 
>>>> 
>>> 
>>> def run_detector(detector, path):
...   img = load_img(path)
...   converted_img  = tf.image.convert_image_dtype(img, tf.float32)[tf.newaxis, ...]
...   start_time = time.time()
...   result = detector(converted_img)
...   end_time = time.time()
...   result = {key:value.numpy() for key,value in result.items()}
...   print("Found %d objects." % len(result["detection_scores"]))
...   print("Inference time: ", end_time-start_time)
...   image_with_boxes = draw_boxes(
...       img.numpy(), result["detection_boxes"],
...       result["detection_class_entities"], result["detection_scores"])
...   display_image(image_with_boxes)
... 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> run_detector(detector, downloaded_image_path)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'detector' is not defined
>>> 
>>> 
>>>


forgot the lines that defined detector

>>> 
>>> detector = hub.load(module_handle).signatures['default']
INFO:tensorflow:Saver not created because there are no variables in the graph to restore
INFO:tensorflow:Saver not created because there are no variables in the graph to restore
2022-01-01 14:27:49.182404: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:176] None of the MLIR Optimization Passes are enabled (registered 2)
2022-01-01 14:27:49.201986: I tensorflow/core/platform/profile_utils/cpu_utils.cc:114] CPU Frequency: 1895645000 Hz
>>> 
>>> 
>>> 
>>> 
>>> 
>>> run_detector(detector, downloaded_image_path)
Found 100 objects.
Inference time:  49.333045959472656
>>> 
>>> 
>>> 
>

images are not display when running from pythod command line

Also I could not get the following to execute in any form I tried


>>> 
>>> display_image(downloaded_image_path)
Traceback (most recent call last):