Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
Image classification
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Cart Milan
Image classification
Commits
5aabffaa
Commit
5aabffaa
authored
1 year ago
by
Cart Milan
Browse files
Options
Downloads
Patches
Plain Diff
Modif
parent
a28545d9
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
knn.py
+13
-33
13 additions, 33 deletions
knn.py
mlp.py
+0
-0
0 additions, 0 deletions
mlp.py
result/knn.png
+0
-0
0 additions, 0 deletions
result/knn.png
with
13 additions
and
33 deletions
knn.py
+
13
−
33
View file @
5aabffaa
...
...
@@ -3,28 +3,24 @@ import numpy as np
import
matplotlib.pyplot
as
plt
def
distance_matrix
(
matrix1
,
matrix2
):
#X_test then X_train in this order
sum_of_squares_matrix1
=
np
.
sum
(
np
.
square
(
matrix1
),
axis
=
1
,
keepdims
=
True
)
#A^2
sum_of_squares_matrix2
=
np
.
sum
(
np
.
square
(
matrix2
),
axis
=
1
,
keepdims
=
True
)
#B^2
sum_of_squares_matrix1
=
np
.
sum
(
np
.
square
(
matrix1
),
axis
=
1
,
keepdims
=
True
)
sum_of_squares_matrix2
=
np
.
sum
(
np
.
square
(
matrix2
),
axis
=
1
,
keepdims
=
True
)
dot_product
=
np
.
dot
(
matrix1
,
matrix2
.
T
)
# A * B (matrix mutliplication)
dot_product
=
np
.
dot
(
matrix1
,
matrix2
.
T
)
dists
=
np
.
sqrt
(
sum_of_squares_matrix1
+
sum_of_squares_matrix2
.
T
-
2
*
dot_product
)
# Compute the product
dists
=
np
.
sqrt
(
sum_of_squares_matrix1
+
sum_of_squares_matrix2
.
T
-
2
*
dot_product
)
return
dists
def
knn_predict
(
dists
,
labels_train
,
k
):
output
=
[]
# Loop on all the images_test
for
i
in
range
(
len
(
dists
)):
# Innitialize table to store the neighbors
res
=
[
0
]
*
10
# Get the closest neighbors
labels_close
=
np
.
argsort
(
dists
[
i
])[:
k
]
for
label
in
labels_close
:
#add a label to the table of result
res
[
labels_train
[
label
]]
+=
1
# Get the class with the maximum neighbors
label_temp
=
np
.
argmax
(
res
)
#Careful to the logic here, if there is two or more maximum, the function the first maximum encountered
label_temp
=
np
.
argmax
(
res
)
output
.
append
(
label_temp
)
return
(
np
.
array
(
output
))
...
...
@@ -32,29 +28,22 @@ def evaluate_knn(data_train, labels_train, data_test, labels_tests, k):
dist
=
distance_matrix
(
data_test
,
data_train
)
result_test
=
knn_predict
(
dist
,
labels_train
,
k
)
#accuracy
N
=
labels_tests
.
shape
[
0
]
accuracy
=
(
labels_tests
==
result_test
).
sum
()
/
N
return
(
accuracy
)
def
bench
_knn
()
:
def
test
_knn
()
:
k_indices
=
[
i
for
i
in
range
(
20
)
if
i
%
2
!=
0
]
accuracies
=
[]
# Load data
data
,
labels
=
read_cifar
.
read_cifar
(
'
/Users/milancart/Documents/GitHub/image-classification/Data/cifar-10-batches-py
'
)
X_train
,
X_test
,
y_train
,
y_test
=
read_cifar
.
split_dataset
(
data
,
labels
,
0.9
)
#Load one batch
# data, labels = read_cifar.read_cifar_batch('image-classification/data/cifar-10-batches-py/data_batch_1')
# X_train, X_test, y_train, y_test = read_cifar.split_dataset(data, labels, 0.9)
# Loop on the k_indices to get all the accuracies
for
k
in
k_indices
:
accuracy
=
evaluate_knn
(
X_train
,
y_train
,
X_test
,
y_test
,
k
)
accuracies
.
append
(
accuracy
)
# Save and show the graph of accuracies
plt
.
figure
(
figsize
=
(
8
,
6
))
plt
.
xlabel
(
'
K
'
)
plt
.
ylabel
(
'
Accuracy
'
)
...
...
@@ -66,15 +55,6 @@ def bench_knn() :
if
__name__
==
"
__main__
"
:
print
(
'
milan
'
)
bench_knn
()
data
,
labels
=
read_cifar
.
read_cifar
(
'
/Users/milancart/Documents/GitHub/image-classification/Data/cifar-10-batches-py
'
)
X_train
,
X_test
,
y_train
,
y_test
=
read_cifar
.
split_dataset
(
data
,
labels
,
0.9
)
print
(
evaluate_knn
(
X_train
,
y_train
,
X_test
,
y_test
,
5
))
print
(
X_train
.
shape
,
X_test
.
shape
,
y_train
.
shape
,
y_test
.
shape
)
y_test
=
[]
x_test
=
np
.
array
([[
1
,
2
],[
4
,
6
]])
x_train
=
np
.
array
([[
2
,
4
],[
7
,
2
],[
4
,
6
]])
y_train
=
[
1
,
2
,
1
]
dist
=
distance_matrix
(
x_test
,
x_train
)
\ No newline at end of file
test_knn
()
\ No newline at end of file
This diff is collapsed.
Click to expand it.
mlp.py
0 → 100644
+
0
−
0
View file @
5aabffaa
This diff is collapsed.
Click to expand it.
result/knn.png
0 → 100644
+
0
−
0
View file @
5aabffaa
2.34 KiB
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment