(input: ImageInput, options?: ImageClassificationOptions)
| 40 | constructor(public graphModel: GraphModel, public dictionary: string[]) {} |
| 41 | |
| 42 | async classify(input: ImageInput, options?: ImageClassificationOptions): |
| 43 | Promise<ImagePrediction[]> { |
| 44 | options = sanitizeOptions(options); |
| 45 | |
| 46 | const scores = tidy(() => { |
| 47 | const preprocessedImg = this.preprocess(input, options); |
| 48 | return this.graphModel.predict(preprocessedImg) as Tensor; |
| 49 | }); |
| 50 | const probabilities = await scores.data() as Float32Array; |
| 51 | scores.dispose(); |
| 52 | const result = Array.from(probabilities) |
| 53 | .map((prob, i) => ({label: this.dictionary[i], prob})); |
| 54 | return result; |
| 55 | } |
| 56 | |
| 57 | private preprocess(input: ImageInput, options: ImageClassificationOptions) { |
| 58 | // Preprocessing involves center crop and normalizing between [-1, 1]. |
nothing calls this directly
no test coverage detected