fix: handle dynamic dimensions in Math output shape
This commit is contained in:
@@ -343,10 +343,31 @@ func preprocessMath(img image.Image) ([]float32, error) {
|
|||||||
func decodeMath(output []float32, outputShape []int64) string {
|
func decodeMath(output []float32, outputShape []int64) string {
|
||||||
// 输出格式: [num_chars, batch, timesteps] = [51, 1, 19]
|
// 输出格式: [num_chars, batch, timesteps] = [51, 1, 19]
|
||||||
// 需要转置为 [batch, timesteps, num_chars] = [1, 19, 51]
|
// 需要转置为 [batch, timesteps, num_chars] = [1, 19, 51]
|
||||||
|
if len(outputShape) < 3 || len(output) == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
numChars := int(outputShape[0]) // 51
|
numChars := int(outputShape[0]) // 51
|
||||||
batch := int(outputShape[1]) // 1
|
batch := int(outputShape[1]) // 1
|
||||||
timesteps := int(outputShape[2]) // 19
|
timesteps := int(outputShape[2]) // 19
|
||||||
|
|
||||||
|
// 处理动态维度(可能返回 -1)
|
||||||
|
if numChars <= 0 {
|
||||||
|
numChars = 51 // 默认字符数
|
||||||
|
}
|
||||||
|
if batch <= 0 {
|
||||||
|
batch = 1
|
||||||
|
}
|
||||||
|
if timesteps <= 0 {
|
||||||
|
// 从输出大小推断
|
||||||
|
timesteps = len(output) / (numChars * batch)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 边界检查
|
||||||
|
if numChars <= 0 || batch <= 0 || timesteps <= 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
// 转置: output[c*batch*t + b*t + t] -> transposed[b*timesteps*numChars + t*numChars + c]
|
// 转置: output[c*batch*t + b*t + t] -> transposed[b*timesteps*numChars + t*numChars + c]
|
||||||
transposed := make([]float32, batch*timesteps*numChars)
|
transposed := make([]float32, batch*timesteps*numChars)
|
||||||
for b := 0; b < batch; b++ {
|
for b := 0; b < batch; b++ {
|
||||||
|
|||||||
Reference in New Issue
Block a user