|
1 | | -import cog |
2 | | -from pathlib import Path |
3 | | -from tensorflow.keras.applications.resnet50 import ResNet50 |
4 | | -from tensorflow.keras.preprocessing import image |
5 | | -from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions |
| 1 | +from typing import Any |
| 2 | + |
6 | 3 | import numpy as np |
| 4 | +from cog import BasePredictor, Input, Path |
| 5 | +from tensorflow.keras.applications.resnet50 import ( |
| 6 | + ResNet50, |
| 7 | + decode_predictions, |
| 8 | + preprocess_input, |
| 9 | +) |
| 10 | +from tensorflow.keras.preprocessing import image as keras_image |
7 | 11 |
|
8 | 12 |
|
9 | | -class ResNetPredictor(cog.Predictor): |
| 13 | +class Predictor(BasePredictor): |
10 | 14 | def setup(self): |
11 | 15 | """Load the model into memory to make running multiple predictions efficient""" |
12 | | - self.model = ResNet50(weights='resnet50_weights_tf_dim_ordering_tf_kernels.h5') |
| 16 | + self.model = ResNet50(weights="resnet50_weights_tf_dim_ordering_tf_kernels.h5") |
13 | 17 |
|
14 | 18 | # Define the arguments and types the model takes as input |
15 | | - @cog.input("input", type=Path, help="Image to classify") |
16 | | - def predict(self, input): |
| 19 | + def predict(self, image: Path = Input(description="Image to classify")) -> Any: |
17 | 20 | """Run a single prediction on the model""" |
18 | 21 | # Preprocess the image |
19 | | - img = image.load_img(input, target_size=(224, 224)) |
20 | | - x = image.img_to_array(img) |
| 22 | + img = keras_image.load_img(image, target_size=(224, 224)) |
| 23 | + x = keras_image.img_to_array(img) |
21 | 24 | x = np.expand_dims(x, axis=0) |
22 | 25 | x = preprocess_input(x) |
23 | 26 | # Run the prediction |
24 | 27 | preds = self.model.predict(x) |
25 | 28 | # Return the top 3 predictions |
26 | | - return str(decode_predictions(preds, top=3)[0]) |
| 29 | + return decode_predictions(preds, top=3)[0] |
0 commit comments