def test_label_encoder(self): d, _ = load_datasets(1) for s in d.take(1): print(s[0].shape) print(s[1].shape) encoder = LabelEncoder() _, l = encoder.encode_batch( s[0], tf.constant([[0, 1, 0, 1]], shape=[1, 1, 4], dtype=tf.float32), tf.constant([[1]], shape=[1, 1])) print(l.shape) b_init = tf.constant_initializer(-np.log((1 - 0.01) / 0.01)) x = tf.random.normal((s[0].shape)) model = build_model(2) y = model(x) print(y.shape)
import os import time import tensorflow as tf from dataset import load_datasets from label_util import num_classes from alexnet import parameters, inference now = datetime.now().strftime("%Y%m%d%H%M%S") model_dir = 'model_%s' % (now) os.mkdir(model_dir) checkpoint_path = os.path.join(model_dir, 'model.ckpt') print('checkpoint path: %s' % (checkpoint_path)) data = load_datasets() learning_rate = 0.05 dropout = 0.8 training_iters = 20000 batch_size = 64 display_step = 10 x = tf.placeholder(tf.float32, [None, 227, 227, 3]) y = tf.placeholder(tf.float32, [None, num_classes]) keep_prob = tf.placeholder(tf.float32) weights, biases = parameters() pred = inference(x, weights, biases, keep_prob)
f"{DATA}/upsample.csv", index_col="image_id", usecols=["image_id", "fold", "target"], dtype={"fold": np.byte, "target": np.byte}, ) _ = df_folds.groupby("fold").target.hist(alpha=0.4) df_folds.groupby("fold").target.mean().to_frame("ratio").T # %% df_test = pd.read_csv(f"{DATA}/test.csv", index_col="image_name") # %% ds_train, ds_val, ds_test = load_datasets(fold_number) # %% len(ds_train), len(ds_val), len(ds_test) # %% [markdown] # # Model # %% from efficientnet_pytorch import EfficientNet from pytorch_lightning.metrics.classification import AUROC from sklearn.metrics import roc_auc_score
learning_rate = 8e-4 def adjust_learning_rate(optimizer, epoch): for param_group in optimizer.param_groups: if epoch < 5: param_group["lr"] = 8e-4 elif epoch < 10: param_group["lr"] = 5e-4 elif epoch < 15: param_group["lr"] = 5e-5 train_dataset, valid_dataset, test_dataset, train_noise_ind = load_datasets( args.dataset_name, args.dataset_path, args.train_ratio, args.noise_type, args.noise_ratio, args.noise_classes, args.seed, ) train_dataloader = DataLoader( train_dataset, batch_size=batch_size, shuffle=True, num_workers=8 ) test_dataloader = DataLoader( test_dataset, batch_size=batch_size, shuffle=False, num_workers=8 ) if valid_dataset is not None: valid_dataloader = DataLoader( valid_dataset, batch_size=batch_size, shuffle=False, num_workers=8 ) else:
def keras_inference(): _, dataset_val = load_datasets(args.data_path) model = tf.keras.models.load_model(args.saved_model) pred = model.predict(dataset_val.take(8 * 2))
help='path to the model dir') # basic params FLAGS = parser.parse_args() BATCH_SZ = FLAGS.batch_size LEARNING_RATE = FLAGS.learning_rate MAX_EPOCH = FLAGS.max_epoch MODEL_SAVE_PATH = FLAGS.model_save_path CHECKPOINT_PATH = FLAGS.checkpoint_path DATA_PATH = FLAGS.data_path MODEL_PATH = FLAGS.model_path MODEL_NAME = FLAGS.model_name dataset_train, dataset_val = load_datasets(DATA_PATH, batch_size=BATCH_SZ, buffer_size=1000, val_size=128) input_shape = [512, 512, 6] model = Nest_Net2(input_shape) print('Dataset spec') print(dataset_train.element_spec) precision = Precision() recall = Recall() #accuracy = BinaryAccuracy() #f1_score = F1Score(num_classes=2, threshold=0.5) #f1_score = F1_score kappa = CohenKappa(num_classes=2) auc = AUC(num_thresholds=20) iou = MeanIoU(num_classes=2) # use LR?
if __name__ == '__main__': args = parser.parse_args() # whether to use cuda args.use_cuda = args.use_cuda and torch.cuda.is_available() # set device (gpu/cpu) according to args.use_cuda device = torch.device("cuda" if args.use_cuda else "cpu") torch.manual_seed(args.seed) # load dataset: MNIST or Fashion_MNIST assert args.data_type in ( 'MNIST', 'Fashion_MNIST'), "Choose data_type between MNIST or Fashion_MNIST" dataset_train, dataset_test = load_datasets(data_type=args.data_type, batch_size=args.batch_size) # Model path(save or load) if (not os.path.exists(args.model_pth)) & (args.mode == 'train'): print("Directory to save the model does not exist. Make one? [y | n]") dir_yn = str(input()) if dir_yn == 'y': os.makedirs(args.model_pth) save_load_flag = True elif dir_yn == 'n': print("Please check directory. Not able to save model this time!") save_load_flag = False else: raise Exception("Input should be either y or n")
parser.add_argument('--model_pth', type=str, default='./model/trained_vae.pkl', help="Path for the model to be saved or loaded from") if __name__ == '__main__': args = parser.parse_args() # whether to use cuda args.use_cuda = args.use_cuda and torch.cuda.is_available() # set device (gpu/cpu) according to args.use_cuda device = torch.device("cuda" if args.use_cuda else "cpu") torch.manual_seed(args.seed) dataset_train, dataset_test = load_datasets(batch_size=args.batch_size) if args.use_cuda: model = VAE(latent_dim=args.latent_dims).cuda() else: model = VAE(latent_dim=args.latent_dims) if args.mode == 'train': train_losses = train(model=model, trainset=dataset_train, lr=1e-3, epochs=args.epochs, log_interval=args.log_interval, cuda=args.use_cuda, save_path=args.model_pth)
if args.algorithm == "Ours": draw_heatmap(args.algorithm, n_samples, log_dir, args.seed, ind_path, save_dir) ( train_dataset, valid_dataset, test_dataset, train_noise_ind, original_labels, ) = load_datasets( dataset_name, args.dataset_path, 1, noise_type, noise_ratio, [], args.seed, need_original=True, ) model = get_model(args.model_name, dataset_name).to("cuda:0") draw_sampels( model, train_dataset, original_labels, args.algorithm, log_dir, args.seed, ind_path, save_dir, )
def main(): # argument parsing parser = argparse.ArgumentParser() parser.add_argument('--max-epochs', type=int, default=2) parser.add_argument('--batch-size', type=int, default=4) parser.add_argument('--max-sequence-length', type=int, default=128) parser.add_argument('--seed', type=int, default=None) parser.add_argument('--data-dir', type=str, default='data') parser.add_argument('--real-dataset', type=str, default='webtext') parser.add_argument('--fake-dataset', type=str, default='xl-1542M-nucleus') parser.add_argument('--save-dir', type=str, default='bert_logs') parser.add_argument('--learning-rate', type=float, default=2e-5) parser.add_argument('--weight-decay', type=float, default=0) parser.add_argument('--model-name', type=str, default='bert-base-cased') parser.add_argument('--wandb', type=bool, default=True) args = parser.parse_args() if args.wandb: wandb.init(project=args.model_name) device = "cuda" if torch.cuda.is_available() else "cpu" # config, tokenizer, model config = AutoConfig.from_pretrained( args.model_name, num_labels=2 ) tokenizer = AutoTokenizer.from_pretrained(args.model_name) tokenization_utils.logger.setLevel('DEBUG') model = AutoModelForSequenceClassification.from_pretrained( args.model_name, config=config ) model.to(device) # load data train_loader, validation_loader, test_loader = load_datasets(args, tokenizer) # my model optimizer = AdamW(model.parameters(), lr=args.learning_rate, weight_decay=args.weight_decay) best_val = 0. for epoch in range(args.max_epochs): train(model, optimizer, train_loader, args, device) val_acc = validation(model, validation_loader, args, device) test_acc = test(model, test_loader, args, device) print(f"Epoch {epoch + 1} | val_acc: {val_acc} test_acc: {test_acc}") if val_acc > best_val: os.makedirs(args.save_dir, exist_ok=True) model_name = 'baseline_' + args.model_name + '.pt' model_to_save = model.module if hasattr(model, 'module') else model torch.save(dict( epoch=epoch+1, model_state_dict=model_to_save.state_dict(), optimizer_state_dict=optimizer.state_dict(), args=args ), os.path.join(args.save_dir, model_name) ) print("Model saved to", args.save_dir) best_val = val_acc
# Compile the model for training. learning_rate_schedule = tf.optimizers.schedules.PiecewiseConstantDecay( boundaries=[125, 250, 500, 240000, 360000], values=[2.5e-07, 0.000625, 0.00125, 0.0025, 0.00025, 2.5e-05]) optimizer = tf.optimizers.SGD(learning_rate=learning_rate_schedule) model.compile(loss=RetinaNetLoss(num_classes), optimizer=optimizer) # Setting up callbacks callbacks_list = [ tf.keras.callbacks.ModelCheckpoint(filepath=os.path.join( checkpoint_dir, "linglong"), monitor="val_loss", save_best_only=True, save_weights_only=True, verbose=1), tf.keras.callbacks.TensorBoard(log_dir=log_dir), LogImages(log_dir, log_image) ] # Load the WIDER dataset using TensorFlow Datasets dataset_train = load_datasets(record_train, args.batch_size, True) dataset_val = load_datasets(record_val, args.batch_size, False) # Training model.fit(dataset_train, validation_data=dataset_val.take(50), epochs=args.epochs, callbacks=callbacks_list, initial_epoch=args.initial_epoch, verbose=1)