本作业的目标如下:

implement a fully-vectorized loss function for the SVM

implement the fully-vectorized expression for its analytic gradient

check your implementation using numerical gradient

use a validation set to tune the learning rate and regularization strength

optimize the loss function with SGD

visualize the final learned weights

归一化图片

mean_image = np.mean(X_train, axis=0)

print mean_image[:10] # print a few of the elements

plt.figure(figsize=(4,4))

plt.imshow(mean_image.reshape((32,32,3)).astype('uint8')) # visualize the mean image

plt.show()

X_train -= mean_image

X_val -= mean_image

X_test -= mean_image

X_dev -= mean_image

损失函数与梯度计算

def svm_loss_naive(W, X, y, reg):

dW = np.zeros(W.shape) # initialize the gradient as zero

# compute the loss and the gradient

num_classes = W.shape[1]

num_train = X.shape[0]

loss = 0.0

for i in xrange(num_train):

scores = X[i].dot(W)

correct_class_score = scores[y[i]]

for j in xrange(num_classes):

if j == y[i]:

continue

margin = scores[j] - correct_class_score + 1 # note delta = 1

if margin > 0:

loss += margin

dW[:, y[i]] += -X[i, :] # compute the correct_class gradients

dW[:, j] += X[i, :] # compute the wrong_class gradients

# Right now the loss is a sum over all training examples, but we want it

# to be an average instead so we divide by num_train.

loss /= num_train

dW /= num_train

dW += 2 * reg * W

# Add regularization to the loss.

loss += 0.5 * reg * np.sum(W * W)

return loss, dW

梯度检验

def grad_check_sparse(f, x, analytic_grad, num_checks=10, h=1e-5):

"""

sample a few random elements and only return numerical

in this dimensions.

"""

for i in xrange(num_checks):

ix = tuple([randrange(m) for m in x.shape])

oldval = x[ix]

x[ix] = oldval + h # increment by h

fxph = f(x) # evaluate f(x + h)

x[ix] = oldval - h # increment by h

fxmh = f(x) # evaluate f(x - h)

x[ix] = oldval # reset

grad_numerical = (fxph - fxmh) / (2 * h)

grad_analytic = analytic_grad[ix]

rel_error = abs(grad_numerical - grad_analytic) / (abs(grad_numerical) + abs(grad_analytic))

print 'numerical: %f analytic: %f, relative error: %e' % (grad_numerical, grad_analytic, rel_error)

from cs231n.gradient_check import grad_check_sparse

f = lambda w: svm_loss_naive(w, X_dev, y_dev, 0.0)[0]

grad_numerical = grad_check_sparse(f, W, grad)

# do the gradient check once again with regularization turned on

# you didn't forget the regularization gradient did you?

loss, grad = svm_loss_naive(W, X_dev, y_dev, 1e2)

f = lambda w: svm_loss_naive(w, X_dev, y_dev, 1e2)[0]

grad_numerical = grad_check_sparse(f, W, grad)

使用向量化方法计算损失函数与梯度

def svm_loss_vectorized(W, X, y, reg):

loss = 0.0

dW = np.zeros(W.shape) # initialize the gradient as zero

scores = X.dot(W) # N by C

num_train = X.shape[0]

num_classes = W.shape[1]

scores_correct = scores[np.arange(num_train), y] #1 by N

scores_correct = np.reshape(scores_correct, (num_train, 1)) # N by 1

margins = scores - scores_correct + 1.0 # N by C

margins[np.arange(num_train), y] = 0.0

margins[margins <= 0] = 0.0

loss += np.sum(margins) / num_train

loss += 0.5 * reg * np.sum(W * W)

margins[margins > 0] = 1.0 #

row_sum = np.sum(margins, axis=1) # 1 by N

margins[np.arange(num_train), y] = -row_sum

dW += np.dot(X.T, margins)/num_train + reg * W # D by C

return loss, dW

随机梯度下降

def train(self, X, y, learning_rate=1e-3, reg=1e-5, num_iters=100,

batch_size=200, verbose=True):

num_train, dim = X.shape

# assume y takes values 0...K-1 where K is number of classes

num_classes = np.max(y) + 1

if self.W is None:

# lazily initialize W

self.W = 0.001 * np.random.randn(dim, num_classes) # D by C

# Run stochastic gradient descent(Mini-Batch) to optimize W

loss_history = []

for it in xrange(num_iters):

X_batch = None

