Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of drawing obfuscation Matrix and Visualization of pytorch Classification Model

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article Xiaobian for you to introduce in detail "pytorch classification model drawing confusion matrix and visualization methods", detailed content, clear steps, details handled properly, I hope that this "pytorch classification model drawing confusion matrix and visualization methods" article can help you solve doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

Step 1. Get confusion matrix # first define a classification number * empty confusion matrix conf_matrix = torch.zeros (Emotion_kinds, Emotion_kinds) # using torch.no_grad () can significantly reduce the GPU occupation of test cases with torch.no_grad (): for step, (imgs, targets) in enumerate (test_loader): # imgs: torch.Size ([50,3,200) Torch.FloatTensor # targets: torch.Size ([50,1]), torch.LongTensor has one more dimension So we need to get rid of targets = targets.squeeze () # [50 targets.shape 1]-> [50] # change the variable to gpu targets = targets.cuda () imgs = imgs.cuda () # print (step,imgs.shape,imgs.type (), targets.shape Targets.type () out = model (imgs) # record confusion matrix parameter conf_matrix= confusion_matrix (out, targets, conf_matrix) conf_matrix=conf_matrix.cpu ()

The obfuscation matrix is obtained using the confusion_matrix function, which is defined as follows:

Def confusion_matrix (preds, labels, conf_matrix): preds = torch.argmax (preds, 1) for p, t in zip (preds, labels): conf_matrix [p, t] + = 1 return conf_matrix

When our program finishes the test_loader, we can get the confusion matrix of this data. The next step is to calculate the correct number of identification and the visualization of the confusion matrix:

Conf_matrix=np.array (conf_matrix.cpu ()) # transfers the confusion matrix from gpu to cpu and then to npcorrects=conf_matrix.diagonal (offset=0) # extract the correct number of identification for each classification of diagonals per_kinds=conf_matrix.sum (axis=1) # extract the total number of test items for each classification print ("Total number of elements of confusion matrix: {0}, total number of test sets: {1}" .format (int (np.sum (conf_matrix) Test_num)) print (conf_matrix) # get the recognition accuracy of each Emotion print ("Total number of each emotion:", per_kinds) print ("number of correct predictions of each emotion:", corrects) print ("recognition accuracy of each emotion: {0}" .format ([rate*100 for rate in corrects/per_kinds]))

The output from performing this step is as follows:

Step 2. Confusion matrix visualization

Visualization of the confusion matrix obtained above

# drawing confusion matrix Emotion=8# this value is the specific number of categories You can modify labels = ['neutral',' calm', 'happy',' sad', 'angry',' fearful', 'disgust',' surprised'] # each category label # display data plt.imshow (conf_matrix, cmap=plt.cm.Blues) # mark the quantity / probability information thresh = conf_matrix.max () / 2 # numeric color threshold in the diagram, if the value exceeds this, the color deepens. For x in range (Emotion_kinds): for y in range (Emotion_kinds): # Note that matrix [y, x] here is not matrix [x, y] info = int (conf_matrix [y, x]) plt.text (x, y, info, verticalalignment='center', horizontalalignment='center') Color= "white" if info > thresh else "black") plt.tight_layout () # ensures that the images do not overlap plt.yticks (range (Emotion_kinds), labels) plt.xticks (range (Emotion_kinds), labels,rotation=45) # X axis font tilt 45 °plt.show () plt.close ()

Well, here is the final visual confusion matrix:

Acquisition of other classification indicators

For example, F1 score, TP, TN, FP, FN, accuracy, recall rate and other indicators need to be added (because they are not used yet) ~

Read this, the "pytorch classification model to draw confusion matrix and visualization methods" article has been introduced, want to master the knowledge of this article still need to practice and use to understand, if you want to know more about the article, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report