confusion_matrix2.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from ChildProject.projects import ChildProject
  2. from ChildProject.annotations import AnnotationManager
  3. from ChildProject.metrics import segments_to_grid, conf_matrix
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6. speakers = ['CHI', 'OCH', 'FEM', 'MAL'] #PUT HERE THE LABELS YOU WANT TO INCLUDE
  7. project = ChildProject('.')
  8. am = AnnotationManager(project)
  9. am.read()
  10. SET_1 = 'eaf_2023/ak' #CHANGE THE FOLDER TO WHERE THE MANUAL ANNOTATIONS ARE
  11. SET_2 = 'vtc' #CHANGE THE FOLDER TO WHERE VTC GENERATED ANNOTATIONS ARE
  12. intersection = AnnotationManager.intersection(am.annotations, [SET_1, SET_2])
  13. segments = am.get_collapsed_segments(intersection)
  14. segments = segments[segments['speaker_type'].isin(speakers)]
  15. # Y
  16. #vtc = segments_to_grid(segments[segments['set'] == 'vtc'], 0, segments['segment_offset'].max(), 100, 'speaker_type', speakers,none = False)
  17. vtc = segments_to_grid(segments[segments['set'] == SET_1], 0, segments['segment_offset'].max(), 100, 'speaker_type', speakers)
  18. # X
  19. #its = segments_to_grid(segments[segments['set'] == 'its'], 0, segments['segment_offset'].max(), 100, 'speaker_type', speakers,none = False)
  20. its = segments_to_grid(segments[segments['set'] == SET_2], 0, segments['segment_offset'].max(), 100, 'speaker_type', speakers)
  21. confusion_counts = conf_matrix(vtc, its)
  22. all_positive = np.delete(confusion_counts, -1, 0)
  23. all_negative = np.delete(confusion_counts, -1, 1)
  24. precision = np.delete(all_negative, -1, 0).trace() / all_positive.sum()
  25. recall = np.delete(all_negative, -1, 0).trace() / all_negative.sum()
  26. fscore = (2 * precision * recall) / (precision + recall)
  27. scores = {}
  28. i=0
  29. with open('scores.txt','w') as f:
  30. for label in speakers:
  31. rec = confusion_counts[i,i] / confusion_counts[ :,i].sum()
  32. preci = confusion_counts[i,i] / confusion_counts[i,: ].sum()
  33. fsc = (2 * preci * rec) / (preci + rec)
  34. #scores[label] = (preci, rec, fsc)
  35. f.write(f"{label}: precision {preci}; recall {rec}; F-score {fsc}\n")
  36. i+=1
  37. f.write(f"General: precision {precision}; recall {recall}; F-score {fscore}\n")
  38. #print(f"General: precision {precision}; recall {recall}; F-score {fscore}")
  39. print(f"Results written to scores.txt")
  40. normalized = confusion_counts
  41. speakers.append("None")
  42. speakers = [""] + speakers
  43. fig, ax = plt.subplots(figsize=(7.5, 7.5))
  44. ax.set_xticklabels(speakers)
  45. ax.set_yticklabels(speakers)
  46. ax.matshow(normalized, cmap=plt.cm.Blues, alpha=0.3)
  47. for i in range(normalized.shape[0]):
  48. for j in range(normalized.shape[1]):
  49. ax.text(x=j, y=i,s=round(normalized[i, j],3), va='center', ha='center', size='xx-large')
  50. ax.xaxis.set_label_position("top")
  51. # set Y and X
  52. plt.ylabel(SET_1, fontsize=18)
  53. plt.xlabel(SET_2, fontsize=18)
  54. plt.title('Confusion Matrix', fontsize=18)
  55. plt.savefig('conf_matrix.png')
  56. normalized = confusion_counts/(np.sum(vtc, axis = 0)[:,None])
  57. fig, ax = plt.subplots(figsize=(7.5, 7.5))
  58. ax.set_xticklabels(speakers)
  59. ax.set_yticklabels(speakers)
  60. ax.matshow(normalized, cmap=plt.cm.Blues, alpha=0.3)
  61. for i in range(normalized.shape[0]):
  62. for j in range(normalized.shape[1]):
  63. ax.text(x=j, y=i,s=round(normalized[i, j],3), va='center', ha='center', size='xx-large')
  64. ax.xaxis.set_label_position("top")
  65. plt.ylabel(SET_1, fontsize=18)
  66. plt.xlabel(SET_2, fontsize=18)
  67. plt.title('Confusion Matrix', fontsize=18)
  68. plt.savefig('conf_matrix_normalized.png')