Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Http Server #37

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Dockerfile.server
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM tensorflow/tensorflow:latest-py3

# Install system packages
RUN apt-get update && apt-get install -y --no-install-recommends \
bzip2 \
g++ \
git \
graphviz \
libgl1-mesa-glx \
libhdf5-dev \
openmpi-bin \
wget && \
rm -rf /var/lib/apt/lists/*

COPY src /src
COPY entrypoints /src/entrypoints
COPY models /models

WORKDIR /src

RUN pip install -r requirements.txt

ENV PYTHONPATH='/src/:$PYTHONPATH'

ENTRYPOINT ["entrypoints/entrypoint.predict.server.sh"]
10 changes: 10 additions & 0 deletions entrypoints/entrypoint.predict.server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -e

BASE_MODEL_NAME=$1
WEIGHTS_FILE=$2

# predict
python -m evaluater.server \
--base-model-name $BASE_MODEL_NAME \
--weights-file $WEIGHTS_FILE
1 change: 0 additions & 1 deletion src/evaluater/predict.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import os
import glob
import json
Expand Down
86 changes: 86 additions & 0 deletions src/evaluater/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
from flask import Flask, request, jsonify
from evaluater.predict import image_file_to_json, image_dir_to_json, predict
import urllib
import shutil
import argparse
from keras import backend as K
from PIL import ImageFile, Image
from handlers.model_builder import Nima
from handlers.data_generator import TestDataGenerator

app = Flask('server')

def load_model(config):
global model
model = Nima(config.base_model_name)
model.build()
model.nima_model.load_weights(config.weights_file)
model.nima_model._make_predict_function() # https://github.com/keras-team/keras/issues/6462
model.nima_model.summary()

def main(image_source, predictions_file, img_format='jpg'):
# load samples
if os.path.isfile(image_source):
image_dir, samples = image_file_to_json(image_source)
else:
image_dir = image_source
samples = image_dir_to_json(image_dir, img_type='jpg')

ImageFile.LOAD_TRUNCATED_IMAGES = True
Image.MAX_IMAGE_PIXELS = None

# initialize data generator
data_generator = TestDataGenerator(samples, image_dir, 64, 10, model.preprocessing_function(),
img_format=img_format)

# get predictions
predictions = predict(model.nima_model, data_generator)
K.clear_session()

# calc mean scores and add to samples
for i, sample in enumerate(samples):
sample['mean_score_prediction'] = calc_mean_score(predictions[i])

if predictions_file is not None:
save_json(samples, predictions_file)

return samples

@app.route('/prediction', methods=['POST'])
def prediction():

global images

if request.method == 'POST':
images = request.json

if images:

shutil.rmtree('temp')
os.mkdir('temp')
for image in images:
filename_w_ext = os.path.basename(image)
try:
urllib.request.urlretrieve(image, 'temp/'+ filename_w_ext)
except:
print('An exception occurred :' + image)

result = main('temp', None)

return jsonify(result)

return jsonify({'error': 'Image is not available'})

if __name__ == '__main__':

parser = argparse.ArgumentParser()
parser.add_argument('-b', '--base-model-name', help='CNN base model name', required=True)
parser.add_argument('-w', '--weights-file', help='path of weights file', required=True)
args = parser.parse_args()

load_model(args)
app.run(host='0.0.0.0', port=5005)
1 change: 1 addition & 0 deletions src/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
keras==2.1.*
nose==1.3.*
pillow==5.0.*
Flask==1.0.*