代码备份:处理 SUN397 的代码,将其分为 80% 训练数据 以及 20% 的测试数据

简介: 处理SUN397 的代码,将其分为80% 训练数据以及20% 的测试数据 2016-07-27 1 %% Code for Process SUN397 Scene Classification 2 % Just the a part : 24 kinds ...
 

处理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

  

相关文章
|
1月前
Mybatis+mysql动态分页查询数据案例——测试类HouseDaoMybatisImplTest)
Mybatis+mysql动态分页查询数据案例——测试类HouseDaoMybatisImplTest)
21 1
|
1月前
|
Java 关系型数据库 数据库连接
Mybatis+MySQL动态分页查询数据经典案例(含代码以及测试)
Mybatis+MySQL动态分页查询数据经典案例(含代码以及测试)
26 1
|
2月前
|
测试技术
包含用例执行时间的测试报告代码
包含用例执行时间的测试报告代码
|
2月前
|
计算机视觉
Google Earth Engine(GEE)——使用MODIS数据单点测试SG滤波和harmonics method 滤波的差异分析
Google Earth Engine(GEE)——使用MODIS数据单点测试SG滤波和harmonics method 滤波的差异分析
47 0
|
3月前
|
SQL 存储 数据库连接
自动生成测试数据—数据库篇
自动生成测试数据—数据库篇
|
17天前
|
算法 安全 Java
java代码 实现AES_CMAC 算法测试
该代码实现了一个AES-CMAC算法的简单测试,使用Bouncy Castle作为安全提供者。静态变量K定义了固定密钥。`Aes_Cmac`函数接受密钥和消息,返回AES-CMAC生成的MAC值。在`main`方法中,程序对给定的消息进行AES-CMAC加密,然后模拟接收ECU的加密结果并进行比较。如果两者匹配,输出&quot;验证成功&quot;,否则输出&quot;验证失败&quot;。辅助方法包括将字节转为16进制字符串和将16进制字符串转为字节。
|
19天前
|
人工智能 分布式计算 Kubernetes
人工智能,应该如何测试?(三)数据构造与性能测试篇
本文探讨了人工智能场景中的性能测试,区别于传统互联网测试,其复杂性更高。主要关注点包括两类AI产品——业务类和平台类,后者涉及AI全生命周期,测试难度更大。测试重点是模型训练的性能,特别是数据模拟。需要构造大量结构化数据,如不同规模、分布、分片和特征规模的数据,以评估算法效率。此外,还涉及模拟设备规模(如视频流)和节点规模(边缘计算),以测试在大规模负载下的系统性能。文中提到了使用工具如Spark、ffmpeg、流媒体服务器和Kubernetes(K8S)的扩展项目,如Kubemark,来模拟大规模环境。最后,文章介绍了使用Golang进行异步IO操作以构建海量小文件,优化IO性能。
42 0
|
1月前
|
测试技术 数据库 Python
python测试代码(二)
python测试代码(二)
19 0
|
1月前
|
Java 测试技术
单元测试编写可测试代码
单元测试编写可测试代码
19 2
|
1月前
|
存储 Android开发 C++
【Android 从入门到出门】第五章:使用DataStore存储数据和测试
【Android 从入门到出门】第五章:使用DataStore存储数据和测试
36 3

热门文章

最新文章