FiratIsmailoglu commited on
Commit
3acd84d
1 Parent(s): 7e733fd

Upload 7 files

Browse files
20_percent_data_effnet1.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:df47133107a62e2ebf218c2067cd825f02a071e5b22cd08b8f226ab63e9d8e3d
3
+ size 26508218
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Thu Feb 8 13:00:08 2024
4
+
5
+ @author: firis
6
+ """
7
+
8
+ import gradio as gr
9
+ import os
10
+ import torch
11
+
12
+ from model import create_eff_model
13
+ from timeit import default_timer as timer
14
+
15
+ class_names=["pizza","steak","sushi"]
16
+
17
+
18
+ eff_model,eff_model_transform=create_eff_model() #bu standart model
19
+ eff_model_dict=torch.load("20_percent_data_effnet1.pth")
20
+ eff_model.load_state_dict(eff_model_dict)
21
+ eff_model.to("cpu")
22
+
23
+ #prediction function
24
+
25
+ def predict(img):
26
+
27
+ start_time = timer()
28
+ img=eff_model_transform(img).unsqueeze(0)
29
+
30
+ eff_model.eval()
31
+ with torch.inference_mode():
32
+ pred_and_probs=torch.softmax(eff_model(img),dim=1)
33
+
34
+ class_with_pred_dict={cl:float(pred_and_probs[0][ind]) for ind,cl in enumerate(class_names)}
35
+
36
+ pred_time = round(timer() - start_time, 5)
37
+
38
+ return class_with_pred_dict, pred_time
39
+
40
+
41
+ ############# Gradio Interface ##########
42
+ title = "FoodVision Mini 🍕🥩🍣"
43
+ description = "An EfficientNetB2 feature extractor computer vision model to classify images of food as pizza, steak or sushi."
44
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
45
+
46
+ # Create the Gradio demo
47
+ demo = gr.Interface(fn=predict, # mapping function from input to output
48
+ inputs=gr.Image(type="pil"), # what are the inputs?
49
+ outputs=[gr.Label(num_top_classes=3, label="Predictions"), # what are the outputs?
50
+ gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
51
+ examples=example_list,
52
+ title=title,
53
+ description=description)
54
+
55
+ # Launch the demo!
56
+ demo.launch(debug=False, # print errors locally?
57
+ share=True)
58
+
59
+
60
+
61
+
62
+
63
+
64
+
examples/138961.jpg ADDED
examples/3541033.jpg ADDED
examples/3886015.jpg ADDED
model.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Thu Feb 8 13:48:19 2024
4
+
5
+ @author: firis
6
+ """
7
+
8
+ import torch
9
+ import torchvision
10
+ from torch import nn
11
+
12
+ def create_eff_model(num_classes:int=3,seed:int=42):
13
+
14
+ weights=torchvision.models.EfficientNet_B1_Weights.DEFAULT
15
+ transforms=weights.transforms()
16
+ model =torchvision.models.efficientnet_b1(weights=weights)
17
+
18
+ # Freeze all layers in base model
19
+ for param in model.parameters():
20
+ param.requires_grad = False
21
+
22
+ torch.manual_seed(seed)
23
+
24
+ model.classifier = nn.Sequential(
25
+ nn.Dropout(p=0.3, inplace=True),
26
+ nn.Linear(in_features=1280, out_features=num_classes))
27
+
28
+ return model, transforms
29
+
30
+
31
+
32
+
requirements.txt.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ There are three requirements only:
2
+ 1. torch==1.12.0
3
+ 2. torchvision==0.13.0
4
+ 3. gradio==3.1.4