Spaces:
Sleeping
Sleeping
sehyun
commited on
Commit
·
afb90db
1
Parent(s):
88cd1b6
First Commit
Browse files- .gitattributes +1 -0
- .idea/.gitignore +8 -0
- .idea/Coffee_Bean_Classifier.iml +8 -0
- .idea/inspectionProfiles/profiles_settings.xml +6 -0
- .idea/misc.xml +7 -0
- .idea/modules.xml +8 -0
- .idea/vcs.xml +6 -0
- app.py +53 -0
- effnetb1.pth +3 -0
- examples/dark.png +0 -0
- examples/green.png +0 -0
- examples/light.png +0 -0
- examples/medium.png +0 -0
- model.py +23 -0
- requirements.txt +5 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
effnetb1.pth filter=lfs diff=lfs merge=lfs -text
|
.idea/.gitignore
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Default ignored files
|
2 |
+
/shelf/
|
3 |
+
/workspace.xml
|
4 |
+
# Editor-based HTTP Client requests
|
5 |
+
/httpRequests/
|
6 |
+
# Datasource local storage ignored files
|
7 |
+
/dataSources/
|
8 |
+
/dataSources.local.xml
|
.idea/Coffee_Bean_Classifier.iml
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<module type="PYTHON_MODULE" version="4">
|
3 |
+
<component name="NewModuleRootManager">
|
4 |
+
<content url="file://$MODULE_DIR$" />
|
5 |
+
<orderEntry type="inheritedJdk" />
|
6 |
+
<orderEntry type="sourceFolder" forTests="false" />
|
7 |
+
</component>
|
8 |
+
</module>
|
.idea/inspectionProfiles/profiles_settings.xml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<component name="InspectionProjectProfileManager">
|
2 |
+
<settings>
|
3 |
+
<option name="USE_PROJECT_PROFILE" value="false" />
|
4 |
+
<version value="1.0" />
|
5 |
+
</settings>
|
6 |
+
</component>
|
.idea/misc.xml
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="Black">
|
4 |
+
<option name="sdkName" value="Python 3.11" />
|
5 |
+
</component>
|
6 |
+
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11" project-jdk-type="Python SDK" />
|
7 |
+
</project>
|
.idea/modules.xml
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="ProjectModuleManager">
|
4 |
+
<modules>
|
5 |
+
<module fileurl="file://$PROJECT_DIR$/.idea/Coffee_Bean_Classifier.iml" filepath="$PROJECT_DIR$/.idea/Coffee_Bean_Classifier.iml" />
|
6 |
+
</modules>
|
7 |
+
</component>
|
8 |
+
</project>
|
.idea/vcs.xml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="VcsDirectoryMappings">
|
4 |
+
<mapping directory="" vcs="Git" />
|
5 |
+
</component>
|
6 |
+
</project>
|
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os, torch, torchvision, torchvision
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
from model import build_effnetb1
|
5 |
+
from typing import Dict
|
6 |
+
from pathlib import Path
|
7 |
+
|
8 |
+
# Define Class names
|
9 |
+
class_names = ["Dark", "Medium", "Light", "Green"]
|
10 |
+
|
11 |
+
# Load path for example photo list
|
12 |
+
exp_list = list(Path("examples/").glob("*.png"))
|
13 |
+
|
14 |
+
# Build and load model params
|
15 |
+
model, transforms = build_effnetb1()
|
16 |
+
model_path = "effnetb1.pth"
|
17 |
+
model.load_state_dict(torch.load(model_path,
|
18 |
+
map_location=torch.device("cpu")))
|
19 |
+
|
20 |
+
|
21 |
+
# Predict based on image given | move everything to device("cpu") / Spaces run on CPU
|
22 |
+
def predict(img) -> Dict:
|
23 |
+
# move model to cpu
|
24 |
+
model.to("cpu")
|
25 |
+
model.eval()
|
26 |
+
with torch.inference_mode():
|
27 |
+
transformed_image = transforms(img).unsqueeze(dim=0)
|
28 |
+
# move input to cpu
|
29 |
+
target_image_pred = model(transformed_image.to("cpu"))
|
30 |
+
|
31 |
+
target_image_pred_probs = torch.softmax(target_image_pred, dim=1)
|
32 |
+
print(target_image_pred_probs)
|
33 |
+
pred_labels_and_probs = {class_names[i]: float(target_image_pred_probs[0][i]) for i in range(len(class_names))}
|
34 |
+
|
35 |
+
return pred_labels_and_probs
|
36 |
+
|
37 |
+
|
38 |
+
# Gradio App
|
39 |
+
title = "Coffee Bean Multi-classifier based on level of roasting ☕️"
|
40 |
+
description = """Created from multi-classifier model using transfer learning from [EfficientNetB1](https://pytorch.org/vision/main/models/generated/torchvision.models.efficientnet_b1.html).
|
41 |
+
Model was trained on 10 epochs on default weights, and demonstrated a testing accuarcy of 98%.\n Further information and the source code is provided at my [Github Repo](https://github.com/sehyunlee217/coffee_bean_multi_classification).
|
42 |
+
\n\n There are four roasting levels: Green and lightly roasted coffee beans are Laos Typica Bolaven. Doi Chaang are the medium roasted, and Brazil Cerrado are dark roasted. All coffee beans are Arabica beans.\n"""
|
43 |
+
article = "Dataset from: Ontoum, S., Khemanantakul, T., Sroison, P., Triyason, T., & Watanapa, B. (2022). Coffee Roast Intelligence. arXiv preprint arXiv:2206.01841."
|
44 |
+
|
45 |
+
demo = gr.Interface(fn=predict,
|
46 |
+
inputs=gr.Image(type="pil"),
|
47 |
+
outputs=[gr.Label(num_top_classes=4, label="Predictions")],
|
48 |
+
examples=exp_list,
|
49 |
+
title=title,
|
50 |
+
description=description,
|
51 |
+
article=article)
|
52 |
+
|
53 |
+
demo.launch()
|
effnetb1.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:95bb93daa3af27fcbcfa78fa9ec0759eaa064bdeeeeca423cce8bd0b7b206daa
|
3 |
+
size 26504890
|
examples/dark.png
ADDED
![]() |
examples/green.png
ADDED
![]() |
examples/light.png
ADDED
![]() |
examples/medium.png
ADDED
![]() |
model.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch, torchvision
|
2 |
+
from torch import nn
|
3 |
+
|
4 |
+
def build_effnetb1():
|
5 |
+
# weight & model initialization
|
6 |
+
effnetb1_weights = torchvision.models.EfficientNet_B1_Weights.DEFAULT
|
7 |
+
effnetb1 = torchvision.models.efficientnet_b1(weights=effnetb1_weights)
|
8 |
+
effnetb1_transforms = effnetb1_weights.transforms()
|
9 |
+
effnetb1.name = "effnetb1"
|
10 |
+
|
11 |
+
# Freeze params
|
12 |
+
for params in effnetb1.parameters():
|
13 |
+
params.requires_grad = False
|
14 |
+
|
15 |
+
# Edit Classifiers
|
16 |
+
effnetb1.classifier = torch.nn.Sequential(
|
17 |
+
nn.Dropout(p=0, inplace=True),
|
18 |
+
nn.Linear(in_features=1280,
|
19 |
+
out_features=4,
|
20 |
+
bias=True).to("cpu")
|
21 |
+
)
|
22 |
+
|
23 |
+
return effnetb1, effnetb1_transforms
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch==2.3.1
|
2 |
+
torchvision==0.18.1
|
3 |
+
gradio==4.37.2
|
4 |
+
httpx==0.24.1
|
5 |
+
numpy==1.26.4
|