2

I've trained a custom model with yolov7 and converted it to mlmodel with export.py (from yolov7 repo: https://github.com/WongKinYiu/yolov7).

Since export mlmodel's missing metadata, I was trying to add a decode layer to it, based on this post: https://rockyshikoku.medium.com/convert-yolov5-to-coreml-also-add-a-decode-layer-113408b7a848 and this coreml convert script: https://github.com/dbsystel/yolov5-coreml-tools/blob/main/src/coreml_export/main.py

I've tested the script and it works fine with yolov7 p5 model, but I cannot get this work with p6 model like yolov7-e6e.

# P6 model
strides = [8, 16, 32, 64]
featureMapDimensions = [640 // stride for stride in strides]
anchors = ([ 19,27,  44,40,  38,94 ], 
           [ 96,68,  86,152,  180,137 ], 
           [ 140,301,  303,264,  238,542 ],
           [ 436,615,  739,380,  925,792 ]) 
anchorGrid = torch.tensor(anchors).float().view(3, -1, 1, 1, 2)

I updated the anchors and anchorGrid, but I cannot get the strides list and featureMapDimenstiosn work. There are 8 outputs in converted mlmodel's spce(strides only contains 4), which leads to "IndexError: list index (1) out of range"

input {
  name: "image"
  type {
    imageType {
      width: 640
      height: 640
      colorSpace: RGB
    }
  }
}
output {
  name: "var_4058"
  type { multiArrayType { dataType: FLOAT32 }
  }
}
output {
  name: "var_4073"
  type { multiArrayType { dataType: FLOAT32 }
  }
}
output {
  name: "var_4088"
  type { multiArrayType { dataType: FLOAT32 }
  }
}
output {
  name: "var_4103"
  type { multiArrayType { dataType: FLOAT32 }
  }
}
output {
  name: "var_3986"
  type { multiArrayType { dataType: FLOAT32 }
  }
}
output {
  name: "var_4000"
  type { multiArrayType { dataType: FLOAT32 }
  }
}
output {
  name: "var_4014"
  type { multiArrayType { dataType: FLOAT32 }
  }
}
output {
  name: "var_4028"
  type { multiArrayType { dataType: FLOAT32 }
  }
}
metadata {
  userDefined {
    key: "com.github.apple.coremltools.source"
    value: "torch==1.10.0+cu111"
  }
  userDefined {
    key: "com.github.apple.coremltools.version"
    value: "6.0"
  }
}

any idea the correct strides could be?

Ray
  • 29
  • 2

1 Answers1

0

One of the reasons for any index error can be that the there are some implicit variables (or assumptions) that have to be changed in order to fit for the customized model. You might just have to change one of the values (limits) of the variables and the code might get working. Another issue could be that the image is not being loaded.

I had a similar error, in which I had not changed the training dataset value (which was larger than my entire dataset value!)

Thanks!