未验证 提交 0023ce55 编写于 作者: W Wang Feng 提交者: GitHub

feat(detection): support time and isort (#42)

上级 848ebfa2
[isort]
line_length = 100
multi_line_output = 3
balanced_wrapping = True
# skip = tools/test_net.py, tools/train_net.py
known_standard_library = setuptools
known_myself = official
known_data_processing = cv2,numpy,scipy,PIL,matplotlib
known_datasets = pycocotools
known_deeplearning = megengine
sections = FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,data_processing,datasets,deeplearning,myself,LOCALFOLDER
no_lines_before=STDLIB,THIRDPARTY,datasets
default_section = FIRSTPARTY
...@@ -6,10 +6,10 @@ ...@@ -6,10 +6,10 @@
# Unless required by applicable law or agreed to in writing, # Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an # software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import megengine as mge
import megengine.functional as F
import numpy as np import numpy as np
import megengine as mge
import megengine.functional as F
from megengine.core import Tensor from megengine.core import Tensor
......
...@@ -24,9 +24,9 @@ ...@@ -24,9 +24,9 @@
# This file has been modified by Megvii ("Megvii Modifications"). # This file has been modified by Megvii ("Megvii Modifications").
# All Megvii Modifications are Copyright (C) 2014-2020 Megvii Inc. All rights reserved. # All Megvii Modifications are Copyright (C) 2014-2020 Megvii Inc. All rights reserved.
# --------------------------------------------------------------------- # ---------------------------------------------------------------------
import megengine.module as M
import numpy as np import numpy as np
import megengine.module as M
from megengine.core import Buffer from megengine.core import Buffer
......
...@@ -8,10 +8,10 @@ ...@@ -8,10 +8,10 @@
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
from abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
import megengine.functional as F
import numpy as np import numpy as np
from megengine.core import tensor, Tensor import megengine.functional as F
from megengine.core import Tensor, tensor
class BaseAnchorGenerator(metaclass=ABCMeta): class BaseAnchorGenerator(metaclass=ABCMeta):
......
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
from abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
import megengine.functional as F import megengine.functional as F
from megengine.core import Tensor from megengine.core import Tensor
......
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
# software distributed under the License is distributed on an # software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import megengine.functional as F import megengine.functional as F
from megengine.core import Tensor from megengine.core import Tensor
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
import math import math
import numpy as np import numpy as np
import megengine as mge import megengine as mge
import megengine.functional as F import megengine.functional as F
......
...@@ -10,7 +10,6 @@ import math ...@@ -10,7 +10,6 @@ import math
from typing import List from typing import List
import megengine.module as M import megengine.module as M
from megengine.core import Tensor from megengine.core import Tensor
from official.vision.detection.layers import basic from official.vision.detection.layers import basic
......
...@@ -7,9 +7,10 @@ ...@@ -7,9 +7,10 @@
# software distributed under the License is distributed on an # software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import megengine as mge import megengine as mge
import megengine.random as rand
import megengine.functional as F import megengine.functional as F
import megengine.module as M import megengine.module as M
import megengine.random as rand
from official.vision.detection import layers from official.vision.detection import layers
from official.vision.detection.tools.gpu_nms import batched_nms from official.vision.detection.tools.gpu_nms import batched_nms
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
# software distributed under the License is distributed on an # software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import numpy as np import numpy as np
import megengine as mge import megengine as mge
import megengine.functional as F import megengine.functional as F
import megengine.module as M import megengine.module as M
......
...@@ -6,10 +6,11 @@ ...@@ -6,10 +6,11 @@
# Unless required by applicable law or agreed to in writing, # Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an # software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import numpy as np
import megengine as mge import megengine as mge
import megengine.functional as F import megengine.functional as F
import megengine.module as M import megengine.module as M
import numpy as np
from official.vision.classification.resnet.model import resnet50 from official.vision.classification.resnet.model import resnet50
from official.vision.detection import layers from official.vision.detection import layers
......
...@@ -5,6 +5,7 @@ import os ...@@ -5,6 +5,7 @@ import os
import struct import struct
import numpy as np import numpy as np
import megengine as mge import megengine as mge
import megengine.functional as F import megengine.functional as F
from megengine._internal.craniotome import CraniotomeBase from megengine._internal.craniotome import CraniotomeBase
......
...@@ -12,8 +12,9 @@ import os ...@@ -12,8 +12,9 @@ import os
import sys import sys
import cv2 import cv2
import megengine as mge
import numpy as np import numpy as np
import megengine as mge
from megengine import jit from megengine import jit
from megengine.data.dataset import COCO from megengine.data.dataset import COCO
......
...@@ -13,13 +13,14 @@ import os ...@@ -13,13 +13,14 @@ import os
import random import random
import sys import sys
from multiprocessing import Process, Queue from multiprocessing import Process, Queue
from tqdm import tqdm
import cv2 import cv2
import megengine as mge
import numpy as np import numpy as np
import megengine as mge
from megengine import jit from megengine import jit
from megengine.data import DataLoader, SequentialSampler from megengine.data import DataLoader, SequentialSampler
from tqdm import tqdm
from official.vision.detection.tools.data_mapper import data_mapper from official.vision.detection.tools.data_mapper import data_mapper
from official.vision.detection.tools.nms import py_cpu_nms from official.vision.detection.tools.nms import py_cpu_nms
......
...@@ -14,16 +14,18 @@ import importlib ...@@ -14,16 +14,18 @@ import importlib
import multiprocessing as mp import multiprocessing as mp
import os import os
import sys import sys
import time
from collections import defaultdict from collections import defaultdict
from tabulate import tabulate
import megengine as mge
import numpy as np import numpy as np
import megengine as mge
from megengine import distributed as dist from megengine import distributed as dist
from megengine import jit from megengine import jit
from megengine import optimizer as optim from megengine import optimizer as optim
from megengine.data import Collator, DataLoader, Infinite, RandomSampler from megengine.data import Collator, DataLoader, Infinite, RandomSampler
from megengine.data import transform as T from megengine.data import transform as T
from tabulate import tabulate
from official.vision.detection.tools.data_mapper import data_mapper from official.vision.detection.tools.data_mapper import data_mapper
...@@ -81,7 +83,8 @@ def worker(rank, world_size, args): ...@@ -81,7 +83,8 @@ def worker(rank, world_size, args):
weights = mge.load(args.weight_file) weights = mge.load(args.weight_file)
model.backbone.bottom_up.load_state_dict(weights) model.backbone.bottom_up.load_state_dict(weights)
logger.info("Prepare dataset") if rank == 0:
logger.info("Prepare dataset")
loader = build_dataloader(model.batch_size, args.dataset_dir, model.cfg) loader = build_dataloader(model.batch_size, args.dataset_dir, model.cfg)
train_loader = iter(loader["train"]) train_loader = iter(loader["train"])
...@@ -155,21 +158,32 @@ def train_one_epoch( ...@@ -155,21 +158,32 @@ def train_one_epoch(
return losses return losses
meter = AverageMeter(record_len=model.cfg.num_losses) meter = AverageMeter(record_len=model.cfg.num_losses)
time_meter = AverageMeter(record_len=2)
log_interval = model.cfg.log_interval log_interval = model.cfg.log_interval
for step in range(tot_steps): for step in range(tot_steps):
adjust_learning_rate(opt, epoch_id, step, model, world_size) adjust_learning_rate(opt, epoch_id, step, model, world_size)
data_tik = time.time()
mini_batch = next(data_queue) mini_batch = next(data_queue)
data_tok = time.time()
model.inputs["image"].set_value(mini_batch["data"]) model.inputs["image"].set_value(mini_batch["data"])
model.inputs["gt_boxes"].set_value(mini_batch["gt_boxes"]) model.inputs["gt_boxes"].set_value(mini_batch["gt_boxes"])
model.inputs["im_info"].set_value(mini_batch["im_info"]) model.inputs["im_info"].set_value(mini_batch["im_info"])
tik = time.time()
opt.zero_grad() opt.zero_grad()
loss_list = propagate() loss_list = propagate()
opt.step() opt.step()
tok = time.time()
time_meter.update([tok - tik, data_tok - data_tik])
if rank == 0: if rank == 0:
info_str = "e%d, %d/%d, lr:%f, "
loss_str = ", ".join(["{}:%f".format(loss) for loss in model.cfg.losses_keys]) loss_str = ", ".join(["{}:%f".format(loss) for loss in model.cfg.losses_keys])
log_info_str = "e%d, %d/%d, lr:%f, " + loss_str time_str = ", train_time:%.3fs, data_time:%.3fs"
log_info_str = info_str + loss_str + time_str
meter.update([loss.numpy() for loss in loss_list]) meter.update([loss.numpy() for loss in loss_list])
if step % log_interval == 0: if step % log_interval == 0:
average_loss = meter.average() average_loss = meter.average()
...@@ -180,8 +194,10 @@ def train_one_epoch( ...@@ -180,8 +194,10 @@ def train_one_epoch(
tot_steps, tot_steps,
opt.param_groups[0]["lr"], opt.param_groups[0]["lr"],
*average_loss, *average_loss,
*time_meter.average()
) )
meter.reset() meter.reset()
time_meter.reset()
def make_parser(): def make_parser():
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册