y_batch = None

# Sampling with replacement is faster than sampling without replacement.

sample_index = np.random.choice(num_train, batch_size, replace=False)

X_batch = X[sample_index, :] # batch_size by D

y_batch = y[sample_index] # 1 by batch_size

# evaluate loss and gradient

loss, grad = self.loss(X_batch, y_batch, reg)

loss_history.append(loss)

# perform parameter update

self.W += -learning_rate * grad

if verbose and it % 100 == 0:

print 'Iteration %d / %d: loss %f' % (it, num_iters, loss)

return loss_history

随着梯度下降过程中,损失函数的变化

# A useful debugging strategy is to plot the loss as a function of

# iteration number:

plt.plot(loss_hist)

plt.xlabel('Iteration number')

plt.ylabel('Loss value')

plt.show()

预测

def predict(self, X):

#print X.shape,self.W.shape

scores = X.dot(self.W)

y_pred = np.argmax(scores, axis = 1)

return y_pred

validation验证集

learning_rates = [1.4e-7, 1.5e-7, 1.6e-7]

regularization_strengths = [(1+i*0.1)*1e4 for i in range(-3,3)] + [(2+0.1*i)*1e4 for i in range(-3,3)]

results = {}

best_val = -1 # The highest validation accuracy that we have seen so far.

best_svm = None # The LinearSVM object that achieved the highest validation rate.

for rs in regularization_strengths:

for lr in learning_rates:

svm = LinearSVM()

loss_hist = svm.train(X_train, y_train, lr, rs, num_iters=3000)

y_train_pred = svm.predict(X_train)

train_accuracy = np.mean(y_train == y_train_pred)

y_val_pred = svm.predict(X_val)

val_accuracy = np.mean(y_val == y_val_pred)

if val_accuracy > best_val:

best_val = val_accuracy

best_svm = svm

results[(lr,rs)] = train_accuracy, val_accuracy

# Print out results.

for lr, reg in sorted(results):

train_accuracy, val_accuracy = results[(lr, reg)]

print 'lr %e reg %e train accuracy: %f val accuracy: %f' % (

lr, reg, train_accuracy, val_accuracy)

print 'best validation accuracy achieved during cross-validation: %f' % best_val

可视化cross-validation结果

import math

x_scatter = [math.log10(x[0]) for x in results]

y_scatter = [math.log10(x[1]) for x in results]

# plot training accuracy

marker_size = 100

colors = [results[x][0] for x in results]

plt.subplot(2, 1, 1)

plt.scatter(x_scatter, y_scatter, marker_size, c=colors)

plt.colorbar()

plt.xlabel('log learning rate')

plt.ylabel('log regularization strength')

plt.title('CIFAR-10 training accuracy')

# plot validation accuracy

colors = [results[x][1] for x in results] # default size of markers is 20

plt.subplot(2, 1, 2)

plt.scatter(x_scatter, y_scatter, marker_size, c=colors)

plt.colorbar()

plt.xlabel('log learning rate')

plt.ylabel('log regularization strength')

plt.title('CIFAR-10 validation accuracy')

plt.show()

将所得bestSVM应用于测试集,结果为 0.381000

可视化wight各图片成分

w = best_svm.W[:-1,:] # strip out the bias

w = w.reshape(32, 32, 3, 10)

w_min, w_max = np.min(w), np.max(w)

classes = ['plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']

for i in xrange(10):

plt.subplot(2, 5, i + 1)

# Rescale the weights to be between 0 and 255

wimg = 255.0 * (w[:, :, :, i].squeeze() - w_min) / (w_max - w_min)

plt.imshow(wimg.astype('uint8'))

plt.axis('off')

plt.title(classes[i])

