Posted on

Tensorflow.js的coco-ssd對象檢測

官方教程

教程網址: https://github.com/tensorflow/tfjs-models/tree/master/coco-ssd

目標檢測模型,旨在定位和識別單個圖像中的多個目標。

該模型是 COCO-SSD 模型的 TensorFlow.js 移植。有關 Tensorflow 對象檢測 API 的更多信息,請查看 tensorflow/object_detection中的自述文件。

該模型檢測 COCO 數據集中定義的對象,這是一個大規模的對象檢測、分割和字幕數據集。您可以在這裡找到更多信息。該模型能夠檢測80 類物體。(SSD 代表單次多盒檢測)。

此 TensorFlow.js 模型不需要您了解機器學習。它可以將輸入作為任何基於瀏覽器的圖像元素(例如<img><video><canvas> 元素),並返回帶有類名稱和置信度的邊界框數組。

<!-- Load TensorFlow.js. This is required to use coco-ssd model. -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"> </script>
<!-- Load the coco-ssd model. -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/coco-ssd"> </script>

<!-- Replace this with your image. Make sure CORS settings allow reading the image! -->
<img id="img" src="cat.jpg"/>

<!-- Place your code in the script tag below. You can also use an external .js file -->
<script>
  // Notice there is no 'import' statement. 'cocoSsd' and 'tf' is
  // available on the index-page because of the script tag above.

  const img = document.getElementById('img');

  // Load the model.
  cocoSsd.load().then(model => {
    // detect objects in the image.
    model.detect(img).then(predictions => {
      console.log('Predictions: ', predictions);
    });
  });
</script>

讀取攝像機

以下為一個簡單讀取攝像機並且作物件偵測的程式範例

<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <title>Test</title>
  <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"> </script>
  <script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/coco-ssd"> </script>
</head>

<body>
  <div class="select">
    <label for="videoSource">Video source: </label><select id="videoSource"></select>
  </div>
  <button id="showVideo">Open camera</button>
  <br />
  <!-- Video element to capture camera input -->
  <video id="video" autoplay playsinline style="position: absolute;z-index: -999;"></video><canvas id="canvas"
    width="100%"></canvas>
  <div id="message"></div>
  <script>
    const video = document.getElementById('video');
    const canvas = document.getElementById('canvas');
    const ctx = canvas.getContext('2d');
    document.querySelector('#showVideo').addEventListener('click', e => init(e));
    const videoSelect = document.querySelector('select#videoSource');
    function gotDevices(deviceInfos) {
      console.log(deviceInfos)
      deviceInfos.forEach(deviceInfo => {
        if (deviceInfo.kind == "videoinput") {
          const option = document.createElement('option');
          option.value = deviceInfo.deviceId;
          option.text = deviceInfo.label || `camera ${videoSelect.length + 1}`;
          videoSelect.appendChild(option);
        }
      });
    }
    function gotStream(stream) {
      window.stream = stream; // make stream available to console
      videoElement.srcObject = stream;
      // Refresh button list in case labels have become available
      return navigator.mediaDevices.enumerateDevices();
    }
    window.onload = () => {

      navigator.mediaDevices.enumerateDevices().then(gotDevices).catch(handleError);
      const constraints = {
        audio: { deviceId: audioSource ? { exact: audioSource } : undefined },
        video: { deviceId: videoSource ? { exact: videoSource } : undefined }
      };
      navigator.mediaDevices.getUserMedia(constraints).then(gotStream).then(gotDevices).catch(handleError);
    }


    function getRandomColor() {
      const randomColor = Math.floor(Math.random() * 16777215).toString(16);
      return "#" + ("000000" + randomColor).slice(-6);
    }
    function handleSuccess(stream) {
      const videoTracks = stream.getVideoTracks();
      var predictions = [];
      video.srcObject = stream;
      // When the video is playing, draw it on the canvas
      video.addEventListener('play', () => {
        canvas.width = video.videoWidth;
        canvas.height = video.videoHeight;
        // Continuously draw the video frames on the canvas
        function drawFrame() {
          // Draw the video frame on the canvas
          ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
          const scaleX = canvas.width / video.videoWidth;
          const scaleY = canvas.height / video.videoHeight;

          // Draw the bounding boxes on the canvas
          predictions.forEach(prediction => {
            const x = prediction.bbox[0] * scaleX;
            const y = prediction.bbox[1] * scaleY;
            const width = prediction.bbox[2] * scaleX;
            const height = prediction.bbox[3] * scaleY;

            ctx.strokeStyle = 'blue';
            ctx.lineWidth = 2;
            ctx.strokeRect(x, y, width, height);

            ctx.fillStyle = 'blue';
            ctx.font = '18px Arial';
            ctx.fillText(prediction.class, x, y);
          });
          // Call the drawFrame function again to continuously update the canvas
          requestAnimationFrame(drawFrame);
        }

        // Start drawing video frames
        drawFrame();
        // Load the model.
        cocoSsd.load().then(model => {
          function detectFrame() {
            // detect objects in the image.
            model.detect(video).then(preds => {
              predictions = preds
              setTimeout(detectFrame, 100);
            });
          }
          detectFrame()
        });
      });
    }

    function handleError(error) {
      if (error.name === 'OverconstrainedError') {
        const v = constraints.video;
        alert(`The resolution ${v.width.exact}x${v.height.exact} px is not supported by your device.`);
      } else if (error.name === 'NotAllowedError') {
        alert('Permissions have not been granted to use your camera and ' +
          'microphone, you need to allow the page access to your devices in ' +
          'order for the demo to work.');
      }
      alert(`getUserMedia error: ${error.name}`, error);
    }

    async function init(e) {
      try {
        const stream = await navigator.mediaDevices.getUserMedia({ audio: false, video: { deviceId: videoSelect.value ? { exact: videoSelect.value } : undefined } });
        handleSuccess(stream);
        e.target.disabled = true;
      } catch (e) {
        handleError(e);
      }
    }
  </script>
</body>

</html>

執行成果

以下為範例執行程式: https://claire-chang.com/wp-content/uploads/2023/08/sample.html

要開啟鏡頭權限需要至網站設定開啟權限

Posted on

TensorFlow目標檢測API – 訓練自己的資料

安裝labelImg來標記物件

到GitHub官網上面可以下載release的版本

下載網址labelImg

labelImg操作介面

準備訓練用資料集

