Levi-Heath commited on
Commit
62615d9
·
verified ·
1 Parent(s): 710c2a5

Upload 16 files

Browse files
Data_Transformation/Run_Modularize_Data.m ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ nmb_of_labels=1000;
2
+ nmb_of_labs_per_module=25;
3
+ nmb_of_modules=(nmb_of_labels/nmb_of_labs_per_module);
4
+ relative_lab_seq=1:nmb_of_labs_per_module;
5
+ nmb_of_subsets=2;
6
+
7
+ patch=0;
8
+ parfor module=1:nmb_of_modules
9
+ m_label_ids=[];
10
+ m_labels=[];
11
+ m_data=[];
12
+ m_label_table=[relative_lab_seq;relative_lab_seq+(module-1)*nmb_of_labs_per_module];
13
+ for imgnt1kdataset=1:10
14
+ % change the path to the folder containing the ImageNet-1k mat
15
+ % files
16
+ reportname1 = sprintf('/work/mathbiology/lheath2/data/imagenet1k/mat/train_data_batch_%d.mat', imgnt1kdataset);
17
+ temp_lpad=load(reportname1,'data','labels') %
18
+ data=temp_lpad.data;
19
+ labels=temp_lpad.labels;
20
+ pos_seq=1:length(labels);
21
+ for labs=relative_lab_seq
22
+ idx=(labels==(labs+(module-1)*nmb_of_labs_per_module));
23
+ aa=pos_seq(idx);
24
+ bb=[0*aa+imgnt1kdataset;aa;labels(idx)];
25
+ m_label_ids=[m_label_ids, bb];
26
+ m_labels=[m_labels,0*aa+labs];
27
+ m_data=[m_data;data(idx,:)];
28
+ end
29
+ end
30
+ nmb_dt=length(m_labels);
31
+ set_lng=fix(nmb_dt/nmb_of_subsets);
32
+ for subset=1:nmb_of_subsets
33
+ if subset<nmb_of_subsets
34
+ set=(1:set_lng)+(subset-1)*set_lng;
35
+ else
36
+ set=(1+(subset-1)*set_lng):nmb_dt;
37
+ end
38
+ data=m_data(set,:);
39
+ labels=m_labels(:,set);
40
+ label_ids=m_label_ids(:,set);
41
+ label_table=m_label_table;
42
+ out=fun_save_modularized_data(patch, module, subset,nmb_of_labs_per_module,data,labels,label_ids,label_table)
43
+ end
44
+ module
45
+ end
Data_Transformation/Run_Transform_Data.m ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ % clear all
2
+ %
3
+ nmb_of_modules=40;
4
+ nmb_of_module_subsets=2;
5
+ channels_names={'R','G','B','RGg1','RBg1','GBg1','RGg2','RBg2','GBg2','RB','RG','GB','eRGB','BW','X','Y','Z'};
6
+ feature_RGB=[1 0 0
7
+ 0 1 0
8
+ 0 0 1
9
+ 0.618 0.382 0
10
+ 0.618 0 0.382
11
+ 0 0.618 0.382
12
+ 0.382 0.618 0
13
+ 0.382 0 0.618
14
+ 0 0.382 0.618
15
+ 0.5 0.5 0
16
+ 0.5 0 0.5
17
+ 0 0.5 0.5
18
+ 1/3 1/3 1/3
19
+ 0.299 0.587 0.114
20
+ 0.4125 0.3576 0.1804
21
+ 0.2126 0.7152 0.0722
22
+ 0.0193 0.1192 0.9502];
23
+ nmb_of_colors=length(channels_names);
24
+ patch=0;
25
+ nmb_of_labs_per_module=25;
26
+ cross_entropy=1;
27
+
28
+ param.image_size=[64,64];
29
+ param.downsizing=2;
30
+ param.x_trim=1;
31
+ param.y_trim=1;
32
+ param.compute_decimal_place=4;
33
+ param.dwnsz_on=1;
34
+
35
+ param.patch=patch;
36
+ param.nmb_of_modules=nmb_of_modules;
37
+ param.nmb_of_module_subsets=nmb_of_module_subsets;
38
+
39
+ param.cross_entropy=cross_entropy;
40
+ param.nmb_of_labs_per_module=nmb_of_labs_per_module;
41
+
42
+ param.channels_names=channels_names;
43
+ param.nmb_of_colors=nmb_of_colors;
44
+ param.feature_RGB=feature_RGB;
45
+
46
+ parfor module=1:nmb_of_modules
47
+ data_param=[];
48
+ for subset=1:nmb_of_module_subsets
49
+ reportname1 = sprintf('Transformed_IN1k_Data/Modularized_Data_for_SGD/modularized_data_patch_%d_module_%d_subset_%d_for_%d_labels_per_module.mat', ...
50
+ patch,module,subset,nmb_of_labs_per_module);
51
+ % save(reportname1,'data','labels','label_ids','label_table','data_param');
52
+ data_load=load(reportname1);
53
+ data_0=data_load.data;
54
+ output=fun_transform_data_rgbfeatures(data_0,param);
55
+ data=output.transformed_image;
56
+ data_param.mnsv=output.mnsv;
57
+ data_param.maxsv=output.maxsv;
58
+ data_param.ipvsz=output.ipvsz;
59
+ labels=data_load.labels;
60
+ label_ids=data_load.label_ids;
61
+ label_table=data_load.label_table;
62
+ out=fun_save_transformed_data(patch, module, subset,nmb_of_labs_per_module,data,labels,label_ids,label_table,data_param);
63
+ end
64
+ module
65
+ end
66
+ %%
Data_Transformation/fun_save_modularized_data.m ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ function out=fun_save_modularized_data(patch, module, subset,nmb_of_labs_per_module,data,labels,label_ids,label_table)
2
+ %
3
+ reportname1 = sprintf('Transformed_IN1k_Data/Modularized_Data_for_SGD/modularized_data_patch_%d_module_%d_subset_%d_for_%d_labels_per_module.mat', ...
4
+ patch,module,subset,nmb_of_labs_per_module);
5
+ % str=struct('data',data,'labels',labels,'label_ids',label_ids,'label_table',label_table,'data_param',data_param);
6
+ % save(reportname1,"-fromstruct",str);
7
+ save(reportname1,'data','labels','label_ids','label_table');
8
+ out=[];
9
+ end
Data_Transformation/fun_save_transformed_data.m ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ function out=fun_save_transformed_data(patch, module, subset,nmb_of_labs_per_module,data,labels,label_ids,label_table,data_param)
2
+ %
3
+ reportname1 = sprintf('Transformed_IN1k_Data/Transformed_Data_for_SGD/train_data_patch_%d_module_%d_subset_%d_for_%d_labels_per_module.mat', ...
4
+ patch,module,subset,nmb_of_labs_per_module);
5
+ % str=struct('data',data,'labels',labels,'label_ids',label_ids,'label_table',label_table,'data_param',data_param);
6
+ % save(reportname1,"-fromstruct",str);
7
+ save(reportname1,'data','labels','label_ids','label_table','data_param');
8
+ out=[];
9
+ end
Data_Transformation/fun_transform_data_rgbfeatures.m ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function output=fun_transform_data_rgbfeatures(img_data,param)
2
+ %
3
+ %img_data=data_load.data;
4
+ output.transformed_image=[];
5
+ output.mnsv=[];
6
+ output.maxsv=[];
7
+ output.ipvsz=[];
8
+
9
+ image_size=param.image_size;
10
+ img_x_dim=image_size(1);
11
+ img_y_dim=image_size(1);
12
+
13
+ compute_decimal_place=param.compute_decimal_place;
14
+ feature_RGB=param.feature_RGB;
15
+ [nmb_of_colors,~]=size(feature_RGB);
16
+
17
+ [data_size,~]=size(img_data);
18
+ reshaped_images=zeros(data_size,img_x_dim,img_y_dim,3);
19
+ for m=1:data_size
20
+ aa=double(img_data(m,:));
21
+ reshaped_images(m,:,:,:)=reshape(aa,img_x_dim,img_y_dim,3);
22
+ end
23
+
24
+ if param.dwnsz_on==1
25
+ x_trim=param.x_trim;
26
+ y_trim=param.y_trim;
27
+ downsizing=param.downsizing;
28
+ x_dwnsz_dim=fix(img_x_dim/downsizing);
29
+ y_dwnsz_dim=fix(img_y_dim/downsizing);
30
+
31
+ ipvsz=(x_dwnsz_dim-2*x_trim)*(y_dwnsz_dim-2*y_trim);
32
+ xsq=1:downsizing;
33
+ ysq=1:downsizing;
34
+ output.transformed_image=zeros(ipvsz,data_size,nmb_of_colors);
35
+ output.mnsv=zeros(data_size,nmb_of_colors);
36
+ output.maxsv=zeros(data_size,nmb_of_colors);
37
+ output.ipvsz=ipvsz;
38
+
39
+ temp_img=zeros(ipvsz,3);
40
+ for m=1:data_size
41
+ img=squeeze(reshaped_images(m,:,:,:));
42
+ for color=1:3
43
+ img1=squeeze(img(:,:,color));
44
+ aa=zeros(ipvsz,1);
45
+ for ii=(1+x_trim):(x_dwnsz_dim-x_trim)
46
+ for jj=(1+y_trim):(y_dwnsz_dim-y_trim)
47
+ aa((jj-y_trim)+((ii-x_trim)-1)*(y_dwnsz_dim-2*y_trim),1)=mean(img1(xsq+2*(ii-1),ysq+2*(jj-1)),'all');
48
+ end
49
+ end
50
+ temp_img(:,color)=aa;
51
+ end
52
+ for color=1:nmb_of_colors
53
+ c1=feature_RGB(color,1);
54
+ c2=feature_RGB(color,2);
55
+ c3=feature_RGB(color,3);
56
+ aa=c1*temp_img(:,1)+c2*temp_img(:,2)+c3*temp_img(:,3);
57
+ mnsv=mean(aa);
58
+ aa=aa-mnsv;
59
+ maxsv=max(abs(aa));
60
+ output.transformed_image(:,m,color)=round(aa/maxsv*10^compute_decimal_place)*10^(-compute_decimal_place);
61
+ output.mnsv(m,color)=mnsv;
62
+ output.maxsv(m,color)=maxsv;
63
+ end
64
+ end
65
+ else
66
+ temp_img=zeros(img_x_dim*img_y_dim,3);
67
+ ipvsz=img_x_dim*img_y_dim;
68
+ output.ipvsz=ipvsz;
69
+ for m=1:data_size
70
+ img=squeeze(reshaped_images(m,:,:,:));
71
+ for color=1:3
72
+ aa=img(:,:,color);
73
+ aa=aa(:);
74
+ temp_img(:,color)=aa;
75
+ end
76
+ for color=1:nmb_of_colors
77
+ c1=feature_RGB(color,1);
78
+ c2=feature_RGB(color,2);
79
+ c3=feature_RGB(color,3);
80
+ aa=c1*temp_img(:,1)+c2*temp_img(:,2)+c3*temp_img(:,3);
81
+ mnsv=mean(aa);
82
+ aa=aa-mnsv;
83
+ maxsv=max(abs(aa));
84
+ output.transformed_image(:,m,color)=round(aa/maxsv*10^compute_decimal_place)*10^(-compute_decimal_place);
85
+ output.mnsv(m,color)=mnsv;
86
+ output.maxsv(m,color)=maxsv;
87
+ end
88
+ end
89
+ end
90
+ if data_size==1
91
+ output.transformed_image=squeeze(output.transformed_image);
92
+ end
93
+ end
Data_Transformation/read_me.m ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ %
2
+ % 1. Create two folders: 'Modularized_Data_for_SGD' and
3
+ % 'Transformed_Data_for_SGD' in the current foler.
4
+ %
5
+ % 2. Run first 'Run_Modularize_Data.m' to organize the ImageNet-1k into
6
+ % modules of labels.
7
+ %
8
+ % 3. Run next 'Run_Transform_Data.m' to transform the modularized data set
9
+ % into the input data to the ANN models
10
+ %
Data_Transformation/run_script.m ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ Run_Modularize_Data;
2
+ Run_Transform_Data;
Run_Confusion_Matrix_Analysis.m ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ % clear all
2
+
3
+ proto_model={'T_h1_m1';'S_h1_m1';'T_h1_m2';'S_h1_m2';'T_h2_m1';'S_h2_m1'};
4
+ nmb_of_proto_models=length(proto_model);
5
+ featured_model={'model_1';'model_2';'model_3';'model_4';'model_5';'model_6'};
6
+ [nmb_of_ft_models,~]=size(featured_model);
7
+
8
+ nmb_of_lab=1000;
9
+ nmb_of_batches=10;
10
+ nmb_of_image_set=zeros(1,10);
11
+ pr_set=zeros(1,10);
12
+ model_accuracy_comparison=zeros(1,nmb_of_ft_models);
13
+ pm.nmb_of_lab=nmb_of_lab;
14
+ edges=1:1:(nmb_of_lab+1);
15
+ % top_n_labels=10;
16
+ confusion_matrixes=zeros(nmb_of_lab,nmb_of_lab,nmb_of_batches,nmb_of_ft_models,nmb_of_proto_models);
17
+ model_c_matrixes=zeros(nmb_of_lab,nmb_of_lab,nmb_of_ft_models,nmb_of_proto_models);
18
+
19
+ for ii=1:nmb_of_proto_models
20
+ md=char(proto_model(ii));
21
+ % confusion_matrixes=zeros(nmb_of_lab,nmb_of_lab,nmb_of_batches,nmb_of_ft_models);
22
+ % model_c_matrixes=zeros(nmb_of_lab,nmb_of_lab,nmb_of_ft_models);
23
+ top_100_pr_by_batch=zeros(nmb_of_batches,nmb_of_ft_models); % 100 postive rate
24
+ top_100_pr_by_model=zeros(1,nmb_of_ft_models); % 100 postive rate
25
+ batch_nmbs=zeros(1,nmb_of_batches);
26
+ for fm=1:nmb_of_ft_models
27
+ top_100_accnt=[];
28
+ c_matrix=zeros(nmb_of_lab,nmb_of_lab);
29
+ for imgnt1kdataset=1:nmb_of_batches
30
+ reportname1 = sprintf('Model_%s/Evaluation_Data/Model_Accuracy/training_data_batch_%d_feature_module_performance_%s_var.mat', md,imgnt1kdataset,md);
31
+ aa=sprintf('classification_data_%d',fm);
32
+ bb=load(reportname1,aa);
33
+ c_data=bb.(aa);
34
+ true_lab=c_data(:,1);
35
+ pred_lab=c_data(:,2);
36
+ nmb_of_data=length(true_lab);
37
+ c_mtx_output=fun_confusion_matrix(true_lab,pred_lab,pm);
38
+ bb=c_mtx_output.conf_matrix;
39
+ confusion_matrixes(:,:,imgnt1kdataset,fm,ii)=bb;
40
+ c_matrix=c_matrix+nmb_of_data*bb;
41
+ top_100_pr_by_batch(imgnt1kdataset,fm)=length(c_mtx_output.lab_100);
42
+ top_100_accnt=[top_100_accnt,c_mtx_output.lab_100];
43
+ batch_nmbs(imgnt1kdataset)=nmb_of_data;
44
+ end
45
+ model_c_matrixes(:,:,fm,ii)=c_matrix/sum(batch_nmbs);
46
+ histN = histcounts(top_100_accnt,edges);
47
+ idx=(histN==nmb_of_batches);
48
+ top_100_pr_by_model(fm)=sum(1*idx);
49
+ end
50
+ % mean(top_100_pr_by_batch,1) % average 100-rate by data batch
51
+ % top_100_pr_by_model
52
+ %%%%%%%
53
+ assignin('base',md, [mean(top_100_pr_by_batch,1);top_100_pr_by_model]')
54
+ end
55
+ %%
56
+ table(featured_model,T_h1_m1,T_h1_m2,T_h2_m1)
57
+
58
+ %%
59
+ % Find the labels that are perfectly classified, by model
60
+ %
61
+ ii=1;
62
+ fm=6;
63
+ lab=1:1000;
64
+ A=model_c_matrixes(:,:,fm,ii);
65
+ dd=diag(A,0);
66
+ idx=(dd==100);
67
+ lab_100=lab(idx)
68
+ nmb_of_lab_100=sum(1*idx)
69
+
70
+ %%
71
+ % Find the labels that are perfectly classified, by batch and model
72
+ %
73
+ ii=1;
74
+ fm=6;
75
+ imgnt1kdataset=1;
76
+ lab=1:1000;
77
+ A=confusion_matrixes(:,:,imgnt1kdataset,fm,ii);
78
+ dd=diag(A,0);
79
+ idx=(dd==100);
80
+ lab_100=lab(idx)
81
+ nmb_of_lab_100=sum(1*idx)
82
+
83
+
Run_Model_Analysis.m ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ % clear all
2
+
3
+ proto_model={'T_h1_m1';'S_h1_m1';'T_h1_m2';'S_h1_m2';'T_h2_m1';'S_h2_m1'};
4
+ nmb_of_proto_models=length(proto_model);
5
+ featured_model={'model_1';'model_2';'model_3';'model_4';'model_5';'model_6'};
6
+ [nmb_of_ft_models,~]=size(featured_model);
7
+ for ii=1:nmb_of_proto_models
8
+ md=char(proto_model(ii));
9
+ %%%%%%%
10
+ nmb_of_image_set=zeros(1,10);
11
+ pr_set=zeros(1,10);
12
+ top_1_set=zeros(1,10);
13
+ model_accuracy_comparison=zeros(2,nmb_of_ft_models);
14
+ for fm=1:nmb_of_ft_models
15
+ for imgnt1kdataset=1:10
16
+ reportname1 = sprintf('Model_%s/Evaluation_Data/Model_Accuracy/training_data_batch_%d_feature_module_performance_%s_var.mat',...
17
+ md,imgnt1kdataset, md);
18
+ aa=sprintf('classification_data_%d',fm);
19
+ bb=load(reportname1,aa);
20
+ c_data=bb.(aa);
21
+ true_lab=c_data(:,1);
22
+ pred_lab=c_data(:,2);
23
+ likelyhood=c_data(:,3);
24
+ top_1_majority=c_data(1,3);
25
+ nmb_of_images=length(true_lab);
26
+ nmb_of_image_set(imgnt1kdataset)=nmb_of_images;
27
+ idx=(abs(true_lab-pred_lab)==0);
28
+ aa=sum(1*idx);
29
+ pr=aa/nmb_of_images*100;
30
+ pr_set(imgnt1kdataset)=pr;
31
+ %%%%%%%% Top-1 rate %%%%%%%%%%%
32
+ bb=likelyhood(idx);
33
+ cc=length(bb);
34
+ idx1=(bb==top_1_majority);
35
+ aa=sum(idx1*1);
36
+ top_1=aa/cc*100;
37
+ top_1_set(imgnt1kdataset)=top_1;
38
+ end
39
+ model_accuracy_comparison(1,fm)=nmb_of_image_set*pr_set'/sum(nmb_of_image_set);
40
+ model_accuracy_comparison(2,fm)=nmb_of_image_set*top_1_set'/sum(nmb_of_image_set);
41
+ % assignin('base',featured_model(fm), model_accuracy_comparison')
42
+ end
43
+ assignin('base',md, model_accuracy_comparison')
44
+ end
45
+
46
+ %%
47
+ tb1=table(featured_model,T_h1_m1,S_h1_m1,T_h1_m2,S_h1_m2,T_h2_m1,S_h2_m1)
48
+
49
+ %%
50
+ table(featured_model,T_h1_m1,T_h1_m2,T_h2_m1)
51
+
52
+ table(featured_model,S_h1_m1,S_h1_m2,S_h2_m1)
53
+
54
+ %%
55
+ for fm=1:nmb_of_ft_models
56
+ %%%%%%%
57
+ nmb_of_image_set=zeros(1,10);
58
+ pr_set=zeros(1,10);
59
+ top_1_set=zeros(1,10);
60
+ model_accuracy_comparison_2=zeros(2,nmb_of_ft_models);
61
+ for ii=1:nmb_of_proto_models
62
+ md=char(proto_model(ii));
63
+ for imgnt1kdataset=1:10
64
+ reportname1 = sprintf('Model_%s/Evaluation_Data/Model_Accuracy/training_data_batch_%d_feature_module_performance_%s_var.mat',...
65
+ md,imgnt1kdataset, md);
66
+ aa=sprintf('classification_data_%d',fm);
67
+ bb=load(reportname1,aa);
68
+ c_data=bb.(aa);
69
+ true_lab=c_data(:,1);
70
+ pred_lab=c_data(:,2);
71
+ likelyhood=c_data(:,3);
72
+ top_1_majority=c_data(1,3);
73
+ nmb_of_images=length(true_lab);
74
+ nmb_of_image_set(imgnt1kdataset)=nmb_of_images;
75
+ idx=(abs(true_lab-pred_lab)==0);
76
+ aa=sum(1*idx);
77
+ pr=aa/nmb_of_images*100;
78
+ pr_set(imgnt1kdataset)=pr;
79
+ %%%%%%%% Top-1 rate %%%%%%%%%%%
80
+ bb=likelyhood(idx);
81
+ cc=length(bb);
82
+ idx1=(bb==top_1_majority);
83
+ aa=sum(idx1*1);
84
+ top_1=aa/cc*100;
85
+ top_1_set(imgnt1kdataset)=top_1;
86
+ end
87
+ model_accuracy_comparison_2(1,ii)=nmb_of_image_set*pr_set'/sum(nmb_of_image_set);
88
+ model_accuracy_comparison_2(2,ii)=nmb_of_image_set*top_1_set'/sum(nmb_of_image_set);
89
+ end
90
+ assignin('base',char(featured_model(fm)), round(model_accuracy_comparison_2,3)')
91
+ end
92
+
93
+ tb2=table(proto_model,model_1,model_2,model_3,model_4,model_5,model_6)
Run_Spoke_Plot.m ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ % clear all
2
+
3
+ channels_names={'R','G','B','RGg1','RBg1','GBg1','RGg2','RBg2','GBg2','RB','RG','GB','eRGB','BW','X','Y','Z'};
4
+ patch=0;
5
+ module=9;%4;%38;
6
+ subset=2;
7
+ color=4;
8
+ nmb_of_labs_per_module=25;
9
+ nmb_of_labels=nmb_of_labs_per_module;
10
+ maxlngth=3000;
11
+
12
+ reportname1 = sprintf('Data_Transformation/Transformed_IN1k_Data/Transformed_Data_for_SGD/train_data_patch_%d_module_%d_subset_%d_for_%d_labels_per_module.mat', ...
13
+ patch,module,subset,nmb_of_labs_per_module);
14
+ data_load=load(reportname1);
15
+ vect_image=data_load.data(:,1:maxlngth,color);
16
+ true_label=data_load.labels(1:maxlngth);
17
+
18
+ model='T_h2_m1';
19
+ reportname1 = sprintf(['Model_%s/Model_Parameter/Trained_Parameter_patch_%d_module_%d_subset_%d_ch_%s.mat'],...
20
+ model,patch, module, subset, char(channels_names(color)));
21
+ % str=struct('W', W, 'b', b);
22
+ temp_load=load(reportname1);
23
+ Wt=temp_load.W;
24
+ bt=temp_load.b;
25
+ reportname1 = sprintf('Model_%s/Training_Evaluation/%s_1_performance.mat',model,model);
26
+ load(reportname1)
27
+ tpr=pstvrt_model(color,module,subset);
28
+
29
+ model='S_h2_m1';
30
+ reportname1 = sprintf('Model_%s/Model_Parameter/Trained_Parameter_patch_%d_module_%d_subset_%d_ch_%s.mat',...
31
+ model, patch, module, subset, char(channels_names(color)));
32
+ % str=struct('W', W, 'b', b);
33
+ temp_load=load(reportname1);
34
+ Ws=temp_load.W;
35
+ bs=temp_load.b;
36
+ reportname1 = sprintf('Model_%s/Training_Evaluation/%s_1_performance.mat',model,model);
37
+ load(reportname1)
38
+ spr=pstvrt_model(color,module,subset);
39
+
40
+ fun_spoke_plot(vect_image,true_label,Ws,bs,spr,Wt,bt,tpr,nmb_of_labels)
Run_Training_Analysis.m ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ % clear all
2
+
3
+ model={'T_h1_m1';'S_h1_m1';'T_h1_m2';'S_h1_m2';'T_h2_m1';'S_h2_m1';};
4
+ nmb_of_models=length(model);
5
+ rtmax=[];
6
+ rtmin=[];
7
+ rtmed=[];
8
+ rtmean=[];
9
+ nmb_of_100rt_module=[];
10
+ nmb_of_100rt=[];
11
+ stat_100rt=[];
12
+ for ii=1:nmb_of_models
13
+ md=char(model(ii));
14
+ reportname1 = sprintf('Model_%s/Training_Evaluation/%s_1_performance.mat',md,md);
15
+ load(reportname1)
16
+
17
+ rtmax=[rtmax;max(pstvrt_model,[],'all')];
18
+ rtmin=[rtmin;min(pstvrt_model,[],'all')];
19
+ rtmed=[rtmed;median(pstvrt_model(:),'all')];
20
+ rtmean=[rtmean;mean(pstvrt_model(:),'all')];
21
+
22
+ aa=squeeze(pstvrt_model(2,:,1));
23
+ bb=squeeze(pstvrt_model(2,:,2));
24
+ cc=[aa,bb];
25
+ idx=(cc==100);
26
+ nmb_of_100rt_module=[nmb_of_100rt_module;sum(1*idx)];
27
+ % rt100=(sum(idx*1)-1)/length(cc);
28
+ dd=[squeeze(pstvrt_model(:,:,1));squeeze(pstvrt_model(:,:,2))];
29
+ idx2=(dd==100);
30
+ nmb_of_100rt=[nmb_of_100rt;sum(1*idx)];
31
+ stat_100rt=[stat_100rt;[sum(1*idx2,'all'),sum(1*idx)]];
32
+ end
33
+ %%
34
+
35
+ perfmn=table(model,rtmin,rtmean,rtmed,rtmax,stat_100rt)
fun_activation.m ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function [out1,out2]=fun_activation(x)
2
+ %
3
+ %
4
+ %
5
+ [sz1,sz2]=size(x);
6
+ A=zeros(sz1,sz2);
7
+ B=zeros(sz1,sz1,sz2);
8
+ for zz=1:sz2
9
+ [A(:,zz),B(:,:,zz)]=ReLU(x(:,zz));
10
+ end
11
+
12
+ out1=A;
13
+ out2=B;
14
+ %%%%%%%%
15
+ function [y,dy]=ReLU(s)
16
+ aa=(s>0).*1;
17
+ y=aa.*s;
18
+ dy=diag(aa);
19
+ end
20
+ %%%%%%%%
21
+ end
fun_confusion_matrix.m ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function output=fun_confusion_matrix(true_lab,pred_lab,pm)
2
+ %
3
+ nmb_of_images=length(true_lab);
4
+ nmb_of_lab=pm.nmb_of_lab;
5
+ % top_n=pm.top_n;
6
+ edges=1:1:(nmb_of_lab+1);
7
+ c_matrix=zeros(nmb_of_lab,nmb_of_lab);
8
+ seq_lab=1:nmb_of_lab;
9
+ % top_n_seq=1:top_n;
10
+ p_seq=(1:nmb_of_images)';
11
+ for lb=1:nmb_of_lab
12
+ idx1=(true_lab==lb);
13
+ aa=p_seq(idx1);
14
+ nmb=length(aa);
15
+ aa=pred_lab(aa);
16
+ [N,~]=histcounts(aa,edges);
17
+ rt=N/nmb*100;
18
+ c_matrix(lb,:)=rt;
19
+ end
20
+ aa=diag(c_matrix);
21
+ % [~,cc]=sort(aa);
22
+ idx100=(aa==100);
23
+ output.lab_100=seq_lab(idx100);
24
+ % idx=flip(cc);
25
+ % c_matrix=c_matrix(idx, idx);
26
+ output.conf_matrix=c_matrix;
27
+ % top_n_lb=idx(top_n_seq);
28
+ % top_n_rt=diag(c_matrix(top_n_seq,top_n_seq));
29
+ % output.top_n_inf=[top_n_rt,top_n_lb];
30
+ end
fun_predicted_vector_2_label.m ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function out=fun_predicted_vector_2_label(vector,nmb_of_labels)
2
+ %
3
+ % predicted vector of length 25 to digit label 1, ..., 25
4
+ % vector=predicted_vector;
5
+ [~,dtsz]=size(vector);
6
+ digit=zeros(1,dtsz);
7
+ distance=zeros(1,dtsz);
8
+ nn=nmb_of_labels;
9
+ v=vector(1:nn,:);
10
+ I=eye(nn);
11
+ vv=zeros(nn,1);
12
+ for j=1:dtsz
13
+ for i=1:nn
14
+ [cc,~]=loss_function(v(:,j),I(:,i));
15
+ vv(i)=cc;
16
+ end
17
+ [aa,idx]=min(vv);
18
+ digit(j)=idx;
19
+ distance(j)=aa;
20
+ end
21
+ out.label=digit;
22
+ out.distance=distance;
23
+ %%%%%%%%%%
24
+ function [loss,dloss]=loss_function(predicted_x,true_y)
25
+ % cross_entropy loss
26
+ %
27
+ eps=1e-8;
28
+ [n, N]=size(true_y);
29
+ loss=sum(sum((true_y+eps).*log((true_y+eps)./(predicted_x+eps))))/N;
30
+ % ind_loss=sum((true_y+eps).*log((true_y+eps)./(predicted_x+eps)),1);
31
+ bb=-(true_y(1:end-1,:)+eps)./(predicted_x(1:end-1,:)+eps);
32
+ w=bb+ones(n-1,1)*(true_y(end,:)+eps)./(predicted_x(end,:)+eps);
33
+ dloss=w'/N;
34
+ end
35
+ %%%%%%%%%%
36
+ end
fun_softmax.m ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function [out1,out2]=fun_softmax(S)
2
+ %
3
+ % Column-wise normaliztion
4
+ %
5
+ [sz1,sz2]=size(S);
6
+ A=zeros(sz1,sz2);
7
+ B=zeros(sz1-1,sz1,sz2);
8
+ for zz=1:sz2
9
+ [aaa,bbb]=softmax(S(:,zz));
10
+ A(:,zz)=aaa;
11
+ B(:,:,zz)=bbb;
12
+ end
13
+ out1=A;
14
+ out2=B;
15
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
16
+ function [y,dy]=softmax(S)
17
+ % bb=1e+2;
18
+ bb=1;
19
+ S=S/bb;
20
+ aa=exp(S);
21
+ sm=sum(exp(S))+1e-16;
22
+ y=aa./sm;
23
+
24
+ [n,~]=size(S);
25
+ m=n-1;
26
+ dy=zeros(m,n);
27
+ for i=1:m
28
+ for j=1:n
29
+ if j~=i
30
+ dy(i,j)=-exp(S(i)).*exp(S(j));
31
+ else
32
+ dy(i,j)=exp(S(i)).*sm-exp(S(i)).*exp(S(j));
33
+ end
34
+ end
35
+ end
36
+ dy=dy/sm^2/bb;
37
+ end
38
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39
+ end
fun_spoke_plot.m ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function []=fun_spoke_plot(vect_image,true_label,Ws,bs,spr,Wt,bt,tpr,nmb_of_labels)
2
+ %
3
+ %
4
+ lb=true_label;
5
+ nml=nmb_of_labels;
6
+ nn=3;
7
+ clr=colormap(cool(nn*(nml+1)));
8
+ lbnm={'a','b','c','d','e','f','g','h','i','j','k','l','m','n',...
9
+ 'o','p','q','r','s','t','u','v','w','x','y','z'};
10
+ gy=.33;
11
+ grdcl='w';
12
+ grlwt=2;
13
+ msz=4;
14
+ redge=1.1;
15
+
16
+ figure(1)
17
+ hold off
18
+
19
+ subplot(1,2,1)
20
+ prediction=fun_prediction(vect_image,Ws,bs);
21
+ predicted=prediction.A_end;
22
+ [~,nmd]=size(predicted);
23
+ offst=.07;
24
+ %%%%%%%%%%%%%%%
25
+ inc=2*pi/nml;
26
+ tht=(0:(nml-1))*inc;
27
+ A=zeros(2,nmb_of_labels);
28
+ for ii=1:nml %tht
29
+ A(:,ii)=[tht(ii);1];
30
+ end
31
+ radii=1./abs(prediction.predicted.distance+offst);
32
+ %%%%%%%%%%%%%%%
33
+ pi_pt=zeros(2,nmd);
34
+ for mm=1:nmd
35
+ pi_pt(:,mm)=A*predicted(:,mm);
36
+ end
37
+
38
+ aa=pi_pt(1,:);
39
+ theta_all=aa(:);
40
+ % aa=pi_pt(2,:);
41
+ % rho_all=aa(:);
42
+ %%%%%%%%%%%%%%%
43
+ % polarplot(twopi,unitclc,'linewidth',1,'Color','w');
44
+ % hold on
45
+ for kk=1:nml
46
+ idx=(lb==kk);
47
+ theta=theta_all(idx);
48
+ rho=radii(idx);
49
+ % rho=rho_all(idx);
50
+ p=polarplot(theta,rho/max(rho));
51
+ hold on
52
+
53
+ p.Marker = 'square';
54
+ p.MarkerSize = msz;
55
+ p.LineStyle = "none";
56
+ p.Color = clr(nn*kk,:);
57
+ p.MarkerFaceColor = clr(nn*kk,:);
58
+ end
59
+ ax = gca;
60
+ ax.RTickLabel = {};
61
+ ax.ThetaTick = rad2deg(tht);
62
+ ax.ThetaTickLabel = lbnm;
63
+ axis([-inf, inf, 0,redge])
64
+ set(ax,'Color',[gy gy gy])
65
+ set(ax,'GridColor',grdcl,'LineWidth',grlwt)
66
+
67
+ % title("SGD - trained Model (" + SGDpr*epoch_size_SGD/epoch_sz_GDT + "%)",'fontsize',14)
68
+ title("SGD - trained Model (" + round(spr,2) + "%)",'fontsize',14)
69
+
70
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%
71
+ subplot(1,2,2)
72
+ prediction=fun_prediction(vect_image,Wt,bt);
73
+ predicted=prediction.A_end;
74
+ [~,nmd]=size(predicted);
75
+ % offst=.05;
76
+ %%%%%%%%%%%%%%%
77
+ inc=2*pi/nml;
78
+ tht=(0:(nml-1))*inc;
79
+ A=zeros(2,nmb_of_labels);
80
+ for ii=1:nml %tht
81
+ A(:,ii)=[tht(ii);1];
82
+ end
83
+ radii=1./abs(prediction.predicted.distance+offst);
84
+ %%%%%%%%%%%%%%%
85
+ pi_pt=zeros(2,nmd);
86
+ for mm=1:nmd
87
+ pi_pt(:,mm)=A*predicted(:,mm);
88
+ end
89
+
90
+ aa=pi_pt(1,:);
91
+ theta_all=aa(:);
92
+ % aa=pi_pt(2,:);
93
+ % rho_all=aa(:);
94
+ %%%%%%%%%%%%%%%
95
+ % polarplot(twopi,unitclc,'linewidth',1,'Color','w');
96
+ % hold on
97
+ for kk=1:nml
98
+ idx=(lb==kk);
99
+ theta=theta_all(idx);
100
+ rho=radii(idx);
101
+ % rho=rho_all(idx);
102
+ p=polarplot(theta,rho/max(rho));
103
+ hold on
104
+
105
+ p.Marker = 'square';
106
+ p.MarkerSize = msz;
107
+ p.LineStyle = "none";
108
+ p.Color = clr(nn*kk,:);
109
+ p.MarkerFaceColor = clr(nn*kk,:);
110
+ end
111
+ ax = gca;
112
+ ax.RTickLabel = {};
113
+ ax.ThetaTick = rad2deg(tht);
114
+ ax.ThetaTickLabel = lbnm;
115
+ axis([-inf, inf, 0,redge])
116
+ set(ax,'Color',[gy gy gy])
117
+ set(ax,'GridColor',grdcl,'LineWidth',grlwt)
118
+
119
+ % title('Fully Trained Model (100%)','fontsize',14)
120
+ title("GDT - trained Model (" + tpr + "%)",'fontsize',14)
121
+
122
+ set(gcf,'Position',[10 80 900 440])
123
+ % set(gcf, 'MenuBar', 'None')
124
+ % sgtitle('Spoke-Plot for Training', 'fontsize', 16)
125
+ sgtitle('Confusion Wheel for Training', 'fontsize', 16)
126
+
127
+ function out=fun_prediction(vect_image,W, b)
128
+ %
129
+ %
130
+ nmb_of_hidden_layers=length(fieldnames(W))-1;
131
+ W1=W.LayerName1;
132
+ W2=W.LayerName2;
133
+ b1=b.LayerName1;
134
+ b2=b.LayerName2;
135
+
136
+ a_0=vect_image;
137
+ % true_label=data_load.labels;
138
+ % dtsz=length(true_label);
139
+ nmb_labels=length(b2);
140
+ z1=W1*a_0+b1;
141
+ [a1,~]=fun_activation(z1);
142
+ z2=W2*a1+b2;
143
+
144
+ if nmb_of_hidden_layers==1
145
+ [a2,~]=fun_softmax(z2);
146
+ predicted_vector=a2;
147
+ else
148
+ W3=W.LayerName3;
149
+ b3=b.LayerName3;
150
+ nmb_labels=length(b3);
151
+ [a2,~]=fun_activation(z2);
152
+
153
+ z3=W3*a2+b3;
154
+ [a3,~]=fun_softmax(z3);
155
+ predicted_vector=a3;
156
+ end
157
+ out.A_end=predicted_vector;
158
+ out.predicted=fun_predicted_vector_2_label(predicted_vector,nmb_labels);
159
+ end
160
+ end