Skip to content

Instantly share code, notes, and snippets.

@mesadowski
Last active March 13, 2019 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mesadowski/ca907c00bf03acdfa098e6566922d621 to your computer and use it in GitHub Desktop.
Save mesadowski/ca907c00bf03acdfa098e6566922d621 to your computer and use it in GitHub Desktop.
GoogleAutoMLPredictionsviaAPI.ipy
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [],
"source": [
"# import libraries, including Google Cloud Auto ML libraries\n",
"import sys\n",
"\n",
"from google.cloud import automl_v1beta1\n",
"from google.cloud.automl_v1beta1.proto import service_pb2\n"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [],
"source": [
"# Note that we're explicitly defining our service account credentials in order to make API calls to the prediction \n",
"# service. This is a path to a JSON file containing keys to a service account that I set up, with access to Auto ML\n",
"# You can create that via the GCP console\n",
"service_account_path ='/Users/michaelsadowski/Desktop/cancer-image-recognition-1298fcf33184.json'"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [],
"source": [
"#Define our prediction client\n",
"def get_prediction(content, project_id, model_id):\n",
"\n",
" prediction_client = automl_v1beta1.PredictionServiceClient.from_service_account_file(service_account_path)\n",
" name = 'projects/{}/locations/us-central1/models/{}'.format(project_id, model_id)\n",
" payload = {'image': {'image_bytes': content }}\n",
" params = {}\n",
" request = prediction_client.predict(name, payload, params)\n",
" return request # waits till request is returned"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [],
"source": [
"# Define some variables: file path to our test image, project ID in Google Cloud Platform\n",
"# and your model ID in AutoML, which is available after training the model\n",
"file_path = '/Users/michaelsadowski/Desktop/cell_images/Parasitized/C100P61ThinF_IMG_20150918_144104_cell_162.png'\n",
"project_id = 'cancer-image-recognition'\n",
"model_id = 'ICN1395947982826315941'"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [],
"source": [
"# read the image file and put it in content\n",
"with open(file_path, 'rb') as ff:\n",
" content = ff.read()"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"payload {\n",
" classification {\n",
" score: 0.9994016885757446\n",
" }\n",
" display_name: \"parasitized\"\n",
"}"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Call the Auto ML prediction service (rest API) and see what it predicts for this image\n",
"get_prediction(content, project_id, model_id)"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"payload {\n",
" classification {\n",
" score: 0.9986653327941895\n",
" }\n",
" display_name: \"uninfected\"\n",
"}"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Let's try another one, which is uninfected\n",
"file_path = '/Users/michaelsadowski/Desktop/cell_images/Uninfected/C1_thinF_IMG_20150604_104722_cell_73.png'\n",
"# read the image file and put it in content\n",
"with open(file_path, 'rb') as ff:\n",
" content = ff.read()\n",
"get_prediction(content, project_id, model_id)"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [],
"source": [
"# OK good, it thinks that one's not infected"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment