def get_model(args): # create model if args.arch == 'wide-resnet': model = model_wideresnet.WideResNet(args.wrn_depth, args.num_classes, args.wrn_widen_factor, drop_rate=args.wrn_droprate) elif args.arch == 'mlp': n_units = [int(x) for x in args.mlp_spec.split('x')] # hidden dims n_units.append(args.num_classes) # output dim n_units.insert(0, 28 * 28 * 3) # input dim model = model_mlp.MLP(n_units) elif args.arch == 'inception': model = model_inception.inception_v3(num_classes=args.num_classes) elif args.arch == 'alexnet': model = model_alexnet.alexnet(num_classes=args.num_classes) # for training on multiple GPUs. # Use CUDA_VISIBLE_DEVICES=0,1 to specify which GPUs to use # model = torch.nn.DataParallel(model).cuda() model = model.cuda() return model
backend='tcp'): os.environ['MASTER_ADDR'] = args.ps_ip os.environ['MASTER_PORT'] = args.ps_port dist.init_process_group(backend, rank=rank, world_size=size) fn(rank, model, train_pics, train_bsz) if __name__ == '__main__': # 随机数设置 manual_seed = random.randint(1, 10000) random.seed(manual_seed) torch.manual_seed(manual_seed) workers = [int(v) for v in str(args.learners).split('-')] model = alexnet(num_classes=10) train_pics = 50000 train_bsz = 64 train_bsz /= len(workers) train_bsz = int(train_bsz) world_size = len(str(args.learners).split('-')) + 1 this_rank = args.this_rank p = TorchProcess(target=init_processes, args=(this_rank, world_size, model, train_pics, train_bsz, run)) p.start() p.join()
def display_net(): net_from_web = alexnet() net_from_torchvision = models.alexnet() print(net_from_web) print(net_from_torchvision)
best_acc = acc best_epoch = epoch if __name__ == '__main__': ### main ### train and validation since = time.time() dataloaders, class_names = data_prepare() dataloaders_train = dataloaders['train'] dataloaders_val = dataloaders['validation'] # use_gpu = torch.cuda.is_available() model = alexnet(pretrained=True) num_classes = 2 model.classifier = nn.Sequential( nn.Dropout(), nn.Linear(256 * 6 * 6, 4096), nn.ReLU(inplace=True), nn.Dropout(), nn.Linear(4096, 4096), nn.ReLU(inplace=True), nn.Linear(4096, num_classes), ) if use_gpu: model = model.cuda() criterion = nn.CrossEntropyLoss()