我是靠谱客的博主 自然过客,最近开发中收集的这篇文章主要介绍模式识别大作业-代码数据增强数据处理SVM训练模型,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

数据增强

数据处理

#将文件下的图片处理成的数据,保存在Dataframe中
import glob
import numpy as np
import pandas as pd
import cv2

def my_dataset(smoke_path, non_path):
    #也可以读取源文件夹的文件列表
    #file_list = os.listdir(source_dir)
    smoke_imgs = glob.glob(smoke_path + '\*.jpg')
    non_imgs = glob.glob(non_path + '\*.jpg')
    len_smoke = len(smoke_imgs)
    len_non = len(non_imgs)
    data = pd.DataFrame(np.zeros((len_smoke + len_non, 10001)), index=None, columns=None)
    for i in range(0, len_smoke):
        img1 = cv2.imread(smoke_imgs[i], 0)
        if img1.shape == (110, 100):
            img1 = cv2.resize(img1, (100, 100))
        img1 = np.ndarray.flatten(img1)
        data.iloc[i, :10000] = img1
        data.iloc[i, -1] = 1  # 标签为1,有烟
    # print(len_smoke)
    for i in range(len_smoke, len_smoke + len_non):
        img1 = cv2.imread(non_imgs[i - len_smoke], 0)
        img1 = np.ndarray.flatten(img1)
        data.iloc[i, :10000] = img1
        data.iloc[i, -1] = 0  # 标签为0,无烟
    # print(data)
    return data

翻转,旋转

# -*- coding:utf-8 -*-
import os
import glob
import cv2

def rotate(image, angle=45 , scale=1.0):
	height, width = image.shape[:2]#获取图像的高和宽
	center = (width / 2, height / 2) #取图像的中点,图像以中心点旋转

	M = cv2.getRotationMatrix2D(center, angle, scale)#获得图像绕着某一点的旋转矩阵
	rotated = cv2.warpAffine(image, M, (height, width))
             #cv2.warpAffine()的第二个参数是变换矩阵,第三个参数是输出图像的大小,大小不变
	return rotated

def creat_roate_hv_dataset(smoke_path, non_path):
    smoke_imgs = glob.glob(smoke_path + '\*.jpg')
    non_imgs = glob.glob(non_path + '\*.jpg')
    len_smoke = len(smoke_imgs)
    len_non = len(non_imgs)
    for i in range(0, len_smoke):
        img1 = cv2.imread(smoke_imgs[i], 0)
        if img1.shape == (110, 100):
            img1 = cv2.resize(img1, (100, 100))
        #旋转45度
        img11=rotate(img1)
        # Flipped Horizontally & Vertically 水平垂直翻转
        img12 = cv2.flip(img1, 1)#水平
        img13 = cv2.flip(img1, 0)#垂直
        # cv2.imshow("还原",img1)#看看旋转后的效果
        # cv2.waitKey(111)
        path1= 'roate0' + str(i) + '.jpg'
        path2= 'hor0' + str(i) + '.jpg'
        path3= 'ver0' + str(i) + '.jpg'

        test_smoke_t1_path= os.path.join(smoke_path_t, path1)
        cv2.imwrite(test_smoke_t1_path, img11)  # 保存到指定路径
        print('第{}张旋转smoke...'.format(i))

        test_smoke_t2_path= os.path.join(smoke_path_t, path2)
        cv2.imwrite(test_smoke_t2_path, img12) #保存到同一路径
        print('第{}张水平翻转smoke...'.format(i))

        test_smoke_t2_path= os.path.join(smoke_path_t, path3)
        cv2.imwrite(test_smoke_t2_path, img13) #保存到同一路径
        print('第{}张垂直翻转smoke...'.format(i))
    for i in range(0 , len_non):
        img1 = cv2.imread(non_imgs[i - len_smoke], 0)
        img11=rotate(img1)
        img12 = cv2.flip(img1, 1)
        img13 = cv2.flip(img1, 0)

        # cv2.imshow("还原",img1)#看看旋转后的效果
        # cv2.waitKey(111)
        path1= 'roate0' + str(i) + '.jpg'
        path2 = 'hor0' + str(i) + '.jpg'
        path3 = 'ver0' + str(i) + '.jpg'

        test_smoke_t1_path= os.path.join(tnon_path_t, path1)
        print('第{}张旋转nonsmoke...'.format(i))
        cv2.imwrite(test_smoke_t1_path, img11)#保存到指定路径

        test_smoke_t1_path= os.path.join(tnon_path_t, path2)
        print('第{}张水平翻转nonsmoke...'.format(i))
        cv2.imwrite(test_smoke_t1_path, img12)#保存到指定路径

        test_smoke_t1_path= os.path.join(tnon_path_t, path3)
        print('第{}张垂直翻转nonsmoke...'.format(i))
        cv2.imwrite(test_smoke_t1_path, img13)#保存到指定路径



