def main(client_num, model, C=1.0, lr=0.1, epoch=1, aggregator=FedAvg(), iid=True, client_eval=False): logger = Logger('output/{}'.format( datetime.now().strftime('%y%m%d%H%M%S')), level='debug') # 读取配置 cfg.merge_from_file('config/{}.yaml'.format(model)) if C == 0: cfg.SOLVER.MAX_EPOCH = 2000 cfg.freeze() # 加载数据 transforms_train = transforms.Compose([ transforms.Resize(cfg.MODEL.BACKBONE.INPUT_RESOLUTION), transforms.ToTensor(), transforms.Normalize((0.1307, ), (0.3081, )) ]) train_set = torchvision.datasets.MNIST(root='data', train=True, download=True, transform=transforms_train) # 分配数据 if iid: sample_idx = iid_sampler(train_set, client_num) else: sample_idx = non_iid_sampler(train_set, client_num) # 初始化联邦 s = Server(config=cfg, dataset_name='MNIST', aggregator=aggregator, logger=logger) def flip(tensor): return (tensor + 2) % 10 for i, sample in enumerate(sample_idx): if i < client_num - 10: c = Client(i, train_set, sample_idx[i], logger=logger) else: c = LabelFlippingClient(i, train_set, sample_idx[i], label_map=flip, logger=logger) s.append_clients(c) s.train(C=C, epoch_num=epoch, lr=lr, local_eval=client_eval) cfg.defrost()
def main(): ''' Main function ''' if cfg.SYSTEM.UPDATE_CFG: cfg.merge_from_file(cfg.SYSTEM.CFG_FILE) cfg.freeze() # 冻结参数 print(cfg) vis.log('configs:\n {}'.format(cfg.clone())) # LOAD DATA from data import PascalBox dataset = PascalBox() # training_data, validation_data = prepare_stanford40(cfg.clone()) # training_data, validation_data = prepare_pascal(cfg.clone()) training_data = DataLoader(dataset, batch_size=46, shuffle=True) validation_data = DataLoader(dataset, batch_size=46) from lib.attention_reid.attention_reid import attentionReid # LOAD MODEL model = attentionReid() # model = model.multi_two(pretrained=True) for params in model.backbone.parameters(): params.require_grad = False model = model.cuda() # ------------- optim -----------------# # optimizer = optim.SGD([ # {'params': model.backbone.parameters(),'lr':1e-4}, # {'params': model.attention.parameters(), 'lr': 1e-3}, # {'params': model.GCN.parameters(),'lr':1e-3} # ], lr=1e-3,) optimizer = optim.SGD(model.parameters(), lr=1e-4) scheduler = ReduceLROnPlateau(optimizer, mode='min', factor=cfg.OPTIM.LR_DECAY, patience=1, verbose=True, threshold=1e-4, threshold_mode='rel', cooldown=1, min_lr=0.00005, eps=1e-8) # ------------- train ----------------# train(model, training_data, validation_data, optimizer, scheduler, cfg.clone())
import torch import torch.nn.functional as F import torch.optim as optim from torch.utils.data import DataLoader from torch.autograd import Variable as V from torchnet import meter from config.config import cfg from util.visualize import Visualizer from util.show_masked_image import show_masked_image from mmcv.runner import save_checkpoint, load_checkpoint import cv2 from util.show_masked_image import tensor_to_np import numpy as np #cfg.merge_from_file("config/un_att_pascal_0001.yaml") cfg.freeze() # 冻结参数 vis = Visualizer("newvis", port=8097) AP = meter.APMeter() mAP = meter.mAPMeter() top3 = meter.ClassErrorMeter(topk=[1, 3, 5], accuracy=True) Loss_meter = meter.AverageValueMeter() os.environ["CUDA_VISIBLE_DEVICES"] = "0,1,2" num = 30 def visualize_func(result): pass def inverse_normalize(img):
import json from config.config import cfg cfg.merge_from_file('./config/config.yaml') cfg.freeze() print(json.dumps(cfg, indent=4))