You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改TuriCreate代码实现多类动物CoreML图像模型训练?

Hey there! Let's get your TuriCreate image classifier set up for all five animal classes (dog, cat, pigeon, squirrel, raccoon) without a hitch. First, let's start with the best practice for organizing your data—this will make labeling your images way simpler and less error-prone:

Step 1: Organize Your Image Files

Create a train directory, then add 5 subdirectories inside it, each named exactly after your animal classes:

  • train/dog → all dog images here
  • train/cat → all cat images here
  • train/pigeon → all pigeon images here
  • train/squirrel → all squirrel images here
  • train/raccoon → all raccoon images here

This structure lets TuriCreate easily pull the category name directly from the folder path, no messy string checks needed.

Step 2: Modified Code for 5 Classes

Here's the updated code tailored to your five categories. I've commented each change so you know what's happening:

import turicreate as tc

# Load all images from the train directory, keeping their file paths
data = tc.image_analysis.load_images('train', with_path=True)

# Extract the category label from the folder path (e.g., "dog" from "train/dog/spot.jpg")
# Adjust the split index if your OS uses backslashes (Windows) – use path.split('\\')[-2] instead
data['label'] = data['path'].apply(lambda path: path.split('/')[-2])

# Save the labeled dataset for later use (avoids reloading images every time)
data.save('animal_images.sframe')

# Split data into 80% training, 20% testing
train_data, test_data = data.random_split(0.8)

# Train the image classifier on your labeled data
model = tc.image_classifier.create(train_data, target='label')

# Evaluate model performance on the test set (check accuracy, precision, etc.)
metrics = model.evaluate(test_data)
print("Model Evaluation Results:\n", metrics)

# Save the TuriCreate model and export to CoreML format
model.save('animal_classifier.model')
model.export_coreml('AnimalImages.mlmodel')

If You Can't Organize into Subfolders

If your images are all in the root train folder (e.g., filenames like squirrel_001.jpg, raccoon_123.png), replace the label extraction line with this to pull the category from the filename:

import re
# Use regex to match any of your five animal classes in the file path/name
data['label'] = data['path'].apply(lambda path: re.search(r'(dog|cat|pigeon|squirrel|raccoon)', path).group())

Quick Tips for Better Results

  • Try to keep the number of images per class roughly balanced – this prevents the model from favoring over-represented classes
  • TuriCreate will use GPU acceleration automatically if available, which cuts down training time significantly
  • If you run into image loading errors, double-check that all files are valid JPG/PNG (TuriCreate supports most common formats)

内容的提问来源于stack exchange,提问作者Yuto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:49:51