Spaces:
Build error
Build error
Download resnet_model.py from vaishanthr/Image-Classifier-TensorFlow: direct link, hf CLI and curl.
- Browser
- Download file 922 Bytes
-
https://huggingface.co/spaces/vaishanthr/Image-Classifier-TensorFlow/resolve/main/resnet_model.py
- Command line
-
hf download hf://spaces/vaishanthr/Image-Classifier-TensorFlow/resnet_model.py
-
curl -L -o resnet_model.py https://huggingface.co/spaces/vaishanthr/Image-Classifier-TensorFlow/resolve/main/resnet_model.py
922 Bytes
| import tensorflow as tf | |
| from tensorflow import keras | |
| from tensorflow.keras import layers | |
| class ResNetClassifier: | |
| def __init__(self): | |
| self.model = keras.applications.ResNet50(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000) | |
| def preprocess_image(self, image): | |
| img = keras.preprocessing.image.array_to_img(image) | |
| img = img.resize((224, 224)) | |
| img_array = keras.preprocessing.image.img_to_array(img) | |
| img_array = tf.expand_dims(img_array, 0) | |
| img_array = keras.applications.resnet50.preprocess_input(img_array) | |
| return img_array | |
| def classify_image(self, image): | |
| # Preprocess the image | |
| img_array = self.preprocess_image(image) | |
| # Classify the image | |
| predictions = self.model.predict(img_array) | |
| predicted_classes = keras.applications.imagenet_utils.decode_predictions(predictions, top=3)[0] | |
| return predicted_classes |