labels.append(label) f.create_dataset('images', data=np.array(images)) f.create_dataset('labels', data=np.array(labels)) f.attrs['index_matrix'] = index_matrix f.attrs['class_names'] = np.array( aid.classes, dtype=h5py.special_dtype(vlen=str)) # load square aid f = h5py.File(data_directory + '/aid' + str(resize_size[0]) + '.h5', 'r') data = f['images'][()] labels = f['labels'][()] class_names = f.attrs['class_names'] f.close() # Normalize data data = utils.normalize(data) data = data.reshape(data.shape[0], data.shape[1], resize_size[0], resize_size[1]) # Datasets dataset = utils.NumpyDataset(data, labels, transform=utils.NumpyToTensor()) # Run the experiments for seed in seeds: # Data loaders logger.info('Split data with seed {}'.format(seed)) torch.manual_seed(seed) np.random.seed(seed) train_indices = [] val_indices = []
index_matrix = index_matrix.reshape(train_data_all.shape[2], train_data_all.shape[3]) train_data_all = train_data_all.reshape(train_data_all.shape[0], train_data_all.shape[1], -1) train_labels_all = train_set.train_labels test_set = datasets.CIFAR10(data_directory, train=False) test_data = test_set.test_data.transpose(0, 3, 1, 2).astype(np.float32) test_data = test_data.reshape(test_data.shape[0], test_data.shape[1], -1) test_labels = test_set.test_labels index_matrix.unsqueeze_(0) index_matrix.unsqueeze_(0) # Normalize data train_data_all = utils.normalize(train_data_all) test_data = utils.normalize(test_data) # Whitening data logger.info('Computing whitening matrices...') train_data_all_flat = train_data_all.reshape(train_data_all.shape[0], -1).T test_data_flat = test_data.reshape(test_data.shape[0], -1).T pca_all = utils.PCA(D=train_data_all_flat, n_components=train_data_all_flat.shape[1]) logger.info('Whitening data...') train_data_all_flat = pca_all.transform(D=train_data_all_flat, whiten=True, ZCA=True) train_data_all = train_data_all_flat.T.reshape(train_data_all.shape[0:2] + (-1, ))