Beispiel #1
0
 def predict(self, image_paths):
     path = Path(image_paths[0]).parent
     logger.info(f'Running model on {len(image_paths)} images in {path}')
     probs, labels = self.model.predict(image_paths)
     pred_list = [{
         'prob': float(prob),
         'label': label
     } for prob, label in zip(probs, labels)]
     return {'predictions': pred_list}
Beispiel #2
0
 def save_images(self, doc, path):
     images_doc = doc['images'][:self.images_limit]
     logger.info(f"Saving {len(images_doc)} images to {path}")
     image_paths = []
     for i, image_doc in enumerate(images_doc):
         img_bytes = base64.b64decode(image_doc['content'])
         image_path = path / f'image-{i}.png'
         with open(image_path, 'wb') as f:
             f.write(img_bytes)
         image_paths.append(image_path)
     return image_paths
Beispiel #3
0
    def on_post(self, req, resp):
        path = None
        try:
            req_doc = json.load(req.bounded_stream)
            path = self.data_path / str(uuid4())
            path.mkdir()
            image_paths = self.save_images(req_doc, path)

            resp_doc = self.predict(image_paths)
            resp.body = json.dumps(resp_doc)
        except IOError as e:
            logger.error(e, exc_info=True)
            raise falcon.HTTPError('500')
        finally:
            if path:
                logger.info(f'Cleaning path {path}')
                shutil.rmtree(path)
def test_service():
    DATA_PATH.mkdir(exist_ok=True)

    logger.info(f'Downloading input data from {TEST_DATA_URL} to {DATA_PATH}')
    curl_sp = subprocess.Popen(f'curl -s {TEST_DATA_URL}'.split(' '),
                               stdout=subprocess.PIPE)
    subprocess.Popen(f'tar xz -C {DATA_PATH}'.split(' '), stdin=curl_sp.stdout)

    path_label_df = pd.read_csv(DATA_PATH / 'path_label.csv')
    logger.info('Label counts: {}'.format(
        path_label_df.label.value_counts().to_dict()))

    image_paths = [DATA_PATH / p for p in path_label_df.path.values]
    image_path_chunks = make_chunk_gen(image_paths, chunk_size=32)

    predictions = []
    with ThreadPoolExecutor(2) as pool:
        for ch_preds in pool.map(predict_chunk, image_path_chunks):
            predictions.extend(ch_preds)

    labels = (path_label_df.label == 'off').astype(int).values
    assert len(predictions) == len(labels)

    preds = [p['prob'] > 0.5 for p in predictions]
    test_accuracy = (preds == labels).mean()
    logger.info(f'Test accuracy: {test_accuracy}')

    assert test_accuracy > 0.95, f'Not enough model test set accuracy: {test_accuracy}'
Beispiel #5
0
import falcon

from off_sample.resources import PingResource, PredictResource
from off_sample.utils import logger


def create_app():
    api = falcon.API()
    api.add_route('/off-sample', PingResource())
    api.add_route('/off-sample/predict', PredictResource())
    return api


def get_app():
    return create_app()


if __name__ == '__main__':
    logger.info('Creating app...')
    app = get_app()
    from wsgiref import simple_server

    httpd = simple_server.make_server('0.0.0.0', 9876, app)
    logger.info('Running debug server...')
    httpd.serve_forever()