def logistic_reg_mnist_download(): mnist_folder = 'data/mnist' utils.download_mnist(mnist_folder) train, val, test = utils.read_mnist(mnist_folder, flatten=True) # We ignore the validation test data in this problem return train, test
def run(input_data): i = np.array(json.loads(input_data)['data']) _, test = download_mnist() x = Variable(np.asarray([test[i][0]])) y = model(x) return np.ndarray.tolist(y.data.argmax(axis=1))
def fetch_data(class_1, class_2): '''reduces MNIST data to a two class classification problem''' mnist_folder = 'data/mnist' if not os.path.isdir(mnist_folder): os.mkdir('data') os.mkdir(mnist_folder) utils.download_mnist(mnist_folder) train, _, test = utils.read_mnist(mnist_folder, flatten=True) train_images = np.array(train[0]) train_images = preprocessing.scale(np.array(train_images), axis=1) train_labels = np.array([np.where(x == 1)[0][0] for x in train[1]]) test_images = np.array(test[0]) test_images = preprocessing.scale(np.array(test_images), axis=1) test_labels = np.array([np.where(x == 1)[0][0] for x in test[1]]) train_data = ((train_labels == class_1) + (train_labels == class_2)) x_train = train_images[train_data] y_train = train_labels[train_data] test_data = ((test_labels == class_1) + (test_labels == class_2)) x_test = test_images[test_data] y_test = test_labels[test_data] return x_train, y_train, x_test, y_test
def train(network, epochs=3): """Train network for given number of epochs""" print('loading data...') filepath = download_mnist() x_train, y_train, x_test, y_test = load_mnist(filepath) plot = nm.PlotLines(None, every_sec=0.2) build_batch = (nm.BuildBatch(128, verbose=False).input( 0, 'vector', 'float32').output(1, 'number', 'int64')) for epoch in range(epochs): print('epoch', epoch + 1) losses = (zip(x_train, y_train) >> nf.PrintProgress(x_train) >> nf.Shuffle(1000) >> build_batch >> network.train() >> plot >> nf.Collect()) acc_test = evaluate(network, x_test, y_test) acc_train = evaluate(network, x_train, y_train) print('train loss : {:.6f}'.format(np.mean(losses))) print('train acc : {:.1f}'.format(acc_train)) print('test acc : {:.1f}'.format(acc_test))
import utils # Define paramaters for the model learning_rate = 0.01 batch_size = 128 n_epochs = 30 n_train = 60000 n_test = 10000 # Step 1: Read in data mnist_folder = 'data/mnist' if os.path.isdir(mnist_folder) != True: os.mkdir('data') os.mkdir('mnist_folder') utils.download_mnist(mnist_folder) train, val, test = utils.read_mnist(mnist_folder, flatten=True) # Step 2: Create datasets and iterator # create training Dataset and batch it train_data = tf.data.Dataset.from_tensor_slices(train) train_data = train_data.shuffle(10000) # if you want to shuffle your data train_data = train_data.batch(batch_size) # create testing Dataset and batch it test_data = tf.data.Dataset.from_tensor_slices(test) test_data = test_data.batch(batch_size) ############################# ########## TO DO ############ #############################
(len(pred_prob), 10), dtype=np.float) # pred label distr gnd_vec = 0.1 * np.ones( (1, 10), dtype=np.float) # gnd label distr, uniform over 10 digits for i, label in enumerate(pred_prob): y_vec[i, np.argmax(pred_prob[i])] = 1.0 y_vec = np.sum(y_vec, axis=0, keepdims=True) y_vec = y_vec / np.sum(y_vec) label_entropy = np.sum(-y_vec * np.log(y_vec)).tolist() label_tv = np.true_divide(np.sum(np.abs(y_vec - gnd_vec)), 2).tolist() label_l2 = np.sum((y_vec - gnd_vec)**2).tolist() pickle.dump( { 'entropy': label_entropy, 'tv': label_tv, 'l2': label_l2 }, open(config.savepath, 'wb')) print("Entropy: {}".format(label_entropy)) print("TV: {}".format(label_tv)) print("L2: {}".format(label_l2)) if __name__ == '__main__': utils.download_mnist() tf.app.run()
import numpy as np import tensorflow as tf import time import utils # Define paramaters for the model learning_rate = 0.01 batch_size = 128 n_epochs = 30 n_train = 60000 n_test = 10000 # Step 1: Read in data mnist_folder = 'data/mnist' utils.download_mnist(mnist_folder) train, val, test = utils.read_mnist(mnist_folder, flatten=True) # Step 2: Create datasets and iterator train_data = tf.data.Dataset.from_tensor_slices(train) train_data = train_data.shuffle(10000) # if you want to shuffle your data train_data = train_data.batch(batch_size) test_data = tf.data.Dataset.from_tensor_slices(test) test_data = test_data.batch(batch_size) iterator = tf.data.Iterator.from_structure(train_data.output_types, train_data.output_shapes) img, label = iterator.get_next() train_init = iterator.make_initializer(train_data) # initializer for train_data
def get_train_data(params): if params['img_chan'] == 1: dataloader = download_mnist(params) else: dataloader = download_celeba(params) return dataloader
# Imports import numpy as np import tensorflow as tf import time import utils # Define paramaters LEARNING_RATE = 0.01 BATCH_SIZE = 128 N_EPOCHS = 30 N_TRAIN = 6000 N_TEST = 10000 # Read in data MNIST_FOLDER = '../data/mnist' utils.download_mnist(MNIST_FOLDER) train, val, test = utils.read_mnist(MNIST_FOLDER, flatten=True) # Create train and test dataset train_data = tf.data.Dataset.from_tensor_slices(train) train_data = train_data.shuffle(10000) test_data = tf.data.Dataset.from_tensor_slices(test) # Process the data in batches train_data = train_data.batch(BATCH_SIZE) test_data = test_data.batch(BATCH_SIZE) # Create Iterator to get samples from the two dataset iterator = tf.data.Iterator.from_structure(train_data.output_types, train_data.output_shapes) img, label = iterator.get_next()
def main(): parser = argparse.ArgumentParser(description='Chainer example: MNIST') parser.add_argument('--batchsize', '-b', type=int, default=100, help='Number of images in each mini-batch') parser.add_argument('--epochs', '-e', type=int, default=20, help='Number of sweeps over the dataset to train') parser.add_argument('--output_dir', '-o', default='./outputs', help='Directory to output the result') parser.add_argument( '--gpu_id', '-g', default=0, help='ID of the GPU to be used. Set to -1 if you use CPU') args = parser.parse_args() # Download the MNIST data if you haven't downloaded it yet train, test = download_mnist() gpu_id = args.gpu_id batchsize = args.batchsize epochs = args.epochs run.log('Batch size', np.int(batchsize)) run.log('Epochs', np.int(epochs)) train_iter = iterators.SerialIterator(train, batchsize) test_iter = iterators.SerialIterator(test, batchsize, repeat=False, shuffle=False) model = MyNetwork() if gpu_id >= 0: # Make a specified GPU current chainer.backends.cuda.get_device_from_id(0).use() model.to_gpu() # Copy the model to the GPU # Choose an optimizer algorithm optimizer = optimizers.MomentumSGD(lr=0.01, momentum=0.9) # Give the optimizer a reference to the model so that it # can locate the model's parameters. optimizer.setup(model) while train_iter.epoch < epochs: # ---------- One iteration of the training loop ---------- train_batch = train_iter.next() image_train, target_train = concat_examples(train_batch, gpu_id) # Calculate the prediction of the network prediction_train = model(image_train) # Calculate the loss with softmax_cross_entropy loss = F.softmax_cross_entropy(prediction_train, target_train) # Calculate the gradients in the network model.cleargrads() loss.backward() # Update all the trainable parameters optimizer.update() # --------------------- until here --------------------- # Check the validation accuracy of prediction after every epoch if train_iter.is_new_epoch: # If this iteration is the final iteration of the current epoch # Display the training loss print('epoch:{:02d} train_loss:{:.04f} '.format( train_iter.epoch, float(to_cpu(loss.array))), end='') test_losses = [] test_accuracies = [] while True: test_batch = test_iter.next() image_test, target_test = concat_examples(test_batch, gpu_id) # Forward the test data prediction_test = model(image_test) # Calculate the loss loss_test = F.softmax_cross_entropy(prediction_test, target_test) test_losses.append(to_cpu(loss_test.array)) # Calculate the accuracy accuracy = F.accuracy(prediction_test, target_test) accuracy.to_cpu() test_accuracies.append(accuracy.array) if test_iter.is_new_epoch: test_iter.epoch = 0 test_iter.current_position = 0 test_iter.is_new_epoch = False test_iter._pushed_position = None break val_accuracy = np.mean(test_accuracies) print('val_loss:{:.04f} val_accuracy:{:.04f}'.format( np.mean(test_losses), val_accuracy)) run.log("Accuracy", np.float(val_accuracy)) serializers.save_npz(os.path.join(args.output_dir, 'model.npz'), model)
preds = (zip(x, y) >> vec2img >> build_pred_batch >> network.predict() >> nf.Map(np.argmax) >> nf.Collect()) (zip(x, y, preds) >> vec2img >> filter_error >> make_label >> view_image >> nf.Consume()) def view_augmented_images(x, y, n=10): """Show n augmented images""" view_image = nm.ViewImageAnnotation(0, 1, pause=1) zip(x, y) >> vec2img >> augment >> nf.Take(n) >> view_image >> nf.Consume() if __name__ == '__main__': print('loading data...') filepath = download_mnist() x_train, y_train, x_test, y_test = load_mnist(filepath) print('creating model...') device = 'cuda:0' if torch.cuda.is_available() else 'cpu' model = Model(device) network = PytorchNetwork(model) network.load_weights() network.print_layers((1, 28, 28)) print('training ...') train(network, x_train, y_train, epochs=3) network.save_weights() print('evaluating ...') print('train acc:', evaluate(network, x_train, y_train))