Blazer007/image_classification_using_conv_mixer
1
1import numpy as np2import tensorflow as tf3import gradio as gr4from huggingface_hub import from_pretrained_keras5 6model = from_pretrained_keras("keras-io/conv_mixer_image_classification")7 8class_names = [9 "Airplane",10 "Automobile",11 "Bird",12 "Cat",13 "Deer",14 "Dog",15 "Frog",16 "Horse",17 "Ship",18 "Truck",19]20 21examples = [22 ['./aeroplane.png'],23 ['./horse.png'],24 ['./ship.png'],25 ['./truck.png']26] 27 28IMG_SIZE = 3229 30def infer(input_image):31 image_tensor = tf.convert_to_tensor(input_image)32 image_tensor.set_shape([None, None, 3])33 image_tensor = tf.image.resize(image_tensor, (IMG_SIZE, IMG_SIZE))34 predictions = model.predict(np.expand_dims((image_tensor), axis=0))35 predictions = np.squeeze(predictions)36 predictions = np.argmax(predictions)37 predicted_label = class_names[predictions.item()]38 return str(predicted_label)39 40 41input = gr.inputs.Image(shape=(IMG_SIZE, IMG_SIZE))42output = [gr.outputs.Label(label = "Model Output")]43 44title = "Image Classification using Conv Mixer Model"45description = "Upload an image or select from examples to classify it.<br>The allowed classes are - Airplane, Automobile, Bird, Cat, Deer, Dog, Frog, Horse, Ship, Truck.<br><p><b>Model Repo - https://huggingface.co/keras-io/conv_mixer_image_classification</b> <br><b>Keras Example - https://keras.io/examples/vision/convmixer//</b></p>"46 47 48article = "<div style='text-align: center;'><a href='https://twitter.com/_Blazer_007' target='_blank'>Space by Vivek Rai</a><br><a href='https://twitter.com/RisingSayak' target='_blank'>Keras example by Sayak Paul</a></div>"49 50gr_interface = gr.Interface(51 infer, 52 input, 53 output, 54 examples=examples, 55 allow_flagging=False, 56 analytics_enabled=False, 57 title=title, 58 description=description,59 article=article).launch(enable_queue=True, debug=True)