def learn(self, epochs=8000, batch_size=10, lr=1e-4, lr_decay=1e-5): """ Traing the model :param epochs: Number of epochs :param batch_size: Batch size :param lr: Learning rate :param lr_decay: Learning rate decay rate :return: """ earlystopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=15, verbose=1, mode='auto', baseline=None, restore_best_weights=True) checkpointer = ModelCheckpoint(filepath=self.save_path, verbose=1, save_best_only=True) self.model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=lr, decay=lr_decay), metrics=['accuracy']) train_files, train_targets = load_dataset(self.image_path_train) valid_files, valid_targets = load_dataset(self.image_path_valid) # pre-process the data for Keras train_tensors = paths_to_tensor(train_files).astype('float32') / 255 valid_tensors = paths_to_tensor(valid_files).astype('float32') / 255 self.model.fit(train_tensors, train_targets, validation_data=(valid_tensors, valid_targets), epochs=epochs, batch_size=batch_size, callbacks=[checkpointer, earlystopping], verbose=1) self.trained = True
def learn(self, epochs=1000, batch_size=10, lr=1e-4, lr_decay=1e-6): """ Train the model :param epochs: number of epochs :param batch_size: batch size :param lr: learning rate :param lr_decay: learning rate decay rate :return: """ earlystopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=15, verbose=1, mode='auto', baseline=None, restore_best_weights=True) checkpointer = ModelCheckpoint(filepath=self.save_path, verbose=1, save_best_only=True) self.model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=lr, decay=lr_decay), metrics=['accuracy']) bottleneck_features = np.load(self.feature_path) train = bottleneck_features['train'] valid = bottleneck_features['valid'] _, train_targets = load_dataset(self.image_path_train) _, valid_targets = load_dataset(self.image_path_valid) self.model.fit(train, train_targets, validation_data=(valid, valid_targets), epochs=epochs, batch_size=batch_size, callbacks=[checkpointer, earlystopping], verbose=1) self.trained = True
def test(self): """ Test model in the test set :return: test accuracy """ self.load() test_files, test_targets = load_dataset(self.image_path_test) test_tensors = paths_to_tensor(test_files).astype('float32') / 255 predictions = [ np.argmax(self.model.predict(np.expand_dims(feature, axis=0))) for feature in test_tensors ] test_accuracy = 100 * np.sum( np.array(predictions) == np.argmax(test_targets, axis=1)) / len( predictions) print('{}, test accuracy: {:.4f}%'.format(self.name, test_accuracy)) return test_accuracy
def test(self): """ Test model on the test set :return: test accuracy """ self.load() bottleneck_features = np.load(self.feature_path) test = bottleneck_features['test'] _, test_targets = load_dataset(self.image_path_test) predictions = [ np.argmax(self.model.predict(np.expand_dims(feature, axis=0))) for feature in test ] test_accuracy = 100 * np.sum( np.array(predictions) == np.argmax(test_targets, axis=1)) / len( predictions) print('{}, test accuracy: {:.4f}%'.format(self.name, test_accuracy)) return test_accuracy
def main(args): Xtr, ytr, Xte, yte, num_classes = load_dataset(args.dataset) nas = MLPNAS(Xtr, ytr, num_classes, task_name=args.dataset) # Traverse MLPNAS's search space # and sample plausible architectures data = nas.search() get_top_n_architectures(args.n, num_classes) # Display Top n architectures # Extract best architecture best_model = nas.extract_best_model(data) # Fine-tune the best architecture on the training set # Xtr for another 50 epochs (unless it stops early) # # Assign the return to a variable which will # hold training's history in case you need to # # i.e.: best_model_history _ = nas.finetune_model(best_model, Xtr, ytr, validation_split=0.2, batch_size=64, shuffle=True, epochs=50, save=args.save) # from utils import plot_history # plot_history(best_model_history) tloss, tacc = MLPNAS.score(best_model, Xte, yte) print(f"NAS-made MLP test loss: {tloss}") print(f"NAS-made MLP test accuracy: {tacc}") # Test against sklearn MLP classifier mlp = MLPClassifier(activation="relu", solver="adam", alpha=0.01).fit(Xtr, ytr) print(f"\nScikit-learn MLP test accuracy: {mlp.score(Xte, yte)}") # Validation accuracies barplot get_accuracy_distribution(args.dataset)
args = parser.parse_args() device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") fmt = { 'time': '.3f' } logger = Logger('logs', base='../logs/', fmt=fmt) # Load data np.random.seed(args.data_seed) torch.manual_seed(args.data_seed) torch.cuda.manual_seed_all(args.data_seed) trainloader, testloader, data_shape = datautils.load_dataset(args.data, args.train_bs, args.test_bs, num_examples=args.num_examples, seed=args.data_seed, complexity=args.data_cmplx) if args.data_save: torch.save(testloader, '../data/testloader.pckl') torch.save(trainloader, '../data/trainloader.pckl') np.random.seed(args.seed) torch.manual_seed(args.seed) torch.cuda.manual_seed_all(args.seed) # Create model if args.prior == 'gmm': if args.gmm_k == 1 and args.prior_train_algo == 'no': prior = torch.distributions.MultivariateNormal(torch.zeros((2,)), torch.eye(2)) else: prior = distributions.GMM(k=args.gmm_k, dim=2, normalize=args.prior_train_algo == 'GD')
torch.set_num_threads(1) fmt = { 'time': '.3f', } logger = Logger('logs', base=args.root, fmt=fmt) # Load data np.random.seed(args.data_seed) torch.manual_seed(args.data_seed) torch.cuda.manual_seed_all(args.data_seed) trainloader, testloader, data_shape, bits = datautils.load_dataset( args.data, args.train_bs, args.test_bs, seed=args.data_seed, num_examples=args.num_examples, supervised=args.supervised, logs_root=args.root, sup_sample_weight=args.sup_sample_weight) # Seed for training process np.random.seed(args.seed) torch.manual_seed(args.seed) torch.cuda.manual_seed_all(args.seed) # Create model dim = int(np.prod(data_shape)) if args.ssl_dim == -1: args.ssl_dim = dim deep_prior = distributions.GaussianDiag(args.ssl_dim) shallow_prior = distributions.GaussianDiag(dim - args.ssl_dim)
parser.add_argument('--hid_dim', type=int, nargs='*', default=[]) args = parser.parse_args() device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") fmt = { 'time': '.3f', 'lr': '.1e', } logger = Logger('logs', base=args.root, fmt=fmt) # Load data np.random.seed(args.data_seed) torch.manual_seed(args.data_seed) torch.cuda.manual_seed_all(args.data_seed) trainloader, testloader, data_shape, bits = datautils.load_dataset( args.data, args.train_bs, args.test_bs, seed=args.data_seed, shuffle=False) # Seed for training process np.random.seed(args.seed) torch.manual_seed(args.seed) torch.cuda.manual_seed_all(args.seed) # Create model dim = int(np.prod(data_shape)) prior = distributions.GaussianDiag(dim).to(device) flow = utils.create_flow(args, data_shape) flow = torch.nn.DataParallel(flow.to(device)) model = flows.FlowPDF(flow, prior).to(device) if args.pretrained is not None and args.pretrained != '':
:return: detection result """ is_dog = self.dog_detector(img_file) if is_dog: return "dog", self.breed_predictor.predict_breed(img_file) is_human = self.face_detector(img_file) if is_human: return "human", self.breed_predictor.predict_breed(img_file) return None if __name__ == "__main__": n_test = 6 predictor = BreedPredictor() dog_files, dog_labels = load_dataset(os.path.join(image_path, 'test')) dog_labels = np.argmax(dog_labels, axis=1) human_files = load_face_files() dog_file_indices = np.random.choice(range(len(dog_files)), n_test//2, replace=False) dog_files = [(dog_files[idx], dog_labels[idx]) for idx in dog_file_indices] face_files = np.random.choice(human_files, n_test // 2, replace=False) face_files = [(ff, -1) for ff in face_files] image_files = dog_files + face_files images = [] res_strs = [] for image_f, label in image_files: res = predictor.predict_breed(image_f) img = mpimg.imread(image_f) images.append(img)