代码备份:处理 SUN397 的代码,将其分为 80% 训练数据 以及 20% 的测试数据
处理SUN397 的代码,将其分为80% 训练数据以及20% 的测试数据 2016-07-27 1 %% Code for Process SUN397 Scene Classification
2 % Just the a part : 24 kinds and 6169 images total
3 % used for train a initial classifier and predict the additional dataset.
4 clc;
5 impath = '/home/wangxiao/Downloads/SUN397/SUN397/a/';
6 files = dir(impath);
7 label = -1 ;
8
9 train_fid = fopen('/home/wangxiao/Downloads/SUN397/selected_sun/train_list.txt', 'a');
10 test_fid = fopen('/home/wangxiao/Downloads/SUN397/selected_sun/test_list.txt', 'a');
11
12 train_im_savePath = '/home/wangxiao/Downloads/SUN397/selected_sun/train_images/' ;
13 test_im_savePath = '/home/wangxiao/Downloads/SUN397/selected_sun/test_images/' ;
14
15 for i = 3:size(files, 1)
16 % disp( [' ==> disp current ', num2str(i-2), '/', num2str(size(files, 1) - 2) , ' waiting . . . ' ]) ;
17 label = label + 1;
18 category = files(i).name ;
19 newPath = [impath, category, '/'] ;
20 images = dir([newPath, '*.jpg']) ;
21
22 for j = 1:size(images, 1)
23 disp( [' ==> deal with Class: ', num2str(i-2), ' ==> disp image: ', num2str(j), '/', num2str(size(images, 1) - 2) , ' waiting . . . ' ]) ;
24 num_per_kind = size(images, 1) - 2 ;
25 random_num = randperm(size(images, 1)) ;
26
27 num_train = round( num_per_kind * 0.8 ) ; %% number of train data
28 num_test = round ( num_per_kind * 0.2 ) ; %% number of test data
29
30 %% train data
31
32 if j <= num_train
33
34 idx = random_num(j) ;
35 trainImage_name = images(idx).name ;
36 im = imread([newPath, trainImage_name]);
37 im = imresize(im, [256, 256]) ;
38 imwrite( im, [train_im_savePath, trainImage_name]) ;
39 fprintf(train_fid, '%s ' , num2str(trainImage_name) ) ;
40 fprintf(train_fid, '%s ', ' ') ;
41 fprintf(train_fid, '%s \n', num2str(label)) ;
42 else
43 if j < num_per_kind
44 idx2 = random_num(j) ;
45 testImage_name = images(idx2).name ;
46 im2 = imread([newPath, testImage_name]);
47 im2 = imresize(im2, [227, 227]) ;
48 imwrite( im2, [test_im_savePath, testImage_name]) ;
49 fprintf(test_fid, '%s ' , num2str(testImage_name) ) ;
50 fprintf(test_fid, '%s ', ' ') ;
51 fprintf(test_fid, '%s \n', num2str(label)) ;
52 else
53 break;
54 end
55 end
56
57
58
59
60
61
62 end
63
64 end
path = '/home/wangxiao/Downloads/SUN397/Sun-100/';
file1 = importdata([path, 'Sun_100_Labeled_Train_0.5_.txt' ]);
file2 = importdata([path, 'Sun_100_UnLabel_Train_0.5_.txt' ]);
file3 = importdata([path, 'Sun_100_Test_0.5_.txt' ]); %% return the index of searched vector.
[C, ia, ic] = unique(file1.data) ;
labelMatrix = zeros(size(file1.data)) ;
for i = 1:size(ia, 1)
count = i-1;
index_1 = ia(i, 1) ; % start index
index_2 = ia(i+1, 1) ; % end index
labelMatrix(index_1:index_2, 1) = count ;
end
% select 80 classes.
select_labelMatrix = labelMatrix(1:9060) ; %% return the index of searched vector.
[C, ia, ic] = unique(file2.data) ;
labelMatrix = zeros(size(file2.data)) ;
for i = 1:size(ia, 1)
count = i-1;
index_1 = ia(i, 1) ; % start index
index_2 = ia(i+1, 1) ; % end index
labelMatrix(index_1:index_2, 1) = count ;
end
% select 80 classes.
select_labelMatrix_2 = labelMatrix(1:9180) ; %% return the index of searched vector.
[C, ia, ic] = unique(file3.data) ;
labelMatrix = zeros(size(file3.data)) ;
for i = 1:size(ia, 1)
count = i-1;
index_1 = ia(i, 1) ; % start index
index_2 = ia(i+1, 1) ; % end index
labelMatrix(index_1:index_2, 1) = count ;
end
% select 80 classes.
select_labelMatrix_3 = labelMatrix(1:4560) ; %% save the selected 80 classes into txt files.
savePath = '/home/wangxiao/Downloads/SUN397/Sun-100/';
fid1 = fopen([savePath, 'Sun_80_50%_Labeled_data.txt'], 'a');
fid2 = fopen([savePath, 'Sun_80_50%_Unlabeled_data.txt'], 'a');
fid3 = fopen([savePath, 'Sun_80_50%_test_data.txt'], 'a'); for i = 1:size(select_labelMatrix, 1)
imageName = file1.textdata{i, 1} ;
imageLabel = select_labelMatrix(i, 1) ;
fprintf(fid1, '%s ', num2str(imageName)) ;
fprintf(fid1, '%s\n ', num2str(imageLabel)) ;
end for i = 1:size(select_labelMatrix_2, 1)
imageName = file2.textdata{i, 1} ;
imageLabel = select_labelMatrix_2(i, 1) ;
fprintf(fid2, '%s ', num2str(imageName)) ;
fprintf(fid2, '%s\n ', num2str(imageLabel)) ;
end for i = 1:size(select_labelMatrix_3, 1)
imageName = file3.textdata{i, 1} ;
imageLabel = select_labelMatrix_3(i, 1) ;
fprintf(fid3, '%s ', num2str(imageName)) ;
fprintf(fid3, '%s\n ', num2str(imageLabel)) ;
end
代码备份:处理 SUN397 的代码,将其分为 80% 训练数据 以及 20% 的测试数据的更多相关文章
- python numpy 三行代码打乱训练数据
今天发现一个用 numpy 随机化数组的技巧. 需求 我有两个数组( ndarray ):train_datasets 和 train_labels.其中,train_datasets 的每一行和 t ...
- 博客使用的CSS代码备份
CSS代码备份 /*simplememory*/ #google_ad_c1, #google_ad_c2 { display: none; } .syntaxhighlighter a, .synt ...
- 1.svn 彻底clear时,注意代码备份 2.借助vc助手加头文件
1.svn 彻底clear时,注意代码备份 2.不小心彻底clear可以在回收站找到 3.借助vc助手加头文件
- 同时将代码备份到Gitee和GitHub
同时将代码备份到Gitee和GitHub 如何将GitHub项目一步导入Gitee 如何保持Gitee和GitHub同步更新 如何将GitHub项目一步导入Gitee 方法一: 登陆 Gitee 账号 ...
- 代码备份 | 博客侧边栏公告(支持HTML代码)(支持JS代码)
博客侧边栏公告(支持HTML代码)(支持JS代码) <div id='btnList'> <a class="ivu-btn ivu-btn-primary" h ...
- 每周一书-编写高质量代码:改善C程序代码的125个建议
首先说明,本周活动有效时间为2016年8月28日到2016年9月4日.本周为大家送出的书是由机械工业出版社出版,马伟编著的<编写高质量代码:改善C程序代码的125个建议>. 编辑推荐 10 ...
- python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例
python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例 新浪爱彩双色球开奖数据URL:http://zst.aicai.com/ssq/openInfo/ 最终输出结果格 ...
- C++统计代码注释行数 & 有效代码行数 & 代码注释公共行 & 函数个数
问题来源,在14年的暑假的一次小项目当中遇到了一个这样的问题,要求统计C++代码的注释行数,有效代码行数,代码注释公共行数,以及函数个数. 下面稍微解释一下问题, 1)注释行数:指有注释的行,包括有代 ...
- c代码中调用c++,c++代码中调用c代码
注意这里的c调用c++或者c++调用c的意思是.c文件中调用.cpp文件中的代码,或者相反 集成开发环境如vc++6.0或者vs都是通过文件后缀来区别当前要编译的是C代码还是C++代码,然后采用相应的 ...
随机推荐
- hadoop生态系统的详细介绍
1.Hadoop生态系统概况 Hadoop是一个能够对大量数据进行分布式处理的软件框架.具有可靠.高效.可伸缩的特点. Hadoop的核心是HDFS和MapReduce,hadoop2.0还包括YAR ...
- (转)SoftReference
本文介绍对象的强.软.弱和虚引用的概念.应用及其在UML中的表示. 1.对象的强.软.弱和虚引用 在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象.也就是说, ...
- init()和deinit()
一.初始化方法(init()) 1.定义:类初始化对象时所调用的方法 2.分类: (1)默认初始化方法 (2)便利初始化方法 (3)使用闭包 3.一些注意点: (1)方法固定名为init,没有返回值, ...
- Stm32_调试出现 Error:Flash Download Failed-"Cortex-M3"
rror:Flash Download Failed-"Cortex-M3"出现一般有两种情况: 1.SWD模式下,Debug菜单中,Reset菜单选项(Autodetect/HW ...
- php中ckeditor(Fckeditor)的配置方法
ckeditor 编辑器php正确配置方法 1. 下载安装 CKEditor: http://ckeditor.com/ 解压下载到的CKEditor放到网站的路径中即可 2. 下载安装 CKFind ...
- 如何实现标准TCODE的屏幕增强
如何实现标准TCODE的屏幕增强(HOWTO:Implement a screen exit to a standard SAP transaction) Introduction SAP provi ...
- Linux中的汇编简介
GNU as汇编语法 GNU汇编语法使用的是AT&T汇编它和Intel汇编的语法主要有以下一些不同: AT&T汇编中的立即操作数前面要加上'$',寄存器操作数名前要加上百分号'%',绝 ...
- VIM_插件
VIM进阶:插件 通过一段时间的练习,你就可以非常熟练的使用VIM.即使是"裸奔",VIM已经足够强大,能够完成日常的绝大部分工作. 但VIM更加强大的是它的扩展机 ...
- JQuery源码分析(五)
分离构造器 通过new 操作符构建一个对象,一般经过四部: A.创建一个新对象 B.将构造函数的作用域赋给新对象(所以this就指向了这个新对象) C.执行构造函数中的代码 D.返回这 ...
- Thread和Service应用场合的区别
Thread和Service的区别: 1) Thread 是程序执行的最小单元,它是分配CPU的基本单位,可以用 Thread 来执行一些异步的操作. 如果是Local Service,那么对应的 S ...