def main(): # Load dataset data = datasets.load_iris() x = data.data y = data.target x_train, y_train, x_test, y_test = dataset.split_train_test_dataset( x, y, split_size=0.6, shuffle=True) # ClassificationDecisionTree tree = ClassificationDecisionTree() tree.train(x_train, y_train) pruned_tree = tree.prune() y_pred = tree.predict(x_test) y_pred_pruned = pruned_tree.predict(x_test) print("Classification result: {}".format( evaluation.accuracy(y_pred, y_test))) print("Classification result pruned: {}".format( evaluation.accuracy(y_pred_pruned, y_test))) # Sklearn tree clf = sktree.DecisionTreeClassifier() clf = clf.fit(x_train, y_train) y_pred = clf.predict(x_test) print("Scikit-learn classification result: {}".format( evaluation.accuracy(y_pred, y_test)))
def test_accuracy(self): # 100% accuracy y_pred = np.asarray([1, 0, 0, 0, 1, 1, 0, 1]) y_true = np.asarray([1, 0, 0, 0, 1, 1, 0, 1]) self.assertEqual(evaluation.accuracy(y_pred, y_true), 1) # 50% accuracy y_pred = np.asarray([1, 0, 0, 0, 1, 1, 0, 1]) y_true = np.asarray([1, 0, 1, 0, 0, 1, 1, 0]) self.assertEqual(evaluation.accuracy(y_pred, y_true), 0.5) # 0% accurancy y_pred = np.asarray([1, 0, 0, 0, 1, 1, 0, 1]) y_true = np.asarray([0, 1, 1, 1, 0, 0, 1, 0]) self.assertEqual(evaluation.accuracy(y_pred, y_true), 0)
def main(): # Load dataset data = datasets.load_digits() x = data.data y = data.target x_train, y_train, x_test, y_test = dataset.split_train_test_dataset(x, y, split_size=0.6, shuffle=True) # GaussianNaiveBayes cls = GaussianNaiveBayes() cls.train(x_train, y_train) y_pred = cls.predict(x_test) print("Classification result: {}".format(evaluation.accuracy(y_pred, y_test))) # Scikit-learn GaussianNaiveBayes cls = skbayes.GaussianNB() y_pred = cls.fit(x_train, y_train).predict(x_test) print("Scikit-learn classification result: {}".format(evaluation.accuracy(y_pred, y_test)))
def main(): # Load dataset, from http://scikit-learn.org/stable/modules/tree.html#regression x_train = [[0., 0.], [2., 2.]] y_train = [0.5, 2.5] x_test = [[1., 1.]] y_test = [0.5] # RegressionDecisionTree tree = RegressionDecisionTree(minimum_sample_count=1) tree.train(x_train, y_train) y_pred = tree.predict(x_test) print("Regression result: {}".format(evaluation.accuracy(y_pred, y_test))) # Sklearn tree clf = sktree.DecisionTreeRegressor() clf = clf.fit(x_train, y_train) y_pred = clf.predict(x_test) print("Scikit-learn Regression result: {}".format( evaluation.accuracy(y_pred, y_test)))
def main(): # Load dataset data = datasets.load_iris() x = data.data y = data.target x_train, y_train, x_test, y_test = dataset.split_train_test_dataset( x, y, split_size=0.6, shuffle=True) clf = KNearestNeighbors() y_pred = clf.predict(x_train, y_train, x_test) print("Classification result: {}".format( evaluation.accuracy(y_pred, y_test))) # Sklearn KNeighborsClassifier clf = skneighbors.KNeighborsClassifier(n_neighbors=2) clf.fit(x_train, y_train) y_pred = clf.predict(x_test) print("Scikit-learn classification result: {}".format( evaluation.accuracy(y_pred, y_test)))
def validate(val_loader, model, criterion, num_classes, debug=False, flip=True): batch_time = AverageMeter() data_time = AverageMeter() losses = AverageMeter() acces = AverageMeter() distes = AverageMeter() # predictions predictions = torch.Tensor(val_loader.dataset.__len__(), num_classes, 2) # switch to evaluate mode model.eval() gt_win, pred_win = None, None end = time.time() bar = Bar('Processing', max=len(val_loader)) for i, (inputs, target) in enumerate(val_loader): # measure data loading time data_time.update(time.time() - end) target = target.cuda(async=True) with torch.no_grad(): input_var = torch.autograd.Variable(inputs.cuda()) target_var = torch.autograd.Variable(target) # compute output output = model(input_var) score_map = output[-1].data.cpu() if flip: flip_input_var = torch.autograd.Variable( torch.from_numpy(fliplr(inputs.clone().numpy())).float().cuda(), volatile=True ) flip_output_var = model(flip_input_var) flip_output = flip_back(flip_output_var[-1].data.cpu()) score_map += flip_output loss = 0 for o in output: loss += criterion(o[:,:21,:,:], target_var) acc, dist = accuracy(score_map[:,:21,:,:].contiguous(), target.cpu(), idx) # generate predictions # preds = final_preds(score_map, meta['center'], meta['scale'], [64, 64]) preds = score_map # for n in range(score_map.size(0)): # predictions[meta['index'][n], :, :] = preds[n, :, :] # print(debug) if debug: gt_batch_img = batch_with_heatmap(inputs, target) pred_batch_img = batch_with_heatmap(inputs, score_map) sz = tuple([x * 4 for x in gt_batch_img[:,:,0].shape]) gt_batch_img = cv2.resize(gt_batch_img,(sz[1],sz[0]),) pred_batch_img = cv2.resize(pred_batch_img, (sz[1],sz[0])) if not gt_win or not pred_win: # plt.imshow(gt_batch_img) plt.subplot(121) gt_win = plt.imshow(gt_batch_img[:,:,::-1]) plt.subplot(122) pred_win = plt.imshow(pred_batch_img[:,:,::-1]) else: gt_win.set_data(gt_batch_img) pred_win.set_data(pred_batch_img) plt.savefig("./tmp/" + str(i) + ".png", dpi = 1000, bbox_inches='tight') # measure accuracy and record loss losses.update(loss.item(), inputs.size(0)) acces.update(acc[0], inputs.size(0)) distes.update(dist[0], inputs.size(0)) # measure elapsed time batch_time.update(time.time() - end) end = time.time() # plot progress bar.suffix = '({batch}/{size}) Data: {data:.1f}s | Batch: {bt:.1f}s | Total: {total:} | ETA: {eta:} | Loss: {loss:.4f} | Acc: {acc: .4f} | Dist {dist:.3f}'.format( batch=i + 1, size=len(val_loader), data=data_time.val, bt=batch_time.avg, total=bar.elapsed_td, eta=bar.eta_td, loss=losses.avg, acc=acces.avg, dist=distes.avg ) bar.next() bar.finish() return losses.avg, acces.avg, predictions
def train(train_loader, model, criterion, optimizer, debug=False, flip=True): batch_time = AverageMeter() data_time = AverageMeter() losses = AverageMeter() acces = AverageMeter() distes = AverageMeter() # switch to train mode model.train() end = time.time() gt_win, pred_win = None, None bar = Bar('Processing', max=len(train_loader)) for i, (inputs, target) in enumerate(train_loader): # measure data loading time data_time.update(time.time() - end) input_var = torch.autograd.Variable(inputs.cuda()) target_var = torch.autograd.Variable(target.cuda(async=True)) # compute output output = model(input_var) score_map = output[-1].data.cpu() loss = criterion(output[0], target_var) for j in range(1, len(output)): loss += criterion(output[j], target_var) acc, dist = accuracy(score_map, target, idx) if debug: # visualize groundtruth and predictions gt_batch_img = batch_with_heatmap(inputs, target) pred_batch_img = batch_with_heatmap(inputs, score_map) if not gt_win or not pred_win: ax1 = plt.subplot(121) ax1.title.set_text('Groundtruth') gt_win = plt.imshow(gt_batch_img[:,:,::-1]) ax2 = plt.subplot(122) ax2.title.set_text('Prediction') pred_win = plt.imshow(pred_batch_img[:,:,::-1]) else: gt_win.set_data(gt_batch_img) pred_win.set_data(pred_batch_img) plt.plot() plt.pause(.5) # measure accuracy and record loss losses.update(loss.item(), inputs.size(0)) acces.update(acc[0], inputs.size(0)) distes.update(dist[0], inputs.size(0)) # compute gradient and do SGD step optimizer.zero_grad() loss.backward() optimizer.step() # measure elapsed time batch_time.update(time.time() - end) end = time.time() # plot progress bar.suffix = '({batch}/{size}) Data: {data:.1f}s | Batch: {bt:.1f}s | Total: {total:} | ETA: {eta:} | Loss: {loss:.4f} | Acc: {acc: .4f} | Dist {dist:.3f}'.format( batch=i + 1, size=len(train_loader), data=data_time.val, bt=batch_time.avg, total=bar.elapsed_td, eta=bar.eta_td, loss=losses.avg, acc=acces.avg, dist=distes.avg ) bar.next() bar.finish() return losses.avg, acces.avg