以下是一個基本步驟:

  1. 資料準備:準備你的圖像和標籤,並將其轉換成適合 TensorFlow 模型訓練的格式。這通常是使用 TensorFlow 的 TFRecord 格式。如果你的資料不是這種格式,你可以使用 TensorFlow 的 tf.Example 類別來轉換它。你需要為每個圖像創建一個 tf.Example,該例包含圖像數據以及每個目標的邊界框和類別。
  2. 下載並選擇模型:從 TensorFlow Object Detection API 的 模型 zoo 下載 SSD MobileNet V2 FPNLite 640×640。這是一個已經在 COCO 數據集上預訓練的模型,可以用來微調。
  3. 修改配置文件:每個模型都附帶一個配置文件(pipeline.config),它定義了訓練過程中使用的各種參數。例如,它指定了模型架構,輸入尺寸,訓練策略等等。你需要修改這個配置文件以適應你的資料。例如,你需要更改 num_classes 參數以反映你的資料中的類別數量,並且需要更改 input_pathlabel_map_path 參數以指向你的訓練和驗證數據。
  4. 訓練模型:然後,你可以使用 TensorFlow Object Detection API 的 model_main_tf2.py 腳本來訓練你的模型。你需要將 pipeline_config_path 參數設置為你的配置文件的位置,並將 model_dir 參數設置為你希望保存訓練模型的位置。
  5. 評估模型:在訓練過程中和訓練完成後,你應評估模型的性能。這也可以使用 model_main_tf2.py 腳本來完成,只需將 checkpoint_dir 參數設置為包含你的訓練模型檢查點的目錄。
  6. 導出模型:一旦你對模型的性能滿意,你可以使用 exporter_main_v2.py 腳本將其導出為用於推理的圖模型。

將圖片和標籤轉換成TFRecord格式

以下為一個範例程式

import os
import tensorflow as tf
from object_detection.utils import dataset_util, label_map_util

# 適應你的標籤。標籤字典應該映射類別名稱到整數ID。
labels_path = 'path_to_your_labels.pbtxt'
label_map = label_map_util.load_labelmap(labels_path)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=90)
category_index = label_map_util.create_category_index(categories)

def create_tf_example(image_path, annotations_list):
    # 從圖像檔案讀取數據並將其轉換為tf.Example。

    img_data = tf.io.gfile.GFile(image_path, 'rb').read()
    img = Image.open(image_path)
    width, height = img.size

    filename = image_path.encode('utf8')
    image_format = b'jpeg'  # or b'png'

    xmins = []  # List of normalized left x coordinates in bounding box (1 per box)
    xmaxs = []  # List of normalized right x coordinates in bounding box (1 per box)
    ymins = []  # List of normalized top y coordinates in bounding box (1 per box)
    ymaxs = []  # List of normalized bottom y coordinates in bounding box (1 per box)
    classes_text = []  # List of string class name of bounding box (1 per box)
    classes = []  # List of integer class id of bounding box (1 per box)

    for ann in annotations_list:
        xmins.append(float(ann['xmin']) / width)
        xmaxs.append(float(ann['xmax']) / width)
        ymins.append(float(ann['ymin']) / height)
        ymaxs.append(float(ann['ymax']) / height)
        classes_text.append(ann['class'].encode('utf8'))
        classes.append(category_index[ann['class']]['id'])

    tf_example = tf.train.Example(features=tf.train.Features(feature={
        'image/height': dataset_util.int64_feature(height),
        'image/width': dataset_util.int64_feature(width),
        'image/filename': dataset_util.bytes_feature(filename),
        'image/source_id': dataset_util.bytes_feature(filename),
        'image/encoded': dataset_util.bytes_feature(img_data),
        'image/format': dataset_util.bytes_feature(image_format),
        'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
        'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
        'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
        'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
        'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
        'image/object/class/label': dataset_util.int64_list_feature(classes),
    }))
    return tf_example

writer = tf.io.TFRecordWriter('path_to_output.tfrecord')

# 適應你的圖像路徑和標籤。
images_path = 'path_to_your_images'
annotations_path = 'path_to_your_annotations'

for image_file in os.listdir(images_path):
    image_path = os.path.join(images_path, image_file)
    annotation_path = os.path.join(annotations_path, image_file.replace('.jpg', '.xml'))

    # 讀取標籤檔案並解析標籤。你可能需要根據你的標籤格式調整這個。
    with open(annotation_path) as f:
        annotation_xml = f.read()
    annotations = parse_annotations(annotation_xml)  # 這個函數根據你的標籤格式。

    tf_example = create_tf_example(image_path, annotations)
    writer.write(tf_example.SerializeToString())

writer.close()

準備分類設定檔案*.pbtxt

接著上面會產出path_to_output.tfrecord這個檔案,到object detection裡新建一個資料夾,把path_to_output.tfrecord丟進去。

接著,修改mscoco_label_map.pbtxt(可在\models\research\object_detection\data找到此檔案),檔案內容如下:

item {
  name: "/m/01g317"
  id: 1
  display_name: "person"
}
item {
  name: "/m/0199g"
  id: 2
  display_name: "bicycle"
}
item {
  name: "/m/0k4j"
  id: 3
  display_name: "car"
}
...

這邊的/m/01g317是一個在Google Knowledge Graph中的專屬識別碼(獨特的標籤),用來代表特定的實體或概念。在這裡,/m/01g317是代表”person”(人)這個概念的識別碼。

Google Knowledge Graph是一個大型的知識庫,由Google建立和維護,用來增強其搜索引擎的搜索結果,提供更準確和詳細的資訊。它儲存了數以億計的物件和實體以及他們之間的連結。每個物件或實體都被分配一個獨特的ID,這就是這裡提到的/m/01g317

在訓練物體檢測模型時,這些識別碼會被用於表示各種可以識別的物體或實體。例如,在你給出的配置中,/m/01g317被指定為表示”person”。

如果你是在自己的專案中,並且與Google Knowledge Graph無關,那麼可以自由地創建你自己的類別標籤或ID。例如,你可以簡單地使用數字(如1,2,3等)或有意義的名稱作為你的類別標籤。在你給出的配置項中,只需要確保每個類別的nameid是唯一的,並且display_name清楚地表示了該類別的含義。

於pipeline.config指定資料集的位置

pipeline.config檔案中,你將會找到兩個主要的部分,分別代表訓練和驗證數據集,它們的名稱通常為 train_input_readereval_input_reader

例如

model {
  // Model settings go here
  ...
}

train_config {
  // Training settings go here
  ...
}

train_input_reader: {
  tf_record_input_reader {
    input_path: "path_to_train.tfrecord"
  }
  label_map_path: "path_to_label_map.pbtxt"
}

eval_config {
  // Evaluation settings go here
  ...
}

eval_input_reader: {
  tf_record_input_reader {
    input_path: "path_to_eval.tfrecord"
  }
  label_map_path: "path_to_label_map.pbtxt"
}

在這個配置檔案中,path_to_train.tfrecordpath_to_eval.tfrecord 是你的訓練和驗證TFRecord檔案的路徑。你需要將這些路徑換成你的TFRecord檔案的實際路徑。

路徑path_to_train.tfrecordpath_to_label_map.pbtxt通常是指向文件系统上的絕對路徑,不是相對於pipeline.config文件的相對路徑。

例如,如果你的訓練TFRecord文件位於/home/user/dataset/train.tfrecord,你應該這樣設置:

train_input_reader: {
  tf_record_input_reader {
    input_path: "/home/user/dataset/train.tfrecord"
  }
  label_map_path: "/path_to/your_label_map.pbtxt"
}

train_input_readereval_input_reader分別用於指定訓練數據和評估數據的路徑。這些數據通常是標註過的圖像,已經被轉化為TFRecord格式。

使用自訂資料集進行訓練

