def check_classifier(with_external_picture: bool): bc = BinClassifier() bc.load(classifier_name="classifier_without_examples") if not with_external_picture: (x_train, y_train), (x_test, y_test) = get_dataset() test(bc, x_test, y_test) else: test_all_local(bc, full_res=False)
def classify_outputs(input, outputs): '''This classifies program evaluation outputs and returns a dictionary mapping each program to the probability its output is correct. Arguments: - input (str): The input string used for each program evaluation - outputs (list): a list of evaluated outputs for each program Returns: - (list) an ordered list of outputs by highest probability that the output was correct. ''' clist=[] clist=classifier.test(input,outputs) o=outputs[clist.index(max(clist))] d=dict(zip(outputs,clist)) #print(d) a1 = sorted(d.items(),key = lambda x:x[1],reverse = True) #print(a1) sample_key_list = [] for item in a1: sample_key_list.append(item[0]) #print(sample_key_list) return sample_key_list
def test(): classfier_name = request.forms.get('classfier_name') classfier_dump = request.forms.get('classfier') features_train, labels_train = wtf.getArrays() clf = pickle.loads(classfier_dump) preditions, accuracy, recall, precision = classifier.test(clf = clf, features = features_train, labels = labels_train) return data1+data2
def test(): callback = request.GET.get('callback') classifier_name = request.GET.get('classifier_name') classifier_dump = request.GET.get('classifier') collection_id = request.GET.get('collection_id') print("Params :") print(classifier_name) features_train, labels_train = wtf.getArrays(collection_id) clf = pickle.loads(classifier_dump) preditions, accuracy, recall, precision = classifier.test(clf = clf, features = features_train, labels = labels_train) print("Test :") print("accuracy" + accuracy) print("precision" + precision) print("recall" + recall) data1 = test_data_to_send(classifier_id, 0, " ", precision, accuracy, recall) post.send("http://localhost:8080/dataprocessing/rest-api","/result_test_classifier","",data1) return '{0}({1})'.format(callback, {'a':1, 'b':2})
def test(): callback = request.GET.get('callback') classifier_name = request.GET.get('classifier_name') classifier_dump = request.GET.get('classifier') collection_id = request.GET.get('collection_id') print("Params :") print(classifier_name) features_train, labels_train = wtf.getArrays(collection_id) clf = pickle.loads(classifier_dump) preditions, accuracy, recall, precision = classifier.test( clf=clf, features=features_train, labels=labels_train) print("Test :") print("accuracy" + accuracy) print("precision" + precision) print("recall" + recall) data1 = test_data_to_send(classifier_id, 0, " ", precision, accuracy, recall) post.send("http://localhost:8080/dataprocessing/rest-api", "/result_test_classifier", "", data1) return '{0}({1})'.format(callback, {'a': 1, 'b': 2})
def _test_custom_classifier(classifier): util.debug("Testing classifier with test data") stats = {} for cls in CLASSES: stats[cls] = { 'total': 0, 'correct': 0, 'incorrect': 0 } # We'll throw away the class it yields for img_pixels, cls in util._test_data(CLASSES): classification, score = classifier.test(img_pixels) # util.debug("Actual: {}, Classification: {}, Score: {}".format(cls, classification, score)) stats[cls]['total'] += 1 if classification == cls: stats[cls]['correct'] += 1 else: stats[cls]['incorrect'] += 1 for cls in stats: stats[cls]['accuracy'] = stats[cls]['correct'] / stats[cls]['total'] return stats
import torch import os import torchvision from model import TinyModel from torchvision import transforms from torch.utils.data import DataLoader from torchvision.datasets import ImageFolder from classifier import test if __name__ == "__main__": model = TinyModel() model.load_state_dict(torch.load("checkpoints/epoch_90_0.955.pt")) test_trans = transforms.Compose([ transforms.ToTensor(), transforms.Lambda(lambda x: x.repeat(1, 1, 1)), transforms.Normalize([0.4100, 0.4100, 0.4100], [0.1165, 0.1165, 0.1165]) ]) test_datasets = ImageFolder(os.path.join("ROI_data", "test"), transform=test_trans) print(test_datasets.classes) test_dataloader = DataLoader(test_datasets, batch_size=32, shuffle=False) criterion = torch.nn.CrossEntropyLoss() acc = test(model, test_dataloader, criterion)
logger.critical("Uncaught exception", exc_info=(exc_type, exc_value, exc_traceback)) sys.excepthook = handle_exception if device == 'cpu': os.environ['CUDA_VISIBLE_DEVICES'] = '' logger.debug("Using CPU only") elif device == 'gpu0': os.environ['CUDA_VISIBLE_DEVICES'] = '0' logger.debug("Using GPU 0") elif device == 'gpu1': os.environ['CUDA_VISIBLE_DEVICES'] = '1' logger.debug("Using GPU 1") elif device == 'gpu': os.environ['CUDA_VISIBLE_DEVICES'] = '0,1' logger.debug("Using GPU 0 and 1") test(logger=logger, dataset=dataset, t=time_length, job_uuid=job_uuid, epoch=epoch, val_loss=val_loss, visualize_score=True, visualize_frame=False) logger.info("Job {} ({}) has finished testing.".format(job_uuid, dataset))
def main(params, input_dir="data", output_dir="out", cuda_enable=False): for data_file in ["zoomeddata_32_64_64.hdf5"]: d_train, d_dev, d_test = load_data(os.path.join(input_dir, data_file), img_size=params["img_size"]) # build the vocabulary for images vocab = { "subfamily": [], "tribe": [], "genus": [] } for d in d_train: vocab["subfamily"].append(d[1][0]) vocab["tribe"].append(d[1][1]) vocab["genus"].append(d[1][2]) for k in vocab: vocab[k] = list(set(vocab[k])) mean_image = np.mean([d[0] for d in d_train]) std_image = np.std([d[0] for d in d_train]) def normalize_fn(X): return ((X - mean_image) / 255.) def process_data(data, tree_label=False): rand_indices = np.random.permutation(len(data)) if not tree_label: data_out = [(normalize_fn(data[i][0]), vocab["genus"].index(data[i][1][2])) for i in rand_indices] else: data_out = [(normalize_fn(data[i][0]), np.array([vocab["subfamily"].index(data[i][1][0]), vocab["tribe"].index(data[i][1][1]), vocab["genus"].index(data[i][1][2])])) for i in rand_indices] return data_out if params["add_synthetic_data"]: synthesized = gen_synthetic(d_train) print("{} synthetic examples added.".format(len(synthesized))) else: synthesized = [] tree_label = params["tree_prediction"] train_data = batch_data(process_data(d_train + synthesized, tree_label), batch_size=params["batch_size"]) dev_data = batch_data(process_data(d_dev, tree_label), batch_size=params["batch_size"]) test_data = batch_data(process_data(d_test, tree_label), batch_size=params["batch_size"]) #net = LinearModel(params, len(vocab["genus"])) #net = SimpleNet(params, len(vocab["genus"])) for model_class in [LinearModel, SimpleNet, SimpleConvNet, ConvNet]: print(">>>>>>>>>> {} <<<<<<<<<<".format(model_class)) if tree_label: num_classes = len(vocab["subfamily"]) + len(vocab["tribe"]) + len(vocab["genus"]) else: num_classes = len(vocab["genus"]) net = model_class(params, num_classes) train_acc, dev_acc = classifier.train(net, train_data, dev_data, vocab, params, cuda_enable) for test_index in [0, 1, 2]: test_acc = classifier.test(net, test_data, vocab, cuda_enable, tree_label, test_index) print("[Test accuracy for label {} is {}]".format(test_index, test_acc)) ## show images #if examples_to_demo > 0: # show_data = [d_dev[i] for i in np.random.randint(len(dev_data), size=examples_to_demo)] # show_data = batch_data(process_data(show_data), batch_size=1) # classifier.test(net, show_data, cuda_enable, show_example=True) with open(os.path.join(output_dir, "result_{}.log".format(model_class.__name__)), "w") as f: f.write("train_acc, dev_acc\n") for i in range(len(train_acc)): f.write("{:.3f}".format(train_acc[i]) + ", " + "{:.3f}".format(dev_acc[i]) + "\n")
config["max_image_size"], config["canvas"], test_set["random"], model) data_test = test_set["genuines"] + test_set["skilled"] + test_set[ "random"] test_classes = [] for i in test_set["genuines"]: test_classes.append(1) for i in test_set["skilled"]: test_classes.append(0) for i in test_set["random"]: test_classes.append(0) test_sets.append(data_test) partial_results = classifier.test(clf_mlp, test_sets, test_classes, test_config["genuine"], test_config["skilled"], test_config["random"]) results["mlp"][0] += (partial_results[0]) results["mlp"][1] += (partial_results[1]) results["mlp"][2] += (partial_results[2]) results["mlp"][3] += (partial_results[3]) results["mlp"][4] += (partial_results[4]) results["mlp"][5] += (partial_results[5]) results["mlp"][6] += (partial_results[6]) partial_results = classifier.test(clf_svm_linear, test_sets, test_classes, test_config["genuine"], test_config["skilled"], test_config["random"]) results["svm_linear"][0] += (partial_results[0]) results["svm_linear"][1] += (partial_results[1]) results["svm_linear"][2] += (partial_results[2])
Meant to be deployed with Apache Spark """ import csv, classifier, db, spark, time, write import numpy as np TRAIN_FILENAME = './data/smtrain.csv' TEST_FILENAME = './data/smtest.csv' SKLEARN_PATH = './data/models/sklearn' SPARK_PATH = './data/models/spark' if __name__ == '__main__': # read the training data xtrain, ytrain = classifier.readdata(TRAIN_FILENAME) xtest, ytest = classifier.readdata(TEST_FILENAME) countvect, transformer, model = classifier.train(xtrain, ytrain) acc, model = classifier.test(model, xtest, ytest, countvect, transformer) print('Sklearn acc: {}'.format(acc)) print('Saving sklearn') classifier.save(SKLEARN_PATH, model, countvect, transformer) countvect, transformer, model = classifier.load(SKLEARN_PATH) print(classifier.predict(model, ['i like something'], countvect, transformer)) # start looping over tweets # for tweet in db.gettweets(): # text = [tweet['text']] # prediction = clf.predict(classifier.transform(text)) # print(text, ' ^^ ', prediction) # time.sleep(0.5)
arr = [(86, 0.001149, '6ab962e0-b3bb-47d1-a513-4d68bdd2e08c', 0)] arr = [(64, 0.000858, 'ae14934b-6961-45b1-b712-00381beef7dc', 0)] arr = [(11, 0.003981, 'c0d6c0db-1a41-4089-9f05-ce9aaf7f9ccd', 0)] arr = [(100, 0.003988, '968b2b8d-ba69-49fb-a27a-6d20787c4629', 0)] arr = [(100, 0.003504, 'd1e9c23c-e754-4027-be93-111069ea3b81', 0)] arr = [(298, 0.003159, 'f5846318-6990-4622-b584-ececf33a54d7', 0)] # UCSD Ped2 # arr = [(749, 0.000554, '537f1112-8ec0-44eb-a380-c3a6e5a3c698', 0)] # CUHK # arr = [(227, 0.000455, 'c51f37de-9c2c-4941-b881-5029044a4564', 0)] for item in arr: print("TESTING UUID", item[2]) print("###Testing epoch", item[0]) test(logger=logger, dataset=dataset, t=item[3], job_uuid=item[2], epoch=item[0], val_loss=item[1], visualize_score=True, visualize_frame=True) # n_videos = {'avenue': 21, 'enter': 6, 'exit': 4, 'UCSD_ped1': 36, 'UCSD_ped2': 12} # save_path = os.path.join(job_folder, 'result') # auc_overall, eer_overall = calc_auc_overall(logger, dataset, n_videos[dataset], save_path) # auc_overall, eer_overall = calc_auc_pixel(logger, dataset, n_videos[dataset], save_path) logger.info("Job {} ({}) has finished testing.".format(job_uuid, dataset))