金子邦彦研究室人工知能人工知能の実行(Python を使用)(Windows 上)TensorFlow 2 Object Detection API (オブジェクト検出 API)のインストールと一般物体検出(TensorFlow のチュートリアルのプログラムを使用)(Google Colab あるいは Ubuntu 上)

TensorFlow 2 Object Detection API (オブジェクト検出 API)のインストールと一般物体検出(TensorFlow のチュートリアルのプログラムを使用)(Google Colab あるいは Ubuntu 上)

一般物体検出を行う.

用語:

Google Colab へのリンク

次の URL で,Google Colaboratory のノートブックを準備している.

次のリンクをクリックすると,Google Colaboratoryノートブックが開く. そして,Google アカウントでログインすると,Google Colaboratory のノートブック内のコード等を編集したり再実行したりができる.編集した場合でも,他の人に影響が出たりということはない.そして,編集後のものを,各自の Google ドライブ内に保存することもできる.

https://colab.research.google.com/drive/1HCLnXLqZcMwV52ss8G57ZpJRvc4ZBqBr?usp=sharing

TensorFlow Models のインストール

  1. protobuf-compiler のインストール
    sudo apt -yV install protobuf-compiler
    
  2. TensorFlow models のダウンロード,TensorFlow Object Detection API のインストール

    まずは、環境変数 MYDIR にディレクトリを設定。MYDIR は、あとで使う。

    次の書き方は、環境変数を使うためのもの.

    sudo mkdir /content
    sudo chown $USER /content
    export MYDIR=/content
    
    Google Colaboratory で環境変数を使うときは,次のように書く
    import os
    os.environ['MYDIR']='/content'
    

    次に,TensorFlow models のダウンロード,TensorFlow Object Detection API のインストール

    MYDIR で設定したディレクトリを使う.

    プロトコル・バッファ・コンパイラ (protocol buffer compiler) を使用

    # TensorFlow models
    rm -rf ${MYDIR}/models
    cd ${MYDIR} && git clone https://github.com/tensorflow/models.git
    # Compile protos.
    cd ${MYDIR}/models/research
    protoc object_detection/protos/*.proto --python_out=.
    # Install TensorFlow Object Detection API
    cd $MYDIR/models/research
    cp object_detection/packages/tf2/setup.py .
    sudo apt -yV install python3-pip3
    cd $MYDIR/models/research && sudo pip3 install .
    
  3. pycocotools のインストール

    MYDIR で設定したディレクトリを使う

    rm -rf $MYDIR/cocoapi
    cd $MYDIR && git clone https://github.com/cocodataset/cocoapi.git
    cd $MYDIR && cd cocoapi/PythonAPI && python3 setup.py build_ext --inplace
    cd $MYDIR/cocoapi/PythonAPI && cp -r pycocotools $MYDIR/models/research/
    
  4. 今までのインストールの確認

    最後に「OK ...」のように表示されることを確認.

    cd $MYDIR/models/research && python3 object_detection/builders/model_builder_tf2_test.py
    
  5. object_detection を Python 実行時のカレントディレクトリに置く操作

    最後に「Finished processing dependencies for object-detection...」のように表示されることを確認.

    cp $MYDIR/models/research/object_detection/packages/tf2/setup.py .
    python3 setup.py build && sudo python3 setup.py install
    
  6. テスト画像のダウンロード、処理対象となる画像ファイル名の設定など

    TensorFlow models のテスト画像をダウンロードする.

    テスト画像のダウンロード、処理対処となる画像ファイル名の設定は、 次で公開されているプログラムをそのまま使用.

    https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/auto_examples/plot_object_detection_saved_model.html#sphx-glr-auto-examples-plot-object-detection-saved-model-py

    import os
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'    # Suppress TensorFlow logging (1)
    import pathlib
    import tensorflow as tf
    
    tf.get_logger().setLevel('ERROR')           # Suppress TensorFlow logging (2)
    
    # Enable GPU dynamic memory allocation
    gpus = tf.config.experimental.list_physical_devices('GPU')
    for gpu in gpus:
        tf.config.experimental.set_memory_growth(gpu, True)
    
    def download_images():
        base_url = 'https://raw.githubusercontent.com/tensorflow/models/master/research/object_detection/test_images/'
        filenames = ['image1.jpg', 'image2.jpg']
        image_paths = []
        for filename in filenames:
            image_path = tf.keras.utils.get_file(fname=filename,
                                                origin=base_url + filename,
                                                untar=False)
            image_path = pathlib.Path(image_path)
            image_paths.append(str(image_path))
        return image_paths
    
    IMAGE_PATHS = download_images()
    
  7. COCO データセットで学習済みのモデルのダウンロード

    TensorFlow models のものをダウンロード

    ダウンロードを行うために,次で公開されているプログラムをそのまま使用.

    https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/auto_examples/plot_object_detection_saved_model.html#sphx-glr-auto-examples-plot-object-detection-saved-model-py

    # Download and extract model
    def download_model(model_name, model_date):
        base_url = 'http://download.tensorflow.org/models/object_detection/tf2/'
        model_file = model_name + '.tar.gz'
        model_dir = tf.keras.utils.get_file(fname=model_name,
                                            origin=base_url + model_date + '/' + model_file,
                                            untar=True)
        return str(model_dir)
    
    MODEL_DATE = '20200711'
    MODEL_NAME = 'centernet_hg104_1024x1024_coco17_tpu-32'
    PATH_TO_MODEL_DIR = download_model(MODEL_NAME, MODEL_DATE)
    
  8. ラベルに関するデータのダウンロード

    TensorFlow models のものをダウンロード

    ダウンロードを行うために,次で公開されているプログラムをそのまま使用.

    https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/auto_examples/plot_object_detection_saved_model.html#sphx-glr-auto-examples-plot-object-detection-saved-model-py

    # Download labels file
    def download_labels(filename):
        base_url = 'https://raw.githubusercontent.com/tensorflow/models/master/research/object_detection/data/'
        label_dir = tf.keras.utils.get_file(fname=filename,
                                            origin=base_url + filename,
                                            untar=False)
        label_dir = pathlib.Path(label_dir)
        return str(label_dir)
    
    LABEL_FILENAME = 'mscoco_label_map.pbtxt'
    PATH_TO_LABELS = download_labels(LABEL_FILENAME)
    
  9. 学習済みモデルのロード

    次で公開されているプログラムをそのまま使用.

    https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/auto_examples/plot_object_detection_saved_model.html#sphx-glr-auto-examples-plot-object-detection-saved-model-py

    import time
    from object_detection.utils import label_map_util
    from object_detection.utils import visualization_utils as viz_utils
    
    PATH_TO_SAVED_MODEL = PATH_TO_MODEL_DIR + "/saved_model"
    
    print('Loading model...', end='')
    start_time = time.time()
    
    # Load saved model and build the detection function
    detect_fn = tf.saved_model.load(PATH_TO_SAVED_MODEL)
    
    end_time = time.time()
    elapsed_time = end_time - start_time
    print('Done! Took {} seconds'.format(elapsed_time))
    
  10. ラベルに関する処理

    次で公開されているプログラムをそのまま使用.

    https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/auto_examples/plot_object_detection_saved_model.html#sphx-glr-auto-examples-plot-object-detection-saved-model-py

    category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS,
                                                                        use_display_name=True)
    
  11. 一般物体認識の実行

    次で公開されているプログラムをそのまま使用.

    https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/auto_examples/plot_object_detection_saved_model.html#sphx-glr-auto-examples-plot-object-detection-saved-model-py

    import numpy as np
    from PIL import Image
    %matplotlib inline
    import matplotlib.pyplot as plt
    import warnings
    warnings.filterwarnings('ignore')   # Suppress Matplotlib warnings
    
    def load_image_into_numpy_array(path):
        """Load an image from file into a numpy array.
    
        Puts image into numpy array to feed into tensorflow graph.
        Note that by convention we put it into a numpy array with shape
        (height, width, channels), where channels=3 for RGB.
    
        Args:
          path: the file path to the image
    
        Returns:
          uint8 numpy array with shape (img_height, img_width, 3)
        """
        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)
        # Things to try:
        # Flip horizontally
        # image_np = np.fliplr(image_np).copy()
        # Convert image to grayscale
        # image_np = np.tile(
        #     np.mean(image_np, 2, keepdims=True), (1, 1, 3)).astype(np.uint8)
        # The input needs to be a tensor, convert it using `tf.convert_to_tensor`.
        input_tensor = tf.convert_to_tensor(image_np)
        # The model expects a batch of images, so add an axis with `tf.newaxis`.
        input_tensor = input_tensor[tf.newaxis, ...]
        # input_tensor = np.expand_dims(image_np, 0)
        detections = detect_fn(input_tensor)
        # All outputs are batches tensors.
        # Convert to numpy arrays, and take index [0] to remove the batch dimension.
        # We're only interested in the first num_detections.
        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
        # detection_classes should be ints.
        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=.30,
            agnostic_mode=False)
        plt.figure(figsize=(10,10), dpi=200)
        plt.imshow(image_np_with_detections)
        print('Done')
    
    plt.show()