Skip to content

Commit

Permalink
Merge pull request #5 from medtune/dev-0.0.3
Browse files Browse the repository at this point in the history
Bump v0.0.3
  • Loading branch information
aelbouchti committed Sep 13, 2018
2 parents d9a05db + f0ee28b commit 4dffd80
Show file tree
Hide file tree
Showing 16 changed files with 433 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ jobs:
- run:
name: push image to registry
command: |
docker push medtune/capsul:v0.0.2
docker push medtune/capsul:v0.0.3
docker push medtune/capsul:latest
72 changes: 57 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
PROJECT=beta-platform
OS_TYPE=$(shell uname -a)
BRANCH=$(shell git rev-parse --abbrev-ref HEAD)
GITCOMMIT=$(shell git rev-parse HEAD)
BUILDDATE=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
MAJOR=0
MINOR=0
PATCH=3
REVISION=alpha
VERSION=v$(MAJOR).$(MINOR).$(PATCH)
GOVERSION=1.11
LONGVERSION=v$(MAJOR).$(MINOR).$(PATCH)-$(REVISION)
CWD=$(shell pwd)
VPATH=github.com/medtune/capsul/pkg
PROJECTPATH=$(CWD)
AUTHORS=Hilaly.Mohammed-Amine/El.bouchti.Alaa
OWNERS=$(AUTHORS)
LICENSETYPE=Apache-v2.0
LICENSEURL=https://raw.githubusercontent.com/medtune/capsul/master/LICENSE.txt


CONTAINER_ENGINE=docker
CAPSUL_VERSION=0.0.2
CAPSUL_CMD_VERSION=0.0.1
MAINTAINERS=AEB MAHs

GOCMD=go
GOVERSION=$(GOCMD) version
Expand All @@ -10,9 +28,6 @@ GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get

BINARY_FILE=cmd/main.go
BINARY_NAME=capsul

build:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -o $(BINARY_NAME) -v $(BINARY_FILE)

Expand Down Expand Up @@ -40,7 +55,7 @@ build-base:

# build capsul package and tag version/latest
capsul: build-base
docker tag medtune/capsul:base medtune/capsul:v0.0.2
docker tag medtune/capsul:base medtune/capsul:$(VERSION)
docker tag medtune/capsul:base medtune/capsul:latest


Expand All @@ -59,7 +74,7 @@ build-cmd:

# build capsul cmd & tag version/latest
capsul-cmd: build-cmd
docker tag medtune/capsul:cmd medtune/capsul:cmd-v0.0.2
docker tag medtune/capsul:cmd medtune/capsul:cmd-$(VERSION)
docker tag medtune/capsul:cmd medtune/capsul:cmd-latest


Expand Down Expand Up @@ -120,19 +135,46 @@ build-mura-cam: build-mura-mn-v2-cam
docker tag medtune/capsul:mura-mn-v2-cam medtune/capsul:mura-cam


# Build chexray
build-chexray:
@echo building model capsul chexray ...
# Build chexray mobilenet v2
build-chexray-mn-v2:
docker build \
-t medtune/capsul:chexray-mn-v2 \
-f build/capsules/chexray_mobilenet_v2.Dockerfile \
.


# Build chexray mobilenet v2 grad cam
build-chexray-mn-v2-cam:
docker build \
-t medtune/capsul:chexray \
-f build/capsules/chexray.Dockerfile \
-t medtune/capsul:chexray-mn-v2 \
-f build/csflask/chexray_mobilenet_v2_cam.Dockerfile \
.


build-csflask: build-mura-mn-v2-cam
# Build chexray densenet 121
build-chexray-dn-121:
@echo building model capsul chexray densenet 121 ...
docker build \
-t medtune/capsul:chexray-dn-121 \
-f build/capsules/chexray_densenet_121.Dockerfile \
.


# Build chexray
build-chexray-pp:
docker build \
-t medtune/capsul:chexray-pp-helper \
-f build/csflask/chexray_pp.Dockerfile \
.


# Build csflask
build-csflask: build-mura-mn-v2-cam \
build-chexray-pp

# Build capsules
build-capsules: build-mnist \
build-inception \
build-mura-mn-v2 \
build-mura-irn-v2
build-mura-irn-v2 \
build-chexray-dn-121
2 changes: 1 addition & 1 deletion build/base.Dockerfile
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
Expand Down
135 changes: 135 additions & 0 deletions csflask/chexray_pp.py
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.
2 changes: 2 additions & 0 deletions csflask/requirements.pp.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pandas
flask
119 changes: 119 additions & 0 deletions examples/chexray-inference/main.go
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))
}
Loading

0 comments on commit 4dffd80

Please sign in to comment.