package libsvm;
import java.io.*;
import java.util.*;
//
// Kernel Cache
//
// l is the number of total data items
// size is the cache size limit in bytes
//
class Cache {
private final int l;
private long size;
private final class head_t
{
head_t prev, next; // a cicular list
float[] data;
int len; // data[0,len) is cached in this entry
}
private final head_t[] head;
private head_t lru_head;
Cache(int l_, long size_)
{
l = l_;
size = size_;
head = new head_t[l];
for(int i=0;i<l;i++) head[i] = new head_t();
size /= 4;
size -= l * (16/4); // sizeof(head_t) == 16
size = Math.max(size, 2* (long) l); // cache must be large enough for two columns
lru_head = new head_t();
lru_head.next = lru_head.prev = lru_head;
}
private void lru_delete(head_t h)
{
// delete from current location
h.prev.next = h.next;
h.next.prev = h.prev;
}
private void lru_insert(head_t h)
{
// insert to last position
h.next = lru_head;
h.prev = lru_head.prev;
h.prev.next = h;
h.next.prev = h;
}
// request data [0,len)
// return some position p where [p,len) need to be filled
// (p >= len if nothing needs to be filled)
// java: simulate pointer using single-element array
int get_data(int index, float[][] data, int len)
{
head_t h = head[index];
if(h.len > 0) lru_delete(h);
int more = len - h.len;
if(more > 0)
{
// free old space
while(size < more)
{
head_t old = lru_head.next;
lru_delete(old);
size += old.len;
old.data = null;
old.len = 0;
}
// allocate new space
float[] new_data = new float[len];
if(h.data != null) System.arraycopy(h.data,0,new_data,0,h.len);
h.data = new_data;
size -= more;
do {int tmp=h.len; h.len=len; len=tmp;} while(false);
}
lru_insert(h);
data[0] = h.data;
return len;
}
void swap_index(int i, int j)
{
if(i==j) return;
if(head[i].len > 0) lru_delete(head[i]);
if(head[j].len > 0) lru_delete(head[j]);
do {float[] tmp=head[i].data; head[i].data=head[j].data; head[j].data=tmp;} while(false);
do {int tmp=head[i].len; head[i].len=head[j].len; head[j].len=tmp;} while(false);
if(head[i].len > 0) lru_insert(head[i]);
if(head[j].len > 0) lru_insert(head[j]);
if(i>j) do {int tmp=i; i=j; j=tmp;} while(false);
for(head_t h = lru_head.next; h!=lru_head; h=h.next)
{
if(h.len > i)
{
if(h.len > j)
do {float tmp=h.data[i]; h.data[i]=h.data[j]; h.data[j]=tmp;} while(false);
else
{
// give up
lru_delete(h);
size += h.len;
h.data = null;
h.len = 0;
}
}
}
}
}
//
// Kernel evaluation
//
// the static method k_function is for doing single kernel evaluation
// the constructor of Kernel prepares to calculate the l*l kernel matrix
// the member function get_Q is for getting one column from the Q Matrix
//
abstract class QMatrix {
abstract float[] get_Q(int column, int len);
abstract double[] get_QD();
abstract void swap_index(int i, int j);
};
abstract class Kernel extends QMatrix {
private svm_node[][] x;
private final double[] x_square;
// svm_parameter
private final int kernel_type;
private final int degree;
private final double gamma;
private final double coef0;
abstract float[] get_Q(int column, int len);
abstract double[] get_QD();
void swap_index(int i, int j)
{
do {svm_node[] tmp=x[i]; x[i]=x[j]; x[j]=tmp;} while(false);
if(x_square != null) do {double tmp=x_square[i]; x_square[i]=x_square[j]; x_square[j]=tmp;} while(false);
}
private static double powi(double base, int times)
{
double tmp = base, ret = 1.0;
for(int t=times; t>0; t/=2)
{
if(t%2==1) ret*=tmp;
tmp = tmp * tmp;
}
return ret;
}
double kernel_function(int i, int j)
{
switch(kernel_type)
{
case svm_parameter.LINEAR:
return dot(x[i],x[j]);
case svm_parameter.POLY:
return powi(gamma*dot(x[i],x[j])+coef0,degree);
case svm_parameter.RBF:
return Math.exp(-gamma*(x_square[i]+x_square[j]-2*dot(x[i],x[j])));
case svm_parameter.SIGMOID:
return Math.tanh(gamma*dot(x[i],x[j])+coef0);
case svm_parameter.PRECOMPUTED:
return x[i][(int)(x[j][0].value)].value;
default:
return 0; // java
}
}
Kernel(int l, svm_node[][] x_, svm_parameter param)
{
this.kernel_type = param.kernel_type;
this.degree = param.degree;
this.gamma = param.gamma;
this.coef0 = param.coef0;
x = (svm_node[][])x_.clone();
if(kernel_type == svm_parameter.RBF)
{
x_square = new double[l];
for(int i=0;i<l;i++)
x_square[i] = dot(x[i],x[i]);
}
else x_square = null;
}
static double dot(svm_node[] x, svm_node[] y)
{
double sum = 0;
int xlen = x.length;
int ylen = y.length;
int i = 0;
int j = 0;
while(i < xlen && j < ylen)
{
if(x[i].index == y[j].index)
sum += x[i++].value * y[j++].value;
else
{
if(x[i].index > y[j].index)
++j;
else
++i;
}
}
return sum;
}
static double k_function(svm_node[] x, svm_node[] y,
svm_parameter param)
{
switch(param.kernel_type)
{
case svm_parameter.LINEAR:
return dot(x,y);
case svm_parameter.POLY:
return powi(param.gamma*dot(x,y)+param.coef0,param.degree);
case svm_parameter.RBF:
{
double sum = 0;
int xlen = x.length;
int ylen = y.length;
int i = 0;
int j = 0;
while(i < xlen && j < ylen)
{
if(x[i].index == y[j].index)
{
double d = x[i++].value - y[j++].value;
sum += d*d;
}
else if(x[i].index > y[j].index)
{
sum += y[j].value * y[j].value;
++j;
}
else
{
sum += x[i].value * x[i].value;
++i;
}
}
while(i < xlen)
{
sum += x[i].value * x[i].value;
++i;
}
while(j < ylen)
{
sum += y[j].value * y[j].value;
++j;
}
return Math.exp(-param.gamma*sum);
}
case svm_parameter.SIGMOID:
return Math.tanh(param.gamma*dot(x,y)+param.coef0);
case svm_parameter.PRECOMPUTED:
return x[(int)(y[0].value)].value;
default:
return 0; // java
}
}
}
// An SMO algorithm in Fan et al., JMLR 6(2005), p. 1889--1918
// Solves:
//
// min 0.5(\alpha^T Q \alpha) + p^T \alpha
//
// y^T \alpha = \delta
// y_i = +1 or -1
// 0 <= alpha_i <= Cp for y_i = 1
// 0 <= alpha_i <= Cn for y_i = -1
//
// Given:
//
// Q, p, y, Cp, Cn, and an initial feasible point \alpha
// l is the size of vectors and matrices
// eps is the stopping tolerance
//
// solution will be put in \alpha, objective value will be put in obj
//
class Solver {
int active_size;
byte[] y;
double[] G; // gradient of objective function
static final byte LOWER_BOUND = 0;
static final byte UPPER_BOUND = 1;
static final byte FREE = 2;
byte[] alpha_status; // LOWER_BOUND, UPPER_BOUND, FREE
double[] alpha;
QMatrix Q;
double[] QD;
double eps;
double Cp,Cn;
double[] p;
int[] active_set;
double[] G_bar; // gradient, if we treat free variables as 0
int l;
boolean unshrink; // XXX
static final double INF = java.lang.Double.POSITIVE_INFINITY;
double get_C(int i)
{
return (y[i] > 0)? Cp : Cn;
}
void update_alpha_status(int i)
{
if(alpha[i] >= get_C(i))
alpha_status[i] = UPPER_BOUND;
else if(alpha[i] <= 0)
alpha_status[i] = LOWER_BOUND;
else alpha_status[i] = FREE;
}
boolean is_upper_bound(int i) { return alpha_status[i] == UPPER_BOUND; }
boolean is_lower_bound(int i) { return alpha_status[i] == LOWER_BOUND; }
boolean is_free(int i) { return alpha_status[i] == FREE; }
// java: information about solution except alpha,
// because we cannot return multiple values otherwise...
static class SolutionInfo {
double obj;
double rho;
double upper_bound_p;
double upper_bound_n;
double r; // for Solver_NU
}
void swap_index(int i, int j)
{
Q.swap_index(i,j);
do {byte tmp=y[i]; y[i]=y[j]; y[j]=tmp;} while(false);
do {double tmp=G[i]; G[i]=G[j]; G[j]=tmp;} while(false);
do {byte tmp=alpha_status[i]; alpha_status[i]=alpha_status[j]; alpha_status[j]=tmp;} whi
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
1.版本:matlab2021a,包含仿真操作录像,操作录像使用windows media player播放。 2.领域:图像分割 3.内容:基于SVM支持向量机的彩色图像目标分割算法matlab仿真,svm采用libsvm工具箱。仿真输出分割后的目标。 4.注意事项:注意MATLAB左侧当前文件夹路径,必须是程序所在文件夹位置,具体可以参考视频录。
资源推荐
资源详情
资源评论
收起资源包目录
基于SVM支持向量机的彩色图像目标分割算法matlab仿真,svm采用libsvm工具箱,含仿真操作录像 (143个子文件)
操作录像0040.avi 5.53MB
svmtrain.c 12KB
svmtrain.c 11KB
svmpredict.c 10KB
svmpredict.c 10KB
svm-train.c 9KB
svm-train.c 9KB
svm-scale.c 8KB
svm-scale.c 8KB
svm_model_matlab.c 8KB
svm_model_matlab.c 8KB
interface.c 6KB
interface.c 6KB
svm-predict.c 5KB
svm-predict.c 5KB
libsvmread.c 4KB
libsvmread.c 4KB
libsvmwrite.c 2KB
libsvmwrite.c 2KB
main.c 398B
main.c 398B
COPYRIGHT 1KB
COPYRIGHT 1KB
svm.cpp 64KB
svm.cpp 63KB
svm-toy.cpp 11KB
svm-toy.cpp 11KB
callbacks.cpp 10KB
callbacks.cpp 10KB
svm-toy.cpp 10KB
svm-toy.cpp 10KB
svm.def 477B
svm.def 477B
libsvm.dll 253KB
libsvm.dll 157KB
.DS_Store 6KB
.DS_Store 6KB
svm-train.exe 243KB
svm-toy.exe 222KB
svm-predict.exe 208KB
svm-scale.exe 162KB
svm-train.exe 152KB
svm-toy.exe 138KB
svm-predict.exe 123KB
svm-scale.exe 80KB
libsvmread.exp 604B
svm-toy.glade 6KB
svm-toy.glade 6KB
svm.h 3KB
svm.h 3KB
callbacks.h 2KB
callbacks.h 2KB
interface.h 203B
interface.h 203B
svm_model_matlab.h 201B
svm_model_matlab.h 201B
heart_scale 27KB
heart_scale 27KB
FAQ.html 81KB
FAQ.html 77KB
test_applet.html 81B
test_applet.html 81B
libsvm.jar 54KB
libsvm.jar 51KB
svm.java 63KB
svm.java 62KB
svm_toy.java 12KB
svm_toy.java 12KB
svm_scale.java 9KB
svm_scale.java 9KB
svm_train.java 8KB
svm_train.java 8KB
svm_predict.java 5KB
svm_predict.java 5KB
svm_parameter.java 1KB
svm_parameter.java 1KB
svm_model.java 868B
svm_model.java 868B
svm_problem.java 136B
svm_problem.java 136B
svm_node.java 115B
svm_node.java 115B
svm_print_interface.java 87B
svm_print_interface.java 87B
test.jpg 25KB
libsvmread.lib 2KB
drawedge.m 3KB
SVMcgForClass.m 3KB
validation_function.m 2KB
Runme.m 2KB
make.m 888B
make.m 777B
do_binary_cross_validation.m 638B
do_binary_predict.m 179B
svm.m4 62KB
svm.m4 62KB
Makefile 1KB
Makefile 1KB
Makefile 732B
Makefile 732B
共 143 条
- 1
- 2
资源评论
- 2301_823327042024-05-04资源不错,对我启发很大,获得了新的灵感,受益匪浅。
fpga和matlab
- 粉丝: 18w+
- 资源: 2639
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 数据库PostgreSQL
- gym-chrome-dino-master.zip
- S&P500全球行业分类标准的网络爬虫获取与解析
- 基于大数据与隐马尔科夫模型的拖拉机定位及轨迹预测系统
- 车道偏离预警系统-LDW,simulink和carsim联合仿真模型 模型中能够准确的实现预警功能,并且报告有驾驶员驾驶风格的判断,利用模糊控制的方法计算不同驾驶风格的驾驶员的预警时间 其中: 仿真
- 活泼轻快轻少年讲座课件模板.pptx
- 乒乓球素材小学体育教学课件模板.pptx
- 水彩风格画小学美术教学课件模板.pptx
- 水彩画儿童美术教学课件模板.pptx
- 小清新小学儿童教学课件模板.pptx
- 云朵山川儿童卡通教学课件模板.pptx
- 大数据技术驱动下的图书馆文献资源重组与再造解决方案
- 格子玻尔兹曼方法(LBM)SC伪势两相流模型
- 基于Java+Swing实现中国象棋游戏源码+说明(高分课程设计)
- JB-T 8126.2-2010 内燃机 冷却水泵 第2部分:总成 试验方法
- 基于Java+Swing实现中国象棋游戏代码+文档说明(高分课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功