text stringlengths 1 93.6k |
|---|
from model import Model
|
from test import validation
|
from modules.rec_sar_loss import SARLoss
|
from modules.dctc_loss import DCTC
|
from optimizer import CosineAnnealingLR
|
os.environ["CUDA_VISIBLE_DEVICES"] = "1" # 可修改
|
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
def train(opt):
|
""" dataset preparation """
|
if not opt.data_filtering_off:
|
print('Filtering the images containing characters which are not in opt.character')
|
print('Filtering the images whose label is longer than opt.batch_max_length')
|
# see https://github.com/clovaai/deep-text-recognition-benchmark/blob/6593928855fb7abb999a99f428b3e4477d4ae356/dataset.py#L130
|
opt.select_data = opt.select_data.split('-')
|
opt.batch_ratio = opt.batch_ratio.split('-')
|
train_dataset = Batch_Balanced_Dataset(opt)
|
log = open(f'./saved_models/{opt.exp_name}/log_dataset.txt', 'a')
|
AlignCollate_valid = AlignCollate(imgH=opt.imgH, imgW=opt.imgW, keep_ratio_with_pad=opt.PAD)
|
valid_dataset, valid_dataset_log = hierarchical_dataset(root=opt.valid_data, opt=opt)
|
valid_loader = torch.utils.data.DataLoader(
|
valid_dataset, batch_size=opt.batch_size,
|
shuffle=True, # 'True' to check training progress with validation function.
|
num_workers=int(opt.workers),
|
collate_fn=AlignCollate_valid, pin_memory=True)
|
log.write(valid_dataset_log)
|
print('-' * 80)
|
log.write('-' * 80 + '\n')
|
log.close()
|
""" model configuration """
|
if 'CTC' in opt.Prediction:
|
if opt.baiduCTC:
|
converter = CTCLabelConverterForBaiduWarpctc(opt.character)
|
else:
|
converter = CTCLabelConverter(opt.character)
|
else:
|
converter = AttnLabelConverter(opt.character)
|
opt.num_class = len(converter.character)
|
if opt.rgb:
|
opt.input_channel = 3
|
model = Model(opt)
|
print('model input parameters', opt.imgH, opt.imgW, opt.num_fiducial, opt.input_channel, opt.output_channel,
|
opt.hidden_size, opt.num_class, opt.batch_max_length, opt.Transformation, opt.FeatureExtraction,
|
opt.SequenceModeling, opt.Prediction)
|
# weight initialization
|
# for name, param in model.named_parameters():
|
# if 'localization_fc2' in name:
|
# print(f'Skip {name} as it is already initialized')
|
# continue
|
# try:
|
# if 'bias' in name:
|
# init.constant_(param, 0.0)
|
# elif 'weight' in name:
|
# init.kaiming_normal_(param)
|
# except Exception as e: # for batchnorm.
|
# if 'weight' in name:
|
# param.data.fill_(1)
|
# continue
|
# data parallel for multi-GPU
|
model = torch.nn.DataParallel(model).to(device)
|
model.train()
|
if opt.saved_model != '':
|
print(f'loading pretrained model from {opt.saved_model}')
|
if opt.FT:
|
model.load_state_dict(torch.load(opt.saved_model), strict=False)
|
else:
|
model.load_state_dict(torch.load(opt.saved_model))
|
# print("Model:") # 打印模型结构
|
# print(model)
|
""" setup loss """
|
if 'CTC' == opt.Prediction:
|
if opt.baiduCTC:
|
# need to install warpctc. see our guideline.
|
from warpctc_pytorch import CTCLoss
|
criterion = CTCLoss()
|
else:
|
criterion = torch.nn.CTCLoss(zero_infinity=True).to(device)
|
elif 'DCTC' in opt.Prediction:
|
criterion = DCTC(use_il=False, alpha=0.01)
|
else:
|
criterion = torch.nn.CrossEntropyLoss(ignore_index=0).to(device) # ignore [GO] token = ignore index 0
|
# loss averager
|
loss_avg = Averager()
|
# filter that only require gradient decent
|
filtered_parameters = []
|
params_num = []
|
for p in filter(lambda p: p.requires_grad, model.parameters()):
|
filtered_parameters.append(p)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.