mirror of
https://github.com/aladdinpersson/Machine-Learning-Collection.git
synced 2026-02-20 13:50:41 +00:00
Initial commit
This commit is contained in:
68
ML_tests/LinearRegression_tests/LinearRegression_GD.py
Normal file
68
ML_tests/LinearRegression_tests/LinearRegression_GD.py
Normal file
@@ -0,0 +1,68 @@
|
||||
# Import folder where sorting algorithms
|
||||
import sys
|
||||
import unittest
|
||||
import numpy as np
|
||||
|
||||
# For importing from different folders
|
||||
# OBS: This is supposed to be done with automated testing,
|
||||
# hence relative to folder we want to import from
|
||||
sys.path.append("ML/algorithms/linearregression")
|
||||
# If run from local:
|
||||
# sys.path.append('../../ML/algorithms/linearregression')
|
||||
from linear_regression_gradient_descent import LinearRegression
|
||||
|
||||
|
||||
class TestLinearRegression_GradientDescent(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# test cases we want to run
|
||||
|
||||
self.linearReg = LinearRegression()
|
||||
self.X1 = np.array([[0, 1, 2]])
|
||||
self.y1 = np.array([[1, 2, 3]])
|
||||
self.W1_correct = np.array([[1, 1]]).T
|
||||
|
||||
self.X2 = np.array([[0, 1]])
|
||||
self.y2 = np.array([[1, 0]])
|
||||
self.W2_correct = np.array([[1, -1]]).T
|
||||
|
||||
self.X3 = np.array([[1, 2, 3], [1, 2, 4]])
|
||||
self.y3 = np.array([[5, 10, 18]])
|
||||
self.W3_correct = np.array([[0, 2, 3]]).T
|
||||
|
||||
self.X4 = np.array([[0, 0]])
|
||||
self.y4 = np.array([[0, 0]])
|
||||
self.W4_correct = np.array([[0, 0]]).T
|
||||
|
||||
self.X5 = np.array([[0, 1, 2, 3, 4, 5]])
|
||||
self.y5 = np.array([[0, 0.99, 2.01, 2.99, 4.01, 4.99]])
|
||||
self.W5_correct = np.array([[0, 1]]).T
|
||||
|
||||
def test_perfectpositiveslope(self):
|
||||
W = self.linearReg.main(self.X1, self.y1)
|
||||
boolean_array = np.isclose(W, self.W1_correct, atol=0.1)
|
||||
self.assertTrue(boolean_array.all())
|
||||
|
||||
def test_perfectnegativeslope(self):
|
||||
W = self.linearReg.main(self.X2, self.y2)
|
||||
boolean_array = np.isclose(W, self.W2_correct, atol=0.1)
|
||||
self.assertTrue(boolean_array.all())
|
||||
|
||||
def test_multipledimension(self):
|
||||
W = self.linearReg.main(self.X3, self.y3)
|
||||
boolean_array = np.isclose(W, self.W3_correct, atol=0.1)
|
||||
self.assertTrue(boolean_array.all())
|
||||
|
||||
def test_zeros(self):
|
||||
W = self.linearReg.main(self.X4, self.y4)
|
||||
boolean_array = np.isclose(W, self.W4_correct, atol=0.1)
|
||||
self.assertTrue(boolean_array.all())
|
||||
|
||||
def test_noisydata(self):
|
||||
W = self.linearReg.main(self.X5, self.y5)
|
||||
boolean_array = np.isclose(W, self.W5_correct, atol=0.1)
|
||||
self.assertTrue(boolean_array.all())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Running Linear Regression Normal Equation tests:")
|
||||
unittest.main()
|
||||
71
ML_tests/LinearRegression_tests/LinearRegression_normal.py
Normal file
71
ML_tests/LinearRegression_tests/LinearRegression_normal.py
Normal file
@@ -0,0 +1,71 @@
|
||||
# Import folder where sorting algorithms
|
||||
import sys
|
||||
import unittest
|
||||
import numpy as np
|
||||
|
||||
# For importing from different folders
|
||||
# OBS: This is supposed to be done with automated testing,
|
||||
# hence relative to folder we want to import from
|
||||
sys.path.append("ML/algorithms/linearregression")
|
||||
|
||||
# If run from local:
|
||||
# sys.path.append('../../ML/algorithms/linearregression/')
|
||||
from linear_regression_normal_equation import linear_regression_normal_equation
|
||||
|
||||
|
||||
class TestLinearRegression_NormalEq(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# test cases we want to run
|
||||
self.X1 = np.array([[0, 1, 2]]).T
|
||||
self.y1 = np.array([1, 2, 3])
|
||||
self.W1_correct = np.array([[1, 1]])
|
||||
|
||||
self.X2 = np.array([[0, 1]]).T
|
||||
self.y2 = np.array([1, 0])
|
||||
self.W2_correct = np.array([[1, -1]])
|
||||
|
||||
self.X3 = np.array([[1, 2, 3], [1, 2, 4]]).T
|
||||
self.y3 = np.array([5, 10, 18])
|
||||
self.W3_correct = np.array([[0, 2, 3]])
|
||||
|
||||
self.X4 = np.array([[0, 0]]).T
|
||||
self.y4 = np.array([0, 0])
|
||||
self.W4_correct = np.array([[0, 0]])
|
||||
|
||||
self.X5 = np.array([[0, 1, 2, 3, 4, 5]]).T
|
||||
self.y5 = np.array([0, 0.99, 2.01, 2.99, 4.01, 4.99])
|
||||
self.W5_correct = np.array([[0, 1]])
|
||||
|
||||
def test_perfectpositiveslope(self):
|
||||
W = linear_regression_normal_equation(self.X1, self.y1)
|
||||
print(W.shape)
|
||||
print(self.W1_correct.shape)
|
||||
boolean_array = np.isclose(W, self.W1_correct)
|
||||
self.assertTrue(boolean_array.all())
|
||||
|
||||
def test_perfectnegativeslope(self):
|
||||
W = linear_regression_normal_equation(self.X2, self.y2)
|
||||
boolean_array = np.isclose(W, self.W2_correct)
|
||||
self.assertTrue(boolean_array.all())
|
||||
|
||||
def test_multipledimension(self):
|
||||
W = linear_regression_normal_equation(self.X3, self.y3)
|
||||
print(W)
|
||||
print(self.W3_correct)
|
||||
boolean_array = np.isclose(W, self.W3_correct)
|
||||
self.assertTrue(boolean_array.all())
|
||||
|
||||
def test_zeros(self):
|
||||
W = linear_regression_normal_equation(self.X4, self.y4)
|
||||
boolean_array = np.isclose(W, self.W4_correct)
|
||||
self.assertTrue(boolean_array.all())
|
||||
|
||||
def test_noisydata(self):
|
||||
W = linear_regression_normal_equation(self.X5, self.y5)
|
||||
boolean_array = np.isclose(W, self.W5_correct, atol=1e-3)
|
||||
self.assertTrue(boolean_array.all())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Running Linear Regression Normal Equation tests:")
|
||||
unittest.main()
|
||||
118
ML_tests/Object_detection_tests/iou_test.py
Normal file
118
ML_tests/Object_detection_tests/iou_test.py
Normal file
@@ -0,0 +1,118 @@
|
||||
import sys
|
||||
import unittest
|
||||
import torch
|
||||
|
||||
sys.path.append("ML/Pytorch/object_detection/metrics/")
|
||||
from iou import intersection_over_union
|
||||
|
||||
|
||||
class TestIntersectionOverUnion(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# test cases we want to run
|
||||
self.t1_box1 = torch.tensor([0.8, 0.1, 0.2, 0.2])
|
||||
self.t1_box2 = torch.tensor([0.9, 0.2, 0.2, 0.2])
|
||||
self.t1_correct_iou = 1 / 7
|
||||
|
||||
self.t2_box1 = torch.tensor([0.95, 0.6, 0.5, 0.2])
|
||||
self.t2_box2 = torch.tensor([0.95, 0.7, 0.3, 0.2])
|
||||
self.t2_correct_iou = 3 / 13
|
||||
|
||||
self.t3_box1 = torch.tensor([0.25, 0.15, 0.3, 0.1])
|
||||
self.t3_box2 = torch.tensor([0.25, 0.35, 0.3, 0.1])
|
||||
self.t3_correct_iou = 0
|
||||
|
||||
self.t4_box1 = torch.tensor([0.7, 0.95, 0.6, 0.1])
|
||||
self.t4_box2 = torch.tensor([0.5, 1.15, 0.4, 0.7])
|
||||
self.t4_correct_iou = 3 / 31
|
||||
|
||||
self.t5_box1 = torch.tensor([0.5, 0.5, 0.2, 0.2])
|
||||
self.t5_box2 = torch.tensor([0.5, 0.5, 0.2, 0.2])
|
||||
self.t5_correct_iou = 1
|
||||
|
||||
# (x1,y1,x2,y2) format
|
||||
self.t6_box1 = torch.tensor([2, 2, 6, 6])
|
||||
self.t6_box2 = torch.tensor([4, 4, 7, 8])
|
||||
self.t6_correct_iou = 4 / 24
|
||||
|
||||
self.t7_box1 = torch.tensor([0, 0, 2, 2])
|
||||
self.t7_box2 = torch.tensor([3, 0, 5, 2])
|
||||
self.t7_correct_iou = 0
|
||||
|
||||
self.t8_box1 = torch.tensor([0, 0, 2, 2])
|
||||
self.t8_box2 = torch.tensor([0, 3, 2, 5])
|
||||
self.t8_correct_iou = 0
|
||||
|
||||
self.t9_box1 = torch.tensor([0, 0, 2, 2])
|
||||
self.t9_box2 = torch.tensor([2, 0, 5, 2])
|
||||
self.t9_correct_iou = 0
|
||||
|
||||
self.t10_box1 = torch.tensor([0, 0, 2, 2])
|
||||
self.t10_box2 = torch.tensor([1, 1, 3, 3])
|
||||
self.t10_correct_iou = 1 / 7
|
||||
|
||||
self.t11_box1 = torch.tensor([0, 0, 3, 2])
|
||||
self.t11_box2 = torch.tensor([1, 1, 3, 3])
|
||||
self.t11_correct_iou = 0.25
|
||||
|
||||
self.t12_bboxes1 = torch.tensor(
|
||||
[
|
||||
[0, 0, 2, 2],
|
||||
[0, 0, 2, 2],
|
||||
[0, 0, 2, 2],
|
||||
[0, 0, 2, 2],
|
||||
[0, 0, 2, 2],
|
||||
[0, 0, 3, 2],
|
||||
]
|
||||
)
|
||||
self.t12_bboxes2 = torch.tensor(
|
||||
[
|
||||
[3, 0, 5, 2],
|
||||
[3, 0, 5, 2],
|
||||
[0, 3, 2, 5],
|
||||
[2, 0, 5, 2],
|
||||
[1, 1, 3, 3],
|
||||
[1, 1, 3, 3],
|
||||
]
|
||||
)
|
||||
self.t12_correct_ious = torch.tensor([0, 0, 0, 0, 1 / 7, 0.25])
|
||||
|
||||
# Accept if the difference in iou is small
|
||||
self.epsilon = 0.001
|
||||
|
||||
def test_both_inside_cell_shares_area(self):
|
||||
iou = intersection_over_union(self.t1_box1, self.t1_box2, box_format="midpoint")
|
||||
self.assertTrue((torch.abs(iou - self.t1_correct_iou) < self.epsilon))
|
||||
|
||||
def test_partially_outside_cell_shares_area(self):
|
||||
iou = intersection_over_union(self.t2_box1, self.t2_box2, box_format="midpoint")
|
||||
self.assertTrue((torch.abs(iou - self.t2_correct_iou) < self.epsilon))
|
||||
|
||||
def test_both_inside_cell_shares_no_area(self):
|
||||
iou = intersection_over_union(self.t3_box1, self.t3_box2, box_format="midpoint")
|
||||
self.assertTrue((torch.abs(iou - self.t3_correct_iou) < self.epsilon))
|
||||
|
||||
def test_midpoint_outside_cell_shares_area(self):
|
||||
iou = intersection_over_union(self.t4_box1, self.t4_box2, box_format="midpoint")
|
||||
self.assertTrue((torch.abs(iou - self.t4_correct_iou) < self.epsilon))
|
||||
|
||||
def test_both_inside_cell_shares_entire_area(self):
|
||||
iou = intersection_over_union(self.t5_box1, self.t5_box2, box_format="midpoint")
|
||||
self.assertTrue((torch.abs(iou - self.t5_correct_iou) < self.epsilon))
|
||||
|
||||
def test_box_format_x1_y1_x2_y2(self):
|
||||
iou = intersection_over_union(self.t6_box1, self.t6_box2, box_format="corners")
|
||||
self.assertTrue((torch.abs(iou - self.t6_correct_iou) < self.epsilon))
|
||||
|
||||
def test_additional_and_batch(self):
|
||||
ious = intersection_over_union(
|
||||
self.t12_bboxes1, self.t12_bboxes2, box_format="corners"
|
||||
)
|
||||
all_true = torch.all(
|
||||
torch.abs(self.t12_correct_ious - ious.squeeze(1)) < self.epsilon
|
||||
)
|
||||
self.assertTrue(all_true)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Running Intersection Over Union Tests:")
|
||||
unittest.main()
|
||||
115
ML_tests/Object_detection_tests/map_test.py
Executable file
115
ML_tests/Object_detection_tests/map_test.py
Executable file
@@ -0,0 +1,115 @@
|
||||
import sys
|
||||
import unittest
|
||||
import torch
|
||||
|
||||
sys.path.append("ML/Pytorch/object_detection/metrics/")
|
||||
from mean_avg_precision import mean_average_precision
|
||||
|
||||
class TestMeanAveragePrecision(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# test cases we want to run
|
||||
self.t1_preds = [
|
||||
[0, 0, 0.9, 0.55, 0.2, 0.3, 0.2],
|
||||
[0, 0, 0.8, 0.35, 0.6, 0.3, 0.2],
|
||||
[0, 0, 0.7, 0.8, 0.7, 0.2, 0.2],
|
||||
]
|
||||
self.t1_targets = [
|
||||
[0, 0, 0.9, 0.55, 0.2, 0.3, 0.2],
|
||||
[0, 0, 0.8, 0.35, 0.6, 0.3, 0.2],
|
||||
[0, 0, 0.7, 0.8, 0.7, 0.2, 0.2],
|
||||
]
|
||||
self.t1_correct_mAP = 1
|
||||
|
||||
self.t2_preds = [
|
||||
[1, 0, 0.9, 0.55, 0.2, 0.3, 0.2],
|
||||
[0, 0, 0.8, 0.35, 0.6, 0.3, 0.2],
|
||||
[0, 0, 0.7, 0.8, 0.7, 0.2, 0.2],
|
||||
]
|
||||
self.t2_targets = [
|
||||
[1, 0, 0.9, 0.55, 0.2, 0.3, 0.2],
|
||||
[0, 0, 0.8, 0.35, 0.6, 0.3, 0.2],
|
||||
[0, 0, 0.7, 0.8, 0.7, 0.2, 0.2],
|
||||
]
|
||||
self.t2_correct_mAP = 1
|
||||
|
||||
self.t3_preds = [
|
||||
[0, 1, 0.9, 0.55, 0.2, 0.3, 0.2],
|
||||
[0, 1, 0.8, 0.35, 0.6, 0.3, 0.2],
|
||||
[0, 1, 0.7, 0.8, 0.7, 0.2, 0.2],
|
||||
]
|
||||
self.t3_targets = [
|
||||
[0, 0, 0.9, 0.55, 0.2, 0.3, 0.2],
|
||||
[0, 0, 0.8, 0.35, 0.6, 0.3, 0.2],
|
||||
[0, 0, 0.7, 0.8, 0.7, 0.2, 0.2],
|
||||
]
|
||||
self.t3_correct_mAP = 0
|
||||
|
||||
self.t4_preds = [
|
||||
[0, 0, 0.9, 0.15, 0.25, 0.1, 0.1],
|
||||
[0, 0, 0.8, 0.35, 0.6, 0.3, 0.2],
|
||||
[0, 0, 0.7, 0.8, 0.7, 0.2, 0.2],
|
||||
]
|
||||
|
||||
self.t4_targets = [
|
||||
[0, 0, 0.9, 0.55, 0.2, 0.3, 0.2],
|
||||
[0, 0, 0.8, 0.35, 0.6, 0.3, 0.2],
|
||||
[0, 0, 0.7, 0.8, 0.7, 0.2, 0.2],
|
||||
]
|
||||
self.t4_correct_mAP = 5 / 18
|
||||
|
||||
self.epsilon = 1e-4
|
||||
|
||||
def test_all_correct_one_class(self):
|
||||
mean_avg_prec = mean_average_precision(
|
||||
self.t1_preds,
|
||||
self.t1_targets,
|
||||
iou_threshold=0.5,
|
||||
box_format="midpoint",
|
||||
num_classes=1,
|
||||
)
|
||||
self.assertTrue(abs(self.t1_correct_mAP - mean_avg_prec) < self.epsilon)
|
||||
|
||||
def test_all_correct_batch(self):
|
||||
mean_avg_prec = mean_average_precision(
|
||||
self.t2_preds,
|
||||
self.t2_targets,
|
||||
iou_threshold=0.5,
|
||||
box_format="midpoint",
|
||||
num_classes=1,
|
||||
)
|
||||
self.assertTrue(abs(self.t2_correct_mAP - mean_avg_prec) < self.epsilon)
|
||||
|
||||
def test_all_wrong_class(self):
|
||||
mean_avg_prec = mean_average_precision(
|
||||
self.t3_preds,
|
||||
self.t3_targets,
|
||||
iou_threshold=0.5,
|
||||
box_format="midpoint",
|
||||
num_classes=2,
|
||||
)
|
||||
self.assertTrue(abs(self.t3_correct_mAP - mean_avg_prec) < self.epsilon)
|
||||
|
||||
def test_one_inaccurate_box(self):
|
||||
mean_avg_prec = mean_average_precision(
|
||||
self.t4_preds,
|
||||
self.t4_targets,
|
||||
iou_threshold=0.5,
|
||||
box_format="midpoint",
|
||||
num_classes=1,
|
||||
)
|
||||
self.assertTrue(abs(self.t4_correct_mAP - mean_avg_prec) < self.epsilon)
|
||||
|
||||
def test_all_wrong_class(self):
|
||||
mean_avg_prec = mean_average_precision(
|
||||
self.t3_preds,
|
||||
self.t3_targets,
|
||||
iou_threshold=0.5,
|
||||
box_format="midpoint",
|
||||
num_classes=2,
|
||||
)
|
||||
self.assertTrue(abs(self.t3_correct_mAP - mean_avg_prec) < self.epsilon)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Running Mean Average Precisions Tests:")
|
||||
unittest.main()
|
||||
95
ML_tests/Object_detection_tests/nms_test.py
Executable file
95
ML_tests/Object_detection_tests/nms_test.py
Executable file
@@ -0,0 +1,95 @@
|
||||
import sys
|
||||
import unittest
|
||||
import torch
|
||||
|
||||
sys.path.append("ML/Pytorch/object_detection/metrics/")
|
||||
from nms import nms
|
||||
|
||||
|
||||
class TestNonMaxSuppression(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# test cases we want to run
|
||||
self.t1_boxes = [
|
||||
[1, 1, 0.5, 0.45, 0.4, 0.5],
|
||||
[1, 0.8, 0.5, 0.5, 0.2, 0.4],
|
||||
[1, 0.7, 0.25, 0.35, 0.3, 0.1],
|
||||
[1, 0.05, 0.1, 0.1, 0.1, 0.1],
|
||||
]
|
||||
|
||||
self.c1_boxes = [[1, 1, 0.5, 0.45, 0.4, 0.5], [1, 0.7, 0.25, 0.35, 0.3, 0.1]]
|
||||
|
||||
self.t2_boxes = [
|
||||
[1, 1, 0.5, 0.45, 0.4, 0.5],
|
||||
[2, 0.9, 0.5, 0.5, 0.2, 0.4],
|
||||
[1, 0.8, 0.25, 0.35, 0.3, 0.1],
|
||||
[1, 0.05, 0.1, 0.1, 0.1, 0.1],
|
||||
]
|
||||
|
||||
self.c2_boxes = [
|
||||
[1, 1, 0.5, 0.45, 0.4, 0.5],
|
||||
[2, 0.9, 0.5, 0.5, 0.2, 0.4],
|
||||
[1, 0.8, 0.25, 0.35, 0.3, 0.1],
|
||||
]
|
||||
|
||||
self.t3_boxes = [
|
||||
[1, 0.9, 0.5, 0.45, 0.4, 0.5],
|
||||
[1, 1, 0.5, 0.5, 0.2, 0.4],
|
||||
[2, 0.8, 0.25, 0.35, 0.3, 0.1],
|
||||
[1, 0.05, 0.1, 0.1, 0.1, 0.1],
|
||||
]
|
||||
|
||||
self.c3_boxes = [[1, 1, 0.5, 0.5, 0.2, 0.4], [2, 0.8, 0.25, 0.35, 0.3, 0.1]]
|
||||
|
||||
self.t4_boxes = [
|
||||
[1, 0.9, 0.5, 0.45, 0.4, 0.5],
|
||||
[1, 1, 0.5, 0.5, 0.2, 0.4],
|
||||
[1, 0.8, 0.25, 0.35, 0.3, 0.1],
|
||||
[1, 0.05, 0.1, 0.1, 0.1, 0.1],
|
||||
]
|
||||
|
||||
self.c4_boxes = [
|
||||
[1, 0.9, 0.5, 0.45, 0.4, 0.5],
|
||||
[1, 1, 0.5, 0.5, 0.2, 0.4],
|
||||
[1, 0.8, 0.25, 0.35, 0.3, 0.1],
|
||||
]
|
||||
|
||||
def test_remove_on_iou(self):
|
||||
bboxes = nms(
|
||||
self.t1_boxes,
|
||||
threshold=0.2,
|
||||
iou_threshold=7 / 20,
|
||||
box_format="midpoint",
|
||||
)
|
||||
self.assertTrue(sorted(bboxes) == sorted(self.c1_boxes))
|
||||
|
||||
def test_keep_on_class(self):
|
||||
bboxes = nms(
|
||||
self.t2_boxes,
|
||||
threshold=0.2,
|
||||
iou_threshold=7 / 20,
|
||||
box_format="midpoint",
|
||||
)
|
||||
self.assertTrue(sorted(bboxes) == sorted(self.c2_boxes))
|
||||
|
||||
def test_remove_on_iou_and_class(self):
|
||||
bboxes = nms(
|
||||
self.t3_boxes,
|
||||
threshold=0.2,
|
||||
iou_threshold=7 / 20,
|
||||
box_format="midpoint",
|
||||
)
|
||||
self.assertTrue(sorted(bboxes) == sorted(self.c3_boxes))
|
||||
|
||||
def test_keep_on_iou(self):
|
||||
bboxes = nms(
|
||||
self.t4_boxes,
|
||||
threshold=0.2,
|
||||
iou_threshold=9 / 20,
|
||||
box_format="midpoint",
|
||||
)
|
||||
self.assertTrue(sorted(bboxes) == sorted(self.c4_boxes))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Running Non Max Suppression Tests:")
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user