local_test_pet.py 3.5 KB
Newer Older
W
wuzewu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
# Copyright (c) 2019  PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from test_utils import download_file_and_uncompress, train, eval, vis, export_model
import os

LOCAL_PATH = os.path.dirname(os.path.abspath(__file__))
DATASET_PATH = os.path.join(LOCAL_PATH, "..", "dataset")
MODEL_PATH = os.path.join(LOCAL_PATH, "models")


def download_pet_dataset(savepath, extrapath):
    url = "https://paddleseg.bj.bcebos.com/dataset/mini_pet.zip"
    download_file_and_uncompress(
        url=url, savepath=savepath, extrapath=extrapath)


def download_unet_coco_model(savepath, extrapath):
    url = "https://bj.bcebos.com/v1/paddleseg/models/unet_coco_init.tgz"
    download_file_and_uncompress(
        url=url, savepath=savepath, extrapath=extrapath)


if __name__ == "__main__":
    download_pet_dataset(LOCAL_PATH, DATASET_PATH)
    download_unet_coco_model(LOCAL_PATH, MODEL_PATH)

    model_name = "unet_pet"
    test_model = os.path.join(LOCAL_PATH, "models", "unet_coco_init")
    cfg = os.path.join(LOCAL_PATH, "..", "configs",
                       "{}.yaml".format(model_name))
    freeze_save_dir = os.path.join(LOCAL_PATH, "inference_model", model_name)
    vis_dir = os.path.join(LOCAL_PATH, "visual", model_name)
    saved_model = os.path.join(LOCAL_PATH, "saved_model", model_name)

    devices = ['0']

    train(
        flags=["--cfg", cfg, "--use_gpu", "--log_steps", "10"],
        options=[
            "SOLVER.NUM_EPOCHS", "1", "TRAIN.PRETRAINED_MODEL", test_model,
            "TRAIN.MODEL_SAVE_DIR", saved_model, "DATASET.TRAIN_FILE_LIST",
            os.path.join(DATASET_PATH, "mini_pet", "file_list",
                         "train_list.txt"), "DATASET.VAL_FILE_LIST",
            os.path.join(DATASET_PATH, "mini_pet", "file_list",
                         "val_list.txt"), "DATASET.TEST_FILE_LIST",
            os.path.join(DATASET_PATH, "mini_pet", "file_list",
                         "test_list.txt"), "DATASET.DATA_DIR",
            os.path.join(DATASET_PATH, "mini_pet"), "BATCH_SIZE", "1"
        ],
        devices=devices)

    eval(
        flags=["--cfg", cfg, "--use_gpu"],
        options=[
            "TEST.TEST_MODEL",
            os.path.join(saved_model, "final"), "DATASET.VAL_FILE_LIST",
            os.path.join(DATASET_PATH, "mini_pet", "file_list", "val_list.txt"),
            "DATASET.DATA_DIR",
            os.path.join(DATASET_PATH, "mini_pet")
        ],
        devices=devices)

    vis(flags=["--cfg", cfg, "--use_gpu", "--local_test", "--vis_dir", vis_dir],
        options=[
            "DATASET.TEST_FILE_LIST",
            os.path.join(DATASET_PATH, "mini_pet", "file_list",
                         "test_list.txt"), "DATASET.DATA_DIR",
            os.path.join(DATASET_PATH, "mini_pet"), "TEST.TEST_MODEL",
            os.path.join(saved_model, "final")
        ],
        devices=devices)

    export_model(
        flags=["--cfg", cfg],
        options=[
            "TEST.TEST_MODEL",
            os.path.join(saved_model, "final"), "FREEZE.SAVE_DIR",
            freeze_save_dir
        ],
        devices=devices)