You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在非TensorFlow环境中测试训练好的自定义目标检测CNN模型?

Hey there! I get exactly what you're trying to do—you've trained a custom SSD MobileNet v1 detector with TF 1.10, and you want folks who don't (and don't want to) mess with TensorFlow environments to test it easily. Since you're also new to this, I'll break down three solutions from easiest to slightly more involved, all tailored for low friction:


难度1:超轻量打包(纯小白友好,零代码修改)

This is the simplest option for both you and your testers—no complex setup, just a pre-configured zip package they can unpack and use in minutes.

Your Preparation Steps:

  1. Create a folder named model_test_package and add these files:

    • Your exported frozen_inference_graph.pb
    • Your label_map.pbtxt (match the one used for training)
    • A sample test image (e.g., test_example.jpg)
    • A pre-written Jupyter Notebook (test_detection.ipynb) with hardcoded paths (so testers don't need to edit anything)
    • An environment.yml file to auto-create the Conda environment
  2. Notebook Code Example (test_detection.ipynb):

    import tensorflow as tf
    from object_detection.utils import label_map_util, visualization_utils as vis_util
    import cv2
    import matplotlib.pyplot as plt
    import numpy as np
    
    # Fixed paths (matches the package folder structure)
    PATH_TO_MODEL = 'frozen_inference_graph.pb'
    PATH_TO_LABELS = 'label_map.pbtxt'
    NUM_CLASSES = 2  # Replace with your number of classes
    
    # Load detection model
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(PATH_TO_MODEL, 'rb') as fid:
            od_graph_def.ParseFromString(fid.read())
            tf.import_graph_def(od_graph_def, name='')
    
    # Load label map
    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)
    
    # Run detection on a single image
    def run_detection(image):
        with detection_graph.as_default():
            with tf.Session() as sess:
                image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')
                boxes = tf.get_default_graph().get_tensor_by_name('detection_boxes:0')
                scores = tf.get_default_graph().get_tensor_by_name('detection_scores:0')
                classes = tf.get_default_graph().get_tensor_by_name('detection_classes:0')
                num_detections = tf.get_default_graph().get_tensor_by_name('num_detections:0')
    
                # Run inference
                (boxes, scores, classes, num) = sess.run(
                    [boxes, scores, classes, num_detections],
                    feed_dict={image_tensor: np.expand_dims(image, 0)})
    
                # Visualize results
                vis_util.visualize_boxes_and_labels_on_image_array(
                    image,
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    category_index,
                    use_normalized_coordinates=True,
                    line_thickness=8)
        return image
    
    # Test sample image (testers can replace this path with their own image)
    image = cv2.imread('test_example.jpg')
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    result_image = run_detection(image_rgb)
    
    # Show result
    plt.figure(figsize=(12,8))
    plt.imshow(result_image)
    plt.axis('off')
    plt.show()
    
  3. Environment File (environment.yml):

    name: tf_detector
    channels:
      - conda-forge
      - defaults
    dependencies:
      - python=3.6  # Compatible with TF 1.10
      - tensorflow-gpu=1.10.0
      - cudatoolkit=8.0
      - cudnn=7.1.4
      - opencv
      - matplotlib
      - jupyter
      - pip:
        - pillow
        - git+https://github.com/tensorflow/models.git#egg=object_detection&subdirectory=research/object_detection
    
  4. Zip the entire model_test_package folder into model_test_package.zip.

Tester's Steps:

  1. Install Miniconda3 (default settings are fine—no technical knowledge needed).
  2. Unzip model_test_package.zip to any folder.
  3. Open Miniconda Prompt, navigate to the unzipped folder (e.g., cd C:\model_test_package).
  4. Run conda env create -f environment.yml (auto-installs all dependencies, no manual TF setup).
  5. Activate the environment: conda activate tf_detector.
  6. Launch Jupyter Notebook: jupyter notebook.
  7. Open test_detection.ipynb, click Kernel > Restart & Run All to see the sample result.
  8. To test their own images: Add the image to the folder, edit the image = cv2.imread(...) line with their image filename, and re-run that cell.

