Skip to content

Instantly share code, notes, and snippets.

@pranjalsatija
Last active July 9, 2018 02:13
Show Gist options
  • Save pranjalsatija/504b5bacffcb518fca5999345a9f31f8 to your computer and use it in GitHub Desktop.
Save pranjalsatija/504b5bacffcb518fca5999345a9f31f8 to your computer and use it in GitHub Desktop.
import os
import turicreate as tc
path = '../' # The path to the root of your project directory.
coreml_path = os.path.join(path, 'pascal.mlmodel') # The path to export the Core ML model to.
model_path = os.path.join(path, 'pascal.model') # The path to export the .model file to.
sf_path = os.path.join(path, 'data.sframe') # The path to load the SFrame from. See gen_sframe.py for more.
result_sf_path = os.path.join(path, 'result_data.sframe') # The path to save the test data with predictions to.
data = tc.SFrame(sf_path) # The training data for our model.
number_of_images = 50
max_iterations = 500
# This line truncates the data to the first number_of_images rows. We do this to make the model train faster, but it
# comes at the expense of potential accuracy. When you have extra time, comment out line 17 and the model will
# take longer to train, but be more accurate.
data = data.head(number_of_images)
# This line splits the data into training and testing sets. We do this so after we train the model, we can evaluate it
# on data that it hasn't seen before. This is important to make sure that the model's learning has generalized properly,
# which makes it more effective when it comes to real-world inference.
train_data, test_data = data.random_split(0.8)
# This line trains the model using our training data. Turi Create automatically takes care of selecting a model
# architecture, choosing metrics for evaluation, and determining the right values for things like dropout rate and
# number of epochs.
model = tc.object_detector.create(train_data, max_iterations=max_iterations)
# This line uses the test data and the trained model to make predictions on inputs the model hasn't seen before.
# After doing it, it saves a part of the SFrame we originally loaded with predictions that the model has made.
# We will take a look at these predictions later so we can get a visual idea of how the model did.
test_data['predictions'] = model.predict(test_data)
test_data.save(result_sf_path)
# This line exports the model as a Turi Create model so we can train it more without having to start over.
model.save(model_path)
# This line exports the model for use with Core ML on iOS and macOS devices.
model.export_coreml(coreml_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment