Spaces:
Runtime error
Runtime error
| import numpy as np | |
| import tensorflow as tf | |
| import gradio as gr | |
| from huggingface_hub import from_pretrained_keras | |
| import cv2 | |
| # import matplotlib.pyplot as plt | |
| model = from_pretrained_keras("keras-io/conv_Mixer") | |
| # functions for inference | |
| IMG_SIZE = 32 | |
| class_names = [ | |
| "Airplane", | |
| "Automobile", | |
| "Bird", | |
| "Cat", | |
| "Deer", | |
| "Dog", | |
| "Frog", | |
| "Horse", | |
| "Ship", | |
| "Truck", | |
| ] | |
| # resize the image and it to a float between 0,1 | |
| def preprocess_image(image, label): | |
| image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE)) | |
| return image, label | |
| def read_image(image): | |
| image = tf.convert_to_tensor(image) | |
| image.set_shape([None, None, 3]) | |
| print('$$$$$$$$$$$$$$$$$$$$$ in read image $$$$$$$$$$$$$$$$$$$$$$') | |
| print(image.shape) | |
| # plt.imshow(image) | |
| # plt.show() | |
| image, _ = preprocess_image(image, 1) # 1 here is a temporary label | |
| return image | |
| def infer(input_image): | |
| print('#$$$$$$$$$$$$$$$$$$$$$$$$$ IN INFER $$$$$$$$$$$$$$$$$$$$$$$') | |
| image_tensor = read_image(input_image) | |
| print(image_tensor.shape) | |
| predictions = model.predict(np.expand_dims((image_tensor), axis=0)) | |
| predictions = np.squeeze(predictions).astype(float) | |
| return dict(zip(class_names, predictions)) | |
| # get the inputs | |
| input = gr.inputs.Image(shape=(IMG_SIZE, IMG_SIZE)) | |
| # the app outputs two segmented images | |
| output = [gr.outputs.Label()] | |
| # it's good practice to pass examples, description and a title to guide users | |
| examples = [["./content/examples/Frog.jpg"], ["./content/examples/Truck.jpg"], ["./content/examples/car.jpg"]] | |
| title = "Image classification with ConvMixer" | |
| description = "Upload an image or select from examples to classify it. This is a <b>ConvMixer Model</b> trained on <b>CIFAR-10</b>. The allowed classes are - Airplane, Automobile, Bird, Cat, Deer, Dog, Frog, Horse, Ship, Truck <p><b>Space author: Harshavardhan</b> <br><b> Keras example author: <a href=\"https://twitter.com/RisingSayak\"> Sayak Paul </a> </b> <br> <a href=\"https://keras.io/examples/vision/convmixer/\">link to the original Keras example</a> </p>" | |
| gr_interface = gr.Interface(infer, input, output, examples=examples, allow_flagging=False, analytics_enabled=False, title=title, description=description).launch(enable_queue=True, debug=False) | |
| gr_interface.launch() | |