1

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')
Yuto
  • 658
  • 2
  • 8
  • 20

2 Answers2

1

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.

  • I assume you mean teach a model to recognize handwritten or printed text characters such as "A" and "a". If so, the answer is yes. You'll need some different training data, though: https://www.nist.gov/itl/iad/image-group/emnist-dataset – jamesonthecrow Mar 16 '18 at 16:38
  • Thanks, I'll try it – Yuto Mar 19 '18 at 13:58
1

Replace the 2nd and 3rd line of code with below 2 lines and you are done:

data = tc.image_analysis.load_images('train', with_path = True, recursive = True)

data["all_images"] = data["path"].apply(lambda path: getImageFromPath(path))

Declare this below the import lines:

def getImageFromPath(path):
return os.path.basename(os.path.dirname(os.path.normpath(path)))

And make sure to keep all the training image folder in a folder named 'train' (as in your case). And keep 'train' folder in the same path as this python file.

Shobhit C
  • 828
  • 10
  • 15