как использовать правильный HOG? Я хочу извлечь изображения с помощью HOG, чтобы найти значение точности метода naivebayes. Может кто-нибудь меня объединит, пожалуйста - PullRequest
0 голосов
/ 25 мая 2020

Это мой код, он еще не закончен:

# -*- coding: utf-8 -*-
"""
Created on Mon May 25 16:36:02 2020

@author: fachr
"""

import cv2 as cv
import glob
from skimage.feature import local_binary_pattern
import numpy as np
import pandas as pd

%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
from sklearn import preprocessing
#importing required libraries
from skimage.io import imread, imshow
from skimage.transform import resize
from skimage.feature import hog
from skimage import exposure
from sklearn import preprocessing


radius = 2
n_points = 8 * radius
METHOD = 'uniform'

x=0
training = []
testing = []
eps=1e-7

for filename in glob.glob('images/TRAIN/EOSINOPHIL/*.jpg'):
    if x<20:
        img = cv.imread(filename)
        fd, hog_image = hog(img, orientations=9, pixels_per_cell=(8, 8), 
                    cells_per_block=(2, 2), visualize=True, multichannel=True)
        fd.shape
        training.extend(hog_image)
        dftraining = pd.DataFrame(training).astype(np.float32)
        x=x+1
    else:
        x=0
        break

for filename in glob.glob('images/TRAIN/LYMPHOCYTE/*.jpeg'):
    if x<20:
        img = cv.imread(filename)
        fd, hog_image = hog(img, orientations=9, pixels_per_cell=(8, 8), 
                    cells_per_block=(2, 2), visualize=True, multichannel=True)
        fd.shape
        training.extend(hog_image)
        dftraining = pd.DataFrame(training).astype(np.float32)
        x=x+1
    else:
        x=0
        break    

for filename in glob.glob('images/TRAIN/MONOCYTE/*.jpeg'):
    if x<20:
        img = cv.imread(filename)
        fd, hog_image = hog(img, orientations=9, pixels_per_cell=(8, 8), 
                    cells_per_block=(2, 2), visualize=True, multichannel=True)
        fd.shape
        training.extend(hog_image)
        dftraining = pd.DataFrame(training).astype(np.float32)
        x=x+1
    else:
        x=0
        break

for filename in glob.glob('images/TRAIN/NEUTROPHIL/*.jpeg'):
    if x<20:
        img = cv.imread(filename)
        fd, hog_image = hog(img, orientations=9, pixels_per_cell=(8, 8), 
                    cells_per_block=(2, 2), visualize=True, multichannel=True)
        fd.shape
        training.extend(hog_image)
        dftraining = pd.DataFrame(training).astype(np.float32)
        x=x+1
    else:
        x=0
        break     

#image extraction data testing 
testimgs = []    
for filename in glob.glob('images/TESTING/*.jpeg' ):
        img = cv.imread(filename)
        fd, hog_image = hog(img, orientations=9, pixels_per_cell=(8, 8), 
                    cells_per_block=(2, 2), visualize=True, multichannel=True)
        fd.shape
        testing.extend(hog_image)
        dftesting = pd.DataFrame().astype(np.float32)

#target
responses = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                      2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
                      3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
                      4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]).astype(np.float32)

y_test = np.array([1., 1., 1., 1., 1., 2., 2., 2., 2., 2., 3., 3., 3., 3., 4., 4., 4., 4., 4., 4.])


#naive bayes
from sklearn.naive_bayes import MultinomialNB
#gaussian naive bayes
#gnb = GaussianNB()
gnb = MultinomialNB()
gnb.fit(dftraining, responses)

#get prediction results
y_pred = gnb.predict(dftesting)
print('')
print('======================================================================')

print('classification : ')
print(y_pred)
print("the amount of classification data :", len(y_pred))
print('')
print('correct  :')
print(y_test)
print("number of correct classes :", len(y_test))

error = ((y_test != y_pred).sum()/len(y_pred))*100
accuracy = 100-error

print('')
print('accuracy = %.2f' %accuracy, '%')

Я получаю эту ошибку:

Traceback (most recent call last):
  File "F:\KULIAH\Semester 4\Citra Digital\Pertemuan 13- 15\blodd cell images\dataset2-master\NaivebayesHOG.py", line 111, in <module>
    gnb.fit(dftraining, responses)
  File "C:\Users\fachr\Anaconda3\lib\site-packages\sklearn\naive_bayes.py", line 609, in fit
    X, y = self._check_X_y(X, y)
  File "C:\Users\fachr\Anaconda3\lib\site-packages\sklearn\naive_bayes.py", line 475, in _check_X_y
    return check_X_y(X, y, accept_sparse='csr')
  File "C:\Users\fachr\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 765, in check_X_y
    check_consistent_length(X, y)
  File "C:\Users\fachr\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 212, in check_consistent_length
    " samples: %r" % [int(l) for l in lengths])
ValueError: Found input variables with inconsistent numbers of samples: [14400, 80]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...