def main(): Place = paddle.fluid.CUDAPlace(0) with fluid.dygraph.guard(Place): model = Transformer(image_size=512, num_classes=15, hidden_unit_num=1024, layer_num=2, head_num=16, dropout=0.8, decoder_name='PUP', hyber=True, visualable=False) preprocess = Transform(512) dataloader_1 = Dataloader('/home/aistudio/dataset', '/home/aistudio/dataset/val_list.txt', transform=preprocess, shuffle=True) val_load = fluid.io.DataLoader.from_generator(capacity=1, use_multiprocess=False) val_load.set_sample_generator(dataloader_1, batch_size=1, places=Place) model_dic, optic_dic = load_dygraph( "./output/SETR-NotZero-Epoch-2-Loss-0.161517-MIOU-0.325002") model.load_dict(model_dic) model.eval() '''result = get_infer_data("/home/aistudio/dataset/infer") infer_load = Load_infer('/home/aistudio/dataset', result, transform=preprocess, shuffle=False) loader_infer= fluid.io.DataLoader.from_generator(capacity=1, use_multiprocess=False) loader_infer.set_sample_generator(infer_load, batch_size=1, places=Place) process_image(model, loader_infer, result)''' validation(val_load, model, 15)
for epoch in range(ARGS.n_epochs): for train_batch in train_iter: train_batch = utils.rebatch(pad_idx, train_batch) train_out = model(train_batch.src, train_batch.trg, train_batch.src_mask, train_batch.trg_mask) train_loss = criterion( train_out.contiguous().view(-1, train_out.size(-1)), train_batch.trg_y.contiguous().view(-1)) / train_batch.ntokens train_loss.backward() optimizer.step() optimizer.optimizer.zero_grad() print(f'{iter_cnt} - train_loss: {train_loss.item()}') if iter_cnt % ARGS.eval_steps == 0: model.eval() val_total_loss = 0 val_ntokens = 0 with torch.no_grad(): for val_batch in val_iter: val_batch = utils.rebatch(pad_idx, val_batch) val_out = model(val_batch.src, val_batch.trg, val_batch.src_mask, val_batch.trg_mask) val_loss = criterion( val_out.contiguous().view(-1, val_out.size(-1)), val_batch.trg_y.contiguous().view(-1)) val_total_loss += val_loss.item() val_ntokens += val_batch.ntokens norm_val_loss = val_total_loss / val_ntokens.float()