Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Instructions don't work. Cant create CoreML model, crashes #112

Closed
amayers opened this issue Dec 17, 2017 · 9 comments
Closed

Instructions don't work. Cant create CoreML model, crashes #112

amayers opened this issue Dec 17, 2017 · 9 comments

Comments

@amayers
Copy link

amayers commented Dec 17, 2017

So I'm trying to make my first model. First the installation instructions are incorrect, if you follow them to the T, they fail with a non useful error on pip install -U turicreate. Turns out this project requires you to downgrade to an ancient Python version. How about the installation instructions include that detail, or better yet, make it work with the current version that ships on all Macs.

Ok, so I worked around that, and started on replicating https://apple.github.io/turicreate/docs/userguide/image_classifier/introduction.html to make sure everything is working properly before I go through the work of gathering real training data.

I downloaded the following images into a images folder, with a subfolder for dogs and tractor
dog_urls.txt
tractor_urls.txt

I copy/pasted the sample code into a new python file and just change the paths.

import turicreate as tc

# Load images
sf = tc.image_analysis.load_images('images', with_path=True)

# From the path-name, create a label column
data['label'] = data['path'].apply(lambda path: 'dog' if 'dogs' in path else 'tractor')

# Save the data for future use
data.save('dogs-tractors.sframe')

# Explore interatively
data.explore()

Then go to run it python <file_just_created>.py and it fails because sf = tc.image_analysis.load_images('train', with_path=True) writes to sf but the rest of the file expects it to be data. Another incorrect doc, but easy enough to fix. Changed sf to data and it runs, and I can see that the SFrame matches what I'd expect. Now I copy & paste the next code snippet from the guide, just changing the file names.


# Load the data
data =  tc.SFrame('dogs-tractors.sframe')

# Make a train-test split
train_data, test_data = data.random_split(0.8)

# Automatically picks the right model based on your data.
model = tc.image_classifier.create(train_data, target='label')

# Save predictions to an SArray
predictions = model.predict(test_data)

# Evaluate the model and save the results into a dictionary
metrics = model.evaluate(test_data)
print(metrics['accuracy'])

# Save the model for later use in Turi Create
model.save('mymodel.model')

# Export for use in Core ML
model.export_coreml('MyCustomImageClassifier.mlmodel')

It goes off and processes for a few minutes as expected, then fails in the export to CoreML step:

python export.py 
[09:35:58] src/nnvm/legacy_json_util.cc:190: Loading symbol saved by previous version v0.8.0. Attempting to upgrade...
[09:35:58] src/nnvm/legacy_json_util.cc:198: Symbol successfully upgraded!
Resizing images...
Performing feature extraction on resized images...
Completed 340/340
PROGRESS: Creating a validation set from 5 percent of training data. This may take a while.
          You can set ``validation_set=None`` to disable validation tracking.

WARNING: The number of feature dimensions in this problem is very large in comparison with the number of examples. Unless an appropriate regularization value is set, this model may not provide accurate predictions for a validation/test set.
WARNING: Detected extremely low variance for feature(s) '__image_features__' because all entries are nearly the same.
Proceeding with model training using all features. If the model does not provide results of adequate quality, exclude the above mentioned feature(s) from the input dataset.
Logistic regression:
--------------------------------------------------------
Number of examples          : 324
Number of classes           : 2
Number of feature columns   : 1
Number of unpacked features : 2048
Number of coefficients    : 2049
Starting L-BFGS
--------------------------------------------------------
+-----------+----------+-----------+--------------+-------------------+---------------------+
| Iteration | Passes   | Step size | Elapsed Time | Training-accuracy | Validation-accuracy |
+-----------+----------+-----------+--------------+-------------------+---------------------+
| 1         | 7        | 0.000046  | 1.121025     | 0.672840          | 0.625000            |
| 2         | 9        | 1.000000  | 1.173264     | 0.944444          | 0.937500            |
| 3         | 10       | 1.000000  | 1.210695     | 0.996914          | 1.000000            |
| 4         | 11       | 1.000000  | 1.242987     | 0.996914          | 1.000000            |
| 5         | 12       | 1.000000  | 1.277638     | 0.990741          | 1.000000            |
| 6         | 14       | 1.000000  | 1.323503     | 0.996914          | 1.000000            |
+-----------+----------+-----------+--------------+-------------------+---------------------+
TERMINATED: Iteration limit reached.
This model may not be optimal. To improve it, consider increasing `max_iterations`.
0.987654320988
Fatal Python error: PyThreadState_Get: no current thread
Abort trap: 6

