Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Мухин Иван. Группа 3821Б1ПМоп3. Лабораторная работа №3. #52

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions MukhinIS/lab3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Классификация изображений с помощью бибилиотеки OpenCV.

Структура лабораторной работы:
1. Для обработки изображений используется программный интерфейс `Data`, внутри которого:
* `__init__` - конструктор класса, который принимает путь до датасета, размер тренировочной и тестовой выборок,
после чего происходит перемешивание путей до изображений;
* `load_images` - метод класса, который загружает и сохраняет в списки изображения для тестовой и тренировочной
выборок;
2. Для обработки детекторов используется программный интерфейс `Model`, внутри которого:
* `__init__` - конструктор класса, принимающий количество кластеров, после чего создает детектор `SIFT`;
* `extract_features` - метод, который применяет детектор на выборке и выделяет из них дескрипторы ключевых точек;

144 changes: 144 additions & 0 deletions MukhinIS/lab3/not_deep_learning_classifer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import cv2 as cv
import numpy as np
import os
from sklearn.svm import SVC
from sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier
from sklearn.cluster import KMeans
from sklearn.metrics import accuracy_score
import argparse
import sys
import random


def cli_argument_parser():
parser = argparse.ArgumentParser()

parser.add_argument('-d', '--data',
help='Path to directory with "Cat" and "Dog"'
'subdirectories',
type=str,
required=False,
dest='data_path')
parser.add_argument('--train_size',
help='Size of train dataset',
type=int,
required=False,
dest='train_size')
parser.add_argument('--test_size',
help='Size of test dataset',
type=int,
required=False,
dest='test_size')
parser.add_argument('--clusters',
help='Number of clusters',
type=int,
required=False,
default=50,
dest='clusters')
parser.add_argument('--method',
help='Classifer',
type=str,
required=False,
dest='method')
args = parser.parse_args()

return args


class Data:
def __init__(self, data_path, train_size, test_size):
self.data = data_path
self.train_size = train_size
self.test_size = test_size
self.folder_cat = [os.path.abspath(f'{self.data}/Cat/{i}') for i in os.listdir(self.data + '/Cat')]
self.folder_dog = [os.path.abspath(f'{self.data}/Dog/{i}') for i in os.listdir(self.data + '/Dog')]
self.folder_all = self.folder_dog + self.folder_cat
random.shuffle(self.folder_all)
self.train = self.folder_all[0:self.train_size]
self.test = self.folder_all[self.train_size:self.train_size + self.test_size]

def load_images(self):
images_train, images_test = [], []
labels_train, labels_test = [], []
for filename_train in self.train:
label = 0 if filename_train.split('/')[-2] == 'Cat' else 1
path = os.path.join(self.data, filename_train)
img = cv.imread(path, cv.IMREAD_GRAYSCALE)
if img is not None:
img = cv.resize(img, (256, 256))
images_train.append(img)
labels_train.append(label)
for filename_test in self.test:
label = 0 if filename_train.split('/')[-2] == 'Cat' else 1
path = os.path.join(self.data, filename_test)
img = cv.imread(path, cv.IMREAD_GRAYSCALE)
if img is not None:
img = cv.resize(img, (256, 256))
images_test.append(img)
labels_test.append(label)
return images_train, labels_train, images_test, labels_test


class Model:
def __init__(self, clusters):
self.sift = cv.SIFT_create(nfeatures=500)
self.clusters = clusters

def extract_features(self, images):
self.descriptors = []
for image in images:
kp, dp = self.sift.detectAndCompute(image, None)
if dp is not None:
self.descriptors.append(dp)
return self.descriptors

def create_bow(self):
self.kmeans = KMeans(self.clusters)
self.kmeans.fit(np.vstack(self.descriptors))

def bow_features(self, images):
features = []
for img in images:
_, descriptors = self.sift.detectAndCompute(img, None)
if descriptors is not None:
histogram = np.zeros(self.clusters)
cluster_indices = self.kmeans.predict(descriptors)
for idx in cluster_indices:
histogram[idx] += 1
features.append(histogram)
else:
features.append(np.zeros(self.clusters))
return np.array(features)


def main():
args = cli_argument_parser()
data = Data(args.data_path, args.train_size, args.test_size)
images_train, labels_train, images_test, labels_test = data.load_images()
model = Model(args.clusters)
model.extract_features(images_train)
model.create_bow()
train_features = model.bow_features(images_train)
test_features = model.bow_features(images_test)
classifer = None
if args.method == 'forest':
classifer = RandomForestClassifier()
elif args.method == 'grad':
classifer = GradientBoostingClassifier()
elif args.method == 'svc':
classifer = SVC(kernel='rbf', probability=True, gamma=0.01)
else:
classifer= GradientBoostingClassifier()

classifer.fit(train_features, labels_train)
predictions_train = classifer.predict(train_features)
predictions_test = classifer.predict(test_features)
acc_train = accuracy_score(labels_train, predictions_train)
acc_test = accuracy_score(labels_test, predictions_test)
print(f'Accuracy on train data = {acc_train}')
print(f'Accuracy on test data = {acc_test}')


if __name__=='__main__':
sys.exit(main() or 0)