hp1318 commited on
Commit
44a9406
·
verified ·
1 Parent(s): b98d18a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -10
app.py CHANGED
@@ -1,36 +1,41 @@
1
  import torch
 
2
  import torchvision.transforms as transforms
3
  from PIL import Image
4
  import gradio as gr
5
 
6
-
7
  classes = ['airplane', 'automobile', 'bird', 'cat', 'deer',
8
  'dog', 'frog', 'horse', 'ship', 'truck']
9
 
 
 
 
 
 
 
10
 
 
11
  transform = transforms.Compose([
12
  transforms.Resize((32, 32)),
13
  transforms.ToTensor(),
14
  transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
15
  ])
16
 
17
-
18
- model = torch.load('model.pth', map_location=torch.device('cpu'))
19
- model.eval()
20
-
21
-
22
  def predict(image):
23
- image = transform(image).unsqueeze(0)
24
  with torch.no_grad():
25
  output = model(image)
26
  _, predicted = torch.max(output, 1)
27
  return classes[predicted.item()]
28
 
29
-
30
  interface = gr.Interface(fn=predict,
31
  inputs=gr.Image(type="pil"),
32
  outputs="label",
33
- title="CIFAR-10 Image Classification with ViT")
34
-
35
 
 
36
  interface.launch()
 
 
1
  import torch
2
+ import torchvision.models as models # Replace with your ViT model if needed
3
  import torchvision.transforms as transforms
4
  from PIL import Image
5
  import gradio as gr
6
 
7
+ # CIFAR-10 class names
8
  classes = ['airplane', 'automobile', 'bird', 'cat', 'deer',
9
  'dog', 'frog', 'horse', 'ship', 'truck']
10
 
11
+ # Define the model architecture (replace with your ViT if needed)
12
+ model = models.resnet18(num_classes=10) # Use your custom model here
13
+
14
+ # Load the model weights
15
+ model.load_state_dict(torch.load('model.pth', map_location=torch.device('cpu')))
16
+ model.eval() # Set the model to evaluation mode
17
 
18
+ # Define image transformations
19
  transform = transforms.Compose([
20
  transforms.Resize((32, 32)),
21
  transforms.ToTensor(),
22
  transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
23
  ])
24
 
25
+ # Define the prediction function
 
 
 
 
26
  def predict(image):
27
+ image = transform(image).unsqueeze(0) # Add batch dimension
28
  with torch.no_grad():
29
  output = model(image)
30
  _, predicted = torch.max(output, 1)
31
  return classes[predicted.item()]
32
 
33
+ # Create Gradio interface
34
  interface = gr.Interface(fn=predict,
35
  inputs=gr.Image(type="pil"),
36
  outputs="label",
37
+ title="CIFAR-10 Image Classification")
 
38
 
39
+ # Launch the app
40
  interface.launch()
41
+