def train(labellist, batch_size, train_number_epochs, learning_rate, round, device): train_dataset = SiameseDataset('images.txt', labellist, transforms.ToTensor()) train_dataloader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True) # training net = SiameseNetwork().to(device) criterion = ContrastiveLoss() optimizer = optim.Adam(net.parameters(), lr=learning_rate) counter = [] loss_history = [] iteration_number = 0 for epoch in range(train_number_epochs): total_loss = 0 start_time = datetime.now() for i, data in enumerate(train_dataloader): img0, img1, label = data img0, img1, label = img0.to(device), img1.to(device), label.to( device) optimizer.zero_grad() output1, output2 = net(img0, img1) loss_contrastive = criterion(output1, output2, label) loss_contrastive.backward() total_loss += loss_contrastive.item() optimizer.step() if i % 20 == 0: iteration_number += 20 counter.append(iteration_number) loss_history.append(loss_contrastive.item()) end_time = datetime.now() print("Epoch number: {} , Current loss: {:.4f}, Epoch Time: {}".format( epoch + 1, total_loss / (i + 1), end_time - start_time)) torch.save(net.state_dict(), "parameters/Parm_Round_" + str(round + 1) + ".pth") return counter, loss_history
train, val = random_split(dataset, [n_train, n_val]) train_loader = DataLoader(train, batch_size=args.batch_size) val_loader = DataLoader(val, batch_size=args.batch_size // 4) # load backbones print("[*] Initializing weights...") imagenet_net = ResNet34() sketches_net = ResNet34() # sketches_net.load_state_dict(torch.load(args.sketches_backbone_weights)) print("[+] Weights loaded") print("[*] Initializing model, loss and optimizer") contrastive_net = SiameseNetwork(sketches_net, imagenet_net) contrastive_net.to(args.device) if args.optimizer == 'sgd': optimizer = torch.optim.SGD(contrastive_net.parameters(), lr=args.lr, momentum=args.momentum) else: optimizer = torch.optim.Adam(contrastive_net.parameters(), lr=args.lr) scheduler = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts( optimizer, T_0=args.t_0) contrastive_loss = contrastive_loss() cross_entropy_loss = torch.nn.CrossEntropyLoss() print("[+] Model, loss and optimizer were initialized successfully") if not args.debug: wandb.init(project='homework1-cc7221', entity='p137') config = wandb.config config.model = contrastive_net.__class__.__name__ + '_contrastive' config.device = device
siamese_network = SiameseNetwork(config) autoencoder_file = '/autoencoder_epoch175_loss1.1991.pth' siamese_file = '/siamese_network_epoch175_loss1.1991.pth' if config.load_model: autoencoder.load_state_dict(torch.load(config.saved_models_folder + autoencoder_file)) siamese_network.load_state_dict(torch.load(config.saved_models_folder + siamese_file)) autoencoder.to(device) autoencoder.train() siamese_network.to(device) siamese_network.train() params = list(autoencoder.parameters()) + list(siamese_network.parameters()) optimizer = torch.optim.Adam(params, lr=config.lr, betas=(0.9, 0.999)) transform = transforms.Compose([ transforms.Grayscale(num_output_channels=1), # transforms.RandomCrop(size=128), # transforms.RandomRotation(degrees=10), transforms.ToTensor(), ]) train_data = torchvision.datasets.ImageFolder(config.data_folder, transform=transform) train_data_loader = DataLoader(train_data, batch_size=config.batch_size, shuffle=True, drop_last=True) transform2 = transforms.Compose([ transforms.RandomCrop(size=128), transforms.RandomRotation(degrees=10),
train=False) if Config.network == 'siamese' else TriMadoriDataset( train=False) # data loaders train_dataloader = DataLoader(train_dataset, shuffle=True, batch_size=Config.batch_size) val_dataloader = DataLoader(val_dataset, shuffle=False, batch_size=Config.batch_size) # models net = SiameseNetwork() if Config.network == 'siamese' else TripletNetwork() net = net.to(device) criterion = ContrastiveLoss() if Config.network == 'siamese' else TripletLoss() optimizer = optim.Adam(net.parameters(), lr=0.0005) def train_siamese(): train_loss_history, val_loss_history = [], [] lowest_epoch_train_loss = lowest_epoch_val_loss = float('inf') for epoch in tqdm(range(Config.train_number_epochs)): # training net.train() epoch_train_loss = 0 for batch_no, data in enumerate(train_dataloader): img0, img1, label = data img0, img1, label = img0.to(device), img1.to(device), label.to( device) optimizer.zero_grad()
val_loader = DataLoader(val, batch_size=args.batch_size) print("[+] Dataset initialized successfully") # load backbones print("[*] Initializing weights...") imagenet_net = ResNet34() sketches_net = ResNet34() # sketches_net.load_state_dict(torch.load(args.sketches_backbone_weights)) print("[+] Weights loaded") print("[*] Adapting output layers...") print("[*] Initializing model, loss and optimizer") siamese_net = SiameseNetwork(sketches_net, imagenet_net) siamese_net.to(args.device) optimizer = torch.optim.Adam(siamese_net.parameters(), lr=args.lr) triplet_loss = triplet_loss() cross_entropy_loss = torch.nn.CrossEntropyLoss() print("[+] Model, loss and optimizer were initialized successfully") if not args.debug: wandb.init(project='homework1-cc7221', entity='p137') config = wandb.config config.model = siamese_net.__class__.__name__ + "_triplet" config.device = device config.batch_size = args.batch_size config.epochs = args.epochs config.learning_rate = args.lr print("[*] Training") best_avg_acc = 0
else: model = SiameseNetwork(args.embedding_size).to(device) summary(model, input_size=[(1025, 126), (1025, 126)]) start = 1 # +) loss if args.loss == 'nll': weight = torch.FloatTensor([1.0, 9.0]).to( device) # weight for loss (spoof:1, genuine:9) criterion = nn.NLLLoss(weight=weight) elif args.loss == 'cs': criterion = ContrastiveLoss() elif args.loss == 'tri': criterion = TripletLoss() # +) optimizer if args.optim == 'adam': optim = torch.optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.wd) # +) scheduler scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau( optim, 'min', factor=args.sched_factor, patience=args.sched_patience, min_lr=args.sched_min_lr, verbose=False) # +) early stopping #early_stopping = EarlyStopping(patience=args.es_patience, verbose=False) # +) fine-tuning mode if args.fit_mode: # +) tensorboardX, log