|
| 1 | +""" |
| 2 | +entropy mining |
| 3 | +""" |
| 4 | +import os |
| 5 | +import sys |
| 6 | + |
| 7 | +import cv2 |
| 8 | +import numpy as np |
| 9 | +import torch |
| 10 | +import torch.distributed as dist |
| 11 | +from mmcv.runner import init_dist |
| 12 | +from mmdet.apis.test import collect_results_gpu |
| 13 | +from tqdm import tqdm |
| 14 | +from ymir_exc import result_writer as rw |
| 15 | +from ymir_exc.util import YmirStage, get_merged_config, write_ymir_monitor_process |
| 16 | +from ymir_mining_cald import split_result, CALDMiner |
| 17 | + |
| 18 | +LOCAL_RANK = int(os.getenv('LOCAL_RANK', -1)) # https://pytorch.org/docs/stable/elastic/run.html |
| 19 | +RANK = int(os.getenv('RANK', -1)) |
| 20 | +WORLD_SIZE = int(os.getenv('WORLD_SIZE', 1)) |
| 21 | + |
| 22 | + |
| 23 | +class EntropyMiner(CALDMiner): |
| 24 | + |
| 25 | + def mining(self): |
| 26 | + with open(self.cfg.ymir.input.candidate_index_file, 'r') as f: |
| 27 | + images = [line.strip() for line in f.readlines()] |
| 28 | + |
| 29 | + max_barrier_times = len(images) // WORLD_SIZE |
| 30 | + if RANK == -1: |
| 31 | + N = len(images) |
| 32 | + tbar = tqdm(images) |
| 33 | + else: |
| 34 | + images_rank = images[RANK::WORLD_SIZE] |
| 35 | + N = len(images_rank) |
| 36 | + if RANK == 0: |
| 37 | + tbar = tqdm(images_rank) |
| 38 | + else: |
| 39 | + tbar = images_rank |
| 40 | + |
| 41 | + monitor_gap = max(1, N // 100) |
| 42 | + mining_result = [] |
| 43 | + for idx, asset_path in enumerate(tbar): |
| 44 | + if idx % monitor_gap == 0 and RANK in [0, -1]: |
| 45 | + write_ymir_monitor_process(self.cfg, task='mining', naive_stage_percent=idx / N, stage=YmirStage.TASK) |
| 46 | + # batch-level sync, avoid 30min time-out error |
| 47 | + if WORLD_SIZE > 1 and idx < max_barrier_times: |
| 48 | + dist.barrier() |
| 49 | + |
| 50 | + img = cv2.imread(asset_path) |
| 51 | + # xyxy,conf,cls |
| 52 | + result = self.predict(img) |
| 53 | + bboxes, conf, _ = split_result(result) |
| 54 | + if len(result) == 0: |
| 55 | + # no result for the image without augmentation |
| 56 | + mining_result.append((asset_path, -10)) |
| 57 | + continue |
| 58 | + conf = conf.data.cpu().numpy() |
| 59 | + mining_result.append((asset_path, -np.sum(conf * np.log2(conf)))) |
| 60 | + |
| 61 | + if WORLD_SIZE > 1: |
| 62 | + mining_result = collect_results_gpu(mining_result, len(images)) |
| 63 | + |
| 64 | + return mining_result |
| 65 | + |
| 66 | + |
| 67 | +def main(): |
| 68 | + if LOCAL_RANK != -1: |
| 69 | + init_dist(launcher='pytorch', backend="nccl" if dist.is_nccl_available() else "gloo") |
| 70 | + |
| 71 | + cfg = get_merged_config() |
| 72 | + miner = EntropyMiner(cfg) |
| 73 | + gpu = max(0, LOCAL_RANK) |
| 74 | + device = torch.device('cuda', gpu) |
| 75 | + miner.model.to(device) |
| 76 | + mining_result = miner.mining() |
| 77 | + |
| 78 | + if RANK in [0, -1]: |
| 79 | + rw.write_mining_result(mining_result=mining_result) |
| 80 | + |
| 81 | + write_ymir_monitor_process(cfg, task='mining', naive_stage_percent=1, stage=YmirStage.POSTPROCESS) |
| 82 | + |
| 83 | + return 0 |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + sys.exit(main()) |
0 commit comments