訓練模型的指令:

python models/research/object_detection/model_main_tf2.py \
    --pipeline_config_path=path_to_your_pipeline.config \
    --model_dir=path_to_output_model_dir \
    --alsologtostderr
Posted on

TensorFlow目標檢測API – 程式使用範例

事前作業

先在這一篇選定想要使用的模型,然後下載後解壓縮在專案的資料夾內,然後參考這篇文章設定環境: TensorFlow 的目標檢測API – 設定環境

程式實作範例

這個範例需要一個攝影機,使用的是SSD MobileNet V2 FPNLite 640×640的預設資料集

import numpy as np
import tensorflow as tf
import VideoStream
import cv2

from object_detection.utils import label_map_util

# 載入模型
PATH_TO_SAVED_MODEL = "./ssd_mobilenet/saved_model"
# 載入模型
detect_fn = tf.saved_model.load(PATH_TO_SAVED_MODEL)

# 載入標籤
PATH_TO_LABELS = './models/research/object_detection/data/mscoco_label_map.pbtxt'
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)

import time

# 設定攝影機
videostream = VideoStream.VideoStream((1920, 1080), 30, 0).start()
cam_quit = 0
while cam_quit == 0:
    imageSource = videostream.read()
    smallImg = cv2.resize(imageSource, (1920//5, 1080//5))
    input_tensor = np.expand_dims(smallImg, 0)
    start_time = time.time()
    detections = detect_fn(input_tensor)
    end_time = time.time()
    num_detections = int(detections.pop('num_detections'))
    elapsed_time = end_time - start_time
    print('Done! Took {} seconds'.format(elapsed_time))
    detections = {key: value[0, :num_detections].numpy()
                  for key, value in detections.items()}
    for detection_boxes, detection_classes, detection_scores in \
            zip(detections['detection_boxes'], detections['detection_classes'], detections['detection_scores']):
        if detection_scores > 0.3:
            y_min_pixel = int(detection_boxes[0] * 1080)
            x_min_pixel = int(detection_boxes[1] * 1920)
            y_max_pixel = int(detection_boxes[2] * 1080)
            x_max_pixel = int(detection_boxes[3] * 1920)
            cv2.rectangle(imageSource, (x_min_pixel, y_min_pixel), (x_max_pixel, y_max_pixel), (255, 0, 0), 2)
    cv2.imshow('frame', imageSource)
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        cam_quit = 1

cv2.destroyAllWindows()
videostream.stop()

執行的速度很不錯,一秒可以有25FPS,適合用於實時串流

執行的畫面如下,因為懶惰所以還沒有把標記的label標上去,若想要標記,可直接使用detections['detection_classes']作為分類的index,從category_index去取分類名稱

Posted on

TensorFlow目標檢測API – 功能介紹

YOLO並沒有含在TF2內建的Object Detection API

TensorFlow Object Detection API 提供了在COCO 2017 數據集上預訓練的檢測模型集合 。有多種類型的模型,包括 CenterNet,EfficientDet,SSD,Faster R-CNN 和 Mask R-CNN 等。

每個模型都有其特點:

  • CenterNet:是一種基於關鍵點檢測的目標檢測算法,它使用特定的關鍵點(例如物體的中心點)進行檢測,可以用於處理各種大小的物體,並且可以輸出更豐富的目標信息(如姿態,關鍵點等)。
  • EfficientDet:是一種基於 EfficientNet 和 BiFPN 的目標檢測算法,其優點在於精度高並且效率也相當高,模型大小和計算量都相對較小。
  • SSD (Single Shot MultiBox Detector):是一種用於實時目標檢測的算法,其特點是速度快,適用於需要實時響應的情況。
  • Faster R-CNN:是一種基於區域提議網絡(RPN)的目標檢測算法,其精度很高,但速度相對較慢。
  • Mask R-CNN:是一種擴展了 Faster R-CNN,能進行實例分割(Instance Segmentation)的算法,即除了檢測目標之外,還可以輸出目標的像素級別的掩碼。

然而,YOLO (You Only Look Once) 並沒有在這個列表中。 YOLO 是一種非常流行的實時目標檢測系統,以其快速和精確而著名。 YOLO 的預訓練模型並不在 TensorFlow Hub 提供,但你可以在 YOLO 的官方網站或者 Github 倉庫找到相關的預訓練模型和代碼(https://github.com/yyccR/yolov5_in_tf2_keras)。

如果你有特定的需求,例如對速度,精度,或者是特定類型的任務(例如實例分割或者姿態估計)有特殊的需求,可根據這些需求來選擇合適的模型。目前YOLO在PyTorch的支持進度比較快,到筆者這篇文章時,TF2對YOLO的支持只到yolov5。

包含的物件檢測模型列表

在這邊Speed越低代表速度越快,因為目標網址的CORSS DOMAIN的問題,要下載會需要按右鍵儲存網址然後到新視窗貼上才可以下載

Model nameSpeed (ms)COCO mAPOutputs
CenterNet HourGlass104 512×5127041.9Boxes
CenterNet HourGlass104 Keypoints 512×5127640.0/61.4Boxes/Keypoints
CenterNet HourGlass104 1024×102419744.5Boxes
CenterNet HourGlass104 Keypoints 1024×102421142.8/64.5Boxes/Keypoints
CenterNet Resnet50 V1 FPN 512×5122731.2Boxes
CenterNet Resnet50 V1 FPN Keypoints 512×5123029.3/50.7Boxes/Keypoints
CenterNet Resnet101 V1 FPN 512×5123434.2Boxes
CenterNet Resnet50 V2 512×5122729.5Boxes
CenterNet Resnet50 V2 Keypoints 512×5123027.6/48.2Boxes/Keypoints
CenterNet MobileNetV2 FPN 512×512623.4Boxes
CenterNet MobileNetV2 FPN Keypoints 512×512641.7Keypoints
EfficientDet D0 512×5123933.6Boxes
EfficientDet D1 640×6405438.4Boxes
EfficientDet D2 768×7686741.8Boxes
EfficientDet D3 896×8969545.4Boxes
EfficientDet D4 1024×102413348.5Boxes
EfficientDet D5 1280×128022249.7Boxes
EfficientDet D6 1280×128026850.5Boxes
EfficientDet D7 1536×153632551.2Boxes
SSD MobileNet v2 320×3201920.2Boxes
SSD MobileNet V1 FPN 640×6404829.1Boxes
SSD MobileNet V2 FPNLite 320×3202222.2Boxes
SSD MobileNet V2 FPNLite 640×6403928.2Boxes
SSD ResNet50 V1 FPN 640×640 (RetinaNet50)4634.3Boxes
SSD ResNet50 V1 FPN 1024×1024 (RetinaNet50)8738.3Boxes
SSD ResNet101 V1 FPN 640×640 (RetinaNet101)5735.6Boxes
SSD ResNet101 V1 FPN 1024×1024 (RetinaNet101)10439.5Boxes
SSD ResNet152 V1 FPN 640×640 (RetinaNet152)8035.4Boxes
SSD ResNet152 V1 FPN 1024×1024 (RetinaNet152)11139.6Boxes
Faster R-CNN ResNet50 V1 640×6405329.3Boxes
Faster R-CNN ResNet50 V1 1024×10246531.0Boxes
Faster R-CNN ResNet50 V1 800×13336531.6Boxes
Faster R-CNN ResNet101 V1 640×6405531.8Boxes
Faster R-CNN ResNet101 V1 1024×10247237.1Boxes
Faster R-CNN ResNet101 V1 800×13337736.6Boxes
Faster R-CNN ResNet152 V1 640×6406432.4Boxes
Faster R-CNN ResNet152 V1 1024×10248537.6Boxes
Faster R-CNN ResNet152 V1 800×133310137.4Boxes
Faster R-CNN Inception ResNet V2 640×64020637.7Boxes
Faster R-CNN Inception ResNet V2 1024×102423638.7Boxes
Mask R-CNN Inception ResNet V2 1024×102430139.0/34.6Boxes/Masks
ExtremeNet (deprecated)Boxes
ExtremeNetBoxes

我所選擇的模型

以我的需求而言,我會需要偵測即時串流內的物件,因此需要高速的演算法,我選擇了SSD,官方API內也包含了多個不同的SSD變種和不同的基礎模型(例如 MobileNet 和 ResNet)。這些模型的主要區別在於其複雜性、精度和運行速度:

  1. MobileNet 是一種針對移動和嵌入式設備設計的輕量級深度卷積神經網絡。相比於 ResNet,MobileNet 通常具有更少的參數和計算量,從而在設備上運行速度更快,但可能在精度上稍遜一籌。 MobileNet 的版本 V1 和 V2 是其結構上的不同版本,V2 通常在性能上優於 V1。
  2. FPN (Feature Pyramid Network) 是一種目標檢測模型中常用的模塊,它能幫助模型更好地處理不同尺度的物體。使用了 FPN 的模型通常在處理小物體上有更好的表現。 FPNlite 是 FPN 的一個更輕量級的版本。
  3. ResNet (Residual Network) 是一種深度卷積神經網絡,相比於 MobileNet,ResNet 的模型更深,參數和計算量更大,但在精度上通常更好。 ResNet 的版本(例如 ResNet50, ResNet101, ResNet152)指的是網絡的深度,數字越大,模型越深,通常精度越高,但計算量也越大。
  4. RetinaNet 是一種目標檢測模型,它是基於 FPN 和 ResNet 的一種網絡,其特點是使用了一種新的損失函數(Focal Loss)來解決類別不平衡的問題,對於包含大量背景和少量物體的圖像有很好的性能。

最後我決定使用SSD MobileNet V2 FPNLite 640x640

使用預訓練的物件偵測模型

SSD MobileNet V2 FPNLite 640x640在這邊下載: http://download.tensorflow.org/models/object_detection/tf2/20200711/ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8.tar.gz

解壓縮後可以看到以下檔案

  1. pipeline.config: 此文件包含了模型訓練過程中所有的配置參數,包括使用的模型類型(例如 Faster R-CNN,SSD 等),輸入圖像的大小,學習率,批量大小等。當您選擇一個預訓練模型進行微調(fine-tuning)時,這個 pipeline.config 文件通常會包含在下載的模型文件夾中。
  2. checkpoint: 此文件(或者一組文件)保存了模型的權重。在訓練過程中,TensorFlow 會定期保存模型的權重到 checkpoint 文件中,以便在訓練中斷後可以恢復訓練,或者在訓練結束後使用訓練好的模型進行預測。
  3. saved_model裡面是saved_model.pb 文件,這個文件包含了模型的結構和參數。

下面的方法可以用saved_model.pb產生checkpoint

import tensorflow as tf
from tensorflow.python.framework import graph_io
from tensorflow.python.tools import freeze_graph
from tensorflow.tools.graph_transforms import TransformGraph

# 載入模型
loaded = tf.saved_model.load(PATH_TO_SAVED_MODEL)

# 將模型的權重保存為 checkpoint
ckpt = tf.train.Checkpoint(model=loaded)
ckpt.save(PATH_TO_CKPT)

Posted on

TensorFlow 目標檢測API – 設定環境

相關教學資源

在Python安裝object_detection

我是在Windows的環境做開發,而官網的教學的環境是Linux,所以相關工具的安裝有花了一些時間,在這邊分享一下我的安裝方式

以下為Linux環境的工具安裝指令

sudo apt install -y protobuf-compiler
cd models/research/
protoc object_detection/protos/*.proto --python_out=.
cp object_detection/packages/tf2/setup.py .
python -m pip install .

大概就是要安裝protobuf,接著下載TensorFlows Model,然後把model放到你要呼叫Object Detection的程式目錄的下方 ,在這邊我執行python -m pip install .時,會一直出現如下錯誤

ERROR: Could not build wheels for opencv-python, which is required to install pyproject.toml-based projects

查了一下網路文章,大概猜到是python版本和opencv版本還有tensorflow版本有所不合,所以到官方網站看一下安裝所需要的環境需求如下,不同版本的Tensorflow所要搭配使用的OpenCV、Python的版本都不同:

最後我是使用python3.7的環境,才成功的安裝好object_detection

conda create -n object_detection python=3.7.16
conda activate object_detection
cd models/research/
python -m pip install .

安裝protobuf工具

在 Windows 上,可以通過以下步驟安裝 protobuf 編譯器:

  • 下載安裝包: 訪問 protobuf 的 GitHub Release 頁面(https://github.com/protocolbuffers/protobuf/releases),找到適用於 Windows 的預編譯二進製文件(例如 protoc-3.x.x-win64.zip)並下載到本地。
  • 解壓縮文件: 將下載的 zip 文件解壓縮到你想要安裝的位置,例如解壓縮到 C:\protobuf 文件夾。
  • 設置環境變量: 將解壓縮後的 bin 文件夾路徑添加到系統的環境變量中,這樣系統就能在任意位置找到 protoc 命令。
  • 驗證安裝: 打開命令提示符視窗,運行protoc --version,檢查 protoc 是否成功安裝。

安裝完成之後,就可以在./models/research下面執行

protoc object_detection/protos/*.proto --python_out=.

接著就可以用下面指令進行測試

python object_detection/builders/model_builder_tf2_test.py

如果沒有把*.proto編譯成.py,在執行物件偵測的程式碼時會出現以下錯誤

from object_detection.protos import string_int_label_map_pb2

ImportError: cannot import name ‘string_int_label_map_pb2

其他遇到的錯誤

DLL載入錯誤

import _ssl # if we can’t import it, let the error propagate

ImportError: DLL load failed: The specified module could not be found.

討論串: Python 3.7 anaconda environment – import _ssl DLL load fail error

解法: From anaconda3\Library\bin copy below files and paste them in anaconda3/DLLs:

setup.cfg指令過期錯誤

site-packages\setuptools\config\setupcfg.py:293: _DeprecatedConfig: Deprecated config in `setup.cfg`

AttributeError: module ‘os’ has no attribute ‘add_dll_directory’

解法: python環境和套件之間版本不合,重新檢查套件版本是否符合官網要求

以下為我最終使用的環境的套件版本清單:

Posted on

使用Tensorboard來觀察模型效能

Tensorboard介紹

在機器學習中,要改進模型的某些參數,通常需要觀察模型數據以進行衡量。TensorBoard 是用於提供機器學習工作流期間所需測量和呈現的工具。它使您能夠跟踪實驗指標(例如損失和準確率),呈現模型計算圖,將嵌入向量投影到較低維度的空間等。

TensorBoard是TensorFlow的可視化工具,用於可視化和分析機器學習實驗的結果。它提供了一組豐富的可視化面板,可以幫助您理解、調節測試和優化訓練過程中的模型。

主要功能和用途

以下是TensorBoard的一些主要功能和用途:

  1. Scalar Dashboard(標量儀表板):顯示訓練過程中的標量指標,如損失函數、準確率、學習率等。您可以隨時跟踪這些指標間的變化,並比較不同實驗之間的結果。
  2. Graph Dashboard(圖形儀表板):顯示模型的計算圖,可視化網絡結構和層之間的連接。這有助於理解和調試模型的結構,查看各層的輸入輸出形狀。
  3. 直方圖儀表板(直方圖儀表板):顯示權重、加權和激活函數的分配情況。這用於觀察參數隨時間的變化、檢測梯度消失或突然爆炸等問題很有幫助。
  4. Image Dashboard(圖像儀表板):可視化輸入圖像、模型生成的圖像或模型層輸出的特徵圖等。這對於在圖像數據中觀察模型上的表現和理解卷積網絡中的特徵提取過程非常有用。
  5. Embedding Dashboard(嵌入儀表板):用於可視化高維嵌入空間中的數據。您可以在三維空間中探索和比較嵌入向量,以及在嵌入空間中相似的樣本。
  6. Projector Dashboard(投影儀表板):提供了一種交互式的界面,可以在高維空間中對數據進行探索和分析。您可以通過選擇和過濾樣本、聚類分析和降維等操作來理解數據集的結構。

TensorBoard 提供了一種觀察的方式來監視和分析機器學習模型,以了解模型的行為並進行調優。您可以使用 TensorFlow 的相關API(如)將數據寫入TensorBoard日誌文件,然後在命令tf.summary中行中運行 TensorBoard 來查看和分析結果。

如何使用

以下為一個範例程式

import tensorflow as tf
import datetime

mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

def create_model():
  return tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(512, activation='relu'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10, activation='softmax')
  ])

model = create_model()
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
# 設定log的資料夾
log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
# 設定tensorboard的callback
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)

model.fit(x=x_train, 
          y=y_train, 
          epochs=5, 
          validation_data=(x_test, y_test), 
          callbacks=[tensorboard_callback])

接著就可以在所指定的資料夾位置運行tensorboard介面

tensorboard --logdir logs/fit

接著開啟http://localhost:6006就可以看到如下畫面,這個畫面是Scalars,顯示損失和指標在每個時期如何變化

Graphs用以可視化模型。可以看出你的模型是如何建構的,有用到那些方法

Distributions是顯示張量隨時間的分佈。DistributionsHistograms這兩頁很相似,可了解模型權重隨時間的變化,作為判斷是否出現問題的初步衡量標準。

Histograms是使用Ridgeline plot來顯示張量隨時間的分佈,橫軸為epochs訓練的次數,縱軸則為權重和偏差的值

Time-Seriesscalars非常相似,都是在評估準確度。然而,一個區別是每次訓練迭代而不是每個時期對目標指標的觀察。

Posted on

如何訓練和測試數據

將資料切分

在數據科學中,訓練數據和測試數據扮演著兩個主要的角色。評估已構建模型的性能與訓練和構建模型同樣重要,因為未經評估性能的模型可能會產生錯誤的預測並導致嚴重的並發症。為了防止這種情況發生並確保預測的準確性,您必須足夠好地測試和驗證模型。

為了構建和評估機器學習模型的性能,我們通常將數據集分成兩個不同的數據集。這兩個數據集是訓練數據測試數據

訓練數據測試數據
用於構建模型用於評估構建的模型
分配更大的數據部分分配較小的數據部分
可進一步劃分以進行驗證不會進一步分割

什麼是驗證數據

驗證數據是從訓練數據中分離出來的子數據集,用於在訓練過程中驗證模型。來自驗證過程的信息幫助我們改變模型的參數、分類器以獲得更好的結果。所以基本上,驗證數據可以幫助我們優化模型。 

使用 Scikit-learn 的 train_test_split來切割數據

使用下面這段程式碼可以將訓練及分割成訓練及驗證集

from sklearn.model_selection import train_test_split
# 載入資料集
....
# 分離訓練和測試數據
X_train, X_val, y_train, y_val = train_test_split(train_images_fold, train_labels_fold, test_size=0.1, random_state=42)
model = create_model()
keras_classifier.fit(X_train, y_train, validation_data=(X_val, y_val))

使用測試資料作驗證

下面這段程式可以使用X_test、y_test來使用model做測試,並且可以用accuracy_score來取得準確率,並將準確率存入一個陣列裡

predictions = keras_classifier.predict(X_test)
accuracy = accuracy_score(y_test, predictions)
accuracy_scores.append(accuracy)

若是想要取得錯誤的資料集,則可以用np.where來取得與正確答案不一樣的資料,並將錯誤的資料存入incorrect陣列裡面

incorrect_indices = np.where(predictions != y_test)[0]
incorrect_images = X_test[incorrect_indices]
incorrect_labels = y_test[incorrect_indices]
incorrect_prediction = predictions[incorrect_indices]
for i in range(len(incorrect_indices)):
    incorrect.append({"image": incorrect_images[i] ,"label": incorrect_labels[i], "pred": incorrect_prediction[i], "idx": fold_index})

完整範例

下面的範例為結合K-Fold概念,將資料及分成五份,並做五次的訓練以判別模型的訓練狀況是否有過擬合的狀況。其中會每次會取其4分來做訓練、1份做測試,再將訓練集中的1/10拆做驗證集。最後使用matplotlib.pyplot來顯示這五次之中,測試集中錯誤結果的圖片

import numpy as np
import matplotlib.pyplot as plt
import pathlib
from sklearn.model_selection import StratifiedKFold
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score
from tensorflow import keras
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_validate

img_path = 'dice3'

def class_names(path):
    return np.array(sorted([item.name for item in pathlib.Path(path).glob('*') if
                            "LICENSE.txt" != item.name]))
def create_model():
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.Rescaling(1. / 255))
    model.add(tf.keras.layers.Conv2D(32, kernel_size=7, activation='relu'))
    model.add(tf.keras.layers.MaxPooling2D())
    model.add(tf.keras.layers.Conv2D(64, kernel_size=5, activation='relu'))
    model.add(tf.keras.layers.MaxPooling2D())
    model.add(tf.keras.layers.Flatten())
    model.add(tf.keras.layers.Dense(64, activation='relu'))
    model.add(tf.keras.layers.Dense(len(class_names(img_path))))

    model.compile(
        optimizer='adam',
        loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
        metrics=['accuracy'])
    return model


train_ds = tf.keras.utils.image_dataset_from_directory(
  img_path,
  seed=7,
  batch_size=32)
train_images = []
train_labels = []

for images, labels in train_ds:
    train_images.append(images.numpy())
    train_labels.append(labels.numpy())

train_images = np.concatenate(train_images, axis=0)
train_labels = np.concatenate(train_labels, axis=0)
label_encoder = LabelEncoder()
train_labels_encoded = label_encoder.fit_transform(train_labels)

# 創建KerasClassifier
keras_classifier = KerasClassifier(build_fn=create_model, epochs=5, batch_size=16)

# 定義StratifiedKFold
kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=123)

fold_index = 1
# 進行交叉驗證
accuracy_scores = []
incorrect = []
for train_indices, val_indices in kfold.split(train_images, train_labels_encoded):
    print("fold_index="+str(fold_index))
    train_images_fold = train_images[train_indices]
    train_labels_fold = train_labels_encoded[train_indices]

    X_test = train_images[val_indices]
    y_test = train_labels_encoded[val_indices]
    
    # 分離訓練和測試數據
    X_train, X_val, y_train, y_val = train_test_split(train_images_fold, train_labels_fold, test_size=0.1, random_state=42)
    model = create_model()
    keras_classifier.fit(X_train, y_train, validation_data=(X_val, y_val))

    predictions = keras_classifier.predict(X_test)
    accuracy = accuracy_score(y_test, predictions)
    accuracy_scores.append(accuracy)

    incorrect_indices = np.where(predictions != y_test)[0]
    incorrect_images = X_test[incorrect_indices]
    incorrect_labels = y_test[incorrect_indices]
    incorrect_prediction = predictions[incorrect_indices]
    for i in range(len(incorrect_indices)):
        incorrect.append({"image": incorrect_images[i] ,"label": incorrect_labels[i], "pred": incorrect_prediction[i], "idx": fold_index})
    # 印出準確率
    print("Accuracy scores:", accuracy_scores)
    print("Mean accuracy:", np.mean(accuracy_scores))
    fold_index += 1

# 顯示出錯誤的答案
images_per_page = 15
num_images_per_row = 5
num_images_per_col = 3
        
num_pages = (len(incorrect) - 1) // images_per_page + 1
for page in range(num_pages):
    start_idx = page * images_per_page
    end_idx = (page + 1) * images_per_page
    page_detail = incorrect[start_idx:end_idx]
    
    fig, axes = plt.subplots(num_images_per_col, num_images_per_row, figsize=(num_images_per_col*2, num_images_per_row*2))
    i=0
    for data in page_detail:
        image = data["image"]
        label = data["label"]
        pred = data["pred"]
        idx = data["idx"]
        row = i // num_images_per_row
        col = i % num_images_per_row
        ax = axes[row, col]
        image = image.astype(np.uint8)
        ax.imshow(image)
        ax.set_title(f"{label}->{pred}({idx})")
        i = i+1
        ax.axis("off")

    plt.tight_layout()
    #plt.suptitle("fold_index:"+str(fold_index)+"-"+str(page+1), x=0, y=1, ha='left', va='top')
    plt.show()

參考資料

Posted on

k-Fold Cross-Validation(交叉驗證)

一般所使用的方式 – Holdout method

這是一種非常基本且簡單的方法,我們將整個數據集分為兩部分,即訓練數據測試數據。顧名思義,我們在訓練數據上訓練模型,然後在測試集上進行評估。通常,訓練數據的大小設置為測試數據的兩倍以上,因此數據按70:30或80:20的比例進行分割。

在這種方法中,數據在分割之前首先被隨機洗牌。由於模型是在不同的數據點組合上訓練的,每次訓練時模型都會給出不同的結果,這可能是不穩定的原因。此外,我們永遠無法保證我們選擇的訓練集能夠代表整個數據集。

此外,當我們的數據集不是太大時,測試數據很可能包含一些我們丟失的重要信息,因為我們沒有在測試集上訓練模型。範例程式如下:

import tensorflow as tf
 
# 建立圖片資料集
dataset = tf.keras.utils.image_dataset_from_directory(
    'image_directory',
    labels='inferred',
    class_names=None,
    label_mode='int'
)
 
# 計算資料集的大小
dataset_size = tf.data.experimental.cardinality(dataset).numpy()
 
# 計算訓練集和測試集的大小
test_size = int(0.2 * dataset_size)
train_size = dataset_size - test_size
 
# 將資料集分割為訓練集和測試集
train_dataset = dataset.take(train_size)
test_dataset = dataset.skip(train_size)

改善方法 – k-Fold Cross-Validation

K 折交叉驗證是改進的一種方法。這種方法保證了我們模型的分數不依賴於我們選擇訓練集和測試集的方式。將數據集分為 k 個子集,並將保留方法重複 k 次。讓我們分步驟完成這個過程:

  1. 將整個數據集隨機分成 k 個折疊(子集)
  2. 對於數據集中的每個折疊,在數據集的 k – 1 個折疊上構建模型。然後,測試模型以檢查第 k 次折疊的有效性
  3. 重複此操作,直到每個 k 重都作為測試集
  4. k 記錄的準確度的平均值稱為交叉驗證準確度,並將作為模型的性能指標。

因為它確保來自原始數據集的每個觀察結果都有機會出現在訓練和測試集中,所以與其他方法相比,這種方法通常會產生偏差較小的模型。如果我們的輸入數據有限,這是最好的方法之一。 

這種方法的缺點是訓練算法必須從頭開始重新運行 k 次,這意味著進行評估需要 k 倍的計算量。

分層k-Fold – Stratified K Fold Cross Validation

在分類問題上使用 K Fold 可能會很棘手。由於我們隨機打亂數據,然後將其劃分為折疊,因此我們可能會得到高度不平衡的折疊,這可能會導致我們的訓練出現偏差。例如,讓我們以某種方式得到一個折疊,其中大多數屬於一個類(例如正類),而只有少數屬於負類。這肯定會破壞我們的訓練,為了避免這種情況,我們使用分層進行分層折疊。

分層是重新排列數據的過程,以確保每次折疊都能很好地代表整體。例如,在每個類包含 50% 數據的二元分類問題中,最好對數據進行排列,使得在每個折疊中,每個類包含大約一半的實例。

下面這張圖,是在分類問題上使用K-Fold折疊的方式,會發現因為不同分類隨機分組導致每個分組的數量不一,而破壞訓練的狀況

資料狀況: 共三種資料,每一種70張,共210張,訓練集168張,測試集42張

而改使用StratifiedKFold去做資料分組之後,能看到訓練過程更加順利

範例程式

以下為使用Tensorflow加上sklearn.model_selection去做資料分組的測試程式

import numpy as np
import pathlib
import tensorflow as tf
from sklearn.model_selection import LeaveOneOut,KFold
from sklearn.model_selection import StratifiedKFold
from sklearn.preprocessing import LabelEncoder
from sklearn.datasets import load_iris

img_path = 'dice3'
train_ds = tf.keras.utils.image_dataset_from_directory(
  img_path,
  seed=7,
  batch_size=32)
X = []
y = []

for images, labels in train_ds:
    X.append(images.numpy())
    y.append(labels.numpy())

X = np.concatenate(X, axis=0)
y = np.concatenate(y, axis=0)
label_encoder = LabelEncoder()
y = label_encoder.fit_transform(y)

def class_names(path):
    return np.array(sorted([item.name for item in pathlib.Path(path).glob('*') if
                            "LICENSE.txt" != item.name]))
def create_model():
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.Rescaling(1. / 255))
    model.add(tf.keras.layers.Conv2D(32, kernel_size=7, activation='relu'))
    model.add(tf.keras.layers.MaxPooling2D())
    model.add(tf.keras.layers.Conv2D(64, kernel_size=5, activation='relu'))
    model.add(tf.keras.layers.MaxPooling2D())
    model.add(tf.keras.layers.Flatten())
    model.add(tf.keras.layers.Dense(64, activation='relu'))
    model.add(tf.keras.layers.Dense(len(class_names(img_path))))
    model.compile(
        optimizer='adam',
        loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
        metrics=['accuracy'])
    return model

def make_dataset(X_data,y_data,n_splits):
    def gen():
        for train_index, test_index in KFold(n_splits).split(X_data):
            X_train, X_test = X_data[train_index], X_data[test_index]
            y_train, y_test = y_data[train_index], y_data[test_index]
            yield X_train,y_train,X_test,y_test

    return tf.data.Dataset.from_generator(gen, (tf.float64,tf.float64,tf.float64,tf.float64))

dataset=make_dataset(X,y,5)


for X_train,y_train,X_test,y_test in dataset:
    print(len(X_train), len(y_train), len(X_test), len(y_test))
    model = create_model()
    model.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_test, y_test))


若要改成使用分層K-Fold,則要使用sklearn.model_selection的StratifiedKFold

def make_dataset(X_data,y_data,n_splits):
    def gen():
        kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=123)
        for train_index, test_index in kfold.split(X_data, y_data):
            X_train, X_test = X_data[train_index], X_data[test_index]
            y_train, y_test = y_data[train_index], y_data[test_index]
            yield X_train,y_train,X_test,y_test

    return tf.data.Dataset.from_generator(gen, (tf.float64,tf.float64,tf.float64,tf.float64))

資料集長這樣

參考資料

Posted on

Optimization loop failed: Cancelled: Operation was cancelled

遇到的錯誤訊息

2023-06-14 16:32:39.652288: W tensorflow/core/data/root_dataset.cc:167] Optimization loop failed: Cancelled: Operation was cancelled

這個錯誤訊息代表TensorFlow 在優化過程中遇到了問題,並且操作被取消了。當操作被取消時,TensorFlow 無法完成所需的計算任務,因此無法產生期望的結果。具體來說,如果你在使用 TensorFlow 的 CPU 模式下運行一個優化循環,並且該循環被取消,以下情況可能發生:

  • 訓練過程中斷:如果你正在訓練一個模型,操作取消將導致訓練過程中止。你將無法完成整個訓練過程,無法獲得最終訓練好的模型。
  • 中斷的結果:在某些情況下,如果操作被取消,TensorFlow 可能會嘗試返回已經計算出的部分結果。這取決於具體的操作和中斷發生的時間點。然而,這些部分結果通常不是完整或不可用的。
  • 推斷錯誤:如果你正在使用模型進行推斷(如輸入一條文本獲取其分類),而操作被取消,你將無法獲得模型對輸入的預測結果。這可能導致你無法得知模型的輸出,或者輸出結果不完整、不准確。

為什麼會出現此錯誤

大部分的狀況是因為資源不足,導致運算被中斷

  • 計算資源不足:TensorFlow 在 CPU 模式下運行時,對於大型、複雜的模型或數據集,可能需要較長的時間來完成計算。如果計算資源有限,操作可能會被取消。
  • 內存不足:TensorFlow 在處理大型模型或數據時需要大量內存。如果內存不足,操作可能會被取消。
  • 配置錯誤:有時,TensorFlow 的配置可能不正確,導致操作無法成功完成。這可能包括錯誤的版本或依賴問題。

觀察電腦CPU與MEMORY使用狀況

要解決此問題,首先要先觀察是哪一部分出了問題,就需要在程式內去監控資源的使用。請參考此篇教學: How to get current CPU and RAM usage in Python

    # Importing the library
    import psutil


    # Getting % usage of virtual_memory ( 3rd field)
    if psutil.virtual_memory()[2] > 80:
        print(time.strftime("%Y-%m-%d_%H-%M-%S") + ' RAM memory % used:', psutil.virtual_memory()[2])
        # Getting usage of virtual_memory in GB ( 4th field)
        print('RAM Used (GB):', psutil.virtual_memory()[3]/1000000000)
    
    cpu_usage = psutil.cpu_percent()
    if cpu_usage > 80:
        print(time.strftime("%Y-%m-%d_%H-%M-%S") + ' The CPU usage is: ', cpu_usage)

解決 – 若是使用太多記憶體

在Tensorflow裡面有一篇討論串,滿多人都會遇到此問題,其中一位大大建議在GPU上提高可使用的內存,可解決此問題

gpus = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(gpu, True)

討論串: Optimization loop failed: Cancelled: Operation was cancelled

另外也可以考慮設置合適的 batch_size、限制模型大小或減少數據集規模是一種在沒有 GPU 或內存受限的情況下控制內存使用的方法。下面是一些方法可以考慮:

  • 調整 batch_size:batch_size 是每次模型訓練時用於處理的樣本數量。較大的 batch_size 可能會導致內存不足。你可以嘗試降低 batch_size 的值,以減少內存需求。但要注意,較小的 batch_size 可能會影響訓練過程的穩定性和收斂速度。
  • 限制模型的大小:模型的大小直接影響內存使用。你可以嘗試以下方法來限制模型的大小:
    • 減少模型的層數或參數數量。
    • 使用輕量級模型架構,例如 MobileNet、SqueezeNet 等。
    • 使用模型剪枝技術,減少模型中冗餘參數的數量。
    • 使用低精度(如 16 位浮點數)表示模型的參數,以減少內存佔用。
  • 減少數據集規模:如果數據集過大而導致內存不足,你可以考慮以下方法來減少數據集的規模:
    • 隨機採樣:從原始數據集中隨機選擇一部分樣本作為子集進行訓練。確保採樣後的數據集仍具有代表性。
    • 數據集切片:將大型數據集切割成多個較小的部分,逐個部分進行訓練。可以使用交叉驗證等技術來利用所有數據並評估模型性能。

解決 – 若是使用太多CPU

可嘗試以下方法來減少 CPU 的消耗:

  • 減少數據的處理量:優化輸入數據的大小和復雜度,可以減少 CPU 的負載。例如,可以嘗試縮小圖像尺寸、減少輸入序列的長度或降低數據的維度。
  • 使用批處理預測:通過將多個樣本組成一個批次進行預測,可以減少每個樣本的計算和內存佔用。 Model.predict() 方法默認可以處理批量輸入,你可以將多個樣本一起傳遞給該方法進行預測。
  • 並行處理:如果你的系統支持多線程或多進程,並且你的模型和數據可以進行並行處理,可以使用並行化方法來提高 CPU 的利用率。例如,使用 Python 的 multiprocessing 模塊或 TensorFlow 的 tf.data.Dataset 的並行化功能。
  • 優化模型:檢查你的模型結構和計算圖,看是否有可以優化的部分。可能存在一些冗餘計算或可以簡化的操作。你可以嘗試使用 TensorFlow 的圖優化工具,如 tf.function 和 tf.autograph,來加速模型的計算過程。
  • 使用更高效的庫或算法:某些情況下,使用其他庫或算法可能比 TensorFlow 更高效。你可以嘗試使用其他機器學習庫,如 PyTorch 或 Scikit-learn,並根據你的需求選擇更適合的庫和算法。
  • 升級硬件:如果你的計算機配置允許,可以考慮升級到更強大的 CPU 或添加更多的 CPU 核心。這樣可以提高系統的並行處理能力和整體性能。
Posted on

建立模型與並使用模型來預測資料

從本機圖片建立模型的簡單範例

以下為從classify資料夾內載入圖片並建構分類模組的範例。圖片資料夾的結構如下:

其中daisy、dandelion、roses、sunflowers、tulips為標籤名稱,在各個資料夾內為圖片

然後使用 image_dataset_from_directory 函數載入圖片,需要傳遞以下參數:

  • directory:包含圖片的目錄。
  • labels:標籤的名稱。預設情況下,每個圖片所在的子目錄將被當作標籤。
  • class_names:標籤的名稱列表。
  • label_mode:標籤的格式。可以是 ‘int’(預設值)、’binary’ 或 ‘categorical’。
  • batch_size: 用於指定每個批次中包含的圖片數量。

在載入圖片時,tf.keras.utils.image_dataset_from_directory 函數會使用 RGB 色彩空間。

tf.keras.utils.image_dataset_from_directory 函數中的 seed 參數用於設置隨機數生成器的種子。

如果指定了 seed 參數,則在載入資料集時,將使用指定的種子初始化隨機數生成器,這樣可以保證每次載入資料集的順序都是一致的。

# TensorFlow and tf.keras
import tensorflow as tf
import pathlib
# Helper libraries
import numpy as np
import matplotlib.pyplot as plt
from keras.callbacks import Callback
print(tf.__version__)

batch_size = 32
img_height = 150
img_width = 150

data_dir = pathlib.Path('flower_photos')
# 從資料集中取出訓練資料,需要將 subset 參數設置為 'training'。
train_ds = tf.keras.utils.image_dataset_from_directory(
    data_dir,
    validation_split=0.2,
    subset="training",
    seed=123,
    image_size=(img_height, img_width),
    batch_size=batch_size)
# 同時設定了 subset 參數和 validation_split 參數,則會從資料集中取出指定的子集作為驗證集。
val_ds = tf.keras.utils.image_dataset_from_directory(
    data_dir,
    validation_split=0.2,
    subset="validation",
    seed=123,
    image_size=(img_height, img_width),
    batch_size=batch_size)

class_names = train_ds.class_names
print(class_names)

image_batch, label_batch = next(iter(train_ds))

num_classes = len(class_names)

model = tf.keras.Sequential([
    # 對輸入的張量進行縮放。將輸入的張量除以 255,這將會將輸入的值縮放到 0 到 1 之間。
    tf.keras.layers.Rescaling(1./255),
    tf.keras.layers.Conv2D(32, 3, activation='relu'),
    tf.keras.layers.MaxPooling2D(),
    tf.keras.layers.Conv2D(32, 3, activation='relu'),
    tf.keras.layers.MaxPooling2D(),
    tf.keras.layers.Conv2D(32, 3, activation='relu'),
    tf.keras.layers.MaxPooling2D(),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(num_classes)
])
model.compile(
    optimizer='adam',
    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    metrics=['accuracy'])

model.fit(
    train_ds,
    validation_data=val_ds,
    epochs=3
)

model.save('model.h5')

然後按下complier之後,會有下面的樣建模的資訊

因為我們在model.compile的metrics選擇了accuracy,因此評估模型的評估方式會以accuracy作為標準

將資料集分割為訓練集和測試集

上面的程式碼我們呼叫了兩次image_dataset_from_directory來分別載入訓練集和測試集
但也可以只載入一次,然後再將資料集分割為訓練集和測試集

範例程式如下

import tensorflow as tf

# 建立圖片資料集
dataset = tf.keras.utils.image_dataset_from_directory(
    'image_directory',
    labels='inferred',
    class_names=None,
    label_mode='int'
)

# 計算資料集的大小
dataset_size = tf.data.experimental.cardinality(dataset).numpy()

# 計算訓練集和測試集的大小
test_size = int(0.2 * dataset_size)
train_size = dataset_size - test_size

# 將資料集分割為訓練集和測試集
train_dataset = dataset.take(train_size)
test_dataset = dataset.skip(train_size)

用 Keras 載入模型並預測答案

以下為一個簡單範例,使用keras.preprocessing.image.load_img所載入的圖片預設格式為 RGB 圖片

import numpy as np
import tensorflow as tf
from tensorflow import keras
import os
import re
import pathlib
# 載入模型
rank_model = keras.models.load_model('model.h5')
# 讓模型輸出的機率值分布在0-1之間
probability_rank_model = tf.keras.Sequential([rank_model, tf.keras.layers.Softmax()])

class_names = ['daisy','dandelion','roses','sunflowers','tulips']

img = keras.preprocessing.image.load_img('./test.jpg', target_size=(150, 150))
if img is None:
    continue
x = keras.preprocessing.image.img_to_array(img)
x = np.expand_dims(x, axis=0)
pred = probability_rank_model.predict(x)[0]
print(path)
top_inds = pred.argsort()[::-1][:5]
if pred[top_inds[0]] > 0.8:
    print('result is: ' + class_names[top_inds[0]])
# 印出機率分布
for data in top_inds:
    print('    {:.3f}  {}'.format(pred[data], class_names[data]))

上面是使用keras.preprocessing.image.load_img來載入圖片
我們也可以使用OpenCV來載入圖片使用

# 使用 OpenCV 讀取圖片
image = cv2.imread('image.jpg')

# 將圖片轉換為 RGB 格式
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = PIL.Image.fromarray(image)
x = keras.preprocessing.image.img_to_array(image)
probability_rank_model.predict(x.tolist())

如何關掉預測時吐出的log

在使用 predict 方法進行預測時,Keras 模型可能會輸出一些訓練過程中的訊息,如果你想要避免輸出這些訊息,則可以在建立模型時設置 verbose 參數。

例如,如果你想要在預測時停止輸出訓練過程中的訊息,則可以在建立模型時使用以下代碼:

# 預測
predictions = model.predict(x_test, verbose=0)

這樣,在進行預測時就不會再輸出任何訓練過程中的訊息了。