(logits,label,filename)
| 109 | return (pred == label).type(torch.FloatTensor).mean().item() |
| 110 | |
| 111 | def confmatrix(logits,label,filename): |
| 112 | |
| 113 | font={'family':'FreeSerif','size':18} |
| 114 | matplotlib.rc('font',**font) |
| 115 | matplotlib.rcParams.update({'font.family':'FreeSerif','font.size':18}) |
| 116 | plt.rcParams["font.family"]="FreeSerif" |
| 117 | |
| 118 | pred = torch.argmax(logits, dim=1) |
| 119 | cm=confusion_matrix(label, pred,normalize='true') |
| 120 | #print(cm) |
| 121 | clss=len(cm) |
| 122 | fig = plt.figure() |
| 123 | ax = fig.add_subplot(111) |
| 124 | cax = ax.imshow(cm,cmap=plt.cm.jet) |
| 125 | if clss<=100: |
| 126 | plt.yticks([0,19,39,59,79,99],[0,20,40,60,80,100],fontsize=16) |
| 127 | plt.xticks([0,19,39,59,79,99],[0,20,40,60,80,100],fontsize=16) |
| 128 | elif clss<=200: |
| 129 | plt.yticks([0,39,79,119,159,199],[0,40,80,120,160,200],fontsize=16) |
| 130 | plt.xticks([0,39,79,119,159,199],[0,40,80,120,160,200],fontsize=16) |
| 131 | else: |
| 132 | plt.yticks([0,199,399,599,799,999],[0,200,400,600,800,1000],fontsize=16) |
| 133 | plt.xticks([0,199,399,599,799,999],[0,200,400,600,800,1000],fontsize=16) |
| 134 | |
| 135 | plt.xlabel('Predicted Label',fontsize=20) |
| 136 | plt.ylabel('True Label',fontsize=20) |
| 137 | plt.tight_layout() |
| 138 | plt.savefig(filename+'.pdf',bbox_inches='tight') |
| 139 | plt.close() |
| 140 | |
| 141 | fig = plt.figure() |
| 142 | ax = fig.add_subplot(111) |
| 143 | cax = ax.imshow(cm,cmap=plt.cm.jet) |
| 144 | cbar = plt.colorbar(cax) # This line includes the color bar |
| 145 | cbar.ax.tick_params(labelsize=16) |
| 146 | if clss<=100: |
| 147 | plt.yticks([0,19,39,59,79,99],[0,20,40,60,80,100],fontsize=16) |
| 148 | plt.xticks([0,19,39,59,79,99],[0,20,40,60,80,100],fontsize=16) |
| 149 | elif clss<=200: |
| 150 | plt.yticks([0,39,79,119,159,199],[0,40,80,120,160,200],fontsize=16) |
| 151 | plt.xticks([0,39,79,119,159,199],[0,40,80,120,160,200],fontsize=16) |
| 152 | else: |
| 153 | plt.yticks([0,199,399,599,799,999],[0,200,400,600,800,1000],fontsize=16) |
| 154 | plt.xticks([0,199,399,599,799,999],[0,200,400,600,800,1000],fontsize=16) |
| 155 | plt.xlabel('Predicted Label',fontsize=20) |
| 156 | plt.ylabel('True Label',fontsize=20) |
| 157 | plt.tight_layout() |
| 158 | plt.savefig(filename+'_cbar.pdf',bbox_inches='tight') |
| 159 | plt.close() |
| 160 | |
| 161 | return cm |
| 162 | |
| 163 | |
| 164 |
no outgoing calls