main_worker is conducted on each GPU.
(gpu, ngpus_per_node, args)
| 67 | |
| 68 | |
| 69 | def main_worker(gpu, ngpus_per_node, args): |
| 70 | ''' |
| 71 | main_worker is conducted on each GPU. |
| 72 | ''' |
| 73 | |
| 74 | global best_acc1 |
| 75 | args.gpu = gpu |
| 76 | |
| 77 | # random seed has to be set for the syncronization of labeled data sampling in each process. |
| 78 | assert args.seed is not None |
| 79 | random.seed(args.seed) |
| 80 | torch.manual_seed(args.seed) |
| 81 | np.random.seed(args.seed) |
| 82 | cudnn.deterministic = True |
| 83 | |
| 84 | # SET UP FOR DISTRIBUTED TRAINING |
| 85 | if args.distributed: |
| 86 | if args.dist_url == "env://" and args.rank == -1: |
| 87 | args.rank = int(os.environ["RANK"]) |
| 88 | if args.multiprocessing_distributed: |
| 89 | args.rank = args.rank * ngpus_per_node + gpu # compute global rank |
| 90 | |
| 91 | # set distributed group: |
| 92 | dist.init_process_group(backend=args.dist_backend, init_method=args.dist_url, |
| 93 | world_size=args.world_size, rank=args.rank) |
| 94 | |
| 95 | # SET save_path and logger |
| 96 | save_path = os.path.join(args.save_dir, args.save_name) |
| 97 | logger_level = "WARNING" |
| 98 | tb_log = None |
| 99 | if args.rank % ngpus_per_node == 0: |
| 100 | tb_log = TBLog(save_path, 'tensorboard', use_tensorboard=args.use_tensorboard) |
| 101 | logger_level = "INFO" |
| 102 | |
| 103 | logger = get_logger(args.save_name, save_path, logger_level) |
| 104 | logger.warning(f"USE GPU: {args.gpu} for training") |
| 105 | |
| 106 | # SET FixMatch: class FixMatch in models.fixmatch |
| 107 | args.bn_momentum = 1.0 - 0.999 |
| 108 | if 'imagenet' in args.dataset.lower(): |
| 109 | _net_builder = net_builder('ResNet50', False, None, is_remix=False) |
| 110 | else: |
| 111 | _net_builder = net_builder(args.net, |
| 112 | args.net_from_name, |
| 113 | {'first_stride': 2 if 'stl' in args.dataset else 1, |
| 114 | 'depth': args.depth, |
| 115 | 'widen_factor': args.widen_factor, |
| 116 | 'leaky_slope': args.leaky_slope, |
| 117 | 'bn_momentum': args.bn_momentum, |
| 118 | 'dropRate': args.dropout, |
| 119 | 'use_embed': False, |
| 120 | 'is_remix': False}, |
| 121 | ) |
| 122 | |
| 123 | model = FixMatch(_net_builder, |
| 124 | args.num_classes, |
| 125 | args.ema_m, |
| 126 | args.T, |
no test coverage detected