Segmentation Fault when calling model.predict
- Dominant language
- Python
- Stars
- 5.4k
- Forks
- 850
- Avg merge
- 4d 5h
- Merged PRs (30d)
- 10
Description
Hello fellow developers 👋
## 🐞Describe the bug
- The python application crashes (SIGSEGV/ segmentation faults) when `model.predict` is called on a model which has TensorType (not ImageType) as input.. Model conversion happens fine. The reason I want to use TensorType is that I want to pass a tensor in (which has negative values, etc) and not an image. The model actually works with an `ImageType`, but I have found that the numbers are very inaccurate when using CoreML compared to Python TensorFlow or TensorFlow Lite (on Python or Android). I can report this inaccuracy in another github issue in the future, but for this issue, I am trying to get TensorType to work.
- Is this a converter issue? Unsure, it happens when I call `model.predict`.
## Trace
No trace, just `[1] 80595 segmentation fault python3 python_file_name.py`
## To Reproduce
First install dependencies: `pip install tensorflow numpy keras-vggface coremltools keras_applications `
```python
import coremltools as ct
from keras_vggface import VGGFace
import numpy as np
from tensorflow.keras.preprocessing import image
from keras_vggface import utils
def create_core_ml_model():
input = ct.TensorType(shape=(1, 224, 224, 3))
keras_model = VGGFace(model="senet50", pooling="avg", include_top=False, input_shape=(224, 224, 3))
coreml_model = ct.convert(keras_model, inputs=[input])
coreml_model.save("model.mlmodel")
create_core_ml_model()
# Download a random image
image_path = "https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftinyjpg.com%2Fimages%2Fsocial%2Fwebsite.jpg&f=1&nofb=1"
import urllib.request
r = urllib.request.urlopen(image_path)
with open("image.jpg", "wb") as f:
f.write(r.read())
img = image.load_img('image.jpg', target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = utils.preprocess_input(x, version=2)
coreml_model = ct.models.MLModel("model.mlmodel")
output_dictionary = coreml_model.predict({"input_1": x}) <---- THIS IS WHERE IT SIGSEGV's without any other warnings.
output = output_dictionary["Identity"][0]
print("output: ", output)
```
- If applicable, please attach the source model
- The model is freely available on the internet, and is automatically downloaded by the script I wrote above (It is done by the keras-vggface dependency you installed). It is 100MB, so I would prefer not to upload it as I have ADSL internet...😅
## System environment (please complete the following information):
- coremltools version (e.g., 3.0b5): 4.0
- OS (e.g., MacOS, Linux): MacOS
- macOS version (if applicable): macOS Big Sur 11.2 Beta (20D5029f), x86_64 (latest update)
- XCode version (if applicable): Version 12.3 (12C33)
- How you install python (anaconda, virtualenv, system): Python3's `venv`
- python version (e.g. 3.7): Python 3.7.9
- any other relevant information:
- Tensorflow 2.3.1 & Tensorflow 2.4 (tried both separately)
Contributor guide
Research direction
Start by running the provided reproducer with the listed coremltools, TensorFlow, Python, and macOS versions, focusing on the ct.models.MLModel.predict call using TensorType input. Compare that path with conversion and ImageType prediction; done means the TensorType prediction completes without a SIGSEGV and returns the expected output dictionary.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- macos, numpy, python, tensorflow
- Domain
- machine-learning, operating-systems
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100