-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from medtune/dev-0.0.3
Bump v0.0.3
- Loading branch information
Showing
16 changed files
with
433 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM golang:1.10 | ||
FROM golang:1.11 | ||
|
||
# Update system (ubuntu) | ||
RUN apt-get update && apt update | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import tensorflow as tf | ||
|
||
import DenseNet.preprocessing.densenet_pre as dp | ||
|
||
import pandas as pd | ||
import json, codecs | ||
import os | ||
|
||
ROLE="chexray preprocessing for densenet 121" | ||
|
||
flags = tf.app.flags | ||
|
||
flags.DEFINE_string('data_path', 'data', 'data directory path') | ||
|
||
# Server flags | ||
flags.DEFINE_integer('port', 12030, 'flask listen port') | ||
flags.DEFINE_boolean('debug', False, 'debug flask server') | ||
FLAGS = flags.FLAGS | ||
|
||
# Define consts | ||
base_path = FLAGS.data_path | ||
|
||
if not os.path.exists(base_path): | ||
print('creating base path:', base_path) | ||
os.mkdir(base_path) | ||
|
||
print("[INFO] initializing tf graph...") | ||
|
||
# Create main graph | ||
MainGraph = tf.Graph() | ||
with MainGraph.as_default(): | ||
tf.logging.set_verbosity(tf.logging.INFO) | ||
file_input = tf.placeholder(tf.string, ()) | ||
image = tf.image.decode_image(tf.read_file(file_input), channels=3) | ||
copy = tf.identity(image) | ||
image = tf.image.convert_image_dtype(image, tf.float32) | ||
image.set_shape([None,None,3]) | ||
image = dp.preprocess_image(image, 224,224, is_training=False) | ||
image = tf.expand_dims(image,0) | ||
|
||
print("[INFO] initialized") | ||
|
||
# Run cam on image | ||
def pp_image(graph, src): | ||
sess = tf.Session(graph=graph) | ||
img = sess.run([image], {file_input : src}) | ||
sess.close() | ||
return img | ||
|
||
if not os.path.exists(base_path): | ||
print('creating base path:', base_path) | ||
os.mkdir(base_path) | ||
|
||
from flask import ( | ||
Flask, | ||
redirect, | ||
url_for, | ||
render_template, | ||
request, | ||
session, | ||
) | ||
|
||
from flask import ( | ||
make_response, | ||
jsonify, | ||
) | ||
|
||
# Flask app | ||
app = Flask(__name__) | ||
|
||
# send json response | ||
def sendJSON(obj): | ||
return make_response(jsonify(obj)) | ||
|
||
@app.route('/process', methods=["POST"]) | ||
def process_image(): | ||
try: | ||
target = request.json.get('target') | ||
targetTemp = target | ||
target = os.path.join(base_path, target) | ||
|
||
array = pp_image(MainGraph, target) | ||
|
||
return sendJSON({ | ||
'success' : True, | ||
'target' : targetTemp, | ||
'out' : pd.Series(array).to_json(orient='values'), | ||
}), 200 | ||
|
||
except Exception as e: | ||
return sendJSON({ | ||
'success' : False, | ||
'target' : targetTemp, | ||
'errors' : [repr(e)], | ||
}), 200 | ||
|
||
@app.route('/status', methods=["GET"]) | ||
def status(): | ||
return sendJSON({ | ||
'success': True, | ||
'status' : 'AVAILABLE', | ||
'version' : 1, | ||
'pwd' : os.getcwd(), | ||
'base_path' : base_path, | ||
'flask_debug' : FLAGS.debug, | ||
'flask_port' : FLAGS.port, | ||
}), 200 | ||
|
||
@app.route('/list', methods=["GET"]) | ||
def get_available_data(): | ||
try: | ||
files = [] | ||
for fname in os.listdir(base_path): | ||
path = os.path.join(base_path, fname) | ||
if os.path.isdir(path): | ||
continue | ||
files += [path] | ||
|
||
return sendJSON({ | ||
'success' : True, | ||
'files' : files, | ||
}) | ||
except Exception as e: | ||
return sendJSON({ | ||
'success' : False, | ||
'errors' : [repr(e)], | ||
}), 200 | ||
|
||
if __name__ == "__main__": | ||
# Run server | ||
app.run( | ||
host='0.0.0.0', | ||
port=FLAGS.port, | ||
debug=FLAGS.debug, | ||
) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pandas | ||
flask |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/medtune/capsul/pkg/pbreq" | ||
"github.com/medtune/capsul/pkg/pbreq/stdimpl" | ||
tfsclient "github.com/medtune/capsul/pkg/tfs-client" | ||
) | ||
|
||
type responsePP struct { | ||
Success bool `json:"success"` | ||
Target string `json:"target"` | ||
Out string `json:"out"` | ||
} | ||
|
||
type imageData struct { | ||
data []float64 | ||
} | ||
|
||
type requestPP struct { | ||
Target string `json:"target"` | ||
} | ||
|
||
func getFloatList(file string) (*responsePP, error) { | ||
url := "http://localhost:12030/process" | ||
|
||
jsonStr, err := json.Marshal(&requestPP{file}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req.Header.Set("Content-Type", "application/json") | ||
|
||
client := &http.Client{} | ||
r, err := client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer r.Body.Close() | ||
|
||
body, err := ioutil.ReadAll(r.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
t := responsePP{} | ||
err = json.Unmarshal(body, &t) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &t, nil | ||
} | ||
|
||
func main() { | ||
start := time.Now() | ||
// Read image file | ||
r, err := getFloatList("00000003_002.png") | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
var data [][][][][]float32 | ||
err = json.Unmarshal([]byte(r.Out), &data) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
var onelist []float32 | ||
d := data[0][0] | ||
|
||
for _, i := range d { | ||
for _, j := range i { | ||
onelist = append(onelist, j[0], j[1], j[2]) | ||
} | ||
} | ||
|
||
fmt.Println(len(onelist)) | ||
|
||
// Connection to tf server | ||
client, err := tfsclient.New("localhost:10031") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// timeout context | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
|
||
// Prediction Request: | ||
meta := stdimpl.ChexrayDN121 | ||
req := pbreq.PredictFTest(meta, onelist) | ||
|
||
// Run prediction | ||
resp, err := client.Predict(ctx, req) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
l := resp.GetOutputs()["scores"].GetFloatVal() | ||
|
||
for _, i := range l { | ||
fmt.Printf("%.2f ", i*float32(100)) | ||
} | ||
fmt.Println(time.Since(start)) | ||
} |
Oops, something went wrong.