def train_pytorch(rank, args, model): torch.manual_seed(args.seed + rank) transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) train_loader = torch.utils.data.DataLoader(CIFAR10('~/datasets', train=True, download=True, transform=transform), batch_size=args.batch_size, shuffle=True, num_workers=1) criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum) for epoch in range(1, args.epochs + 1): train_epoch_pytorch(epoch, args, model, train_loader, optimizer, criterion)
def test(model_file): tf.reset_default_graph() # Load CIFAR10 dataset cifar10 = CIFAR10() x_test = cifar10.x_test y_test = cifar10.y_test y_test_onehot = cifar10.y_test_onehot num_classes = cifar10.num_classes input_size = cifar10.input_size model = CNN(input_size=input_size, num_classes=num_classes, optimizer='Adam') model.load(filepath=model_file) test_prediction_onehot = model.test(data=x_test) test_prediction = np.argmax(test_prediction_onehot, axis=1).reshape( (-1, 1)) test_accuracy = model_accuracy(label=y_test, prediction=test_prediction) print('Test Accuracy: %f' % test_accuracy)
def test_from_frozen_graph(model_filepath): tf.reset_default_graph() # Load CIFAR10 dataset cifar10 = CIFAR10() x_test = cifar10.x_test y_test = cifar10.y_test y_test_onehot = cifar10.y_test_onehot num_classes = cifar10.num_classes input_size = cifar10.input_size # Test 500 samples x_test = x_test[0:500] y_test = y_test[0:500] model = CNN(model_filepath=model_filepath) test_prediction_onehot = model.test(data=x_test) test_prediction = np.argmax(test_prediction_onehot, axis=1).reshape( (-1, 1)) test_accuracy = model_accuracy(label=y_test, prediction=test_prediction) print('Test Accuracy: %f' % test_accuracy)
def train(learning_rate, learning_rate_decay, dropout_rate, mini_batch_size, epochs, optimizer, random_seed, model_directory, model_filename, log_directory): np.random.seed(random_seed) if not os.path.exists(log_directory): os.makedirs(log_directory) # Load CIFAR10 dataset cifar10 = CIFAR10() x_train = cifar10.x_train y_train = cifar10.y_train y_train_onehot = cifar10.y_train_onehot x_valid = cifar10.x_valid y_valid = cifar10.y_valid y_valid_onehot = cifar10.y_valid_onehot num_classes = cifar10.num_classes input_size = cifar10.input_size print('CIFAR10 Input Image Size: {}'.format(input_size)) model = CNN(input_size=input_size, num_classes=num_classes, optimizer=optimizer) train_accuracy_log = list() valid_accuracy_log = list() train_loss_log = list() for epoch in range(epochs): print('Epoch: %d' % epoch) learning_rate *= learning_rate_decay # Prepare mini batches on train set shuffled_idx = np.arange(len(x_train)) np.random.shuffle(shuffled_idx) mini_batch_idx = [ shuffled_idx[k:k + mini_batch_size] for k in range(0, len(x_train), mini_batch_size) ] # Validate on validation set valid_prediction_onehot = model.test(data=x_valid) valid_prediction = np.argmax(valid_prediction_onehot, axis=1).reshape( (-1, 1)) valid_accuracy = model_accuracy(label=y_valid, prediction=valid_prediction) print('Validation Accuracy: %f' % valid_accuracy) valid_accuracy_log.append(valid_accuracy) # Train on train set for i, idx in enumerate(mini_batch_idx): train_loss = model.train(data=x_train[idx], label=y_train_onehot[idx], learning_rate=learning_rate, dropout_rate=dropout_rate) if i % 200 == 0: train_prediction_onehot = model.test(data=x_train[idx]) train_prediction = np.argmax(train_prediction_onehot, axis=1).reshape((-1, 1)) train_accuracy = model_accuracy(label=y_train[idx], prediction=train_prediction) print('Training Loss: %f, Training Accuracy: %f' % (train_loss, train_accuracy)) if i == 0: train_accuracy_log.append(train_accuracy) train_loss_log.append(train_loss) model.save(directory=model_directory, filename=model_filename) print('Trained model saved successfully') model.save_as_pb(directory=model_directory, filename=model_filename) print('Trained model saved as pb successfully') # The directory should not exist before calling this method signature_dir = os.path.join(model_directory, 'signature') assert (not os.path.exists(signature_dir)) model.save_signature(directory=signature_dir) print('Trained model with signature saved successfully') plot_curve(train_losses = train_loss_log, train_accuracies = train_accuracy_log, valid_accuracies = valid_accuracy_log, \ filename = os.path.join(log_directory, 'training_curve.png'))
mode_set = [('normal', 'normal'), ('normal', 'random'), ('normal', 'partially-0.1'), ('normal', 'partially-0.3'), ('normal', 'partially-0.5'), ('normal', 'partially-0.7'), ('normal', 'partially-0.9'), ('random', 'normal'), ('shuffled', 'normal')] for (mode1, mode2) in mode_set: print(mode1, ' and ', mode2, ' :') img_transforms = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)) ]) training_dataset = CIFAR10(root, train=True, transform=img_transforms, image_mode=mode1, label_mode=mode2) training_loader = DataLoader(training_dataset, BATCH_SIZE, shuffle=True, pin_memory=True) testing_dataset = CIFAR10(root, train=False, transform=img_transforms) testing_loader = DataLoader(testing_dataset, BATCH_SIZE, shuffle=False, pin_memory=True) loaders = {'train': training_loader, 'test': testing_loader}
transform_train = transforms.Compose([ transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)), ]) transform_test = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)), ]) # trainset = torchvision.datasets.CIFAR10( # root='./data', train=True, download=True, transform=transform_train) trainset = CIFAR10(root='./data', train=True, download=True, transform=transform_train) trainloader = torch.utils.data.DataLoader(trainset, batch_size=128, shuffle=False, num_workers=2) testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test) testloader = torch.utils.data.DataLoader(testset, batch_size=100, shuffle=False, num_workers=2)
from sklearn.neighbors import KNeighborsClassifier from sklearn.pipeline import Pipeline root = './' mode_set = [('normal', 'normal'), ('normal', 'random'), ('normal', 'partially-0.1'), ('normal', 'partially-0.3'), ('normal', 'partially-0.5'), ('normal', 'partially-0.7'), ('normal', 'partially-0.9'), ('random', 'normal'), ('shuffled', 'normal')] error = [] for (mode1, mode2) in mode_set: training_dataset = CIFAR10(root, train=True, image_mode=mode1, label_mode=mode2) testing_dataset = CIFAR10(root, train=False) training_data, training_labels = training_dataset.train_data.reshape( (50000, -1)) / 255, training_dataset.train_labels testing_data, testing_labels = testing_dataset.test_data.reshape( (10000, -1)) / 255, testing_dataset.test_labels n = 1 pipline = Pipeline([('knn', KNeighborsClassifier(n_neighbors=n, n_jobs=-1))]) pipline.fit(training_data, training_labels) testing_predictions = pipline.predict(testing_data) testing_error = np.mean(testing_predictions != testing_labels)
from cifar import CIFAR10 if __name__ == '__main__': # Instantiate the dataset. If the dataset is not found in `dataset_root`, # the first time it is automatically downloaded and extracted there. dataset = CIFAR10(dataset_root='./cifar10') # That's it. Now all examples are in `dataset.samples` dictionary. There # are 50000 train examples and 10000 test examples. print(dataset.samples['train'].shape) # (50000,) print(dataset.samples['test'].shape) # (10000,) # Each example is constituted by a 32x32 RGB image and its # corresponding label, both numeric and human readable. print(dataset.samples['train'][0].image.shape) # (32, 32, 3) print(dataset.samples['train'][0].label) # 6 print(dataset.samples['train'][0].label_hr) # frog print(dataset.samples['train'] [0].filename) # leptodactylus_pentadactylus_s_000004.png # You can also directly print the example print(dataset.samples['train'] [0]) # [frog] - leptodactylus_pentadactylus_s_000004.png # You can convert the CIFARSamples to ndarray. Images are possibly flattened # and/or normalized to be centered on zero (i.e. in range [-0.5, 0.5]) x_train, y_train = CIFAR10.to_ndarray(dataset.samples['train'], normalize=True, flatten=True) x_test, y_test = CIFAR10.to_ndarray(dataset.samples['test'], normalize=True,
mean = [x / 255 for x in [125.3, 123.0, 113.9]] std = [x / 255 for x in [63.0, 62.1, 66.7]] train_transform = transform.Compose([ transform.RandomHorizontalFlip(), RandomCrop(32, padding=4), ToTensor(), transform.ImageNormalize(mean, std) ]) test_transform = transform.Compose( [ToTensor(), transform.ImageNormalize(mean, std)]) if args.dataset == 'cifar10': train_data = CIFAR10(args.data_path, train=True, transform=train_transform, batch_size=args.batch_size, num_workers=args.prefetch) test_data = CIFAR10(args.data_path, train=False, transform=test_transform, shuffle=False, num_workers=args.prefetch, batch_size=args.test_bs) nlabels = 10 else: train_data = CIFAR100(args.data_path, train=True, transform=train_transform, download=True, batch_size=args.batch_size,
import torch import torchvision import torchvision.transforms as transforms import time from cifar import CIFAR10 import matplotlib.pyplot as plt import numpy as np from pathlib import Path transform = transforms.Compose([transforms.ToTensor()]) trainset = CIFAR10(root='/home/aarati/datasets', train=True, download=False, transform=transforms.ToTensor()) trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=1) time.sleep(4) all_times = [] d = [] total_time = 0 start = time.time() for i, data in enumerate(trainloader, 0): if i == 2000: break end = time.time()
import torch import torchvision import transformsCV as transforms import torch.nn as nn import torch.nn.functional as F import torch.optim as optim from cifar import CIFAR10 transform = transforms.Compose([ transforms.Pad(8), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) trainset = CIFAR10(root='./data', train=True, download=True, transform=transform) trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=2) testset = CIFAR10(root='./data', train=False, download=True, transform=transform) testloader = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=False, num_workers=2)