def train(self): """ This method is executed once per epoch and takes care of model weight update cycle """ print(f"Training epoch {self.epoch}...") self.model.train() # Loop over our minibatches for i, batch in enumerate(self.train_loader): self.optimizer.zero_grad() # TASK: You have your data in batch variable. Put the slices as 4D Torch Tensors of # shape [BATCH_SIZE, 1, PATCH_SIZE, PATCH_SIZE] into variables data and target. # Feed data to the model and feed target to the loss function # data = batch['image'].float().to(self.device) target = batch['seg'].to(self.device) prediction = self.model(data) # We are also getting softmax'd version of prediction to output a probability map # so that we can see how the model converges to the solution prediction_softmax = F.softmax(prediction, dim=1) loss = self.loss_function(prediction, target[:, 0, :, :]) # TASK: What does each dimension of variable prediction represent? # ANSWER: [BATCH_SIZE, PREDICTED_CLASSES, CORONAL, AXIAL] loss.backward() self.optimizer.step() if (i % 10) == 0: # Output to console on every 10th batch print(f"\nEpoch: {self.epoch} Train loss: {loss}, {100*(i+1)/len(self.train_loader):.1f}% complete") counter = 100*self.epoch + 100*(i/len(self.train_loader)) # You don't need to do anything with this function, but you are welcome to # check it out if you want to see how images are logged to Tensorboard # or if you want to output additional debug data log_to_tensorboard( self.tensorboard_train_writer, loss, data, target, prediction_softmax, prediction, counter) print(".", end='') print("\nTraining complete")
def train(self): """ This method is executed once per epoch and takes care of model weight update cycle """ print(f"Training epoch {self.epoch}...") self.model.train() # Loop over our minibatches for i, batch in enumerate(self.train_loader): self.optimizer.zero_grad() # We have our data in batch variable. Put the slices as 4D Torch Tensors of # shape [BATCH_SIZE, 1, PATCH_SIZE, PATCH_SIZE] into variables data and target. # Feed data to the model and feed target to the loss function # data = batch['image'].to(self.device, dtype=torch.float) target = batch['seg'].to(self.device) prediction = self.model(data) # We are also getting softmax'd version of prediction to output a probability map # so that we can see how the model converges to the solution prediction_softmax = F.softmax(prediction, dim=1) loss = self.loss_function(prediction, target[:, 0, :, :]) # What does each dimension of variable prediction represent? # Each dimension is the probability for each pixel of a imput 2D slice for each class loss.backward() self.optimizer.step() if (i % 10) == 0: # Output to console on every 10th batch print(f"\nEpoch: {self.epoch} Train loss: {loss}, {100*(i+1)/len(self.train_loader):.1f}% complete") counter = 100*self.epoch + 100*(i/len(self.train_loader)) log_to_tensorboard( self.tensorboard_train_writer, loss, data, target, prediction_softmax, prediction, counter) print(".", end='') print("\nTraining complete")
def train(self): """ This method is executed once per epoch and takes care of model weight update cycle """ print(f"Training epoch {self.epoch}...") self.model.train() # Loop over our minibatches for i, batch in enumerate(self.train_loader): self.optimizer.zero_grad() data = batch["image"] target = batch["seg"] prediction = self.model(data) # We are also getting softmax'd version of prediction to output a probability map # so that we can see how the model converges to the solution prediction_softmax = F.softmax(prediction, dim=1) loss = self.loss_function(prediction, target[:, 0, :, :].long().to(self.device)) loss.backward() self.optimizer.step() if (i % 10) == 0: # Output to console on every 10th batch print(f"\nEpoch: {self.epoch} Train loss: {loss}, {100*(i+1)/len(self.train_loader):.1f}% complete") counter = 100*self.epoch + 100*(i/len(self.train_loader)) # You don't need to do anything with this function, but you are welcome to # check it out if you want to see how images are logged to Tensorboard # or if you want to output additional debug data log_to_tensorboard( self.tensorboard_train_writer, loss, data, target, prediction_softmax, prediction, counter) print(".", end='') print("\nTraining complete")
def train(self): """ This method is executed once per epoch and takes care of model weight update cycle """ print(f"Training epoch {self.epoch}...") self.model.train() # Loop over our minibatches for i, batch in enumerate(self.train_loader): self.optimizer.zero_grad() # Put the slices as 4D Torch Tensors of # shape [BATCH_SIZE, 1, PATCH_SIZE, PATCH_SIZE] into variables data and target. # Feed data to the model and feed target to the loss function data = batch['image'].float().to(self.device) target = batch['seg'].long().to(self.device) prediction = self.model(data) # We are also getting softmax'd version of prediction to output a probability map # so that we can see how the model converges to the solution prediction_softmax = F.softmax(prediction, dim=1) loss = self.loss_function(prediction, target[:, 0, :, :]) # prediction shape is [8, 3, 64, 64] # dim 0 = 8: batch size # dim 1 = 3: prediction label [0,1, or 2] # dim 2 = 64: patch width # dim 3 = 64: patch height loss.backward() self.optimizer.step() if (i % 10) == 0: # Output to console on every 10th batch print( f"\nEpoch: {self.epoch} Train loss: {loss}, {100*(i+1)/len(self.train_loader):.1f}% complete" ) counter = 100 * self.epoch + 100 * (i / len(self.train_loader)) log_to_tensorboard(self.tensorboard_train_writer, loss, data, target, prediction_softmax, prediction, counter) print(".", end='') print("\nTraining complete")
def validate(self): """ This method runs validation cycle, using same metrics as Train method. Note that model needs to be switched to eval mode and no_grad needs to be called so that gradients do not propagate """ print(f"Validating epoch {self.epoch}...") # Turn off gradient accumulation by switching model to "eval" mode self.model.eval() loss_list = [] with torch.no_grad(): for i, batch in enumerate(self.val_loader): # TASK: Write validation code that will compute loss on a validation sample data = batch['image'].to(self.device, dtype=torch.float) target = batch['seg'].to(self.device) prediction = self.model(data) # We are also getting softmax'd version of prediction to output a probability map # so that we can see how the model converges to the solution prediction_softmax = F.softmax(prediction, dim=1) loss = self.loss_function(prediction, target[:, 0, :, :]) print(f"Batch {i}. Data shape {data.shape} Loss {loss}") # We report loss that is accumulated across all of validation set loss_list.append(loss.item()) self.scheduler.step(np.mean(loss_list)) log_to_tensorboard( self.tensorboard_val_writer, np.mean(loss_list), data, target, prediction_softmax, prediction, (self.epoch+1) * 100) print(f"Validation complete")
def validate(self): """ This method runs validation cycle, using same metrics as Train method. Note that model needs to be switched to eval mode and no_grad needs to be called so that gradients do not propagate """ print(f"Validating epoch {self.epoch}...") # Turn off gradient accumulation by switching model to "eval" mode self.model.eval() loss_list = [] with torch.no_grad(): for i, batch in enumerate(self.val_loader): # TASK: Write validation code that will compute loss on a validation sample # <YOUR CODE HERE> data, target = batch["image"], batch["seg"] data = torch.reshape(data, (-1, 1,data.shape[2], data.shape[3])).type(torch.float).to(self.device) target = torch.reshape(target, (-1, 1, target.shape[2], target.shape[3])).to(self.device) prediction = self.model(data) prediction_softmax = F.softmax(prediction, dim=1) loss = self.loss_function(prediction, target[:, 0, :, :]) print(f"Batch {i}. Data shape {data.shape} Loss {loss}") # We report loss that is accumulated across all of validation set loss_list.append(loss.item()) self.scheduler.step(np.mean(loss_list)) log_to_tensorboard( self.tensorboard_val_writer, np.mean(loss_list), data, target, prediction_softmax, prediction, (self.epoch+1) * 100) print(f"Validation complete")
def validate(self): """ This method runs validation cycle, using same metrics as Train method. Note that model needs to be switched to eval mode and no_grad needs to be called so that gradients do not propagate """ print(f"Validating epoch {self.epoch}...") # Turn off gradient accumulation by switching model to "eval" mode self.model.eval() loss_list = [] with torch.no_grad(): for i, batch in enumerate(self.val_loader): data = batch['image'] target = batch['seg'] prediction = self.model(data) prediction_softmax = F.softmax(prediction, dim=1) loss = self.loss_function(prediction, target[:, 0, :, :].long().to(self.device)) print(f"Batch {i}. Data shape {data.shape} Loss {loss}") # We report loss that is accumulated across all of validation set loss_list.append(loss.item()) self.scheduler.step(np.mean(loss_list)) log_to_tensorboard( self.tensorboard_val_writer, np.mean(loss_list), data, target, prediction_softmax, prediction, (self.epoch+1) * 100) print(f"Validation complete")
def train(self): """ This method is executed once per epoch and takes care of model weight update cycle """ print(f"Training epoch {self.epoch}...") self.model.train() # Loop over our minibatches for i, batch in enumerate(self.train_loader): self.optimizer.zero_grad() data = batch['image'].to(self.device, dtype=torch.float) target = batch['seg'].to(self.device) prediction = self.model(data) prediction_softmax = F.softmax(prediction, dim=1) loss = self.loss_function(prediction, target[:, 0, :, :]) loss.backward() self.optimizer.step() if (i % 10) == 0: print( f"\nEpoch: {self.epoch} Train loss: {loss}, {100*(i+1)/len(self.train_loader):.1f}% complete" ) counter = 100 * self.epoch + 100 * (i / len(self.train_loader)) log_to_tensorboard(self.tensorboard_train_writer, loss, data, target, prediction_softmax, prediction, counter) print(".", end='') print("\nTraining complete")
def train(self): """ This method is executed once per epoch and takes care of model weight update cycle """ print(f"Training epoch {self.epoch}...") self.model.train() # Loop over our minibatches for i, batch in enumerate(self.train_loader): self.optimizer.zero_grad() # DONE: You have your data in batch variable. Put the slices as 4D Torch Tensors of # shape [BATCH_SIZE, 1, PATCH_SIZE, PATCH_SIZE] into variables data and target. # Feed data to the model and feed target to the loss function # data = batch['image'] target = batch['seg'] data = data.to(device=self.device, dtype=torch.float) target = target.to(device=self.device, dtype=torch.long) prediction = self.model(data) # We are also getting softmax'd version of prediction to output a probability map # so that we can see how the model converges to the solution prediction_softmax = F.softmax(prediction, dim=1) loss = self.loss_function(prediction, target[:, 0, :, :]) # DONE: What does each dimension of variable prediction represent? # ANSWER: # Based on the prediction.shape obtained with VS Code Debug, a sample value is torch.Size([32, 3, 64, 64]) # Here, the first parameter (32 in sample) is the Batch Size defined in Config # Second parameter (3 in sample) shows the number of classes = anterior, posterior and bg # Third and Fourth parameters defines the image size used = 64x64 loss.backward() self.optimizer.step() if (i % 10) == 0: # Output to console on every 10th batch print(f"\nEpoch: {self.epoch} Train loss: {loss}, {100*(i+1)/len(self.train_loader):.1f}% complete") counter = 100*self.epoch + 100*(i/len(self.train_loader)) # You don't need to do anything with this function, but you are welcome to # check it out if you want to see how images are logged to Tensorboard # or if you want to output additional debug data log_to_tensorboard( self.tensorboard_train_writer, loss, data, target, prediction_softmax, prediction, counter) print(".", end='') print("\nTraining complete")
def train(self): """ This method is executed once per epoch and takes care of model weight update cycle """ print(f"Training epoch {self.epoch}...") self.model.train() # Loop over our minibatches for i, batch in enumerate(self.train_loader): self.optimizer.zero_grad() # TASK: You have your data in batch variable. Put the slices as 4D Torch Tensors of # shape [BATCH_SIZE, 1, PATCH_SIZE, PATCH_SIZE] into variables data and target. # Feed data to the model and feed target to the loss function # data = <YOUR CODE HERE> # data = data.to('cuda') data = batch['images'] # import matplotlib # import numpy as np # import matplotlib.pyplot as plt # %matplotlib inline # data_np = data.cpu().detach().numpy() # plt.imshow(data_np[2][0]); plt.show() # target = <YOUR CODE HERE> target = batch['segs'] # prediction prediction = self.model(data) # We are also getting softmax'd version of prediction to output a probability map # so that we can see how the model converges to the solution prediction_softmax = F.softmax(prediction, dim=1) loss = self.loss_function(prediction, target[:, 0, :, :]) # TASK: What does each dimension of variable prediction represent? # ANSWER: prediction = [number_of_images, classes, width_of_image, height_of_image] loss.backward() self.optimizer.step() if (i % 10) == 0: # Output to console on every 10th batch print( f"\nEpoch: {self.epoch} Train loss: {loss}, {100*(i+1)/len(self.train_loader):.1f}% complete" ) counter = 100 * self.epoch + 100 * (i / len(self.train_loader)) # You don't need to do anything with this function, but you are welcome to # check it out if you want to see how images are logged to Tensorboard # or if you want to output additional debug data log_to_tensorboard(self.tensorboard_train_writer, loss, data, target, prediction_softmax, prediction, counter) print(".", end='') print("\nTraining complete")