def gen_training_accuracy(args): # load data and model params = utils.load_params(args.model_dir) ckpt_dir = os.path.join(args.model_dir, 'checkpoints') ckpt_paths = [int(e[11:-3]) for e in os.listdir(ckpt_dir) if e[-3:] == ".pt"] ckpt_paths = np.sort(ckpt_paths) # csv headers = ["epoch", "acc_train", "acc_test"] csv_path = utils.create_csv(args.model_dir, 'accuracy.csv', headers) for epoch, ckpt_paths in enumerate(ckpt_paths): if epoch % 5 != 0: continue net, epoch = tf.load_checkpoint(args.model_dir, epoch=epoch, eval_=True) # load data train_transforms = tf.load_transforms('test') trainset = tf.load_trainset(params['data'], train_transforms, train=True) trainloader = DataLoader(trainset, batch_size=500, num_workers=4) train_features, train_labels = tf.get_features(net, trainloader, verbose=False) test_transforms = tf.load_transforms('test') testset = tf.load_trainset(params['data'], test_transforms, train=False) testloader = DataLoader(testset, batch_size=500, num_workers=4) test_features, test_labels = tf.get_features(net, testloader, verbose=False) acc_train, acc_test = svm(args, train_features, train_labels, test_features, test_labels) utils.save_state(args.model_dir, epoch, acc_train, acc_test, filename='accuracy.csv') print("Finished generating accuracy.")
# train/test pass print("Forward pass - train features") Z_train = model(X_train, y_train) utils.save_loss(model.loss_dict, model_dir, "train") print("Forward pass - test features") Z_test = model(X_test) utils.save_loss(model.loss_dict, model_dir, "test") # save features utils.save_features(model_dir, "X_train", X_train, y_train) utils.save_features(model_dir, "X_test", X_test, y_test) utils.save_features(model_dir, "Z_train", Z_train, y_train) utils.save_features(model_dir, "Z_test", Z_test, y_test) # evaluation train _, acc_svm = evaluate.svm(Z_train, y_train, Z_train, y_train) acc_knn = evaluate.knn(Z_train, y_train, Z_train, y_train, k=5) acc_svd = evaluate.nearsub(Z_train, y_train, Z_train, y_train, n_comp=1) acc = {"svm": acc_svm, "knn": acc_knn, "nearsub-svd": acc_svd} utils.save_params(model_dir, acc, name="acc_train.json") # evaluation test _, acc_svm = evaluate.svm(Z_train, y_train, Z_test, y_test) acc_knn = evaluate.knn(Z_train, y_train, Z_test, y_test, k=5) acc_svd = evaluate.nearsub(Z_train, y_train, Z_test, y_test, n_comp=1) acc = {"svm": acc_svm, "knn": acc_knn, "nearsub-svd": acc_svd} utils.save_params(model_dir, acc, name="acc_test.json") # plot plot.plot_combined_loss(model_dir) plot.plot_heatmap(X_train, y_train, "X_train", model_dir)