def convert(model_data_path, output_path='/tmp/model.ckpt'): '''Convert the caffe model parameters to .ckpt.''' # Set the data specifications for the model width = None height = None channels = None input_node_name = None assert width and height and channels and input_node_name is not None, \ 'Please, fill the model parameters' # Create a placeholder for the input image input_node = tf.placeholder(tf.float32, shape=(None, height, width, channels), name='inputs') # Construct the network net = Net({'input_node_name': input_node}) with tf.Session() as sesh: # Load the converted parameters print('Loading the model...') net.load(model_data_path, sesh) saver = tf.train.Saver() save_path = saver.save(sesh, output_path) print("\nModel saved in file: %s" % save_path)
def inception_mod(inputs, hyperparams: dict, name: str): """ Well-tuned inception module used in the subsequent GoogLeNet-like CNN :param inputs: tensor :param hyperparams: dictionnary of hyperparameters for the sizes of the kernels :param name: for variable scope """ with tf.variable_scope(name): # 1x1 pathway x1 = Net.conv2d(layer_name='1x1_conv', inputs=inputs, kernel_shape=hyperparams["1x1_conv_kernel"], strides=1, activation_func=tf.nn.tanh, padding='SAME') # 1x1 to 3x3 pathway x2 = Net.conv2d(layer_name='3x3_conv1', inputs=inputs, kernel_shape=hyperparams["3x3_conv_kernel2"], strides=1, activation_func=tf.nn.tanh, padding='SAME', kernel_shape_pre=hyperparams["3x3_conv_kernel1"]) # 1x1 to 5x5 pathway x3 = Net.conv2d(layer_name='5x5_conv1', inputs=inputs, kernel_shape=hyperparams["5x5_conv_kernel2"], strides=1, activation_func=tf.nn.tanh, padding='SAME', kernel_shape_pre=hyperparams["5x5_conv_kernel1"]) # 3x3 to 1x1 pathway x4 = tf.nn.max_pool(inputs, ksize=[1, 3, 3, 1], strides=[1, 1, 1, 1], padding='SAME', name="pooling1") x4 = Net.conv2d(layer_name='pooling1_conv', inputs=x4, kernel_shape=hyperparams["pooling1_conv_kernel"], strides=1, activation_func=tf.nn.tanh, padding='SAME') x = tf.concat([x1, x2, x3, x4], axis=3) # Concat in the 4th dim to stack outputs = tf.tanh(x) return outputs
def __init__(self, opt): self.opt = opt self.device = torch.device( 'cuda:0' if torch.cuda.is_available() else 'cpu') if opt.net == 'vgg': self.net = vgg.vgg16(num_classes=opt.n_classes) elif opt.net == 'normal': self.net = Net(num_classes=opt.n_classes) elif opt.net == 'att': self.net = AttNet(num_classes=opt.n_classes) elif opt.net == 'vgg_att': self.net = VGGAttNet(num_classes=opt.n_classes) else: raise ValueError('[%s] cannot be used!' % opt.net) self.net = self.net.to(self.device) if not opt.is_train: self.load_network(self.opt.which_epoch) else: if self.opt.resume_train: self.load_network(self.opt.which_epoch) self.cls_criterion = nn.CrossEntropyLoss() if opt.training_type == 'att_consist': self.mask_criterion = nn.MSELoss() self.optimizer = optim.SGD(self.net.parameters(), lr=0.001, momentum=0.9)
def projection_shortcut(inputs): return Net.conv2d(inputs=inputs, kernel_shape=(1, 1, inputs.shape[-1], filters_out), strides=strides, padding='SAME', activation_func=tf.identity, layer_name='projection')
def _building_block(inputs, filters, training, projection_shortcut, strides, name: str): """ :param inputs: A tensor of size[batch, height_in, width_in, channels] :param filters: The number of filters for the first convolution of the layer. :param training: Either True or False, whether we are currently training the model. Needed for batch norm. :param projection_shortcut: The function to use for projection shortcuts (typically a 1x1 convolution when downsampling the input). strides: The block's stride. If greater than 1, this block will ultimately downsample the input. :param strides: The block's stride. If greater than 1, this block will ultimately downsample the input. :param name: A string name for the tensor output of the block layer. :return: The output tensor of the block; shape should match inputs. """ with tf.variable_scope(name): shortcut = inputs inputs = Net.batch_normalization(inputs, training) inputs = tf.nn.relu(inputs) # The projection shortcut should come after the first batch norm and ReLU # since it performs a 1x1 convolution. if projection_shortcut is not None: shortcut = projection_shortcut(inputs) inputs = Net.conv2d(inputs=inputs, kernel_shape=(3, 3, inputs.shape[-1], filters), strides=strides, padding='SAME', activation_func=tf.identity, layer_name='conv1_building_block') inputs = Net.batch_normalization(inputs, training) inputs = tf.nn.relu(inputs) inputs = Net.conv2d(inputs=inputs, kernel_shape=(3, 3, inputs.shape[-1], filters), strides=1, padding='SAME', activation_func=tf.identity, layer_name='conv2_building_block') return inputs + shortcut
def main(): DIR = "C:/Users/roger/git/wiener-impact-model/data/test8augmented" net = Net() train_dataset = Test8Augmented(root=DIR, name="Test8Augmented") train_loader = DataLoader(train_dataset, batch_size=5) optimizer = torch.optim.Adam(net.parameters(), lr=0.001, weight_decay=5e-4) # Treinamento losses = [] for epoch in range(5): running_loss = 0.0 for i, data in enumerate(train_loader): optimizer.zero_grad() outputs = net(data) loss = F.mse_loss(outputs, data.y) loss.backward() optimizer.step() running_loss += loss.item() losses.append(running_loss) # print("Epoch: {} - Running Loss: {}".format(epoch + 1, running_loss)) # Teste erro = [] predicted = [] target = [] for data in train_loader: for e in net(data): predicted.append(e.item()) for e in data.y: target.append(e.item()) for e, i in zip(net(data), data.y): erro.append(F.mse_loss(e, i).item()) print("MSE: {}".format(sum(erro) / len(erro))) plt.subplot(2, 1, 1) plt.plot(losses) plt.subplot(2, 1, 2) plt.plot(predicted) plt.plot(target) plt.show()
def predict(): model = Net(model_name).to(device) model_save_path = os.path.join(config.model_path, '{}.bin'.format(model_name)) model.load_state_dict(torch.load(model_save_path)) data_len = len(os.listdir(config.image_test_path)) test_path_list = [ '{}/{}.jpg'.format(config.image_test_path, x) for x in range(0, data_len) ] test_data = np.array(test_path_list) test_dataset = MyDataset(test_data, test_transform, 'test') test_loader = DataLoader(test_dataset, batch_size=config.batch_size, shuffle=False) model.eval() pred_list = [] with torch.no_grad(): for batch_x, _ in tqdm(test_loader): batch_x = batch_x.to(device) # compute output logits = model(batch_x) preds = torch.argmax(logits, dim=1) pred_list += [p.cpu().data.numpy() for p in preds] submission = pd.DataFrame({ "id": range(len(pred_list)), "label": pred_list }) submission.to_csv('submission.csv', index=False, header=False)
def test(**kwargs): config.parse(kwargs) test_loader = DataLoader( WholeFaceDatasets(config.whole_csv_test, config.whole_imgs_base_dir, train=False, transform=transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ])), batch_size=config.batch_size, shuffle=False, num_workers=config.num_workers ) model = Net() if config.using_pretrain_model: model.load_state_dict(torch.load(config.pretrain_model_path)) model.cuda() model.eval() test_loss = 0 tables = None for data, target in test_loader: target = target.type(torch.FloatTensor) data, target = data.cuda(), target.cuda() data, target = Variable(data, volatile=True), Variable(target) output = model(data) # print(output) loss = F.l1_loss(output, target).cpu() a = output.cpu().data.numpy() b = np.reshape(target.cpu().data.numpy(), (-1, 1)) pair = np.hstack((a, b)) if tables is None: tables = pair else: tables = np.vstack((tables, pair)) test_loss += loss.data[0] * config.batch_size test_loss /= len(test_loader.dataset) # print(tables) np.save('predicted_and_real.npy', tables) print(f'Testing Accuracy is {test_loss}')
def main(args): if args.output_dir: mkdir(args.output_dir) print(args) if 'cuda' in args.device and torch.cuda.is_available(): device = torch.device("cuda:0") torch.backends.cudnn.benchmark = True else: device = torch.device('cpu') datasets, dataloaders = load_data(args.data_path, batch_size=args.batch_size, num_workers=args.workers) print("Creating model") model = Net(num_classes=datasets['train'].num_classes).to(device) print(device) criterion, optimizer, lr_scheduler = setup_optim(model, args) logger = Logger(len(dataloaders['train'])) trainer = Trainer(model, criterion, optimizer, lr_scheduler, device, logger, args.print_freq) metrics = get_metrics(criterion) evaluator = Evaluator(trainer.trainer_engine, model, metrics, dataloaders['val'], device, logger) if args.test_only: evaluator.run() return print("Start training") start_time = time.time() trainer.run(dataloaders['train'], args.epochs) total_time = time.time() - start_time total_time_str = str(datetime.timedelta(seconds=int(total_time))) print('Training time {}'.format(total_time_str))
def train(train_data, val_data, fold_idx=None): train_data = MyDataset(train_data, train_transform) train_loader = DataLoader(train_data, batch_size=config.batch_size, shuffle=True) val_data = MyDataset(val_data, val_transform) val_loader = DataLoader(val_data, batch_size=config.batch_size, shuffle=False) model = Net(model_name).to(device) # criterion = nn.CrossEntropyLoss() criterion = FocalLoss(0.5) optimizer = torch.optim.Adam(model.parameters(), lr=1e-3) scheduler = optim.lr_scheduler.ExponentialLR(optimizer, gamma=0.1) if fold_idx is None: print('start') model_save_path = os.path.join(config.model_path, '{}.bin'.format(model_name)) else: print('start fold: {}'.format(fold_idx + 1)) model_save_path = os.path.join( config.model_path, '{}_fold{}.bin'.format(model_name, fold_idx)) if os.path.isfile(model_save_path): print('加载之前的训练模型') model.load_state_dict(torch.load(model_save_path)) best_val_acc = 0 last_improved_epoch = 0 adjust_lr_num = 0 for cur_epoch in range(config.epochs_num): start_time = int(time.time()) model.train() print('epoch:{}, step:{}'.format(cur_epoch + 1, len(train_loader))) cur_step = 0 for batch_x, batch_y in train_loader: batch_x, batch_y = batch_x.to(device), batch_y.to(device) optimizer.zero_grad() probs = model(batch_x) train_loss = criterion(probs, batch_y) train_loss.backward() optimizer.step() cur_step += 1 if cur_step % config.train_print_step == 0: train_acc = accuracy(probs, batch_y) msg = 'the current step: {0}/{1}, train loss: {2:>5.2}, train acc: {3:>6.2%}' print( msg.format(cur_step, len(train_loader), train_loss.item(), train_acc[0].item())) val_loss, val_acc = evaluate(model, val_loader, criterion) if val_acc >= best_val_acc: best_val_acc = val_acc torch.save(model.state_dict(), model_save_path) improved_str = '*' last_improved_epoch = cur_epoch else: improved_str = '' msg = 'the current epoch: {0}/{1}, val loss: {2:>5.2}, val acc: {3:>6.2%}, cost: {4}s {5}' end_time = int(time.time()) print( msg.format(cur_epoch + 1, config.epochs_num, val_loss, val_acc, end_time - start_time, improved_str)) if cur_epoch - last_improved_epoch > config.patience_epoch: print("No optimization for a long time, adjust lr...") scheduler.step() last_improved_epoch = cur_epoch # 加上,不然会连续更新的 adjust_lr_num += 1 if adjust_lr_num > config.adjust_lr_num: print("No optimization for a long time, auto stopping...") break del model gc.collect()
def test_train_net_output(): net = Net() x = torch.randn(1, 1, 28, 28) y = net(x) assert y.size() == (1, 10)
'-r', default=None, help='checkpoint file name. It ends with .ckpt') args = parser.parse_args() use_cuda = 'cuda' if torch.cuda.is_available() else 'cpu' run_id = uuid1() comment = "lr_" + str(args.learning_rate) + "_de_" + str( args.decay) + "_dg_" + str(args.decay_gamma) + "_e_" + str( args.epochs) + "_b_" + str(args.batch) + "_o_" + str(args.optimizer) best_acc = 0 sw = SummaryWriter(log_dir=args.log_dir + "/" + str(run_id), comment=comment) net = Net() if use_cuda == 'cuda': ltype = torch.cuda.LongTensor ftype = torch.cuda.FloatTensor net = net.cuda() # net = torch.nn.DataParallel(net) cudnn.benchmark = True else: ltype = torch.LongTensor ftype = torch.FloatTensor dummy_input = torch.randn(1, 1, 28, 28) if args.resume: print('==> Resuming from checkpoint..')
def get_model(opts): if opts.model=="ssdlitemn2": model = Net(SSDLiteMN2(width_mult=opts.model_width)) return model, train_ssd, MultiboxLoss raise Exception("Model unknown:", opts.model)
batch_size=args.batch_size, shuffle=True) test_loader = DataLoader(test_dataset, batch_size=args.test_batch_size, shuffle=True) args.cuda = args.device != -1 and torch.cuda.is_available() print('Parameters:') for attr, value in sorted(args.__dict__.items()): print('\t{}={}'.format(attr.upper(), value)) logging.info(f"train data all steps: {len(train_loader)}, " + f"test data all steps : {len(test_loader)}") model = Net(args) # model = DataParallel(model) if args.cuda: model = model.cuda(args.device) # Prepare optimizer and schedule(linear warmup and decay) no_decay = ['bias', 'LayerNorm.weight'] optimizer_grouped_parameters = [{ 'params': [ p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay) ], 'weight_decay': 0.0 }, { 'params': [
def train(**kwargs): config.parse(kwargs) writer = SummaryWriter(config.logs_dir) train_loader = DataLoader( WholeFaceDatasets(config.whole_csv_train, config.whole_imgs_base_dir, train=True, transform=transforms.Compose([ transforms.Resize((224, 224)), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ])), batch_size=config.batch_size, shuffle=True, num_workers=config.num_workers ) test_loader = DataLoader( WholeFaceDatasets(config.whole_csv_test, config.whole_imgs_base_dir, train=False, transform=transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ])), batch_size=config.batch_size, shuffle=False, num_workers=config.num_workers ) weights_vector = Variable(torch.arange(0, 101)).view(-1, 1).cuda() model = Net() if config.using_pretrain_model: model.load_state_dict(torch.load(config.pretrain_model_path)) model.cuda() fc_params = list(map(id, model.fc.parameters())) base_params = filter(lambda p: id(p) not in fc_params, model.parameters()) optimizer = optim.SGD([{'params': base_params}, {'params': model.fc.parameters(), 'lr': config.fc_learning_rate}], lr=config.learning_rate, weight_decay=config.weight_decay, momentum=config.momentum) scheduler = StepLR(optimizer, step_size=config.decay_epoches, gamma=config.decay_gamma) for epoch in range(config.epoch): scheduler.step() model.train() # print learning rate # for param_group in optimizer.param_groups: # print(param_group['lr'], end='\t') # print('\n') for batch_idx, (data, target) in enumerate(train_loader): target = target.type(torch.FloatTensor) data, target = data.cuda(), target.cuda() data, target = Variable(data), Variable(target) optimizer.zero_grad() output = model(data) loss = F.l1_loss(output, target) loss.backward() optimizer.step() if batch_idx % config.log_interval == 0: # writer.add_scalar('train/loss', loss.data[0], # (epoch - 1) * len(train_loader.dataset) + batch_idx * config.batch_size) print( f'Train Epoch: {epoch} [{batch_idx*len(data)}/{len(train_loader.dataset)} ({100 * batch_idx/len(train_loader):.0f}%)]\tLoss: {loss.data[0]:.6f}') # if epoch % config.checkpoint_interval == 0: # torch.save(model.state_dict(), os.path.join(config.checkpoint_dir, f'checkpoint_whole3-{epoch}.pth')) val(epoch, writer, test_loader, model) writer.close()
def __build_model(self): dropout = self.hparams.get('dropout', 0.0) loss_func = self.hparams.get('loss_func', nn.CrossEntropyLoss()) hidden_dim = self.hparams.get('hidden_dim', 128) return Net(768, self.output_dim, loss_func, hidden_dim, dropout)
def main(): global device, cfg args = parse_args() cfg = Config.from_file(args.config) out = cfg.train.out if not os.path.exists(out): os.makedirs(out) # save config and command commands = sys.argv with open(f'{out}/command.txt', 'w') as f: f.write('## Command ################\n\n') f.write(f'python {commands[0]} ') for command in commands[1:]: f.write(command + ' ') f.write('\n\n\n') f.write('## Args ###################\n\n') for name in vars(args): f.write(f'{name} = {getattr(args, name)}\n') shutil.copy(args.config, f'./{out}') # Log logdir = os.path.join(out, 'log') if not os.path.exists(logdir): os.makedirs(logdir) writer = SummaryWriter(log_dir=logdir) # Set device cuda = torch.cuda.is_available() if cuda and args.gpu >= 0: print('# cuda available! #') device = torch.device(f'cuda:{args.gpu}') else: device = 'cpu' # Set models VGG = vgg.VGG VGG.load_state_dict(torch.load(args.vgg)) VGG = torch.nn.Sequential(*list(VGG.children())[:31]) model = Net(VGG) model.to(device) # Prepare dataset content_dataset = FaceDataset(cfg, cfg.train.content_dataset) content_loader = torch.utils.data.DataLoader( content_dataset, batch_size=cfg.train.batchsize, shuffle=True, num_workers=min(cfg.train.batchsize, 16), pin_memory=True, drop_last=True) style_dataset = FaceDataset(cfg, cfg.train.style_dataset) style_loader = torch.utils.data.DataLoader( style_dataset, batch_size=cfg.train.batchsize, sampler=InfiniteSamplerWrapper(style_dataset), num_workers=0, pin_memory=True, drop_last=True) style_iter = iter(style_loader) print(f'content dataset contains {len(content_dataset)} images.') print(f'style dataset contains {len(style_dataset)} images.') opt = Adam(model.decoder.parameters(), lr=cfg.train.parameters.lr, betas=(0.5, 0.999)) iteration = 0 batchsize = cfg.train.batchsize iterations_per_epoch = len(content_loader) epochs = cfg.train.iterations // iterations_per_epoch for epoch in range(epochs): for i, batch in enumerate(content_loader): model.train() content_images = Variable(batch).to(device) style_images = Variable(next(style_iter)).to(device) loss_c, loss_s = model(content_images, style_images) loss = cfg.train.parameters.lam_c * loss_c + cfg.train.parameters.lam_s * loss_s opt.zero_grad() loss.backward() opt.step() writer.add_scalar('loss_content', loss_c.item(), iteration + 1) writer.add_scalar('loss_style', loss_s.item(), iteration + 1) lr = poly_lr_scheduler(opt, cfg.train.parameters.lr, iteration, lr_decay_iter=10, max_iter=cfg.train.iterations) iteration += 1 if iteration % cfg.train.print_interval == 0: print( f'Epoch:[{epoch}][{iteration}/{cfg.train.iterations}] loss content:{loss_c.item():.5f} loss style:{loss_s.item():.5f}' ) if iteration % cfg.train.save_interval == 0: if not os.path.exists(os.path.join(out, 'checkpoint')): os.makedirs(os.path.join(out, 'checkpoint')) path = os.path.join(out, 'checkpoint', f'iter_{iteration:04d}.pth.tar') state = { 'state_dict': model.state_dict(), 'opt_state_dict': opt.state_dict(), 'iteration': iteration, } torch.save(state, path) if iteration % cfg.train.preview_interval == 0: if not os.path.exists(os.path.join(out, 'preview')): os.makedirs(os.path.join(out, 'preview')) sample = generate_sample(model, content_images, style_images) save_image( sample.data.cpu(), os.path.join(out, 'preview', f'iter_{iteration:04d}.png'))