Sign inSign up

developmentseed/hv_grid

By developmentseed

•Updated almost 8 years ago

TensorFlow Serving model/image for detecting high-voltage infrastructure in satellite images.

Image
0

722

developmentseed/hv_grid repository overview

⁠Overview

This is a TensorFlow Serving image containing a model for detecting High-Voltage (HV) grid infrastructure. The model here is a classification model for detecting HV towers and substations in z18 satellite imagery. The model is based off the Xception architecture and was trained on about 47,000 images sampled from the whole of Pakistan. The dataset was expert validated by our Data Team.

⁠Sending images/Receiving predictions via REST

The docker image takes in a json-like dict (via a POST request) with base64 encoded images as input. The input should be encoded as in the example below and all keywords much match exactly. It's okay to remove the newlines though — they are only included here for readability. You can include as many images as will fit in memory, but it's likely worth testing out different batch sizes to optimize throughput.

{
  "instances": [
    {
      "image_bytes": {"b64": "iVBO…Oxs6"}
    },
    {
      "image_bytes": {"b64": "0KGg…Pyg8"}
    },
    {
      "image_bytes": {"b64": "AABr…EKA0"}
    }
  ]
}

The model will return a json response with 3 float values per image encoding the predicted probability the three potential classes: [background, tower, substation].

⁠Running with docker

Run the docker CPU-based image like: docker run -p 8501:8501 -t developmentseed/hv_grid:v1

Or the GPU version like: docker run --runtime=nvidia -p 8501:8501 -t developmentseed/hv_grid:v1-gpu

Make sure you have the requirements satisfied (GPU, NVIDIA drivers, and nvidia-docker) as outlined here⁠.

⁠Inference example

The bare bones python script to send prediction requests is below.

import os
import os.path as op
import json
import pprint
import time
import base64
import requests

################################
# Build local prediction request
################################
data_dir = 'my/data/dir'
server_endpoint = 'http://localhost:8501/v1/models/hv_grid:predict'

img_fnames = ['negatives/18-181844-106012.png',
                          'towers/18-184234-105926.png',
                          'substations/18-185040-106631.png']

img_fpaths = [op.join(data_dir, img_fname) for img_fname in img_fnames]

instances = []
for img_fpath in img_fpaths:
    with open(img_fpath, 'rb') as imageFile:
        b64_image = base64.b64encode(imageFile.read())
        instances.append({'image_bytes': {'b64': b64_image.decode('utf-8')}})

payload = json.dumps({"instances": instances})

#########################
# Send prediction request
#########################
print('Num of images in payload: {}'.format(len(instances)))

start = time.time()
r = requests.post(server_endpoint, data=payload)
elapsed = time.time() - start

#########################
# Print results
#########################
pp = pprint.PrettyPrinter()
print('\nPredictions from local images:')
pp.pprint(json.loads(r.content)['predictions'])

print('Elapsed time: {} sec'.format(elapsed))

Tag summary

Content type

Image

Digest

Size

1.1 GB

Last updated

almost 8 years ago

docker pull developmentseed/hv_grid:v1-gpu