def load_data(args, data_partition="train"): X, Y = None, None if args.dataset == 'ImageNet': shape, classes = (args.box_sz, args.box_sz, args.box_chls), args.num_classes X, Y = load_imagenet(args.batch_size, classes, shape, args.data_dir + "/" + data_partition) elif args.dataset == 'cifar10': X, Y = load_cifar10(args.num_classes) else: X, Y = load_mnist(args.num_classes) X = np.expand_dims(X, axis=3) return X, Y
def return_dataset(data, scale=False, usps=False, all_use='no'): if data == 'svhn': train_image, train_label, \ test_image, test_label = load_svhn() if data == 'mnist': train_image, train_label, \ test_image, test_label = load_mnist(scale=scale, usps=usps, all_use=all_use) print(train_image.shape) if data == 'synth': train_image, train_label, \ test_image, test_label = load_syntraffic() if data == 'gtsrb': train_image, train_label, \ test_image, test_label = load_gtsrb() return train_image, train_label, test_image, test_label
updates=updates) self.test_fn = theano.function([input_var], T.argmax(test_prediction, axis=1)) print "Model Compiled!" def initialize_model(self, data, n_samples=50000): for initializer in self.initializers: initializer(data, n_samples=n_samples) print "Model initialized!" if __name__ == '__main__': np.random.seed(12345) X_train, y_train, X_val, y_val, X_test, y_test = load_mnist() for pool_type in ['bof', 'spp']: model = LeNeT_Model(pooling=pool_type) if pool_type == 'bof': model.initialize_model(X_train, n_samples=50000) model.train_model(X_train, y_train, validation_data=X_val, validation_labels=y_val, n_iters=50, batch_size=256) print "Evaluated model = ", pool_type
def main(): """ The main function. """ #========================================================================= # Parse the arguments. Handle the parameters #========================================================================= args = parseArgs() num_epochs = args.num_epochs batchsize = args.batchsize hp_lambda = args.hp_lambda label_rate = args.label_rate label_mom = args.label_mom domain_rate = args.domain_rate domain_mom = args.domain_mom # Set up the naming information : data_name = 'MNISTRMat' model = 'ClassWiseCorrector' title = '{}-{}-lambda-{:.2e}'.format(data_name, model, hp_lambda) #========================================================================= # Load, Generate the datasets #========================================================================= # Load MNIST Dataset source_data = load_mnist(batchsize=batchsize) target_data = random_mat_dataset(source_data, normalize=True) domain_data = make_domain_dataset([source_data, target_data]) corrector_data = dict(target_data) corrector_data.update({ 'y_train': source_data['X_train'], 'y_val': source_data['X_val'], 'y_test': source_data['X_test'], 'labels': source_data['y_train'], 'batchsize': batchsize, }) #========================================================================= # Prepare the logger #========================================================================= # f_log = log_fname(title) logger = new_logger() # Print general information logger.info('Model: {}'.format(model)) logger.info('Data: {}'.format(data_name)) logger.info('Batchsize: {}'.format(batchsize)) logger.info('hp_lambda = {:.4e}'.format(hp_lambda)) #========================================================================= # Build the neural network architecture #========================================================================= # Prepare Theano variables for inputs and targets input_var = T.tensor3('inputs') target_var = T.tensor3('targets') shape = (batchsize, 28, 28) # Build the layers input_layer = lasagne.layers.InputLayer(shape=shape, input_var=input_var) src_layer = lasagne.layers.InputLayer(shape=shape, input_var=T.tensor3('src')) # feature = lasagne.layers.DenseLayer( # input_layer, # num_units=np.prod(shape[1:]), # nonlinearity=lasagne.nonlinearities.tanh, # # W=lasagne.init.Uniform(range=0.01, std=None, mean=0.0), # ) feature = lasagne.layers.DenseLayer( input_layer, num_units=np.prod(shape[1:]), nonlinearity=None, # W=lasagne.init.Uniform(range=0.01, std=None, mean=0.0), ) reshaper = lasagne.layers.ReshapeLayer(feature, (-1,) + shape[1:]) output_layer = reshaper # Compilation logger.info('Compiling functions') corrector_trainner = Trainner(squared_error_sgd_mom(output_layer, lr=label_rate, mom=label_mom, target_var=target_var), 'corrector',) corrector_trainner.preprocess = epoch_shuffle if hp_lambda != 0.0: domain_trainner = Trainner(adversarial([src_layer, output_layer], hp_lambda=hp_lambda, lr=domain_rate, mom=domain_mom), 'domain') #========================================================================= # Train the Neural Network #========================================================================= if hp_lambda != 0.0: stats = training([corrector_trainner, domain_trainner], [corrector_data, domain_data], num_epochs=num_epochs, logger=logger) else: stats = training([corrector_trainner,], [corrector_data,], num_epochs=num_epochs, logger=logger) #========================================================================= # Print, Plot, Save the final results #========================================================================= # Plot learning accuracy curve fig, ax = plt.subplots() ax.plot(stats['corrector valid loss'], label='source') ax.set_xlabel('epoch') ax.set_ylabel('loss') ax.set_title(title) handles, labels = ax.get_legend_handles_labels() ax.legend(handles, labels, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) fig.savefig('fig/'+title+'.png', bbox_inches='tight') fig.clf() # Clear plot window # Plot some sample images: fig = plt.figure() n_sample = 4 rand = np.random.RandomState() for n in range(n_sample): i = rand.randint(source_data['X_test'].shape[0]) sample_src = source_data['X_test'][i] sample_trg = target_data['X_test'][i] sample_corrected = corrector_trainner.output(target_data['X_test'][i][np.newaxis]) sample_corrected = np.array(sample_corrected).reshape((28,28)) ax = fig.add_subplot(n_sample, 3, n*3+1) ax.axis('off') ax.imshow(sample_src, cmap='Greys_r') ax.set_title('Source image') ax = fig.add_subplot(n_sample, 3, n*3+2) ax.axis('off') ax.imshow(sample_trg, cmap='Greys_r') ax.set_title('Target image') ax = fig.add_subplot(n_sample, 3, n*3+3) ax.axis('off') ax.imshow(sample_corrected, cmap='Greys_r') ax.set_title('Corrected image') fig.savefig('fig/{}-sample.png'.format(title)) plt.close(fig) # Clear plot window # Plot the weights of the corrector W = feature.W.get_value() plt.imshow(W, interpolation='nearest', cmap=plt.cm.coolwarm) plt.title(title) plt.colorbar() plt.tight_layout() plt.savefig('fig/{}-Weights.png'.format(title))
def get_data(): (x_train, t_train), (x_test, t_test) = load_mnist(normalize=True, flatten=True, one_hot_label=False) return x_test, t_test
import sys, os sys.path.append(os.curdir) from datasets.mnist import load_mnist import numpy as np from two_layer_net import TwoLayerNet (x_train, t_train), (x_test, t_test) = load_mnist(normalize=True, one_hot_label=True) iters_num = 10000 train_size = x_train.shape[0] batch_size = 100 learning_rate = 0.1 train_loss_list = [] train_acc_list = [] test_acc_list = [] iter_per_epoch = max(train_size / batch_size, 1) network = TwoLayerNet(input_size=784, hidden_size=50, output_size=10) for i in range(iters_num): batch_mask = np.random.choice(train_size, batch_size) x_batch = x_train[batch_mask] t_batch = t_train[batch_mask] grad = network.gradient(x_batch, t_batch) for key in ('W1', 'b1', 'W2', 'b2'):
import sys, os sys.path.append(os.curdir) from datasets.mnist import load_mnist # 最初の呼び出しは数分待ちます… (x_train, t_train), (x_test, t_test) = load_mnist(flatten=True, normalize=False) print(x_train.shape) # (60000, 784) print(t_train.shape) # (60000,) print(x_test.shape) # (10000, 784) print(t_test.shape) # (10000,)