I am trying to make a coreML model using turicreate. (This is my first time touching python)
Lets say I have five files, "dog", "cat", "pigeon", "squirrel", "raccoon".
How can I change this code so that the program to access and train it.
import turicreate as tc
data = tc.image_analysis.load_images('train', with_path=True)
data['label'] = data['path'].apply(lambda path: 'cat' if 'cat' in path else 'dog')
data.save('images.sframe')
train_data, test_data = data.random_split(0.8)
model = tc.image_classifier.create(train_data, target='label')
predictions = model.predict(test_data)
metrics = model.evaluate(test_data)
model.save('mymodel.model')
model.export_coreml('AnimalImages.mlmodel')
Just as a note, you're probably going to need more than a single image of each additional animal (or category) that you want the model to recognize. If you don't have a bunch of images for each category yet, you can try to find some here: http://image-net.org/synset?wnid=n01318894
The Turi Create demo you are using assumes that your all of the images for each category are saved in their own folder. So you would have separate folders for images/dog
, images/cat
, images/pigeon
, etc.
The tutorial uses this directory structure to generate a set of labels for each image. You can generalize what they have by replacing the data['label'] = ...
line with:
import os
data['label'] = data['path'].apply(
lambda path: os.path.dirname(path).split('/')[-1]
)
This will look at the path of every image and use the deepest folder name as the label for training.