def debug_me(): # Define network net = Net() print(net) data_transform = transforms.Compose( [Rescale(250), RandomCrop(224), Normalize()]) aww_dataset = FacialKeypointsDataset( csv_file='data/aww_frames_keypoints.csv', root_dir='data/aww/', transform=data_transform) sample = aww_dataset[0] print(sample['image'].shape, sample['keypoints'].shape) print(np.max(sample['keypoints'])) aww_loader = DataLoader(aww_dataset, batch_size=10, shuffle=True, num_workers=4) aww_images, aww_outputs, gt_pts = net_sample_output(net, aww_loader) visualize_output(aww_images, aww_outputs, gt_pts, 1) '''
def show_transformed(self, index): # test out some of these transforms rescale = Rescale(100) crop = RandomCrop(50) composed = transforms.Compose([Rescale(250), RandomCrop(224)]) # apply the transforms to a sample image test_num = 500 sample = face_dataset[test_num] fig = plt.figure() for i, tx in enumerate([rescale, crop, composed]): transformed_sample = tx(sample) ax = plt.subplot(1, 3, i + 1) plt.tight_layout() ax.set_title(type(tx).__name__) show_keypoints(transformed_sample['image'], transformed_sample['keypoints']) plt.show()
def main(): hyp_batch_size = 20 net = Net2() model_dir = '../saved_models/' model_name = 'keypoints_model_2.pt' data_transform = transforms.Compose([ Rescale(256), RandomCrop(224), Normalize(), ToTensor() ]) # retreive the saved model net_state_dict = torch.load(model_dir+model_name) net.load_state_dict(net_state_dict) # load the test data test_dataset = FacialKeypointsDataset(csv_file='../files/test_frames_keypoints.csv', root_dir='../files/test/', transform=data_transform) # load test data in batches batch_size = hyp_batch_size test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=True, num_workers=0) test_images, test_outputs, gt_pts = net_sample_output(test_loader, net) print(test_images.data.size()) print(test_outputs.data.size()) print(gt_pts.size()) # Get the weights in the first conv layer, "conv1" # if necessary, change this to reflect the name of your first conv layer weights1 = net.conv1.weight.data w = weights1.numpy() filter_index = 0 print(w[filter_index][0]) print(w[filter_index][0].shape) # display the filter weights plt.imshow(w[filter_index][0], cmap='gray') #plt.show() ##TODO: load in and display any image from the transformed test dataset i = 1 show_image(test_images, w, i)
def __init__(self, root_dir=None): self.data_root_dir = root_dir or './data/' self.training_csv = os.path.join(self.data_root_dir, 'training_frames_keypoints.csv') self.training_data_dir = os.path.join(self.data_root_dir, 'training/') self.test_csv = os.path.join(self.data_root_dir, 'test_frames_keypoints.csv') self.test_data_dir = os.path.join(self.data_root_dir, 'test/') self.key_pts_frame = pd.read_csv(self.training_csv) self.face_dataset = FacialKeypointsDataset( csv_file=self.training_csv, root_dir=self.training_data_dir) self.face_dataset_len = len(self.face_dataset) # define the data tranform # order matters! i.e. rescaling should come before a smaller crop self.data_transform = transforms.Compose( [Rescale(250), RandomCrop(224), Normalize(), ToTensor()]) self.transformed_training_data = self.transform_data( self.training_csv, self.training_data_dir) self.transformed_test_data = self.transform_data( self.test_csv, self.test_data_dir)
def data_transform(npy_train, npy_test, batch_size=64, shuffle=True): # Setting up our image transforms DATA_TRANSFORM = transforms.Compose( [Rescale(256), RandomCrop(224), Normalize(), ToTensor()]) # importing in the Custome dataset TRANSFORMED_DATASET_TRAIN = KeypointDataSet(npy_file=npy_train, root_dir='data/', transform=DATA_TRANSFORM) TRANSFORMED_DATASET_TEST = KeypointDataSet(npy_file=npy_test, root_dir='data/', transform=DATA_TRANSFORM) # passing in the trainformed data into the dataLoader TRAIN_LOADER = DataLoader(TRANSFORMED_DATASET_TRAIN, batch_size=64, shuffle=True, pin_memory=True) TEST_LOADER = DataLoader(TRANSFORMED_DATASET_TEST, batch_size=64, shuffle=True, pin_memory=True) return TRAIN_LOADER, TEST_LOADER
from torch.utils.data import DataLoader from torchvision import transforms from data_load import FacialKeypointsDataset, Rescale, RandomCrop, Normalize, ToTensor from models import Net net = Net() net.load_state_dict( torch.load( r'C:\Users\Semanti Basu\Documents\OneDrive_2020-02-19\3D Ceaser dataset\Image and point generation\Image and point generation\frontaltrainedmodel_10epoch.pth' )) ## print out your net and prepare it for testing (uncomment the line below) net.eval() data_transform = transforms.Compose( [Rescale(225), RandomCrop(224), Normalize(), ToTensor()]) transformed_dataset = FacialKeypointsDataset( csv_file= r'C:\Users\Semanti Basu\Documents\OneDrive_2020-02-19\3D Ceaser dataset\Image and point generation\Image and point generation\frontalpoints.csv', root_dir= r'C:\Users\Semanti Basu\Documents\OneDrive_2020-02-19\3D Ceaser dataset\Image and point generation\Image and point generation\ceasar_mat', transform=data_transform) # load training data in batches batch_size = 10 train_loader = DataLoader(transformed_dataset, batch_size=batch_size, shuffle=True, num_workers=0)
from torch.utils.data import DataLoader from torchvision import transforms from data_load import FacialKeypointsDataset, Rescale, RandomCrop, Normalize, ToTensor from models import Net net = Net() print(net) criterion = nn.SmoothL1Loss() optimizer = optim.Adam(net.parameters(), lr=0.01) """ Load data """ # define the data transform data_transform = transforms.Compose([Rescale(250), RandomCrop(224), Normalize(), ToTensor()]) # create the transformed dataset transformed_dataset = FacialKeypointsDataset(csv_file='data/training_frames_keypoints.csv', root_dir='data/training/', transform=data_transform) # load training data in batches batch_size = 50 train_loader = DataLoader(transformed_dataset, batch_size=batch_size, shuffle=True, num_workers=0)
options = options() opts = options.parse() from models import Net net = Net(1).to(device) from torch.utils.data import DataLoader from torchvision import transforms from data_load import FacialKeypointsDataset from data_load import Rescale, RandomCrop, Normalize, ToTensor, RotateScale, HorizontalFlip, VerticalFlip data_transform = transforms.Compose([RotateScale(60), Rescale((256, 256)), RandomCrop((224, 224)), Normalize(), ToTensor()]) batch_size = opts.batch transformed_dataset = FacialKeypointsDataset(csv_file='/data/training_frames_keypoints.csv', root_dir='/data/training/', transform=data_transform) train_loader = DataLoader(transformed_dataset, batch_size=batch_size, shuffle=True, num_workers=4) test_dataset = FacialKeypointsDataset(csv_file='/data/test_frames_keypoints.csv', root_dir='/data/test/', transform=data_transform) test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=True, num_workers=4) import torch.optim as optim criterion = opts.criterion optimizer = optim.Adam(net.parameters(), lr=opts.lr)
if is_debug: print('fc1.out_shape:', x.shape) # a modified x, having gone through all the layers of your model, should be returned return x if __name__ == '__main__': import os os.chdir('/Users/rawk/Projects/Project Zero/MLND-projects/cv-project0-facial-keypoints/') from data_load import FacialKeypointsDataset from data_load import Rescale, RandomCrop, Normalize, ToTensor from torch.utils.data import Dataset, DataLoader from torchvision import transforms, utils data_transform = transforms.Compose([Rescale((250, 250)), RandomCrop((224, 224)), Normalize(), ToTensor()]) transformed_dataset = FacialKeypointsDataset(csv_file='./data/training_frames_keypoints.csv', root_dir='./data/training/', transform=data_transform) batch_size = 2 data_loader = DataLoader(transformed_dataset, batch_size=batch_size, shuffle=True, num_workers=4) for i, sample in enumerate(data_loader): images = sample['image'] images = images.type(torch.FloatTensor) net = Net()
net = Net().to(device) print(net) from torch.utils.data import Dataset, DataLoader from torchvision import transforms, utils # the dataset we created in Notebook 1 is copied in the helper file `data_load.py` #from data_load import FacialKeypointsDataset # the transforms we defined in Notebook 1 are in the helper file `data_load.py` from data_load import Rescale, RandomCrop, Normalize, ToTensor ## TODO: define the data_transform using transforms.Compose([all tx's, . , .]) # order matters! i.e. rescaling should come before a smaller crop data_transform = transforms.Compose( [Rescale(250), RandomCrop(200), Normalize(), ToTensor()]) # testing that you've defined a transform assert (data_transform is not None), 'Define a data_transform' # create the transformed dataset transformed_dataset = FacialKeypointsDataset( csv_file='./data/training_frames_keypoints.csv', root_dir='./data/training/', transform=data_transform) print('Number of images: ', len(transformed_dataset)) # iterate through the transformed dataset and print some stats about the first few samples for i in range(4):
print("Downloading data...") r = requests.get(URL + DATA_FILE, stream=True) assert (r.ok), 'Input data files could not be downloaded from URL' with open(str(Path(DOWNLOAD_PATH, DATA_FILE)), 'wb+') as f: for chunk in r.iter_content(chunk_size=1024): if chunk: f.write(chunk) z = zipfile.ZipFile(str(Path(DOWNLOAD_PATH, DATA_FILE))) z.extractall(DOWNLOAD_PATH) print("Downloading and extraction complete.") ### PREPARE DATA ### # order matters! i.e. rescaling should come before a smaller crop data_transform = transforms.Compose( [Rescale(100), RandomCrop(96), Normalize(), ToTensor()]) # create the transformed dataset transformed_dataset = FacialKeypointsDataset( csv_file='./data/training_frames_keypoints.csv', root_dir='./data/training/', transform=data_transform) print('Number of images: ', len(transformed_dataset)) # create the test dataset test_dataset = FacialKeypointsDataset( csv_file='./data/test_frames_keypoints.csv', root_dir='./data/test/', transform=data_transform)
(conv1): Conv2d(1, 32, kernel_size=(5, 5), stride=(1, 1)) ) [IN][2]: from torch.utils.data import Dataset, DataLoader from torchvision import transforms, utils # the dataset we created in Notebook 1 is copied in the helper file `data_load.py` from data_load import FacialKeypointsDataset # the transforms we defined in Notebook 1 are in the helper file `data_load.py` from data_load import Rescale, RandomCrop, Normalize, ToTensor ## TODO: define the data_transform using transforms.Compose([all tx's, . , .]) # order matters! i.e. rescaling should come before a smaller crop crop = RandomCrop(224) scale = Rescale(224) Composed = transforms.Compose([Rescale(224), RandomCrop(224)]) data_transform = Composed # testing that you've defined a transform assert(data_transform is not None), 'Define a data_transform' [IN][3]: # create the transformed dataset transformed_dataset = FacialKeypointsDataset(csv_file='/data/training_frames_keypoints.csv', root_dir='/data/training/', transform=data_transform)
def main(): # Define network net = Net() print(net) data_transform = transforms.Compose( [Rescale(250), Normalize(), RandomCrop(224)]) train_dataset = FacialKeypointsDataset( csv_file='data/training_frames_keypoints.csv', root_dir='data/training/', transform=data_transform) # iterate through the transformed dataset and print some stats about the first few samples for i in range(4): sample = train_dataset[i] print(i, sample['image'].size, sample['keypoints'].size) train_loader = DataLoader(train_dataset, batch_size=10, shuffle=True, num_workers=4) test_dataset = FacialKeypointsDataset( csv_file='data/test_frames_keypoints.csv', root_dir='data/test/', transform=data_transform) test_loader = DataLoader(test_dataset, batch_size=1, shuffle=True, num_workers=4) test_images, test_outputs, gt_pts = net_sample_output(net, test_loader) # print out the dimensions of the data to see if they make sense print(test_images.data.size()) print(test_outputs.data.size()) print(gt_pts.size()) # call it visualize_output(test_images, test_outputs, gt_pts, 1) criterion = nn.MSELoss() optimizer = optim.Adam(net.parameters(), lr=0.001, betas=(0.9, 0.999), eps=1e-8) n_epochs = 2 train_net(net, criterion, optimizer, train_loader, n_epochs) # get a sample of test data again test_images, test_outputs, gt_pts = net_sample_output(net, test_loader) print(test_images.data.size()) print(test_outputs.data.size()) print(gt_pts.size()) model_dir = 'saved_models/' model_name = 'keypoints_model_1.pt' # after training, save your model parameters in the dir 'saved_models' torch.save(net.state_dict(), model_dir + model_name) weights1 = net.conv1.weight.data w = weights1.numpy() filter_index = 0 print(w[filter_index][0]) print(w[filter_index][0].shape) # display the filter weights plt.imshow(w[filter_index][0], cmap='gray')
# In[24]: from torch.utils.data import Dataset, DataLoader from torchvision import transforms, utils # the dataset we created in Notebook 1 is copied in the helper file `data_load.py` from data_load import FacialKeypointsDataset # the transforms we defined in Notebook 1 are ien the helper file `data_load.py` from data_load import Rescale, RandomCrop, Normalize, ToTensor ## define the data_transform using transforms.Compose([all tx's, . , .]) # order matters! i.e. rescaling should come before a smaller crop data_transform = None rescale = Rescale(100) crop = RandomCrop(50) data_transform = transforms.Compose( [Rescale(250), RandomCrop(224), Normalize(), ToTensor()]) # testing that you've defined a transform assert (data_transform is not None), 'Define a data_transform' # In[25]: # create the transformed dataset transformed_dataset = FacialKeypointsDataset( csv_file='/data/training_frames_keypoints.csv', root_dir='/data/training/', transform=data_transform) print('Number of images: ', len(transformed_dataset))
def run_epochs(config, checkpoint_path): print("CUDA is available: {}".format(torch.cuda.is_available())) # Define data loader: data preprocessing and augmentation # I use same procedures for all models that consumes imagenet-2012 dataset for simplicity imagenet_train_transform = transforms.Compose([ Rescale(256), RandomHorizontalFlip(0.5), RandomCrop(224), ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0), ToTensor(), # https://github.com/pytorch/examples/blob/master/imagenet/main.py#L195 # this is pre-calculated mean and std of imagenet dataset Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ]) imagenet_val_transform = transforms.Compose([ Rescale(256), CenterCrop(224), ToTensor(), Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ]) train_loader = initialize_train_loader(imagenet_train_transform, config) val_loader = initialize_val_loader(imagenet_val_transform, config) # Define the neural network. Model = config.get('model') model_params = config.get('model_params') if model_params is not None: net = Model(**model_params) else: net = Model() # transfer variables to GPU if present net.to(device=device) # Print the network structure given 3x32x32 input # need to put this before DataParallel to avoid "Expected more than 1 value per channel when training" error # https://github.com/pytorch/pytorch/issues/4534 summary(net, (3, 224, 224)) # Wrap it with DataParallel to train with multiple GPUs if torch.cuda.device_count() > 1: print("Using", torch.cuda.device_count(), "GPUs!") net = nn.DataParallel(net) # Define the loss function. CrossEntrophyLoss is the most common one for classification task. criterion = nn.CrossEntropyLoss() # Define the optimizer Optim = config.get('optimizer') optimizer = Optim( net.parameters(), **config.get('optimizer_params'), ) # Define the scheduler Sched = config.get('scheduler') scheduler = Sched( optimizer, **config.get('scheduler_params'), ) loggers = initialize_loggers() model_id = time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime()) model_name = config.get('name') start_epoch = 1 if checkpoint_path is not None: net, optimizer, scheduler, loggers, start_epoch = load_checkpoint( checkpoint_path, net, optimizer, scheduler, loggers, ) validate(val_loader, net, criterion, 0, loggers) for epoch in range(start_epoch, config.get('total_epochs') + 1): train( train_loader, net, criterion, optimizer, epoch, loggers, ) val_loss, top1_acc, top5_acc = validate( val_loader, net, criterion, epoch, loggers, ) # for ReduceLROnPlateau scheduler, we need to use top1_acc as metric if isinstance(scheduler, optim.lr_scheduler.ReduceLROnPlateau): scheduler.step(top1_acc) else: scheduler.step() checkpoint_file = '{}-{}-epoch-{}.pt'.format( model_name, model_id, epoch, ) torch.save( { 'epoch': epoch, 'model': net.state_dict(), 'optimizer': optimizer.state_dict(), 'scheduler': scheduler.state_dict(), 'loggers': loggers, }, model_dir + checkpoint_file)
def main(): #------------------------------------------------------------------------------------------------------------------ # Hyperparameters hyp_epochs = 5 hyp_batch_size = 20 #hyp_optim = "SGD" hyp_optim = "Adam" #hyp_net = Net1() hyp_net = Net2() print("Hyperparameters") print("--------------") print("Epochs = ", hyp_epochs) print("Batch Size = ", hyp_batch_size) print("Optimizer = ", hyp_optim) print("--------------") ## TODO: Define the Net in models.py net = hyp_net print(net) ## TODO: define the data_transform using transforms.Compose([all tx's, . , .]) # order matters! i.e. rescaling should come before a smaller crop data_transform = transforms.Compose( [Rescale(256), RandomCrop(224), Normalize(), ToTensor()]) # testing that you've defined a transform assert (data_transform is not None), 'Define a data_transform' # create the transformed dataset transformed_dataset = FacialKeypointsDataset( csv_file='../files/training_frames_keypoints.csv', root_dir='../files/training/', transform=data_transform) print('Number of images: ', len(transformed_dataset)) # iterate through the transformed dataset and print some stats about the first few samples for i in range(4): sample = transformed_dataset[i] print(i, sample['image'].size(), sample['keypoints'].size()) # load training data in batches batch_size = hyp_batch_size train_loader = DataLoader(transformed_dataset, batch_size=batch_size, shuffle=True, num_workers=0) # load in the test data, using the dataset class # AND apply the data_transform you defined above # create the test dataset test_dataset = FacialKeypointsDataset( csv_file='../files/test_frames_keypoints.csv', root_dir='../files/test/', transform=data_transform) # load test data in batches batch_size = hyp_batch_size test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=True, num_workers=0) # test the model on a batch of test images # call the above function # returns: test images, test predicted keypoints, test ground truth keypoints test_images, test_outputs, gt_pts = net_sample_output(test_loader, net) # print out the dimensions of the data to see if they make sense print(test_images.data.size()) print(test_outputs.data.size()) print(gt_pts.size()) # visualize the output # by default this shows a batch of 10 images # call it _visualise = False if _visualise == True: visualize_output(test_images, test_outputs, gt_pts) ## TODO: Define the loss and optimization import torch.optim as optim criterion = nn.MSELoss() hyp_optimizer = None if hyp_optim == "Adam": hyp_optimizer = optim.Adam(net.parameters(), lr=0.001) if hyp_optim == "SGD": hyp_optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) optimizer = hyp_optimizer # train your network n_epochs = hyp_epochs # start small, and increase when you've decided on your model structure and hyperparams # this is a Workspaces-specific context manager to keep the connection # alive while training your model, not part of pytorch train_net(n_epochs, train_loader, net, criterion, optimizer) # get a sample of test data again test_images, test_outputs, gt_pts = net_sample_output(test_loader, net) print(test_images.data.size()) print(test_outputs.data.size()) print(gt_pts.size()) ## TODO: change the name to something uniqe for each new model model_dir = '../saved_models/' model_name = 'keypoints_model_2.pt' # after training, save your model parameters in the dir 'saved_models' torch.save(net.state_dict(), model_dir + model_name) # -------------------------------------------------------------------- # To run the following code after retreiving an existing model, # you can do so in the resume.py file # -------------------------------------------------------------------- # Get the weights in the first conv layer, "conv1" # if necessary, change this to reflect the name of your first conv layer weights1 = net.conv1.weight.data w = weights1.numpy() filter_index = 0 print(w[filter_index][0]) print(w[filter_index][0].shape) # display the filter weights plt.imshow(w[filter_index][0], cmap='gray') ##TODO: load in and display any image from the transformed test dataset i = 1 show_image(test_images, w, i)
n = 30 if args['model'] == "NaimishNet": net = NaimishNet(n) elif args['model'] == "VggFace": net = VggFace(n) elif args['model'] == "Custom": net = Net2(n) else: net = LeNet5(n) model_name = args['model'] print(net) device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') # order matters! i.e. rescaling should come before a smaller crop train_transform = transforms.Compose([Rescale(110),RandomCrop(96),Albu(),Normalize(args["dataset"]),ToTensor()]) test_transform = transforms.Compose([Normalize(args["dataset"]),ToTensor()]) # testing that you've defined a transform assert(train_transform is not None and test_transform is not None), 'Define a data_transform' # create the transformed dataset if args["dataset"] == "Kaggle": X, y = load_KagggleDataset(split=args['split'],train_30=args['train30'],train_8=args['train8']) X_test, y_test = X[:300], y[:300] X_train, y_train = X[300:], y[300:] transformed_dataset = KagggleDataset(X_train, y_train, train_transform) test_dataset = KagggleDataset(X_test, y_test, test_transform) sub, div = 48., 48. else:
from torchvision import transforms, utils from data_load import FacialKeypointsDataset from data_load import Rescale, RandomCrop, Normalize, ToTensor import torch.optim as optim from models import * # instantiate the model net = AlexNet() print(net) # define the data transform using transfroms.Compose([..]) # Note the order is matter data_tranform = transforms.Compose( [Rescale((250, 250)), RandomCrop((227, 227)), Normalize(), ToTensor()]) # Create the transformed dataset transformed_dataset = FacialKeypointsDataset( csv_file='data/training_frames_keypoints.csv', root_dir='data/training/', transform=data_tranform) print('Number of images: ', len(transformed_dataset)) # iterate through the transformed dataset and print some stats about the first few samples for i in range(4): sample = transformed_dataset[i] print(i, sample['image'].size(), sample['keypoints'].size())
import numpy as np from train import train_net if __name__ == "__main__": torch.cuda.empty_cache() offline = True device = torch.device("cuda" if torch.cuda.is_available() else "cpu") n_epochs = 20 print("AI Running on", device) net = FCN().apply(initialize_weights_advance_).to(device) print(net) data_transform = transforms.Compose( [Rescale(250), RandomCrop(224), Normalize(), ToTensor()]) transformed_dataset = FacialKeypointsDataset( csv_file='data/training_frames_keypoints.csv', root_dir='data/training/', transform=data_transform) # load training data in batches batch_size = 128 train_loader = DataLoader(transformed_dataset, batch_size=batch_size, shuffle=True, num_workers=4)
# In[133]: from torch.utils.data import Dataset, DataLoader from torchvision import transforms, utils # the dataset we created in Notebook 1 is copied in the helper file `data_load.py` from data_load import FacialKeypointsDataset # the transforms we defined in Notebook 1 are in the helper file `data_load.py` from data_load import Rescale, RandomCrop, Normalize, ToTensor crop = 224 data_transform = transforms.Compose([Rescale(250), RandomCrop(crop), Normalize(crop), ToTensor()]) # testing that you've defined a transform assert(data_transform is not None), 'Define a data_transform' # In[134]: # create the transformed dataset transformed_dataset = FacialKeypointsDataset(csv_file='training_frames_keypoints.csv', root_dir='/home/tianbai/P1_Facial_Keypoints/data/training/', transform=data_transform)