0

I'm trying to generate my own training data for recognition problem. I have two folders s0 and s1 and the folder containing is data. Images, lables are the two list in which the labels contains the names of the folder.

data
  |—- s0
  |    |—- 1.pgm
  |    |—- 2.pgm
  |    |—- ...
  |—- s1
  |    |—- 1.pgm
  |    |—- 2.pgm
  |    |—- ...

Below is the code, it's showing me the following error on line classifier.fit(images, lables)

 Traceback (most recent call last):
 File "mint.py", line 34, in <module>
   classifier.fit(images, lables)
 File "/usr/local/lib/python2.7/dist-packages/sklearn/svm/base.py",  line 150, in fit
  X = check_array(X, accept_sparse='csr', dtype=np.float64, order='C')
 File "/usr/local/lib/python2.7/dist-         packages/sklearn/utils/validation.py", line 396, in check_array
 % (array.ndim, estimator_name)) 

ValueError: Found array with dim 3. Estimator expected <= 2. here

Code:

import os,sys
import cv2
import numpy as np
from sklearn.svm import SVC
fn_dir ='/home/aquib/Desktop/Natural/data'

# Create a list of images and a list of corresponding names
(images, lables, names, id) = ([], [], {}, 0)
for (subdirs, dirs, files) in os.walk(fn_dir):
  for subdir in dirs:
     names[id] = subdir
     mypath = os.path.join(fn_dir, subdir)
    for item in os.listdir(mypath):
      if '.png' in item:  
      label=id
      image = cv2.imread(os.path.join(mypath, item),0)
      r_image = np.resize(image,(30,30))
      if image is not None:
         images.append(r_image)
         lables.append(int(label))
    id += 1  
# Create a Numpy array from the two lists above
(images, lables) = [np.array(lis) for lis in [images, lables]]
classifier = SVC(verbose=0, kernel='poly', degree=3)
classifier.fit(images, lables)

I really don't understand how to correct it in 2 dimension. I am trying the following, but the error is same:

images = np.array(images)
im_sq = np.squeeze(images).shape
images = images.reshape(images.shape[:2]) 
michael
  • 9,161
  • 2
  • 52
  • 49
Aquib
  • 320
  • 6
  • 20
  • Without checking the code too exact: can it be, that your images are of 2d-dimension (30x30), so that your training-set has dimension (x,30,30)? That's not possible. You need to flatten out the images to train with dim (X, 30x30) = (x,900). – sascha Jul 27 '16 at 11:59
  • okay In which line I have to made changes? Do I need to use np.flatten in line `r_image = np.resize(image,(30,30))` – Aquib Jul 27 '16 at 12:01
  • ```r_image = np.resize(image,(30,30))``` needs to be flat. So ```r_image = np.resize(image,(30,30)).flatten()``` *might work* (if not, just reshape to 900). But i don't like the code-style that much. Sometimes you build lists instead of numpy arrays (generator-expressions). By the way: are you trying to resize the image with numpy.resize? That's bad. np.resize is not really image-resizing (which is basically interpolation)! Use scikit-image or opencv for resizing images. This code really needs work i think! – sascha Jul 27 '16 at 12:04
  • yes, It works... this time it didn't give me any error. I am sorry for the code style I am just a newbie in this field, just learning from from Internet and trying. What do you feel, will this code work for recognition? Thanks a lot :) – Aquib Jul 27 '16 at 12:09
  • It won't, because you probably destroyed any useful information from your images (np.resize). Besides that, normally, in the pre-deep-learning area, one would use PCA/LDA as feature-extraction before training svm's. Stick to the ideas used [here](http://scikit-learn.org/stable/auto_examples/applications/face_recognition.html). There are other scikit-learn examples regarding face-recognition. These codes are quit good (even if not research-grade). – sascha Jul 27 '16 at 12:11
  • Okay, What changes are required to make it work.? Please... – Aquib Jul 27 '16 at 12:13
  • There are many good tutorials on the internet. The scikit-learn user-guides and examples are great. You should check the basic image-processing pipeline as well as your basics regarding the use of python/numpy. And usually it's good idea to test with *common datasets*. To repair your code: (1) use some real image-resize operation instead of np.resize. (2) use a feature-extraction step before svm (pca or others) (3) use cross-validation to tune svm params. **all these steps are important** – sascha Jul 27 '16 at 12:14
  • But they've predefined datasets, I want to create my own datasets and then split it into training and testing data. Okay I'll surely go through you'r instructions thank you so much... – Aquib Jul 27 '16 at 12:18
  • possible duplicate of http://stackoverflow.com/questions/34972142/sklearn-logistic-regression-valueerror-found-array-with-dim-3-estimator-expec – michael Dec 14 '16 at 05:23

0 Answers0