def train(): # Load the data sets train_dataset = NucleusDataset( "data", train=True, transform=Compose([Rescale(256), ToTensor()]), target_transform=Compose([Rescale(256), ToTensor()])) # Use cuda if available device = "cuda" if torch.cuda.is_available() else "cpu" # Set model to GPU/CPU if args.from_checkpoint: model = UNet.load(args.from_checkpoint) else: model = UNet() model.to(device) # Initialize optimizer optimizer = optim.Adam(model.parameters(), lr=args.learning_rate) # Initialize trainer trainer = Trainer(dataset=train_dataset, model=model, optimizer=optimizer, batch_size=args.batch_size, device=args.device, output_dir=output_dir) # Run the training trainer.run_train_loop(epochs=args.epochs)
def __init__(self, model: UNet): self.model = model # Pre-processing for input images self.transforms = transforms.Compose([Rescale(256), ToTensor()]) # Post-processing for output of the model self.output_transforms = transforms.Compose([ToPILImage()])
def read_image(dir): transform = transforms.Compose( [Rescale(224), RandomCrop(223), Normalize(), ToTensor()]) image = cv2.imread(dir) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) sample = {'image': image, 'keypoints': None} transformed_image = transform(sample) images = transformed_image['image'] print(images.shape) images = images.type(torch.FloatTensor) images = images.unsqueeze(0) out = model(images) out = out.view(-1, 2) out = out.data * 50.0 + 100 images = np.transpose(images, (0, 2, 3, 1)).numpy().squeeze(3) display(images.squeeze(0), out)
print(m) def save_models(models, path): for i, model in enumerate(models): target_dir = f'{path}/model_{i}.pt' torch.save(model.state_dict(), target_dir) WORKING_DIRECTORY = 'Documents/studia/mgr/master-thesis' if WORKING_DIRECTORY not in os.getcwd(): os.chdir(WORKING_DIRECTORY) dataset = FMASpectrogramsDataset(csv_file='track_mapping.txt', lookup_table='genres.txt', root_dir='spectrograms', transform=transforms.Compose([ToRGB(), Rescale((128, 128)), MelspectrogramNormalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)), ToTensor()])) torch.manual_seed(100) train_size = int(0.8 * len(dataset)) validation_size = int(0.1 * len(dataset)) test_size = len(dataset) - validation_size - train_size train_dataset, test_dataset, validation_dataset = random_split( dataset, [train_size, validation_size, test_size]) torch.cuda.empty_cache() # hyper num_epochs = 30 num_classes = 8 batch_size = 16 learning_rate = 0.001
plt.imshow(cv2.filter2D(image, -1, weights[i][0]), cmap='gray') plt.show() #================================================================= csv_dir_train = 'data/training_frames_keypoints.csv' root_dir_train = 'data/training/' csv_dir_test = 'data/test_frames_keypoints.csv' root_dir_test = 'data/test/' batch_size = 8 transform = transforms.Compose([ RandomRotation(15), Rescale(224), RandomCrop(223), Normalize(), ToTensor() ]) trainDataset = FacialDataset(csv_dir_train, root_dir_train, transform) testDataset = FacialDataset(csv_dir_test, root_dir_test, transform) ''' sample = trainDataset[0] angle = 22.5 sample_x = transform(sample) display(sample_x['image'], sample_x['keypoints'])
else: image = self.test_data[item] if self.transform: image = self.transform(image) return image def _check_exists(self): return osp.exists(osp.join(self.root_dir, "train")) and osp.exists( osp.join(self.root_dir, "test")) if __name__ == "__main__": nucleus_dataset = NucleusDataset(root_dir="./data", train=True, transform=Compose([Rescale(256)]), target_transform=Compose([Rescale(256)])) # Display 5 images side-by-side for i in range(len(nucleus_dataset)): image, mask = nucleus_dataset[i] print(i, image.shape, mask.shape) cv2.imshow('image', image) cv2.imshow('mask', mask) cv2.waitKey(0) cv2.destroyAllWindows() if i == 5: break