def make_model(): set_seeds() model = SeaNet({ 1: (mm.MorphConv2d(1, 32, kernel_size=3, padding=1), 0), 2: (mm.MorphConv2d(32, 32, kernel_size=3, padding=1), 1), 3: (mm.MorphBCRLayer(32, 32, kernel_size=3, padding=1), 2), 4: (nn.MaxPool2d(2), 3), 5: (mm.MorphFlatLinear(6272, 10), 4) }) model.eval() return model
def train_mp(models, num_gpus=2, **kwargs): manager = mp.Manager() results = manager.dict() processes = [] for gpu_id, model_ids in enumerate( np.array_split(list(models.keys()), num_gpus)): kwargs.update({ "models": models, "model_ids": model_ids, "gpu_id": gpu_id, "results": results, }) p = mp.Process(target=_mp_train_worker, kwargs=kwargs) p.start() processes.append(p) for p in processes: p.join() results = dict(results) for k in models.keys(): results[k]['model'] = SeaNet.load(results[k]['model_path']) return results
def make_seablock(in_planes, planes, stride=1, input_dim=32): if stride == 1: # simple return SeaNet( { 1: (mm.MorphConv2d(in_planes, planes, kernel_size=3, padding=1, stride=1, bias=False), 0), 2: (mm.AddLayer(alpha=0.5), [1, 0]), 3: (mm.IdentityLayer(), 2), # To allow layers to be added at the end }, input_shape=(in_planes, input_dim, input_dim), tags='simple') else: # downsample return SeaNet( { 1: (mm.MorphConv2d(in_planes, planes, kernel_size=3, padding=1, stride=stride, bias=False), 0), 2: (mm.MorphConv2d(in_planes, planes, kernel_size=1, stride=stride, bias=False), 0), 3: (mm.AddLayer(alpha=0.5), [2, 1]), 4: (mm.IdentityLayer(), 3), # To allow layers to be added at the end }, input_shape=(in_planes, input_dim, input_dim), tags='downsample')
set_seeds(args.seed) for d in ['models', 'logs']: try: d_run = os.path.join('results', args.run_name, d) assert not os.path.exists(d_run), '%s already exists!' % d_run _ = os.makedirs(d_run) except: pass base_model = SeaNet( { 1: (mm.MorphBCRLayer(3, 64, kernel_size=3, padding=1), 0), 2: (mm.MaxPool2d(2), 1), 3: (mm.MorphBCRLayer(64, 128, kernel_size=3, padding=1), 2), 4: (mm.MaxPool2d(2), 3), 5: (mm.MorphBCRLayer(128, 128, kernel_size=3, padding=1), 4), 6: (mm.MorphFlatLinear(2048 * 4, 10), 5), }, input_shape=(3, 32, 32)) pretrain_epochs = 20 additional_epochs = 20 dataloaders = make_dataloaders(train_size=1.0) base_model, base_performance = train(base_model, dataloaders, epochs=pretrain_epochs, verbose=True) for p in base_performance:
from trainer import make_dataloaders, train from helpers import to_numpy def parse_args(): parser = argparse.ArgumentParser() parser.add_argument('--inpath', type=str) parser.add_argument('--outpath', type=str) return parser.parse_args() # -- # Run if __name__ == "__main__": args = parse_args() print('%s -> %s' % (args.inpath, args.outpath)) model = SeaNet.load(args.inpath).cuda().eval() dataloader = make_dataloaders(root='../data', train_size=1.0)['test'] all_outputs = [] for batch_idx, (data, targets) in enumerate(dataloader): data = Variable(data.cuda(), volatile=True) all_outputs.append(to_numpy(model(data))) all_outputs = np.vstack(all_outputs) np.save(args.outpath, all_outputs)
sys.path.append('..') from seanet import SeaNet from trainer import make_dataloaders, train from lr import LRSchedule from helpers import reset_parameters def parse_args(): parser = argparse.ArgumentParser() parser.add_argument('--inpath', type=str) parser.add_argument('--epochs', type=int, default=100) return parser.parse_args() # -- # Run if __name__ == "__main__": args = parse_args() model = SeaNet.load(args.inpath) model = reset_parameters(model) dataloaders = make_dataloaders(train_size=1.0) model, performance = train(model, dataloaders, epochs=args.epochs, gpu_id=1, verbose=True) model.save('./retrained') json.dump(performance, open('./retrained-performance.json', 'w'))