File size: 5,612 Bytes
2b7aae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# STARRY Python ML Services

Unified ML inference services for STARRY music notation recognition system.

## Architecture

This module provides lightweight wrappers around serialized ML models:
- **TorchScript** (.pt) for PyTorch models (layout, mask, semantic, gauge, loc)
- **SavedModel** for TensorFlow models (ocr, brackets)

## Services

| Service | Port | Framework | Description |
|---------|------|-----------|-------------|
| layout | 12022 | PyTorch | Page layout detection (intervals, rotation) |
| gauge | 12023 | PyTorch | Staff gauge prediction (height, slope) |
| mask | 12024 | PyTorch | Staff foreground/background mask |
| semantic | 12025 | PyTorch | Symbol semantic detection (77 classes) |
| loc | 12026 | PyTorch | Text location detection (13 categories) |
| ocr | 12027 | TensorFlow | Text recognition (DenseNet-CTC) |
| brackets | 12028 | TensorFlow | Bracket sequence recognition |

## Installation

```bash
pip install -r requirements.txt
```

## Model Export

Before using the services, models need to be exported from the original projects.

### PyTorch Models (TorchScript)

Run in the deep-starry environment:

```bash
cd /home/camus/work/deep-starry
python /path/to/scripts/export_torchscript.py \
    --mode layout \
    --config configs/your-config \
    --output models/layout.pt
```

### TensorFlow Models (SavedModel)

Run in the starry-ocr environment:

```bash
cd /home/camus/work/starry-ocr
python /path/to/scripts/export_tensorflow.py \
    --mode ocr \
    --config pretrained/OCR_Test/config.yaml \
    --output models/ocr_savedmodel
```

## Usage

### Start a Service

```bash
python main.py -m layout -w models/layout.pt -p 12022 -dv cuda
```

### With Configuration File

```bash
python main.py -m semantic -w models/semantic.pt -p 12025 --config config/semantic.yaml
```

### Client Example (Python)

```python
import zmq
from msgpack import packb, unpackb

# Connect
ctx = zmq.Context()
sock = ctx.socket(zmq.REQ)
sock.connect("tcp://localhost:12022")

# Send request
with open('image.png', 'rb') as f:
    image_buffer = f.read()

sock.send(packb({
    'method': 'predict',
    'args': [[image_buffer]],
    'kwargs': {}
}))

# Receive response
result = unpackb(sock.recv())
print(result)
```

## Directory Structure

```
python-services/
├── common/
│   ├── __init__.py
│   ├── zero_server.py      # ZeroMQ server
│   ├── image_utils.py      # Image processing utilities
│   └── transform.py        # Data transformation pipeline
├── predictors/
│   ├── __init__.py
│   ├── torchscript_predictor.py   # PyTorch loader
│   └── tensorflow_predictor.py    # TensorFlow loader
├── services/
│   ├── __init__.py
│   ├── layout_service.py
│   ├── mask_service.py
│   ├── semantic_service.py
│   ├── gauge_service.py
│   ├── loc_service.py
│   ├── ocr_service.py
│   └── brackets_service.py
├── config/
│   └── semantic.yaml       # Example configuration
├── scripts/
│   ├── export_torchscript.py
│   └── export_tensorflow.py
├── main.py                 # Unified entry point
├── requirements.txt
└── README.md
```

## Docker Deployment

### Prerequisites

1. Docker with NVIDIA GPU support (nvidia-docker2 / nvidia-container-toolkit)
2. User must be in the `docker` group:
   ```bash
   sudo usermod -aG docker $USER
   # Re-login to apply group membership
   ```

### Quick Start

```bash
cd backend/python-services

# Build all-in-one image
docker build -f Dockerfile --target all-in-one -t starry-ml:latest ../../..

# Run layout service (example)
docker run --gpus all -p 12022:12022 \
  -v /path/to/models/starry-dist:/models/starry-dist:ro \
  -v /path/to/deep-starry:/app/deep-starry:ro \
  starry-ml:latest \
  python /app/deep-starry/streamPredictor.py \
  /models/starry-dist/20221125-scorelayout-1121-residue-u-d4-w64-d4-w64 \
  -p 12022 -dv cuda -m layout
```

### Using Docker Compose

```bash
# Test single service
docker compose -f docker-compose.test.yml up layout

# Production: all services
docker compose up -d
```

### Model Volumes

Mount the following directories to the container:
- `starry-dist/` - PyTorch model weights (layout, mask, semantic, gauge)
- `ocr-dist/` - TensorFlow/PyTorch weights (loc, ocr, brackets)

### Build Targets

The Dockerfile provides multiple build targets:

| Target | Description | Size |
|--------|-------------|------|
| `pytorch-services` | PyTorch only (layout, mask, semantic, gauge) | ~5GB |
| `tensorflow-services` | TensorFlow only (ocr, brackets) | ~4GB |
| `all-in-one` | Both frameworks (all services) | ~9GB |

### Example Dockerfile (lightweight)

## Protocol

Communication uses ZeroMQ REP/REQ pattern with MessagePack serialization.

### Request Format

```python
{
    'method': 'predict',
    'args': [[buffer1, buffer2, ...]],  # List of image byte buffers
    'kwargs': {}                         # Optional keyword arguments
}
```

### Response Format

```python
{
    'code': 0,           # 0 for success, -1 for error
    'msg': 'success',
    'data': [...]        # List of prediction results
}
```

## Notes

1. **TorchScript Compatibility**: Some dynamic operations may not be supported. Test models after export.

2. **Preprocessing Consistency**: Ensure preprocessing matches the original implementation exactly.

3. **TensorFlow Version**: SavedModel format requires compatible TF version for loading.

4. **GPU Memory**: TensorFlow models use memory growth to prevent OOM. Configure as needed.