IMDB_GO_CLIENT.md 4.3 KB
Newer Older
D
Dong Daxiang 已提交
1 2
# How to use Go Client of Paddle Serving

3 4
([简体中文](./IMDB_GO_CLIENT_CN.md)|English)

D
Dong Daxiang 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
This document shows how to use Go as your client language. For Go client in Paddle Serving, a simple client package is provided https://github.com/PaddlePaddle/Serving/tree/develop/go/serving_client, a user can import this package as needed. Here is a simple example of sentiment analysis task based on IMDB dataset.

### Install

We suppose you have 1.9.2 or later version installed and python 2.7 version installed

```shell
go get github.com/PaddlePaddle/Serving/go/serving_client
go get github.com/PaddlePaddle/Serving/go/proto
pip install paddle-serving-server
```

### Download Text Classification Model

``` shell
20
wget https://paddle-serving.bj.bcebos.com/data/text_classification/imdb_serving_example.tar.gz
D
Dong Daxiang 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
tar -xzf imdb_serving_example.tar.gz
```

### Server Side Code

```python
# test_server_go.py
import os
import sys
from paddle_serving_server import OpMaker
from paddle_serving_server import OpSeqMaker
from paddle_serving_server import Server

op_maker = OpMaker()
read_op = op_maker.create('general_text_reader')
D
Dong Daxiang 已提交
36 37
general_infer_op = op_maker.create('general_infer')
general_response_op = op_maker.create('general_text_response')
D
Dong Daxiang 已提交
38 39 40 41

op_seq_maker = OpSeqMaker()
op_seq_maker.add_op(read_op)
op_seq_maker.add_op(general_infer_op)
D
Dong Daxiang 已提交
42
op_seq_maker.add_op(general_response_op)
D
Dong Daxiang 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

server = Server()
server.set_op_sequence(op_seq_maker.get_op_sequence())
server.load_model_config(sys.argv[1])
server.prepare_server(workdir="work_dir1", port=9292, device="cpu")
server.run_server()
```

### Start Server

``` shell
python test_server_go.py ./serving_server_model/ 9292
```

### Client code example

``` go
// imdb_client.go
package main

import (
       "io"
       "fmt"
       "strings"
       "bufio"
       "strconv"
       "os"
D
Dong Daxiang 已提交
70
       serving_client "github.com/PaddlePaddle/Serving/go/serving_client"
D
Dong Daxiang 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
)

func main() {
     var config_file_path string
     config_file_path = os.Args[1]
     handle := serving_client.LoadModelConfig(config_file_path)
     handle = serving_client.Connect("127.0.0.1", "9292", handle)

     test_file_path := os.Args[2]
     fi, err := os.Open(test_file_path)
     if err != nil {
     	fmt.Print(err)
     }

     defer fi.Close()
     br := bufio.NewReader(fi)

     fetch := []string{"cost", "acc", "prediction"}     

     var result map[string][]float32

     for {
     	 line, err := br.ReadString('\n')
D
Dong Daxiang 已提交
94
	 if err == io.EOF {
D
Dong Daxiang 已提交
95
	       break
D
Dong Daxiang 已提交
96
	 }
D
Dong Daxiang 已提交
97

D
Dong Daxiang 已提交
98
	 line = strings.Trim(line, "\n")
D
Dong Daxiang 已提交
99

D
Dong Daxiang 已提交
100
	 var words = []int64{}
D
Dong Daxiang 已提交
101

D
Dong Daxiang 已提交
102 103 104
	 s := strings.Split(line, " ")
	 value, err := strconv.Atoi(s[0])
	 var feed_int_map map[string][]int64
D
Dong Daxiang 已提交
105
       
D
Dong Daxiang 已提交
106
	 for _, v := range s[1:value + 1] {
D
Dong Daxiang 已提交
107 108
	       int_v, _ := strconv.Atoi(v)
	       words = append(words, int64(int_v))
D
Dong Daxiang 已提交
109
	 }
D
Dong Daxiang 已提交
110

D
Dong Daxiang 已提交
111
	 label, err := strconv.Atoi(s[len(s)-1])
D
Dong Daxiang 已提交
112

D
Dong Daxiang 已提交
113
	 if err != nil {
D
Dong Daxiang 已提交
114
	       panic(err)
D
Dong Daxiang 已提交
115
	 }
D
Dong Daxiang 已提交
116

D
Dong Daxiang 已提交
117 118 119
	 feed_int_map = map[string][]int64{}
	 feed_int_map["words"] = words
	 feed_int_map["label"] = []int64{int64(label)}
D
Dong Daxiang 已提交
120
	 
D
Dong Daxiang 已提交
121 122
	 result = serving_client.Predict(handle, feed_int_map, fetch)
	 fmt.Println(result["prediction"][1], "\t", int64(label))
D
Dong Daxiang 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    }
}
```

### Prediction based on IMDB Test set

``` python
go run imdb_client.go serving_client_conf/serving_client_conf.stream.prototxt test.data > result
```



### Compute accuracy

```python
// acc.go
package main

import (
       "io"
       "os"
       "fmt"
       "bufio"
       "strings"
       "strconv"
)

func main() {
     score_file := os.Args[1]
     fi, err := os.Open(score_file)
     if err != nil {
     	fmt.Print(err)
     }

     defer fi.Close()
     br := bufio.NewReader(fi)     
    
     total := int(0)
     acc := int(0)
     for {
     	 line, err := br.ReadString('\n')
     if err == io.EOF {
        break
     }
    
     line = strings.Trim(line, "\n")
     s := strings.Split(line, "\t")
     prob_str := strings.Trim(s[0], " ")
     label_str := strings.Trim(s[1], " ")
     prob, err := strconv.ParseFloat(prob_str, 32)
     if err != nil {
        panic(err)
     }
     label, err := strconv.ParseFloat(label_str, 32)
     if err != nil {
        panic(err)
     }
     if (prob - 0.5) * (label - 0.5) > 0 {
        acc++
     }
     total++
    }
    fmt.Println("total num: ", total)
    fmt.Println("acc num: ", acc)
    fmt.Println("acc: ", float32(acc) / float32(total))    

}
```

D
Dong Daxiang 已提交
192
```shell
D
Dong Daxiang 已提交
193
go run acc.go result
D
Dong Daxiang 已提交
194 195 196
total num:  25000
acc num:  22014
acc:  0.88056
D
Dong Daxiang 已提交
197
```