svm实现图片分类(python)_SVM分类器python实现相关推荐

  1. svm实现图片分类(python)

    目录 前言 knn vs. svm svm & linear classifier bias trick loss function regularization optimization 代 ...

  2. 图片分类-K近邻分类器

    你可以注意到当我们做预测的时候,如果仅仅用最近的图片的是远远不够的.其实,我们更常用的是k近邻分类器.这个思想非常简单.代替寻找训练集中最近的图片,我们会寻找k个最相近的图片,并且让他们再测试图片上投 ...

  3. HOG + SVM 实现图片分类(python3)

    前言 大家能看到这篇文章,想必对HOG还是有些了解了,那我就不赘述了,其实我自己不太懂,但是还是比刚开始好一些了.下面我的代码是参考这位博主的:点我查看 上面那位博主是用的cifar-10数据集,但是 ...

  4. svm+特征提取做分类

    使用SVM做一个图片分类器,主要使用的技术是,各种特征提取方法加上PCA主成分提取,最后用SVM进行图片分类处理. 特征提取这个东西还是比较简单的,前人做的工作很多,源代码也不少.主要采用的不变距.H ...

  5. 图片分类-python

    目的:做一个简易的图片分类. 使用到的算法:hog.surf+svm 图片集:cifar-10.cifar-100.stl-10.自制图片集 分类完整代码链接 使用说明: 1.cifar-10.cif ...

  6. python鸢尾花分类svm测试集_使用SVM对鸢尾花分类

    使用SVM对鸢尾花分类 百度AI Studio中的一个入门项目,增加了自己在实践时的一些注释,对小白来说阅读更顺畅.源码和数据在github上. 任务描述: 构建一个模型,根据鸢尾花的花萼和花瓣大小将 ...

  7. Python+sklearn使用支持向量机算法实现数字图片分类

    关于支持向量机的理论知识,大家可以查阅机器学习之类的书籍或网上资源,本文主要介绍如何使用Python扩展库sklearn中的支持向量机实现数字图片分类. 1.首先编写代码生成一定数量的含有数字的图片 ...

  8. EasyDL之图片分类API接口实现-基于python语言

    因最近需要用到图片分类的应用,但是找来找去没有用的比较顺手的,所以就自己找到了EasyDL平台进行一个简单的图片分类开发,今天就说一下用python实现对EasyDL平台训练好的模型进行API调用. ...

  9. python图片分类管理系统_图片分类工具MyQcloudImage免费版下载(图片管理系统) V1.0 绿色版_数码资源网...

    需要智能人脸识别图片管理软件?MyQcloudImage免费版肯定是您需要的哦!图片分类工具这里有最新的最强大的图片数据分类和图片识别功能可以了解哦!MyQcloudImage免费版最好用的图片管理系 ...

最新文章

  1. 2022-2028年中国特高压电网行业深度调研及投资前景预测报告
  2. 【camera】基于YOLO的车辆多维特征识别系统(车色,车品牌,车标,车型)与PYQT实现(课程设计)
  3. win7 64位安装mysql教程视频_64位Win7系统安装Mysql 5.7.22图文教程
  4. 集合-ArrayList
  5. 【Python应用】Python+Kepler.gl轻松制作酷炫路径动画
  6. 跑faster rcnn测试时遇到错误Attribute Error: 'NoneType' object has no attribute 'astype'
  7. 【参数】REMOTE_LOGIN_PASSWORDFILE参数三种取值及其行为特性分析
  8. 转【FullPage.js 应用参数参考与简单调用】
  9. mysql数据导入导出 CSV格式_MySQL中导入导出CSV格式数据
  10. java内功 ---- jvm虚拟机原理总结,侧重于虚拟机类加载执行系统
  11. 进阶程序员都不会急于学习编程语言,知道原因吗?
  12. axios get和post请求带参数和headers配置
  13. c语言队列ADT 学习总结
  14. 领域驱动设计核心概念
  15. adb shell /system/bin/screencap screenrecord
  16. unity3——Humanoid与generic的区别
  17. 泰国将于5月1日全面开放,来曼谷骑行探索老城区
  18. 精益和敏捷的较量:你知道敏捷开发有 Scrum 和 Kanban 两种管理模式吗?
  19. Flask后端实践 连载十三 Flask输出Excel报表
  20. java正则表达式控制格式_“如果Java受到一两个大型供应商的控制,则可能会遭受挫折”...

热门文章

  1. DES, RC4, RC5, AES, RSA, MD5, SHA1 安全算法分析
  2. 04154 项目采购管理 自考学习笔记 -第一章项目采购管理概述
  3. MySQL开发规范与使用技巧总结
  4. 好玩的切西瓜游戏有哪些?教你玩转好玩的切西瓜游戏
  5. 南工大计算机录取分数线,南京工业大学2016年录取分数线
  6. PHP地方装修门户系统
  7. 如何实现表格中客户数据自动录入CRM系统
  8. (小记)matlab画二维图
  9. 微型计算机测控系统课程设计报告,微机控制课程设计报告电阻加热炉温度控制系统设计.doc...
  10. 网站web一键生成exe应用 chrome