sony / nnabla-examples

Neural Network Libraries https://nnabla.org/ - Examples
Apache License 2.0
305 stars 93 forks source link

[Colab Demo] Inference for a set of images by CenterNet #394

Open TakuyaYashima opened 1 year ago

TakuyaYashima commented 1 year ago

We got a question in CenterNet Colab demo tutorial in Youtube.

Hi, thanks for the video. This really good. How do I run an inference for a set of images and get the prediction results saved?

TakuyaYashima commented 1 year ago

Here's one way for that.

  1. Run the cells until where you download the pretrained weight.

  2. Instead of uploading one single image, make directories for input and output images first.

    !mkdir inputs && mkdir outputs
  3. Upload images of interest via Colaboratory UI or use files.upload() for multiple times and save them in inputs.

  4. Import some packages.

    import os
    import shutil
    import subprocess
  5. Run the script below.

    for i, image in enumerate(os.listdir("inputs")):
        if os.path.splitext(image)[1] not in ('.jpg', '.jpeg', '.png', '.webp'):
            continue
    
        cmd = ["python", "src/demo.py", "ctdet",
               "--dataset", dataset,
               "--arch", architecture,
               "--num_layers", str(num_layer),
               "--trained_model_path", param_path,
               "--demo", "inputs/" + image,
               "--gpus", "0",
               "--debug", "1",
               "--save_dir", "outputs"]
        _ = subprocess.run(cmd)
        shutil.move("outputs/ctdet.jpg",
                    f"outputs/{os.path.splitext(image)[0]}.jpg")
  6. You should find the images with anchor boxes in outputs. You can download them by Colaboratory UI.

TakuyaYashima commented 1 year ago

Also I found that currently the Colab demo does not work as is. --checkpoint is obsolete and that must be changed to --trained_model_path probably due to this commit.