支持向量机SVM-对图像进行分类原理讲解和代码示例

对于图像分类,我们往往都想到卷积神经网络,深度学习,可是深度学习很多时候需要很大的计算开销,而且代码编写和调试也较为复杂,对于小型且特征明显数据集的图像分类,有点小试牛刀

今天我们就奖一种机器学习的算法SVM对图像进行分类

今天我们讲的一个实例是关于一个三分类问题,数据是工业中的图片。
我已将数据集和测试集发在我的资源中,需要练习的可以下载

对于图像分类,比较少见,图像的的数据为一种三通道的二维像素点的形式,对于这种数据形式,往往我们使用卷积神经网络去建立模型,但是计算量往往太大,不能在短时间内求解,我们想到,将二维图片展平,即使用python opencv 库的flatten 函数将图片从二维降到一维,这样就可以将数据用于传统的机器学习算法建模。

但同时对于图片而言一张图片size800x600那么展平之后的都像素点数量为480000个,也就是说一个样本的特征向量的长度为480000,这对于计算机而言大大增加了计算量。同时一张图片中其实很多信息是不需要的,所以我们考虑到对数据进行降维,将图片用函数resize到(30,30)也就是(800,600)->(30,30)。

在resize之在讲三通道像素点进行转换,转换成灰度图像,之后再展平,就可以把图像转换成一个长度为900的向量。
之后,我们将数据投入svm进行训练,考虑到数据是较为复杂的,同时特征向量长度太大,数据在超平面的分布是趋近线性分布的概率很小,所以我们直接尝试分线性支持向量机,使用核函数“rbf”,经过多次调试得到gamma的最佳取值为:0.001。

下面看代码

trian.py



#import sys 
#import os

 
#os.system("pause")

#Svm 训练:
import sys
import os
import cv2
import numpy as np
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
import time
import pickle
#help(SVC)

SHAPE = (30, 30)
def getImageData(directory):
   s = 1
   feature_list = list()
   label_list   = list()
   num_classes = 0
   for root, dirs, files in os.walk(directory):
      for d in dirs:
         num_classes += 1
         images = os.listdir(root+d)
         for image in images:
            s += 1
            label_list.append(d)
            feature_list.append(extractFeaturesFromImage(root + d + "/" + image))

   return np.asarray(feature_list), np.asarray(label_list)

def extractFeaturesFromImage(image_file):
   img = cv2.imread(image_file)
   img = cv2.resize(img, SHAPE, interpolation = cv2.INTER_CUBIC)
   img = img.flatten()
   img = img / np.mean(img)
   return img
   

if __name__ == "__main__":
   
   directory ="F:/train/" 


   feature_array, label_array = getImageData(directory)

   X_train, X_test, y_train, y_test = train_test_split(feature_array, label_array, test_size = 0.2, random_state = 42)

   if os.path.isfile("svm_model.pkl"):
      svm = pickle.load(open("svm_model.pkl", "rb"))
   else:
      svm = SVC(kernel='rbf',gamma=0.001)
      svm.fit(X_train, y_train)
      pickle.dump(svm, open("F:\svm_model.pkl", "wb"))

   print("Testing...\n")
 
   right = 0
   total = 0
   for x, y in zip(X_test, y_test):
      x = x.reshape(1, -1)
      prediction = svm.predict(x)[0]
      if y == prediction:
         right += 1
      total += 1

   accuracy = float(right) / float(total)*100

   print (str(accuracy) + "% accuracy")
   print ("Manual Testing\n")
print("success")
   


os.system("pause")



然后是调用模型。并进行测试
test.py


#加载和调用
import csv
from sklearn.externals import joblib
import os
import cv2
import numpy as np
clf = joblib.load("F:\svm_model.pkl")
SHAPE = (30, 30)


def extractFeaturesFromImage(image_file):
   img = cv2.imread(image_file)
   img = cv2.resize(img, SHAPE, interpolation = cv2.INTER_CUBIC)
   img = img.flatten()
   img = img / np.mean(img)
   return img



r_1=[]
r_2=[]
r_3=[]
#for i in os.listdir("F:/train"):
#    print(i)
#    if i=="0":
#        for j in os.listdir("F:/train"+"/"+i):
#            print("F:/train"+"/"+i+"/"+j)

#imageFeature = img.reshape(1, -1)

#print(clf.predict(imageFeature)[0])
fp=open("F:/test.csv","w")
f_csv=csv.writer(fp)
ll=[]
ll.append(["id","label"])
for i in range(0,60): 
          
            img=extractFeaturesFromImage("F:/test"+"/"+str(i)+".jpg")
            imageFeature = img.reshape(1, -1)
            r_1.append(clf.predict(imageFeature)[0])
            print(str(i)+".jpg" , clf.predict(imageFeature)[0])
            a=int(clf.predict(imageFeature)[0])
            #print(str(a))
            ll.append([str(i)+".jpg",str(a)])
f_csv.writerows(ll)
fp.close()
os.system("pause")
os.system("pause")

我已将数据上传至我的资源中,需要练习的请自行下载
另外:
训练集解压码:apa5E0e1L!
测试集解压码: Cmes-BigData@2020
注:如果你不能运行代码,和碰到数据集的问题,请看我之后写的一篇补充博客
链接在这里

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