text
stringlengths
1
93.6k
opt.lr_scheduler = True
if not opt.exp_name:
opt.exp_name = f'{opt.Transformation}-{opt.FeatureExtraction}-{opt.SequenceModeling}-{opt.Prediction}'
opt.exp_name += f'-Seed{opt.manualSeed}'
# print(opt.exp_name)
os.makedirs(f'./saved_models/{opt.exp_name}', exist_ok=True)
opt.train_dataset_num = 8919241 + 5522807
opt.one_epoch_steps = int(opt.train_dataset_num / opt.batch_size)
opt.num_epochs = int(opt.num_iter / opt.one_epoch_steps) + 1
""" vocab / character number configuration """
# opt.character += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
# opt.character = ''
# opt.character = string.printable[:-6]
# with open("/home/chengxf/cqc/text-recognition/word_dict_cn_full.txt", "r", encoding='utf-8') as fin: # 可修改
# all_characters = fin.readlines()
# for ch in all_characters:
# opt.character += ch.strip('\n')
""" Seed and GPU setting """
print("Random Seed: ", opt.manualSeed)
random.seed(opt.manualSeed)
np.random.seed(opt.manualSeed)
torch.manual_seed(opt.manualSeed)
torch.cuda.manual_seed(opt.manualSeed)
cudnn.benchmark = True
cudnn.deterministic = True
opt.num_gpu = torch.cuda.device_count()
print('device count', opt.num_gpu)
if opt.num_gpu > 1:
print('------ Use multi-GPU setting ------')
print('if you stuck too long time with multi-GPU setting, try to set --workers 0')
# check multi-GPU issue https://github.com/clovaai/deep-text-recognition-benchmark/issues/1
opt.workers = opt.workers * opt.num_gpu
opt.batch_size = opt.batch_size * opt.num_gpu
""" previous version
print('To equlize batch stats to 1-GPU setting, the batch_size is multiplied with num_gpu and multiplied batch_size is ', opt.batch_size)
opt.batch_size = opt.batch_size * opt.num_gpu
print('To equalize the number of epochs to 1-GPU setting, num_iter is divided with num_gpu by default.')
If you dont care about it, just commnet out these line.)
opt.num_iter = int(opt.num_iter / opt.num_gpu)
"""
print('begin')
train(opt)
# <FILESEP>
import os
import os.path as osp
import time
import argparse
import torch
import torch.distributed as dist
from utils.logger import get_logger
from utils.pyt_utils import parse_devices, all_reduce_tensor, extant_file
try:
from apex.parallel import DistributedDataParallel, SyncBatchNorm
except ImportError:
raise ImportError(
"Please install apex from https://www.github.com/nvidia/apex .")
logger = get_logger()
class Engine(object):
def __init__(self, custom_parser=None):
logger.info(
"PyTorch Version {}".format(torch.__version__))
self.devices = None
self.distributed = False
if custom_parser is None:
self.parser = argparse.ArgumentParser()
else:
assert isinstance(custom_parser, argparse.ArgumentParser)
self.parser = custom_parser
self.inject_default_parser()
self.args = self.parser.parse_args()
self.continue_state_object = self.args.continue_fpath
# if not self.args.gpu == 'None':
# os.environ["CUDA_VISIBLE_DEVICES"]=self.args.gpu
if 'WORLD_SIZE' in os.environ:
self.distributed = int(os.environ['WORLD_SIZE']) > 1
if self.distributed:
self.local_rank = self.args.local_rank
self.world_size = int(os.environ['WORLD_SIZE'])
torch.cuda.set_device(self.local_rank)
dist.init_process_group(backend="nccl", init_method='env://')
self.devices = [i for i in range(self.world_size)]