-1

I'm trying to build a Machine Learning - Image Recognition using Create ML in Xcode 10.1 Playground but I'm having some problems to put my data in the model.

I have a folder with images numbered from 1 to 1336 and a .csv file with 2 columns (the image name and the image classification).

I don't know exactly how to put this in the model.

I have this until now:

import Cocoa
import CreateML

let data = try MLDataTable(contentsOf: URL(fileURLWithPath: "/Users/x/Desktop/CoreML/project/file.csv"))

let(trainingData, testingData) = data.randomSplit(by: 0.8, seed: 1)

let Classifier = try MLImageClassifier *need help here*

let evaluationMetrics = sentimentClassifier.evaluation(on: testingData)

let evaluationAccuracy = (1 - evaluationMetrics.classificationError) * 100

let metaData = MLModelMetadata(author: "x", shortDescription: "Model", version: "1.0")

try classifier.write(to: URL(fileURLWithPath: "/Users/x/Desktop/CoreML/project/XClassifier.mlmodel"))
Kaushik Makwana
  • 1,329
  • 2
  • 14
  • 24
Leonardo
  • 1
  • 3

2 Answers2

0

I believe it is not possible to feed labels to MLImageClassifier via .csv or any other separate file. You have only two options: use file names as labels or use directories as labels (probably preferable in your case of many images):

let model = try MLImageClassifier(trainingData: .labeledDirectories(at: trainingDir))
let evaluation = model.evaluation(on: .labeledDirectories(at: testingDir))

You will need to put images into subdirectories named as labels in your .csv file.

Maxim Volgin
  • 3,957
  • 1
  • 23
  • 38
0

I was just struggling with this myself. Here is a solution to re-organise data for CreateML. All credit goes to Tony T1 who came up with this script.

  1. Place images and CSV file into a single folder.

  2. In Automator, create a new workflow like this: enter image description here

  1. Run the workflow. Select your CSV and watch the images get sorted into their respective folders!

The script is as follows:

cd "${1%/*}"
while read line         
do         
     FolderName=${line%;*}
     ImageName=${line#*;}
     mkdir "$FolderName"
     mv "$ImageName" "$FolderName"
done < "$1"
  • DF20 is the starting folder, you can change that to whatever you wish
  • my CSV was separated by ";". If your CSV is separated by ",", change that symbol in the script (e.g. FolderName=${line%,*} )
  • In my CSV, classes were columnA and images columnB. Switch this around depending on your case.
blu-Fox
  • 401
  • 6
  • 14