image.domain\self_evolve\auto_repair.mbt

86.8% · 66 / 76 points · 169 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// # 自动修复 self_evolve/auto_repair
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 退化自动修复 — 检测退化后自动修正参数
  7. 7// Auto repair: detect degradation and auto-fix parameters
  8. 8//
  9. 9// Phase 5 v282: 将退化检测从"发现"推向"修复" — Resize(0,0)→Resize(1,1) 等。
  10. 10
  11. 11///|
  12. 12/// 单项修复动作。
  13. 13/// 描述 Single repair action.
  14. 14#derive(Eq, @debug.Debug)
  15. 15pub(all) struct RepairAction {
  16. 16 op_tag : String
  17. 17 repaired_op_tag : String
  18. 18 repair_desc : String
  19. 19}
  20. 20
  21. 21///|
  22. 22pub extend RepairAction with @debug.Debug::{to_repr}
  23. 23
  24. 24///|
  25. 25pub extend RepairAction with Eq::{not_equal, equal}
  26. 26
  27. 27///|
  28. 28/// 修复报告。
  29. 29/// 描述 Repair report.
  30. 30#derive(Eq, @debug.Debug)
  31. 31pub(all) struct RepairReport {
  32. 32 original_op_count : Int32
  33. 33 repaired_op_count : Int32
  34. 34 repairs : Array[RepairAction]
  35. 35 remaining_degradations : Array[DegradationCase]
  36. 36 summary : String
  37. 37}
  38. 38
  39. 39///|
  40. 40pub extend RepairReport with @debug.Debug::{to_repr}
  41. 41
  42. 42///|
  43. 43pub extend RepairReport with Eq::{not_equal, equal}
  44. 44
  45. 45///|
  46. 46/// 修复单个算子的退化参数。
  47. 47/// 描述 Repair a single op's degraded parameters.
  48. 48pub fn repair_op(op : @imgir.IROp) -> @imgir.IROp {
  49. 49● match op {
  50. 50● Resize(w, h) => {
  51. 51● let nw = if w == 0 { 1 } else { w }
  52. 52● let nh = if h == 0 { 1 } else { h }
  53. 53 @imgir.Resize(nw, nh)
  54. 54 }
  55. 55● Crop(x, y, w, h) => {
  56. 56◐ let nw = if w == 0 { 1 } else { w }
  57. 57◐ let nh = if h == 0 { 1 } else { h }
  58. 58 @imgir.Crop(x, y, nw, nh)
  59. 59 }
  60. 60● GaussianBlur(r, sigma) => {
  61. 61 let nsigma = if sigma == 0.0F {
  62. 62● 1.0F
  63. 63● } else if sigma < 0.0F {
  64. 64○ 0.0F - sigma
  65. 65 } else {
  66. 66● sigma
  67. 67 }
  68. 68 @imgir.GaussianBlur(r, nsigma)
  69. 69 }
  70. 70● Sharpen(amount) => {
  71. 71 let na = if amount > 100.0F {
  72. 72○ 100.0F
  73. 73● } else if amount < -100.0F {
  74. 74○ -100.0F
  75. 75 } else {
  76. 76● amount
  77. 77 }
  78. 78 @imgir.Sharpen(na)
  79. 79 }
  80. 80◐ FieldClamp(lo, hi) => if lo > hi { @imgir.FieldClamp(hi, lo) } else { op }
  81. 81 FieldMask(threshold) =>
  82. 82● if threshold > 1000000.0F || threshold < -1000000.0F {
  83. 83○ @imgir.FieldMask(0.0F)
  84. 84 } else {
  85. 85● op
  86. 86 }
  87. 87● STSnapshot(t) => {
  88. 88◐ let nt = if t < 0.0F { 0.0F } else if t > 1.0F { 1.0F } else { t }
  89. 89 @imgir.STSnapshot(nt)
  90. 90 }
  91. 91● STInterpolate(t) => {
  92. 92◐ let nt = if t < 0.0F { 0.0F } else if t > 1.0F { 1.0F } else { t }
  93. 93 @imgir.STInterpolate(nt)
  94. 94 }
  95. 95● _ => op
  96. 96 }
  97. 97}
  98. 98
  99. 99///|
  100. 100/// 修复管道中所有算子的退化。
  101. 101/// 描述 Repair all ops in a pipeline.
  102. 102pub fn repair_pipeline(pipeline : @imgir.IRPipeline) -> @imgir.IRPipeline {
  103. 103● let mut pipe = @imgir.IRPipeline::new()
  104. 104 for op in pipeline.ops {
  105. 105● pipe = pipe.add(repair_op(op))
  106. 106 }
  107. 107 pipe
  108. 108}
  109. 109
  110. 110///|
  111. 111/// 生成修复报告。
  112. 112/// 生成 Generate repair report for a pipeline.
  113. 113pub fn repair_report(pipeline : @imgir.IRPipeline) -> RepairReport {
  114. 114● let ops = pipeline.ops
  115. 115● let original_degs = scan_degradations(ops)
  116. 116● let repaired_pipe = repair_pipeline(pipeline)
  117. 117● let repaired_degs = scan_degradations(repaired_pipe.ops)
  118. 118 let repairs : Array[RepairAction] = []
  119. 119 for op in ops {
  120. 120● let degs = detect_degradation(op)
  121. 121● if degs.length() > 0 {
  122. 122● let repaired = repair_op(op)
  123. 123● repairs.push({
  124. 124● op_tag: op.tag(),
  125. 125● repaired_op_tag: repaired.tag(),
  126. 126● repair_desc: "修复 " + degs.length().to_string() + " 处退化",
  127. 127 })
  128. 128 }
  129. 129 }
  130. 130● let repaired_count = original_degs.length() - repaired_degs.length()
  131. 131 let summary = "修复 " +
  132. 132● repaired_count.to_string() +
  133. 133 " 处退化,剩余 " +
  134. 134● repaired_degs.length().to_string() +
  135. 135 " 处"
  136. 136 {
  137. 137● original_op_count: ops.length(),
  138. 138● repaired_op_count: repaired_pipe.ops.length(),
  139. 139 repairs,
  140. 140 remaining_degradations: repaired_degs,
  141. 141 summary,
  142. 142 }
  143. 143}
  144. 144
  145. 145///|
  146. 146/// 对全部 19 种算子生成修复报告。
  147. 147/// 生成 Generate repair report for all 19 op variants.
  148. 148pub fn repair_all() -> Array[RepairReport] {
  149. 149● let reports : Array[RepairReport] = []
  150. 150● for op in all_ops() {
  151. 151● let pipe = @imgir.IRPipeline::new().add(op)
  152. 152● reports.push(repair_report(pipe))
  153. 153 }
  154. 154 reports
  155. 155}
  156. 156
  157. 157///|
  158. 158/// 修复后退化是否清零。
  159. 159/// 描述 Whether all degradations are fixed.
  160. 160pub fn RepairReport::is_fully_repaired(self : RepairReport) -> Bool {
  161. 161● self.remaining_degradations.length() == 0
  162. 162}
  163. 163
  164. 164///|
  165. 165/// 修复动作数。
  166. 166/// 描述 Number of repair actions.
  167. 167pub fn RepairReport::repair_count(self : RepairReport) -> Int32 {
  168. 168● self.repairs.length()
  169. 169}