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
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Brudy Saintespes Baptiste
Image classification
Compare revisions
8f73389a9c372703860c0e6a935e34b9b977a53e to 8cb839500de9cc2e513e6fbc58fc624248d43154
Compare revisions
Changes are shown as if the
source
revision was being merged into the
target
revision.
Learn more about comparing revisions.
Source
bbrudysa/image-classification
Select target project
No results found
8cb839500de9cc2e513e6fbc58fc624248d43154
Select Git revision
Branches
main
1 result
Swap
Target
bbrudysa/image-classification
Select target project
bbrudysa/image-classification
1 result
8f73389a9c372703860c0e6a935e34b9b977a53e
Select Git revision
Branches
main
1 result
Show changes
Only incoming changes from source
Include changes to target since source was created
Compare
Commits on Source (3)
Update README.md
· a414165b
BaptisteBrd
authored
1 year ago
a414165b
Update README.md
· 12ff5e60
BaptisteBrd
authored
1 year ago
12ff5e60
modif knn
· 8cb83950
BaptisteBrd
authored
1 year ago
8cb83950
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
README.md
+2
-3
2 additions, 3 deletions
README.md
knn.py
+10
-15
10 additions, 15 deletions
knn.py
with
12 additions
and
18 deletions
README.md
View file @
8cb83950
# Image classification
# Image classification
## k-nearest neighbors
The codes are available in the different .py files.
This readme.md file will only present the mathematical part of the Artificial Neural Network.
## Artificial Neural Network
## Artificial Neural Network
...
...
This diff is collapsed.
Click to expand it.
knn.py
View file @
8cb83950
...
@@ -3,26 +3,21 @@ import read_cifar
...
@@ -3,26 +3,21 @@ import read_cifar
import
matplotlib.pyplot
as
plt
import
matplotlib.pyplot
as
plt
def
distance_matrix
(
A
,
B
):
def
distance_matrix
(
A
,
B
):
# Calculating the squared sum of elements in each row for matrices A and B
sum_of_squares_A
=
np
.
sum
(
A
**
2
,
axis
=
1
,
keepdims
=
True
)
sum_of_squares_A
=
np
.
sum
(
A
**
2
,
axis
=
1
,
keepdims
=
True
)
sum_of_squares_B
=
np
.
sum
(
B
**
2
,
axis
=
1
,
keepdims
=
True
).
T
sum_of_squares_B
=
np
.
sum
(
B
**
2
,
axis
=
1
,
keepdims
=
True
).
T
dot_product
=
np
.
dot
(
A
,
B
.
T
)
dot_product
=
np
.
dot
(
A
,
B
.
T
)
# Computing the Euclidean distance matrix
dists
=
np
.
sqrt
(
sum_of_squares_A
+
sum_of_squares_B
-
2
*
dot_product
)
dists
=
np
.
sqrt
(
sum_of_squares_A
+
sum_of_squares_B
-
2
*
dot_product
)
return
dists
return
dists
#def knn_predict(dists, labels_train, k):
#
#
def
knn_predict
(
dists
,
labels_train
,
k
):
def
knn_predict
(
dists
,
labels_train
,
k
):
predicted_labels
=
[]
predicted_labels
=
[]
#
For every image
in th
e test set
#
Iterat
in
g
th
rough each test data point's distances to train data
for
i
in
range
(
len
(
dists
)):
for
i
in
range
(
len
(
dists
)):
#
Initialize an array to store the
neighbors
#
Counting the frequency of each class among the k nearest
neighbors
classes
=
[
0
]
*
10
classes
=
[
0
]
*
10
# indexes of the closest neighbors
# indexes of the closest neighbors
indexes_closest_nb
=
np
.
argsort
(
dists
[
i
])[:
k
]
indexes_closest_nb
=
np
.
argsort
(
dists
[
i
])[:
k
]
...
@@ -37,8 +32,7 @@ def evaluate_knn(data_train, labels_train, data_test, labels_test, k):
...
@@ -37,8 +32,7 @@ def evaluate_knn(data_train, labels_train, data_test, labels_test, k):
rate
=
0
rate
=
0
dist_train_test
=
distance_matrix
(
data_test
,
data_train
)
dist_train_test
=
distance_matrix
(
data_test
,
data_train
)
prediction
=
knn_predict
(
dist_train_test
,
labels_train
,
k
)
prediction
=
knn_predict
(
dist_train_test
,
labels_train
,
k
)
print
(
len
(
prediction
))
# Comparing predictions to actual test labels to calculate accuracy
print
(
len
(
labels_test
))
for
j
in
range
(
len
(
prediction
)):
for
j
in
range
(
len
(
prediction
)):
if
prediction
[
j
]
==
labels_test
[
j
]:
if
prediction
[
j
]
==
labels_test
[
j
]:
rate
+=
1
rate
+=
1
...
@@ -52,11 +46,15 @@ def knn_final():
...
@@ -52,11 +46,15 @@ def knn_final():
data
,
labels
=
read_cifar
.
read_cifar
(
"
data/cifar-10-batches-py
"
)
data
,
labels
=
read_cifar
.
read_cifar
(
"
data/cifar-10-batches-py
"
)
data_train_f
,
labels_train_f
,
data_test_f
,
labels_test_f
=
read_cifar
.
split_dataset
(
data
,
labels
,
0.9
)
data_train_f
,
labels_train_f
,
data_test_f
,
labels_test_f
=
read_cifar
.
split_dataset
(
data
,
labels
,
0.9
)
# Testing KNN for different values of k and storing the accuracy
for
k
in
range_k
:
for
k
in
range_k
:
print
(
k
)
print
(
k
)
rate_k
=
evaluate_knn
(
data_train_f
,
labels_train_f
,
data_test_f
,
labels_test_f
,
k
)
rate_k
=
evaluate_knn
(
data_train_f
,
labels_train_f
,
data_test_f
,
labels_test_f
,
k
)
rates
.
append
(
rate_k
)
rates
.
append
(
rate_k
)
# Plotting the accuracy as a function of k
plt
.
figure
(
figsize
=
(
10
,
7
))
plt
.
figure
(
figsize
=
(
10
,
7
))
plt
.
xlabel
(
'
k
'
)
plt
.
xlabel
(
'
k
'
)
plt
.
ylabel
(
'
Accuracy rate
'
)
plt
.
ylabel
(
'
Accuracy rate
'
)
...
@@ -73,6 +71,3 @@ def knn_final():
...
@@ -73,6 +71,3 @@ def knn_final():
if
__name__
==
"
__main__
"
:
if
__name__
==
"
__main__
"
:
knn_final
()
knn_final
()
\ No newline at end of file
#a1 = np.array([[0,0,1],[0,0,0],[1,1,2]])
#b1 = np.array([[1,3,1], [1,1,4], [1,5,1]])
#print(distance_matrix(a1,b1))
\ No newline at end of file
This diff is collapsed.
Click to expand it.