难度2:Docker镜像打包(无需手动环境配置)

If you want testers to avoid even Conda setup, Docker is a great option—it packages the entire environment into a single image.

Your Preparation Steps:

  1. Create a folder docker_detector with these files:

    • frozen_inference_graph.pb
    • label_map.pbtxt
    • test_detection.py (command-line script that accepts an image path)
    • Dockerfile (defines the TF environment)
    • Sample image test_example.jpg
  2. Dockerfile:

    FROM tensorflow/tensorflow:1.10.0-gpu-py3
    RUN apt-get update && apt-get install -y libsm6 libxext6 libxrender-dev
    RUN pip install opencv-python matplotlib pillow
    RUN git clone https://github.com/tensorflow/models.git && \
        cd models/research && \
        protoc object_detection/protos/*.proto --python_out=. && \
        pip install -e .
    COPY . /app
    WORKDIR /app
    
  3. Test Script (test_detection.py):

    import tensorflow as tf
    from object_detection.utils import label_map_util, visualization_utils as vis_util
    import cv2
    import numpy as np
    import sys
    
    def main():
        if len(sys.argv) != 2:
            print("Usage: python test_detection.py <image_path>")
            return
    
        PATH_TO_MODEL = 'frozen_inference_graph.pb'
        PATH_TO_LABELS = 'label_map.pbtxt'
        NUM_CLASSES = 2  # Replace with your class count
    
        # Load model and labels (same as notebook code)
        detection_graph = tf.Graph()
        with detection_graph.as_default():
            od_graph_def = tf.GraphDef()
            with tf.gfile.GFile(PATH_TO_MODEL, 'rb') as fid:
                od_graph_def.ParseFromString(fid.read())
                tf.import_graph_def(od_graph_def, name='')
    
        label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
        categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
        category_index = label_map_util.create_category_index(categories)
    
        # Run detection
        image = cv2.imread(sys.argv[1])
        image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        with detection_graph.as_default():
            with tf.Session() as sess:
                image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')
                boxes = tf.get_default_graph().get_tensor_by_name('detection_boxes:0')
                scores = tf.get_default_graph().get_tensor_by_name('detection_scores:0')
                classes = tf.get_default_graph().get_tensor_by_name('detection_classes:0')
                num_detections = tf.get_default_graph().get_tensor_by_name('num_detections:0')
    
                (boxes, scores, classes, num) = sess.run(
                    [boxes, scores, classes, num_detections],
                    feed_dict={image_tensor: np.expand_dims(image_rgb, 0)})
    
                vis_util.visualize_boxes_and_labels_on_image_array(
                    image_rgb,
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    category_index,
                    use_normalized_coordinates=True,
                    line_thickness=8)
    
        # Save result
        result_image = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2BGR)
        cv2.imwrite('detection_result.jpg', result_image)
        print("Detection complete! Result saved as detection_result.jpg")
    
    if __name__ == '__main__':
        main()
    
  4. Build the Docker image:

    docker build -t custom_detector .
    
  5. Export the image to a zip:

    docker save -o custom_detector.tar custom_detector
    

    Zip the custom_detector.tar and a README.txt with usage instructions.

Tester's Steps:

  1. Install Docker Desktop (with NVIDIA Docker if they have a GPU; CPU works too).
  2. Unzip the package, load the image: docker load -i custom_detector.tar.
  3. Run detection on their image (replace /path/to/their/image.jpg with the actual path):
    docker run --gpus all -v /path/to/their/images:/app/images custom_detector python test_detection.py images/their_image.jpg
    
  4. The result will be saved to their images folder as detection_result.jpg.

难度3:EXE可执行文件(完全零环境安装)

For absolute beginners who don't want to install anything except the package, use PyInstaller to bundle the script and dependencies into a single EXE.

Your Preparation Steps:

  1. In your existing TF 1.10 environment, install PyInstaller:
    pip install pyinstaller
    
  2. Create a GUI script (detector_gui.py) with Tkinter for easy image selection:
    import tensorflow as tf
    from object_detection.utils import label_map_util, visualization_utils as vis_util
    import cv2
    import numpy as np
    import tkinter as tk
    from tkinter import filedialog, messagebox
    from PIL import Image, ImageTk
    
    # Model settings
    PATH_TO_MODEL = 'frozen_inference_graph.pb'
    PATH_TO_LABELS = 'label_map.pbtxt'
    NUM_CLASSES = 2
    
    # Load model once on startup
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(PATH_TO_MODEL, 'rb') as fid:
            od_graph_def.ParseFromString(fid.read())
            tf.import_graph_def(od_graph_def, name='')
    
    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)
    
    def run_detection(image):
        with detection_graph.as_default():
            with tf.Session() as sess:
                image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')
                boxes = tf.get_default_graph().get_tensor_by_name('detection_boxes:0')
                scores = tf.get_default_graph().get_tensor_by_name('detection_scores:0')
                classes = tf.get_default_graph().get_tensor_by_name('detection_classes:0')
                num_detections = tf.get_default_graph().get_tensor_by_name('num_detections:0')
    
                (boxes, scores, classes, num) = sess.run(
                    [boxes, scores, classes, num_detections],
                    feed_dict={image_tensor: np.expand_dims(image, 0)})
    
                vis_util.visualize_boxes_and_labels_on_image_array(
                    image,
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    category_index,
                    use_normalized_coordinates=True,
                    line_thickness=8)
        return image
    
    # GUI setup
    class DetectorApp:
        def __init__(self, root):
            self.root = root
            self.root.title("Custom Object Detector")
            self.root.geometry("800x600")
    
            self.select_btn = tk.Button(root, text="Select Image", command=self.load_image, font=("Arial", 14))
            self.select_btn.pack(pady=20)
    
            self.canvas = tk.Canvas(root, width=700, height=500, bg="white")
            self.canvas.pack()
    
        def load_image(self):
            file_path = filedialog.askopenfilename(filetypes=[("Image Files", "*.jpg;*.png")])
            if not file_path:
                return
            try:
                image = cv2.imread(file_path)
                image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                result = run_detection(image_rgb)
    
                # Display result
                pil_img = Image.fromarray(result)
                pil_img.thumbnail((700, 500))
                tk_img = ImageTk.PhotoImage(pil_img)
                self.canvas.create_image(0, 0, anchor=tk.NW, image=tk_img)
                self.canvas.image = tk_img
    
                # Save result
                cv2.imwrite("detection_result.jpg", cv2.cvtColor(result, cv2.COLOR_RGB2BGR))
                messagebox.showinfo("Success", "Result saved as detection_result.jpg!")
            except Exception as e:
                messagebox.showerror("Error", f"Failed to detect: {str(e)}")
    
    if __name__ == '__main__':
        root = tk.Tk()
        app = DetectorApp(root)
        root.mainloop()
    
  3. Bundle the script into an EXE:
    pyinstaller --onefile --add-data "frozen_inference_graph.pb;." --add-data "label_map.pbtxt;." --hidden-import "tensorflow.contrib" --hidden-import "object_detection.utils" detector_gui.py
    
  4. Copy the EXE from the dist folder, along with frozen_inference_graph.pb and label_map.pbtxt, into a zip package.

Tester's Steps:

  1. Unzip the package to any folder.
  2. Double-click detector_gui.exe.
  3. Click "Select Image", choose their test image, and wait for the result to appear in the window.
  4. The result is automatically saved as detection_result.jpg in the same folder.

内容的提问来源于stack exchange,提问作者ktii

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:19:06