file = "C:\Users\Curry\Desktop\pattern_recognize"
train_val_smoke_path = os.path.join(file, 'lib81_1\smoke')
smoke_path_t = os.path.join(file, 'lib81_1\smoke_t')
train_val_non_path = os.path.join(file, 'lib81_1\non')
tnon_path_t = os.path.join(file, 'lib81_1\non_t')

creat_roate_hv_dataset(train_val_smoke_path, train_val_non_path)

SVM训练模型

import pandas as pd
from sklearn import svm
import numpy as np

from sklearn.decomposition import PCA
from matplotlib import pyplot as plt
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import LinearSVC
from sklearn.preprocessing import MinMaxScaler


data = pd.read_excel('./训练集特征向量.xls')  # SHAPE:(n,60)

# data2 = pd.read_excel('./训练增强数据集特征向量.xls')  # SHAPE:(n,60)
data2 = pd.read_excel('./训练增强数据集特征向量.xls')  # SHAPE:(n,60)

data = pd.concat([data,data2], ignore_index=True)

data = data.reindex(np.random.permutation(data.index))  # 随机打乱
# data = data.reindex(np.random.permutation(data.index))  # 随机打乱
# data = data.reindex(np.random.permutation(data.index))  # 随机打乱

# data = data.iloc[:250, :]

# data_test = pd.read_excel('TestsetData.xls')
data_test = pd.read_excel('./测试集特征向量.xls')
data_test = data_test.reindex(np.random.permutation(data_test.index))  # 随机打乱
# data_test = data_test.reindex(np.random.permutation(data_test.index))  # 随机打乱
# data_test = data_test.reindex(np.random.permutation(data_test.index))  # 随机打乱


img = data.iloc[:, 0:58]
# for i in range( feature_testset.shape[0] ):
#     img.iloc[i, 0] = sum( img.iloc[i, 0:20] )
#     img.iloc[i, 1] = sum( img.iloc[i, 20:39] )
#     img.iloc[i, 2] = sum( img.iloc[i, 39:] )

# img = img.iloc[:, 0:3]

# img = StandardScaler().fit_transform(img)
# img = MinMaxScaler().fit_transform( img )
# img.iloc[:, 0:28] = sum(img.iloc[:, 0:28])
# img.iloc[:, 0:28] = data.iloc[:, 28:58]

img = pd.DataFrame(img)

label = data.iloc[:, 59]


img_test = data_test.iloc[:, 0:58]
# for i in range( feature_testset.shape[0] ):
#     img_test.iloc[i, 0] = sum( img_test.iloc[i, 0:20] )
#     img_test.iloc[i, 1] = sum( img_test.iloc[i, 20:39] )
#     img_test.iloc[i, 2] = sum( img_test.iloc[i, 39:] )
# img_test = img_test.iloc[:, 0:3]

# img_test = StandardScaler().fit_transform(img_test)
# img_test = MinMaxScaler().fit_transform( img_test )
img_test = pd.DataFrame(img_test)

label_test = data_test.iloc[:, 59]# train_val_data = data.reindex(np.random.permutation(data.index))  # 随机打乱

feature_trainset = img
feature_testset = img_test

train_validation_data = pd.DataFrame(feature_trainset)
feature_trainset['label'] = label
feature_testset['label'] = label_test
    # test_data = pd.DataFrame(test_data)
train_validation = feature_trainset.iloc[:, :-1]
train_validation_labels = feature_trainset.iloc[:, -1].ravel()

model = svm.SVC(C=53, kernel='rbf', gamma = 54, decision_function_shape='ovo')
model = svm.SVC(C=99, kernel='rbf', gamma = 99, decision_function_shape='ovo')#测试集: 0.9528239202657808

model.fit(train_validation, train_validation_labels.astype(int))

# model =clf.fit(train_validation, train_validation_labels.astype(int))

test = feature_testset.iloc[:, :-1]
test_labels = feature_testset.iloc[:, -1].ravel()

train_score = model.score(train_validation, train_validation_labels)
print("训练集:", train_score)

test_score = model.score(test, test_labels)
print("测试集:", test_score)

最后

以上就是自然过客为你收集整理的模式识别大作业-代码数据增强数据处理SVM训练模型的全部内容,希望文章能够帮你解决模式识别大作业-代码数据增强数据处理SVM训练模型所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(44)

评论列表共有 0 条评论

立即
投稿
返回
顶部