Bai04 KNN

Download as pdf or txt
Download as pdf or txt
You are on page 1of 87

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN

KHOA KHOA HỌC MÁY TÍNH

CS231. Nhập môn Thị giác máy tính

Machine Learning Image


Classification

Mai Tiến Dũng


Content

1. Introduction to image classification


2. Image classification with KNN
3. Image feature

2
1. Introduction to Image Classification

• What is image classification


• Challenges of image classification

3
What is image classification

• Process of taking an image or picture

Cat

98%

4
Use of image classification

5
Targets:

6
7
8
9
10
11
12
Challenges of image
classification

13
Challenges: change in viewpoint

14
Challenges: Illumination

15
Challenges: deformation

16
Challenges: occlusion

17
Challenges: background clutter

18
Image Classification with KNN

19
Image Classification with KNN

20
Image Classification with KNN

21
Image Classification with KNN

22
Image Classification with KNN

23
Image Classification with KNN

24
Image Classification with KNN

25
Image Classification with KNN

26
27
Image Classification with KNN

28
Image Classification with KNN

29
Image Classification with KNN

30
Image Classification with KNN

31
Image Classification with KNN

32
Image Classification with KNN

33
Image Classification with KNN

34
Image Classification with KNN

35
Image Classification with KNN

36
Image Classification with KNN

37
Image Classification with KNN

38
Image Classification with KNN

39
Image Classification with KNN

40
Image Classification with KNN

41
Image Classification with KNN

42
Image Classification with KNN

43
Image Classification with KNN

44
Image Classification with KNN

45
Image Classification with KNN

46
Image Classification with KNN

47
Image Classification with KNN

48
Image Classification with KNN

49
Image Classification with KNN

Cat

50
Image Classification with KNN

51
Image Classification with KNN

52
Image Features

53
Image Features

54
Image Features

55
Image Features

56
Image Features

57
Image Features

58
Image Features

59
Image Features

60
Image Features

61
Image Features

62
Image Features

63
Image Features

64
Image Features

65
Image Features

66
Image Features

67
Image Features

68
Image Features

69
Image Features

70
Image Features

71
Image Features

72
Image Features

73
Image Features

74
Image Features

75
Image Features

76
Image Features

77
Image Features

78
Image Features

79
Tài liệu tham khảo

• https://www.coursera.org/learn/introduction-
computer-vision-watson-opencv

80
Thực hành

81
Yêu cầu

• Phân lớp ảnh: dogs vs cats


• Phương pháp: KNN
• Features: histogram
• Color: grayscale

82
Project structure

CS231.
├──Dataset
│ └──dogscats_small
│ ├──train
│ └──test
├──Baitap3
│ └──MSSV.ipynb

83
Tính histogram cho ảnh

#Hàm tính histogram

def TinhHist(pathfilename):
img = cv.imread(pathfilename,0)
hist = cv.calcHist([img],[0],None,
[256],[0,256])
size = img.shape[0]*img.shape[1]
hist = hist / size
return hist
84
Code: gợi ý

def ReadData(path_to_files):
for file_name in os.listdir(path_to_files):
# Lấy label của ảnh
# -> Thêm vào danh sách labels
# Tính đặc trưng của ảnh
# -> Thêm vào danh sách features
return features, labels

85
Code: gợi ý

• os.listdir(path_to_files)  đọc tất cả file trong thư


mục path_to_files

• Sorted (list_files)  sắp xếp danh sách list_files


theo thứ tự tăng dần

86
Code: gợi ý

from sklearn.neighbors import KNeighborsClassifier

---
label = imagePath.split(os.path.sep)[-1].split(".")[0]

----
features = np.array(features)
labels = np.array(labels)

---
model = KNeighborsClassifier(n_neighbors=1)
model.fit(features, labels)
acc = model.score(test_features, test_labels)
87

You might also like