如何在Turi Create中导入自定义Apple Core Motion数据集?
Hey there! I’ve worked with Turi Create’s activity classifier using custom Core Motion data before, so let me walk you through the exact steps to get your dataset processed, imported, and turned into a working model.
First, Turi Create works best with structured tabular data for activity classification. Your recorded Core Motion data (accelerometer + gyroscope values) needs to be formatted correctly—here’s the setup I recommend:
- Use a CSV file(s) where each row represents a single timestamp’s sensor reading
- Include these columns:
acc_x,acc_y,acc_z: 3-axis accelerometer valuesgyro_x,gyro_y,gyro_z: 3-axis gyroscope valueslabel: The action category (e.g.,"jump","walk","sit")- (Optional but helpful)
session_id: A unique identifier for each continuous recording session (e.g., one session = 30 seconds of walking). This helps Turi Create group sequential data correctly.
If you recorded each action separately (e.g., one file for jumps, one for walks), that’s totally fine—we’ll merge them later.
Open your terminal and run:
pip3 install turicreate
(Use pip instead of pip3 if you’re using a Python 2 environment, though Python 3 is recommended.)
Fire up a Python script or Jupyter notebook and start by importing Turi Create, then load your data.
Option A: Single CSV File with All Data
import turicreate as tc # Load the full dataset data = tc.SFrame.read_csv("your_core_motion_data.csv") # Verify the data loaded correctly print(data.head())
Option B: Multiple CSV Files (One per Action)
If you have separate files for each action, load them individually, add labels, then merge:
import turicreate as tc # Load each action's data walk_data = tc.SFrame.read_csv("walk_data.csv") jump_data = tc.SFrame.read_csv("jump_data.csv") sit_data = tc.SFrame.read_csv("sit_data.csv") # Add label columns to each walk_data["label"] = "walk" jump_data["label"] = "jump" sit_data["label"] = "sit" # Add session IDs (replace the numbers with unique IDs for each recording session) walk_data["session_id"] = 1 # If this is one continuous walk session jump_data["session_id"] = 2 sit_data["session_id"] = 3 # Merge all into one SFrame data = walk_data.append(jump_data).append(sit_data)
Activity classification relies on analyzing sequences of sensor data, not individual data points. Use Turi Create’s built-in utility to group your data into sequences by session_id:
# Aggregate data into sequences grouped by session_id and labeled sequences = tc.activity_classifier.util.aggregate( data, session_id="session_id", label="label", features=["acc_x", "acc_y", "acc_z", "gyro_x", "gyro_y", "gyro_z"] )
This converts your raw timestamp-based data into sequence objects that the model can learn from.
Split your sequences into a training set (to teach the model) and a test set (to evaluate its performance):
train_sequences, test_sequences = sequences.random_split(0.8) # 80% training, 20% testing
Now build the model using your training sequences:
model = tc.activity_classifier.create( train_sequences, session_id="session_id", label="label", features=["acc_x", "acc_y", "acc_z", "gyro_x", "gyro_y", "gyro_z"] )
The model will automatically extract time-based features (like mean, variance, peak values) from your sequences to learn the patterns of each action.
Check how well your model performs on the test data:
evaluation_metrics = model.evaluate(test_sequences) # Print overall accuracy print(f"Model Accuracy: {evaluation_metrics['accuracy']}") # Print confusion matrix to see which actions are being misclassified print("\nConfusion Matrix:") print(evaluation_metrics["confusion_matrix"])
Once you’re happy with the model, use it to predict actions from new Core Motion data:
# Example: Load a new sequence of sensor data (format must match training data) new_data = tc.SFrame.read_csv("new_activity_data.csv") new_sequence = tc.activity_classifier.util.aggregate( new_data, session_id="session_id", features=["acc_x", "acc_y", "acc_z", "gyro_x", "gyro_y", "gyro_z"] ) # Make predictions predictions = model.predict(new_sequence) print(f"Predicted Action: {predictions[0]}")
If you want to use this model on iOS, export it as a Core ML model:
# Save the Turi Create model model.save("core_motion_activity_model") # Export to Core ML format model.export_coreml("CoreMotionActivityClassifier.mlmodel")
Quick Tips for Better Results
- Record enough samples: Aim for at least 10-20 unique recording sessions per action (e.g., 10 different walking clips, 10 different jumping clips) to improve generalization.
- Simulate real-world conditions: Record data in different environments (indoors/outdoors) and with variations (e.g., fast walking, slow walking, different jump heights) so the model handles diverse scenarios.
- Clean noisy data: If your sensor data has spikes or noise, apply a simple smoothing filter (like a moving average) before importing it into Turi Create.
内容的提问来源于stack exchange,提问作者AlbF




