optimizer_rotate = optim.SGD(fc.parameters(), lr=opt.lr_init, momentum=0.9, weight_decay=0.0003) scheduler_rotate = optim.lr_scheduler.MultiStepLR(optimizer_rotate, opt.lr_mul, gamma=opt.lr_gamma) # ======================== 设置log路径 ====================== log = Log(opt) # ======================== 训练及测试 ======================= for i_epoch in range(opt.train.max_epoch): # -------------------- 训练 ------------------------- net.train() fc.train() optimizer.zero_grad() scheduler.step() optimizer_rotate.zero_grad() scheduler_rotate.step() log.init() trainloader_len = len(trainloader) for i_iter, data in enumerate(trainloader): img, label, label_rotate = data img, label, label_rotate = img.to(opt.device), label.to(opt.device), label_rotate.to(opt.device) optimizer.zero_grad() optimizer_rotate.zero_grad() x, feat = net(img, label, is_train=True) loss_info = criterion(x, label) x_rotate = fc(feat) loss_rotate = criterion_rotate(x_rotate, label_rotate) loss = (loss_info[0] + loss_rotate).mean() loss.backward() optimizer.step() optimizer_rotate.step()
import waitress # noqa: E402 from rich import pretty, traceback # noqa: E402 from rich.console import Console # noqa: E402 from rich.panel import Panel # noqa: E402 pretty.install() traceback.install(show_locals=False) from about import project, title, version # noqa: E402 from api import api # noqa: E402 from reader.arg import Arg_Reader # noqa: E402 from utils.log import Log # noqa: E402 db = Arg_Reader.read() if db.version is not None: print(db.version) else: ident = f'{project} - {title} v:{version}' console = Console() console.print(Panel.fit(ident)) Log.init(config=db.log_config) api_instance = api(title=title, version=version) Log.get('api').success(f'Accept requests at {db.host}:{db.port}') waitress.serve(api_instance, host=db.host, port=db.port, expose_tracebacks=False, ident=ident, _quiet=True)