I know its getting past the model.save('mymodel.model') line fine as I can see that file being created, and have even been able to open it in another python script successfully.

How do you actually get this thing to export the CoreML file?

@srikris
Copy link
Contributor

srikris commented Dec 18, 2017

Are you on a python installed by homebrew? Currently, coremltools doesn't work with that Python version.
apple/coremltools#88

This could explain your current crash.

@amayers
Copy link
Author

amayers commented Dec 18, 2017

Yes I am on Python installed by homebrew. How do I install a version of python that works with this project since it seems like it's so picky about the version & install method???

@igiloh
Copy link
Collaborator

igiloh commented Dec 18, 2017

Hi @amayers , thank you for taking interest in Turi Create!

The documentation issue have already been fixed, as you can see here.

As @srikris have mentioned - the CoreML team is aware of the problem with Homebrew.
There are several recommended ways to install python on macOS:

  1. Using Anaconda - my personal favorite.
  2. Downloading directly from python.org.
  3. Use the native python version that comes with macOS, which is located in /usr/bin/python.

All of which have been tested and work well with both turicreate and coremltools.
If you have any further questions about installing or using turicreate - please feel free to ask.

@amayers
Copy link
Author

amayers commented Dec 18, 2017

Hi @igiloh I would have loved to use the native python version that ships with macOS, however Turi Create isn't even found when you try to use pip install -U turicreate on it. It wasn't until I installed 2.7.14 from Homebrew that Turi Create could be pip installed.

@igiloh
Copy link
Collaborator

igiloh commented Dec 18, 2017

Hi @amayers, thanks again for the further input.

I've just re-tested on a clean macOS10.12 and 10.13 machines - and on both I was able to successfully perform

pip install --upgrade pip
pip install -U turicreate

I was also able to create a model and export it to Core ML.

Which macOS version are you using?
Can you please print out the current pip version you're using, and the error you're getting?

@amayers
Copy link
Author

amayers commented Dec 18, 2017

I just uninstalled python from homebrew leaving just the system python installed.

python --version
Python 3.6.3 :: Anaconda, Inc.

Then I recreated the virtual environment:

virtualenv egg_environment
Using base prefix '/anaconda3'
New python executable in /Users/amayers/Code/Side_Projects/egg_environment/bin/python
Installing setuptools, pip, wheel...done.
Andrews-13-MacBook-Pro:Side_Projects amayers$ source egg_environment/bin/activate
(egg_environment) Andrews-13-MacBook-Pro:Side_Projects amayers$ pip install -U turicreate
Collecting turicreate
  Could not find a version that satisfies the requirement turicreate (from versions: )
No matching distribution found for turicreate

I also tried again adding the pip install --upgrade pip line that wasn't in the instructions. It gave the output Requirement already up-to-date: pip in ./egg_environment/lib/python3.6/site-packages, and didn't fix the install error.

I'm running macOS 10.13.2 (17C88), and pip: pip 9.0.1 from /Users/amayers/Code/Side_Projects/egg_environment/lib/python3.6/site-packages (python 3.6)

@igiloh
Copy link
Collaborator

igiloh commented Dec 18, 2017

As mentioned in the documentation, turicreate only supports python 2.7 at the moment. Any python 2 version from 2.7.8 and up to the latest 2.7.14 (released 9/2017) should work.
Python 3 support is still an ongoing process, as you can see here: #2

macOS 10.13 comes natively with python 2.7.10, and should support the turicreate package out of the box.
If you do choose to install Anaconda (which again, I personally really enjoy) - please make sure to install the python 2.7 version, which is available here.

@srikris
Copy link
Contributor

srikris commented Dec 18, 2017

For the macOS native python, it should look like this. (It seems you have another version of python installed)

$ python --version
Python 2.7.10
$ which python
/usr/bin/python

If you see /usr/local/bin/python for your python location then you probably have a mismatched version of Python and pip.

As @igiloh mentioned, your Python seems to be 3.6 which isn't yet supported (we will be fixing that soon). So Python 2.7 should work.

@amayers
Copy link
Author

amayers commented Dec 18, 2017

After I installed Anaconda 2.7 it now works. But this is the first time I've done any python on this machine, and it had 3.6 on it. I guess some 3rd party app updated it?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants