0
# Model Loading and Configuration
1
2
BodyPix model loading system with configurable architectures and performance characteristics. Choose between MobileNet for speed or ResNet for accuracy, with various configuration options for different use cases.
3
4
## Capabilities
5
6
### Load Function
7
8
Loads and initializes a BodyPix model with optional configuration.
9
10
```typescript { .api }
11
/**
12
* Loads a BodyPix model instance
13
* @param config - Optional model configuration
14
* @returns Promise resolving to loaded BodyPix model instance
15
*/
16
function load(config?: ModelConfig): Promise<BodyPix>;
17
18
interface ModelConfig {
19
/** Neural network architecture - MobileNetV1 for speed, ResNet50 for accuracy */
20
architecture: 'MobileNetV1' | 'ResNet50';
21
/** Output stride - smaller values = higher accuracy, larger values = faster inference */
22
outputStride: 8 | 16 | 32;
23
/** Depth multiplier for MobileNet only - affects model size and accuracy */
24
multiplier?: 0.50 | 0.75 | 1.0;
25
/** Custom model URL for offline or alternative model hosting */
26
modelUrl?: string;
27
/** Weight quantization - higher values = larger model size, better accuracy */
28
quantBytes?: 1 | 2 | 4;
29
}
30
```
31
32
**Usage Examples:**
33
34
```typescript
35
import * as bodyPix from '@tensorflow-models/body-pix';
36
37
// Load with default configuration (MobileNetV1, balanced settings)
38
const net = await bodyPix.load();
39
40
// Load ResNet model for higher accuracy
41
const highAccuracyNet = await bodyPix.load({
42
architecture: 'ResNet50',
43
outputStride: 16,
44
quantBytes: 4
45
});
46
47
// Load MobileNet model optimized for speed
48
const fastNet = await bodyPix.load({
49
architecture: 'MobileNetV1',
50
outputStride: 32,
51
multiplier: 0.50,
52
quantBytes: 2
53
});
54
55
// Load from custom URL
56
const customNet = await bodyPix.load({
57
architecture: 'MobileNetV1',
58
outputStride: 16,
59
modelUrl: 'https://custom-server.com/bodypix-model/'
60
});
61
```
62
63
### Model Architecture Options
64
65
#### MobileNetV1
66
- **Best for**: Real-time applications, mobile devices, speed-critical use cases
67
- **Output Strides**: 8, 16, 32
68
- **Multipliers**: 0.50, 0.75, 1.0
69
- **Trade-offs**: Faster inference, smaller model size, lower accuracy
70
71
#### ResNet50
72
- **Best for**: High-accuracy applications, batch processing, desktop applications
73
- **Output Strides**: 16, 32
74
- **Multipliers**: 1.0 only
75
- **Trade-offs**: Higher accuracy, larger model size, slower inference
76
77
### Configuration Guidelines
78
79
**For Real-time Video Applications:**
80
```typescript
81
const config = {
82
architecture: 'MobileNetV1',
83
outputStride: 16,
84
multiplier: 0.75,
85
quantBytes: 2
86
};
87
```
88
89
**For High-Quality Image Processing:**
90
```typescript
91
const config = {
92
architecture: 'ResNet50',
93
outputStride: 16,
94
quantBytes: 4
95
};
96
```
97
98
**For Maximum Speed (30+ FPS):**
99
```typescript
100
const config = {
101
architecture: 'MobileNetV1',
102
outputStride: 32,
103
multiplier: 0.50,
104
quantBytes: 1
105
};
106
```
107
108
### Low-Level Activation Methods
109
110
For advanced use cases requiring access to intermediate neural network outputs.
111
112
```typescript { .api }
113
class BodyPix {
114
/**
115
* Returns low-level activation tensors for person segmentation
116
* @param input - Image input
117
* @param internalResolution - Internal processing resolution
118
* @param segmentationThreshold - Segmentation confidence threshold
119
* @returns Object containing segmentation tensor and intermediate outputs
120
*/
121
segmentPersonActivation(
122
input: BodyPixInput,
123
internalResolution: BodyPixInternalResolution,
124
segmentationThreshold?: number
125
): Promise<{
126
segmentation: tf.Tensor2D;
127
heatmapScores: tf.Tensor3D;
128
offsets: tf.Tensor3D;
129
displacementFwd: tf.Tensor3D;
130
displacementBwd: tf.Tensor3D;
131
}>;
132
133
/**
134
* Returns low-level activation tensors for body part segmentation
135
* @param input - Image input
136
* @param internalResolution - Internal processing resolution
137
* @param segmentationThreshold - Segmentation confidence threshold
138
* @returns Object containing part segmentation tensor and intermediate outputs
139
*/
140
segmentPersonPartsActivation(
141
input: BodyPixInput,
142
internalResolution: BodyPixInternalResolution,
143
segmentationThreshold?: number
144
): Promise<{
145
partSegmentation: tf.Tensor2D;
146
heatmapScores: tf.Tensor3D;
147
offsets: tf.Tensor3D;
148
displacementFwd: tf.Tensor3D;
149
displacementBwd: tf.Tensor3D;
150
}>;
151
}
152
```
153
154
### Model Disposal
155
156
```typescript { .api }
157
class BodyPix {
158
/**
159
* Disposes the model and frees memory resources
160
*/
161
dispose(): void;
162
}
163
```
164
165
**Usage:**
166
```typescript
167
// Clean up model when done
168
net.dispose();
169
```
170
171
## Performance Considerations
172
173
- **Output Stride**: Lower values (8) provide higher accuracy but require more computation
174
- **Architecture**: MobileNetV1 is 2-4x faster than ResNet50 but less accurate
175
- **Multiplier**: Only affects MobileNetV1; lower values reduce model size and increase speed
176
- **Quantization**: Lower quantBytes reduce model size and loading time but may affect accuracy
177
- **Memory**: Call `dispose()` to free GPU memory when model is no longer needed