def get_preprocessing(input_shape=(64, 64), mean=(0.485, 0.456, 0.406), std=[0.229, 0.224, 0.225], train=False): resize = Resize(input_shape) normalize = Normalize(mean, std) if train: return Compose([ resize, get_augmentations(), ToTensor(), normalize, ]) else: return Compose([resize, ToTensor(), normalize])
def main(): #读入数据,并存为列表,每一个item为列表中的一个元素 train = pd.read_csv(args.folds_csv) #设置的模型保存路径 MODEL_PATH = os.path.join(args.models_dir, args.network + args.alias) #将数据分折,原始输入数据就是预先分好折的,这个是存储的分折信息,12345列表元素 folds = [int(f) for f in args.fold.split(',')] print('Training Model:', args.network + args.alias) for fold in folds: K.clear_session() print('***************************** FOLD {} *****************************'.format(fold)) if fold == 0: if os.path.isdir(MODEL_PATH): raise ValueError('Such Model already exists') os.system("mkdir {}".format(MODEL_PATH)) # Train/Validation sampling df_train = train[train.fold != fold].copy().reset_index(drop=True) df_valid = train[train.fold == fold].copy().reset_index(drop=True) # Train on pseudolabels only if args.pseudolabels_dir != '': pseudolabels = pd.read_csv(args.pseudolabels_csv) df_train = pseudolabels.sample(frac=1, random_state=13).reset_index(drop=True) # Keep only non-black images ids_train, ids_valid = df_train[df_train.unique_pixels > 1].id.values, df_valid[ df_valid.unique_pixels > 1].id.values print('Training on {} samples'.format(ids_train.shape[0])) print('Validating on {} samples'.format(ids_valid.shape[0])) # Initialize model weights_path = os.path.join(MODEL_PATH, 'fold_{fold}.hdf5'.format(fold=fold)) # Get the model model, preprocess = get_model(args.network, input_shape=(args.input_size, args.input_size, 3), freeze_encoder=args.freeze_encoder) # LB metric threshold def lb_metric(y_true, y_pred): return Kaggle_IoU_Precision(y_true, y_pred, threshold=0 if args.loss_function == 'lovasz' else 0.5) model.compile(optimizer=RMSprop(lr=args.learning_rate), loss=make_loss(args.loss_function), metrics=[lb_metric]) if args.pretrain_weights is None: print('No weights passed, training from scratch') else: wp = args.pretrain_weights.format(fold) print('Loading weights from {}'.format(wp)) model.load_weights(wp, by_name=True) # Get augmentations augs = get_augmentations(args.augmentation_name, p=args.augmentation_prob) # Data generator dg = SegmentationDataGenerator(input_shape=(args.input_size, args.input_size), batch_size=args.batch_size, augs=augs, preprocess=preprocess) train_generator = dg.train_batch_generator(ids_train) validation_generator = dg.evaluation_batch_generator(ids_valid) # Get callbacks callbacks = get_callback(args.callback, weights_path=weights_path, fold=fold) # Fit the model with Generators: model.fit_generator(generator=ThreadsafeIter(train_generator), steps_per_epoch=ids_train.shape[0] // args.batch_size * 2, epochs=args.epochs, callbacks=callbacks, validation_data=ThreadsafeIter(validation_generator), validation_steps=np.ceil(ids_valid.shape[0] / args.batch_size), workers=args.num_workers) gc.collect()
def setup(self): # called on every process in DDP self.train_transform, self.test_transform = get_augmentations( p=self.aug_p, image_size=self.img_sz) self.train_df = pd.read_pickle(self.path / "train_df.pkl") self.valid_df = pd.read_pickle(self.path / "valid_df.pkl")
def main(): random.seed(1) torch.manual_seed(1) torch.cuda.manual_seed(1) np.random.seed(1) torch.backends.cudnn.deterministic = True # torch.backends.cudnn.benchmark = False torch.cuda.empty_cache() args = argparser() now = datetime.datetime.now() args.out = osp.join(here, 'logs', args.model + '_' + now.strftime('%Y%m%d_%H%M%S')) if not osp.exists(args.out): os.makedirs(args.out) with open(osp.join(args.out, 'config.yaml'), 'w') as f: yaml.safe_dump(args.__dict__, f, default_flow_style=False) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print(f'Start training {args.model} using {device.type}\n') # 1. dataset root = args.dataset_root loader = get_loader(args.dataset) augmentations = get_augmentations(args) train_loader = DataLoader( loader(root, split='train', base_size=args.base_size, augmentations=augmentations), batch_size=args.batch_size, shuffle=True, num_workers=args.workers) val_loader = DataLoader( loader(root, split='val', base_size=args.base_size), batch_size=1, shuffle=False, num_workers=args.workers) args.n_classes = loader.NUM_CLASS # 2. model model = model_loader(args.model, args.n_classes, backbone=args.backbone, norm_layer=nn.BatchNorm2d, multi_grid=args.multi_grid, multi_dilation=args.multi_dilation) model = model.to(device) print(model) start_epoch = 1 if args.resume: checkpoint = torch.load(args.resume) model.load_state_dict(checkpoint['model_state_dict']) start_epoch = checkpoint['epoch'] else: checkpoint = None # 3. optimizer optim = torch.optim.SGD( model.parameters(), lr=args.lr, momentum=args.momentum, weight_decay=args.weight_decay ) # optim = torch.optim.SGD( # [{'params': model.get_parameters(key='1x'), 'lr': args.lr}, # {'params': model.get_parameters(key='10x'), 'lr': args.lr * 10}], # momentum=args.momentum, # weight_decay=args.weight_decay # ) if args.resume: optim.load_state_dict(checkpoint['optim_state_dict']) scheduler = get_scheduler(optim, args) # 4. train trainer = Trainer( device=device, model=model, optimizer=optim, scheduler=scheduler, train_loader=train_loader, val_loader=val_loader, out=args.out, epochs=args.epochs, n_classes=args.n_classes, val_epoch=args.val_epoch, ) trainer.epoch = start_epoch trainer.train()
def main(args): # Setup Logger log_file_path = os.path.join(os.getcwd(), 'logs') if not os.path.exists(log_file_path): os.makedirs(log_file_path) start_time = time.ctime() logging.basicConfig(filename=os.path.join( log_file_path, "training_log_" + str(start_time).replace(':', '').replace(' ', ' ').replace(' ', '_') + ".log"), format='%(asctime)s - %(message)s', level=logging.INFO) # Create model directory if not os.path.exists(args.model_path): os.makedirs(args.model_path) data_augmentations = get_augmentations(args.crop_size) # Image preprocessing, normalization for the pretrained resnet transform = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((args.crop_size, args.crop_size)), transforms.ToTensor(), transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)) ]) train_coco = COCO(args.train_caption_path) val_coco = COCO(args.val_caption_path) # Load vocabulary wrapper with open(args.vocab_path, 'rb') as f: vocab = pickle.load(f) # Build data loader train_dataloader = get_loader(args.train_dir, train_coco, vocab, transform, data_augmentations, args.batch_size, shuffle=True, num_workers=args.num_workers) validation_dataloader = get_loader(args.val_dir, val_coco, vocab, transform, None, args.batch_size, shuffle=False, num_workers=args.num_workers) # Load word embeddings fasttext_wv = load_fasttext(args.train_caption_path) print("Loaded FastText word embeddings") embed_dim = fasttext_wv.vectors_vocab.shape[1] embedding_weights = np.zeros((len(vocab), embed_dim)) for idx, word in enumerate(vocab.word2idx): embedding_weights[idx] = fasttext_wv[word] # Build the models encoder = EncoderCNN().to(device) decoder = DecoderRNN(args.lstm1_size, args.lstm2_size, args.att_size, vocab, args.embed_size, embedding_weights, feature_size=args.feature_size).to(device) # Metrics train_metrics = [Accuracy(), RougeBleuScore(train_coco, vocab)] val_metrics = [Accuracy(), RougeBleuScore(val_coco, vocab)] # Loss and optimizer loss = nn.CrossEntropyLoss(ignore_index=0) for p in encoder.parameters(): p.requires_grad = False optimizer = torch.optim.Adam([{ 'params': encoder.parameters(), 'lr': 0.5 * args.learning_rate }, { 'params': decoder.parameters() }], lr=args.learning_rate) # optimizer = torch.optim.Adam(encoder.parameters(), lr=args.learning_rate) scheduler = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(optimizer, 10, T_mult=1, eta_min=0) encoder_unfreeze_epoch = 2 train_model( train_dataloader=train_dataloader, validation_dataloader=validation_dataloader, model=[encoder, decoder], loss=loss, train_metrics=train_metrics, val_metrics=val_metrics, optimizer=optimizer, scheduler=scheduler, batch_size=args.batch_size, num_epochs=args.num_epochs, encoder_unfreeze_epoch=encoder_unfreeze_epoch, device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu"), logger=logging, verbose=True, model_save_path=os.path.join(os.getcwd(), 'model'), plots_save_path=os.path.join(os.getcwd(), 'plots'))
def main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter, ) parser.add_argument('--model', type=str, default='deeplab-largefov', help='model to train for') parser.add_argument('--epochs', type=int, default=50, help='total epochs') parser.add_argument('--val_epoch', type=int, default=10, help='validation interval') parser.add_argument('--batch_size', type=int, default=16, help='number of batch size') parser.add_argument('--img_size', type=tuple, default=None, help='resize images to proper size') parser.add_argument('--dataset_type', type=str, default='voc', help='choose which dataset to use') parser.add_argument('--dataset_root', type=str, default='/home/ecust/Datasets/PASCAL VOC/VOC_Aug', help='path to dataset') parser.add_argument('--n_classes', type=int, default=21, help='number of classes') parser.add_argument('--resume', default=None, help='path to checkpoint') parser.add_argument('--optim', type=str, default='sgd', help='optimizer') parser.add_argument('--lr', type=float, default=0.001, help='learning rate') parser.add_argument('--lr_policy', type=str, default='poly', help='learning rate policy') parser.add_argument('--weight-decay', type=float, default=0.0005, help='weight decay') parser.add_argument('--beta1', type=float, default=0.9, help='momentum for sgd, beta1 for adam') parser.add_argument('--lr_decay_step', type=float, default=10, help='step size for step learning policy') parser.add_argument('--lr_power', type=int, default=0.9, help='power parameter for poly learning policy') parser.add_argument('--pretrained', type=bool, default=True, help='whether to use pretrained models') parser.add_argument('--iter_size', type=int, default=10, help='iters to accumulate gradients') parser.add_argument('--crop_size', type=tuple, default=(321, 321), help='crop sizes of images') parser.add_argument('--flip', type=bool, default=True, help='whether to use horizontal flip') args = parser.parse_args() now = datetime.datetime.now() args.out = osp.join(here, 'logs', args.model + '_' + now.strftime('%Y%m%d_%H%M%S')) if not osp.exists(args.out): os.makedirs(args.out) with open(osp.join(args.out, 'config.yaml'), 'w') as f: yaml.safe_dump(args.__dict__, f, default_flow_style=False) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print(f'Start training {args.model} using {device.type}\n') random.seed(1337) torch.manual_seed(1337) torch.cuda.manual_seed(1337) # 1. dataset root = args.dataset_root loader = get_loader(args.dataset_type) augmentations = get_augmentations(args) train_loader = DataLoader(loader(root, n_classes=args.n_classes, split='train_aug', img_size=args.img_size, augmentations=augmentations, pretrained=args.pretrained), batch_size=args.batch_size, shuffle=True, num_workers=4) val_loader = DataLoader(loader(root, n_classes=args.n_classes, split='val_id', img_size=args.img_size, pretrained=args.pretrained), batch_size=1, shuffle=False, num_workers=4) # 2. model model, start_epoch, ckpt = model_loader(args.model, args.n_classes, args.resume) model = model.to(device) # 3. optimizer optim = get_optimizer(args, model) if args.resume: optim.load_state_dict(ckpt['optim_state_dict']) scheduler = get_scheduler(optim, args) # 4. train trainer = Trainer(device=device, model=model, optimizer=optim, scheduler=scheduler, train_loader=train_loader, val_loader=val_loader, out=args.out, epochs=args.epochs, n_classes=args.n_classes, val_epoch=args.val_epoch, iter_size=args.iter_size) trainer.epoch = start_epoch trainer.train()
from albumentations import (HorizontalFlip, ShiftScaleRotate, RandomContrast, RandomBrightness, Compose) from augmentations import get_augmentations import pandas as pd import numpy as np from params import args # Read the folds train = pd.read_csv(args.folds_csv) df_train = train[train.fold != fold].copy().reset_index(drop=True) df_valid = train[train.fold == fold].copy().reset_index(drop=True) ids_train, ids_valid = df_train[ df_train.unique_pixels > 1].id.values, df_valid[ df_valid.unique_pixels > 1].id.values augs = get_augmentations('valid', 1.0) dg = SegmentationDataGenerator(input_shape=(args.input_size, args.input_size), batch_size=args.batch_size, augs=augs, preprocess=None) train_generator = dg.train_batch_generator(ids_train) valid_generator = dg.evaluation_batch_generator(ids_valid) model = Unet() model_checkpoint = ModelCheckpoint('unet_membrane.hdf5', monitor='loss', verbose=1, save_best_only=True) model.fit_generator(generator=train_generator,
PATH = '../data/' LOG_DIR = '../logs/' WEIGHT_DIR = '../trained_models/' device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") print(device) df = pd.read_csv(f'{PATH}train.csv') image_data = np.load(f'{PATH}train.npy') val_mask = np.load(f'{PATH}val_mask_{fold}.npy') nunique = list(df.nunique())[1:-1] print(nunique, df.shape) print(list(df[~val_mask].nunique())[1:-1]) trn_aug, val_aug = get_augmentations(IMG_SZ) train_dataset = BengaliDataset(df[~val_mask], image_data[~val_mask], trn_aug) valid_dataset = BengaliDataset(df[val_mask], image_data[val_mask], val_aug) del df, image_data gc.collect() train_dataloader = DataLoader(train_dataset, batch_size=bs, shuffle=True) valid_dataloader = DataLoader(valid_dataset, batch_size=bs, shuffle=True) model = MODEL_DISPATCHER[model_name].to(device) criterion = Combined_loss() optimizer = torch.optim.Adam(model.parameters(), lr=3e-3) scheduler = torch.optim.lr_scheduler.OneCycleLR( optimizer,