Spaces:
Runtime error
Runtime error
| from transformers import ViTConfig, ViTForImageClassification | |
| from transformers import ViTFeatureExtractor | |
| from PIL import Image | |
| import requests | |
| import matplotlib.pyplot as plt | |
| import gradio as gr | |
| from transformers import ImageClassificationPipeline, PerceiverForImageClassificationConvProcessing, PerceiverFeatureExtractor | |
| import torch | |
| # option 1: load with randomly initialized weights (train from scratch) | |
| config = ViTConfig(num_hidden_layers=12, hidden_size=768) | |
| model = ViTForImageClassification(config) | |
| #print(config) | |
| feature_extractor = ViTFeatureExtractor() | |
| # or, to load one that corresponds to a checkpoint on the hub: | |
| #feature_extractor = ViTFeatureExtractor.from_pretrained("google/vit-base-patch16-224") | |
| #the following gets called by classify_image() | |
| feature_extractor = PerceiverFeatureExtractor.from_pretrained("deepmind/vision-perceiver-conv") | |
| model = PerceiverForImageClassificationConvProcessing.from_pretrained("deepmind/vision-perceiver-conv") | |
| image_pipe = ImageClassificationPipeline(model=model, feature_extractor=feature_extractor) | |
| def classify_image(image): | |
| results = image_pipe(image) | |
| # convert to format Gradio expects | |
| output = {} | |
| for prediction in results: | |
| predicted_label = prediction['label'] | |
| score = prediction['score'] | |
| output[predicted_label] = score | |
| return output | |
| image = gr.inputs.Image(type="pil") | |
| image_piped = "" | |
| label = gr.outputs.Label(num_top_classes=5) | |
| examples = [["cats.jpg"], ["dog.jpg"]] | |
| title = "Generate a Story from an Image" | |
| description = "Demo for classifying images with Perceiver IO. To use it, simply upload an image and click 'submit' to let the model predict the 5 most probable ImageNet classes. Results will show up in a few seconds." + image_piped | |
| article = "<p style='text-align: center'></p>" | |
| gr.Interface(fn=classify_image, inputs=image, outputs=label, title=title, description=description, examples="", enable_queue=True).launch(debug=True) | |