Skip to content

Commit

Permalink
updated raw python
Browse files Browse the repository at this point in the history
  • Loading branch information
AFathi committed Dec 17, 2017
1 parent fecbda4 commit 25448f9
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 11 deletions.
8 changes: 6 additions & 2 deletions notebooks/Classify Data Example-kNN.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import turicreate as turi"
Expand Down Expand Up @@ -185,7 +187,9 @@
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"userinput = turi.SFrame({\n",
Expand Down
8 changes: 5 additions & 3 deletions notebooks/Classify Image Example-ResNet50.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"url = \"data/food_images\""
Expand Down Expand Up @@ -130,8 +132,8 @@
],
"source": [
"data = turi.image_analysis.load_images(url)\n",
"data['foodType'] = data['path'].apply(lambda path: 'Eggs' if 'eggs' in path else 'Soup')\n",
"data.save('egg_or_soup.sframe')\n",
"data[\"foodType\"] = data[\"path\"].apply(lambda path: \"Eggs\" if \"eggs\" in path else \"Soup\")\n",
"data.save(\"egg_or_soup.sframe\")\n",
"data.explore()"
]
},
Expand Down
33 changes: 33 additions & 0 deletions raw_python/classify_data_knn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Import Turi Create package
import turicreate as turi

# Create an SFrame object.
'''
Each column in the `SFrame` is an `SArray` object.
In the following `SFrame` object, we have created a data buffer (dataset) that specifies the size of each pet's eyes, nose, and head.
Each number in the dataset is a ratio from 15cm.
Note:- The ratios in the dataset are NOT accurate or related to reality. Those are random ratios used for demonstration purposes.
'''
dataBuffer = turi.SFrame({
"pet_types": ["cat", "dog", "wolf", "cat", "wolf", "dog"],
"eyes": [0.23, 0.64, 0.89, 0.26, 0.93, 0.66 ],
"nose": [0.11, 0.68, 0.78, 0.08, 0.74, 0.57 ],
"head": [0.34, 0.47, 0.66, 0.37, 0.68, 0.45 ]
})

# Create a nearest neighbor classifier using the SFrame object above.
classifier = turi.nearest_neighbor_classifier.create(dataBuffer, target="pet_types")

# Edit the input below. Make sure the numbers are between 0 and 1.
userinput = turi.SFrame({
"eyes": [0.32],
"nose": [0.20],
"head": [0.42]
})

# Predict the input using the nearest neighbor classifier.
result = classifier.predict(userinput, max_neighbors=2)
print result
37 changes: 37 additions & 0 deletions raw_python/classify_image_resnet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Import Turi Create package
import turicreate as turi

# Refrence the dataset path
url = "/Users/ahmedbekhit/Documents/Data/Development/TuriCreate/repo/turicreate-notebook/notebooks/data/food_images"

# Label the dataset
## Load the dataset folder image content using the image_analysis property
data = turi.image_analysis.load_images(url)
## Create a "foodType" key for each image in the dataset to specify whether it's an Egg or Soup, based on which folder it's located in.
data["foodType"] = data["path"].apply(lambda path: "Eggs" if "eggs" in path else "Soup")
## Export the labeled images as an SFrame object in order to use it while creating our image classifier.
data.save("egg_or_soup.sframe")
## Visualize the new labeled images list.
data.explore()

# Load the SFrame object that contains the labeled images.
dataBuffer = turi.SFrame("egg_or_soup.sframe")

# Randmly split the SFrame object
'''
90% of the data from the original SFrame object will be used to train the image classifier.
10% of the data from the original SFrame object will be used to test the image classifier.
'''
trainingBuffers, testingBuffers = dataBuffer.random_split(0.9)

# Train the image classifier using the Residual Network architecture and pre-trained model.
model = turi.image_classifier.create(trainingBuffers, target="foodType", model="resnet-50")

# Evaluate the test data to determine the model accuracy
evaluations = model.evaluate(testingBuffers)

# Save the Turi Create model to retrieve it later
model.save("egg_or_soup.model")

# Export our new trained image classification model for Core ML.
model.export_coreml("FoodClassifier.mlmodel")
19 changes: 19 additions & 0 deletions raw_python/create_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Import Turi Create package
import turicreate as turi

# Create an SFrame object.
'''
Each column in the `SFrame` is an `SArray` object.
In the following `SFrame` object, we have created a data buffer (dataset) that specifies the size of each pet's eyes, nose, and head.
Each number in the dataset is a ratio from 15cm.
Note:- The ratios in the dataset are NOT accurate or related to reality. Those are random ratios used for demonstration purposes.
'''
dataBuffer = turi.SFrame({
"pet_types": ["cat", "dog", "wolf", "cat", "wolf", "dog"],
"eyes": [0.23, 0.64, 0.89, 0.26, 0.93, 0.66 ],
"nose": [0.11, 0.68, 0.78, 0.08, 0.74, 0.57 ],
"head": [0.34, 0.47, 0.66, 0.37, 0.68, 0.45 ]
})
16 changes: 16 additions & 0 deletions raw_python/sample_CSV_reading.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Import Turi Create package
import turicreate as turi
# Reads the data file, in this case we will read data from a CSV file
'''
Each column in the `SFrame` is an `SArray` object.
In the following line of code, the parameter `header` is set to `False` because the **CSV** data file we're using doesn't have a header.
Note:- The `header` parameter is `True` by default and if we didn't set it to `False` it would show us the first **n+1** rows since it counts the first as a header automatically.
'''
dataBuffer = turi.SFrame.read_csv('/Users/ahmedbekhit/Documents/Data/Development/TuriCreate/SampleCSVFile_119kb.csv', header=False)

# Load the first 5 rows.
dataBuffer.head(5)

print dataBuffer
6 changes: 0 additions & 6 deletions raw_python/sample_reading.py

This file was deleted.

0 comments on commit 25448f9

Please sign in to comment.