Spaces:
Runtime error
Runtime error
first update
Browse files- .gitignore +138 -0
- app.py +124 -0
- class_names.txt +39 -0
- config.yaml +19 -0
- model/epoch=08.ckpt +3 -0
- requirements.txt +3 -0
.gitignore
ADDED
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
|
6 |
+
# C extensions
|
7 |
+
*.so
|
8 |
+
|
9 |
+
# Distribution / packaging
|
10 |
+
.Python
|
11 |
+
build/
|
12 |
+
develop-eggs/
|
13 |
+
dist/
|
14 |
+
downloads/
|
15 |
+
eggs/
|
16 |
+
.eggs/
|
17 |
+
lib/
|
18 |
+
lib64/
|
19 |
+
parts/
|
20 |
+
sdist/
|
21 |
+
var/
|
22 |
+
wheels/
|
23 |
+
pip-wheel-metadata/
|
24 |
+
share/python-wheels/
|
25 |
+
*.egg-info/
|
26 |
+
.installed.cfg
|
27 |
+
*.egg
|
28 |
+
MANIFEST
|
29 |
+
|
30 |
+
# PyInstaller
|
31 |
+
# Usually these files are written by a python script from a template
|
32 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
33 |
+
*.manifest
|
34 |
+
*.spec
|
35 |
+
|
36 |
+
# Installer logs
|
37 |
+
pip-log.txt
|
38 |
+
pip-delete-this-directory.txt
|
39 |
+
|
40 |
+
# Unit test / coverage reports
|
41 |
+
htmlcov/
|
42 |
+
.tox/
|
43 |
+
.nox/
|
44 |
+
.coverage
|
45 |
+
.coverage.*
|
46 |
+
.cache
|
47 |
+
nosetests.xml
|
48 |
+
coverage.xml
|
49 |
+
*.cover
|
50 |
+
*.py,cover
|
51 |
+
.hypothesis/
|
52 |
+
.pytest_cache/
|
53 |
+
|
54 |
+
# Translations
|
55 |
+
*.mo
|
56 |
+
*.pot
|
57 |
+
|
58 |
+
# Django stuff:
|
59 |
+
*.log
|
60 |
+
local_settings.py
|
61 |
+
db.sqlite3
|
62 |
+
db.sqlite3-journal
|
63 |
+
|
64 |
+
# Flask stuff:
|
65 |
+
instance/
|
66 |
+
.webassets-cache
|
67 |
+
|
68 |
+
# Scrapy stuff:
|
69 |
+
.scrapy
|
70 |
+
|
71 |
+
# Sphinx documentation
|
72 |
+
docs/_build/
|
73 |
+
|
74 |
+
# PyBuilder
|
75 |
+
target/
|
76 |
+
|
77 |
+
# Jupyter Notebook
|
78 |
+
.ipynb_checkpoints
|
79 |
+
|
80 |
+
# IPython
|
81 |
+
profile_default/
|
82 |
+
ipython_config.py
|
83 |
+
|
84 |
+
# pyenv
|
85 |
+
.python-version
|
86 |
+
|
87 |
+
# pipenv
|
88 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
89 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
90 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
91 |
+
# install all needed dependencies.
|
92 |
+
#Pipfile.lock
|
93 |
+
|
94 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
95 |
+
__pypackages__/
|
96 |
+
|
97 |
+
# Celery stuff
|
98 |
+
celerybeat-schedule
|
99 |
+
celerybeat.pid
|
100 |
+
|
101 |
+
# SageMath parsed files
|
102 |
+
*.sage.py
|
103 |
+
|
104 |
+
# Environments
|
105 |
+
.env
|
106 |
+
.venv
|
107 |
+
env/
|
108 |
+
venv/
|
109 |
+
ENV/
|
110 |
+
env.bak/
|
111 |
+
venv.bak/
|
112 |
+
|
113 |
+
# Spyder project settings
|
114 |
+
.spyderproject
|
115 |
+
.spyproject
|
116 |
+
|
117 |
+
# Rope project settings
|
118 |
+
.ropeproject
|
119 |
+
|
120 |
+
# mkdocs documentation
|
121 |
+
/site
|
122 |
+
|
123 |
+
# mypy
|
124 |
+
.mypy_cache/
|
125 |
+
.dmypy.json
|
126 |
+
dmypy.json
|
127 |
+
|
128 |
+
# Pyre type checker
|
129 |
+
.pyre/
|
130 |
+
.neptune
|
131 |
+
|
132 |
+
#dataset
|
133 |
+
data
|
134 |
+
crop_data
|
135 |
+
examples
|
136 |
+
#model
|
137 |
+
lightning_logs
|
138 |
+
ckpts
|
app.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Any
|
2 |
+
import pytorch_lightning as pl
|
3 |
+
from torchvision.models import efficientnet_v2_s, EfficientNet_V2_S_Weights
|
4 |
+
import torch
|
5 |
+
from torch import nn
|
6 |
+
from torchvision import transforms
|
7 |
+
from torch.nn import functional as F
|
8 |
+
import yaml
|
9 |
+
from yaml.loader import SafeLoader
|
10 |
+
from PIL import Image
|
11 |
+
import gradio as gr
|
12 |
+
import os
|
13 |
+
|
14 |
+
class WeedModel(pl.LightningModule):
|
15 |
+
def __init__(self, params):
|
16 |
+
super().__init__()
|
17 |
+
self.params = params
|
18 |
+
|
19 |
+
model = self.params["model"]
|
20 |
+
|
21 |
+
if(model.lower() == "efficientnet"):
|
22 |
+
if(self.params["pretrained"]): self.base_model = efficientnet_v2_s(weights=EfficientNet_V2_S_Weights.IMAGENET1K_V1)
|
23 |
+
else: self.base_model = efficientnet_v2_s(weights=None)
|
24 |
+
num_ftrs = self.base_model.classifier[-1].in_features
|
25 |
+
self.base_model.classifier[-1] = nn.Linear(num_ftrs, self.params["n_class"])
|
26 |
+
|
27 |
+
else:
|
28 |
+
print("not prepared model yet!!")
|
29 |
+
|
30 |
+
def forward(self, x):
|
31 |
+
embedding = self.base_model(x)
|
32 |
+
return embedding
|
33 |
+
|
34 |
+
def configure_optimizers(self):
|
35 |
+
if(self.params["optimizer"] == "Adam"):
|
36 |
+
optimizer = torch.optim.Adam(self.parameters(), lr=self.params["Lr"])
|
37 |
+
elif(self.params["optimizer"] == "SGD"):
|
38 |
+
optimizer = torch.optim.SGD(self.parameters(), lr=self.params["Lr"])
|
39 |
+
return optimizer
|
40 |
+
|
41 |
+
def training_step(self, train_batch, batch_idx):
|
42 |
+
x = train_batch["image"]
|
43 |
+
y = train_batch["label"]
|
44 |
+
|
45 |
+
y_hat = self(x)
|
46 |
+
loss = F.cross_entropy(y_hat, y)
|
47 |
+
self.log('metrics/batch/train_loss', loss, prog_bar=False)
|
48 |
+
|
49 |
+
preds = F.softmax(y_hat, dim=-1)
|
50 |
+
|
51 |
+
return loss
|
52 |
+
|
53 |
+
def validation_step(self, val_batch, batch_idx):
|
54 |
+
|
55 |
+
x = val_batch["image"]
|
56 |
+
y = val_batch["label"]
|
57 |
+
|
58 |
+
y_hat = self(x)
|
59 |
+
loss = F.cross_entropy(y_hat, y)
|
60 |
+
self.log('metrics/batch/val_loss', loss)
|
61 |
+
|
62 |
+
def predict_step(self, batch: Any, batch_idx: int=0, dataloader_idx: int = 0) -> Any:
|
63 |
+
y_hat = self(batch)
|
64 |
+
preds = torch.softmax(y_hat, dim=-1).tolist()
|
65 |
+
|
66 |
+
# preds = torch.argmax(preds, dim=-1)
|
67 |
+
return preds
|
68 |
+
|
69 |
+
|
70 |
+
def predict(image):
|
71 |
+
|
72 |
+
tensor_image = transform(image)
|
73 |
+
outs = model.predict_step(tensor_image.unsqueeze(0))
|
74 |
+
labels = {class_names[k]: float(v) for k, v in enumerate(outs[0][:-1])}
|
75 |
+
|
76 |
+
return labels
|
77 |
+
|
78 |
+
|
79 |
+
title = " AISeed AI Application Demo "
|
80 |
+
description = "# A Demo of Deep Learning for Weed Classification"
|
81 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
82 |
+
|
83 |
+
with open("class_names.txt", "r", encoding='utf-8') as f:
|
84 |
+
class_names = f.read().splitlines()
|
85 |
+
|
86 |
+
with gr.Blocks() as demo:
|
87 |
+
demo.title = title
|
88 |
+
gr.Markdown(description)
|
89 |
+
with gr.Tabs():
|
90 |
+
with gr.TabItem("for Images"):
|
91 |
+
with gr.Row():
|
92 |
+
with gr.Column():
|
93 |
+
im = gr.Image(type="pil", label="input image")
|
94 |
+
with gr.Column():
|
95 |
+
label_conv = gr.Label(label="Predictions", num_top_classes=4)
|
96 |
+
btn = gr.Button(value="predict")
|
97 |
+
btn.click(predict, inputs=im, outputs=[label_conv])
|
98 |
+
gr.Examples(examples=example_list, inputs=[im], outputs=[label_conv])
|
99 |
+
with gr.TabItem("for Webcam"):
|
100 |
+
with gr.Row():
|
101 |
+
with gr.Column():
|
102 |
+
webcam = gr.Image(type="pil", label="input image", source="webcam")
|
103 |
+
# capture = gr.Image(type="pil", label="output image")
|
104 |
+
with gr.Column():
|
105 |
+
label = gr.Label(label="Predictions", num_top_classes=4)
|
106 |
+
|
107 |
+
webcam.change(predict, inputs=webcam, outputs=[label])
|
108 |
+
|
109 |
+
|
110 |
+
if __name__ == '__main__':
|
111 |
+
with open('config.yaml') as f:
|
112 |
+
PARAMS = yaml.load(f, Loader=SafeLoader)
|
113 |
+
print(PARAMS)
|
114 |
+
model = WeedModel.load_from_checkpoint("model\epoch=08.ckpt", params=PARAMS).cpu()
|
115 |
+
model.eval()
|
116 |
+
|
117 |
+
transform = transforms.Compose([
|
118 |
+
transforms.Resize(256),
|
119 |
+
transforms.CenterCrop(224),
|
120 |
+
transforms.ToTensor(),
|
121 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
122 |
+
])
|
123 |
+
|
124 |
+
demo.launch()
|
class_names.txt
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
๋ชฉํ_๊ฐ๋ง์ด
|
2 |
+
๋ชฉํ_๊ฐ์ฅ๊ฐ
|
3 |
+
๋ชฉํ_๋จํ์๋ผ์งํ
|
4 |
+
๋ชฉํ_๋ผ์งํ
|
5 |
+
๋ชฉํ_๋ง์ด
|
6 |
+
๋ชฉํ_๋ฏธ๊ตญ๊ฐ๋ง์ฌ๋ฆฌ
|
7 |
+
๋ชฉํ_๋ฐฉ๊ฐ์ง๋ฅ
|
8 |
+
๋ชฉํ_๋ณ๊ฝ์์ฌ๋น
|
9 |
+
๋ชฉํ_๋ถ์์๋๋ฌผ
|
10 |
+
๋ชฉํ_์ธ์ฐ๋๊นจ๋น๋ฐ๋
|
11 |
+
๋ชฉํ_์ฃผํ์๋๋ฌผ
|
12 |
+
๋ชฉํ_ํฐ๋๊ผฌ๋ง๋ฆฌ
|
13 |
+
๋ชฉํ_ํฐ๋ง์ด
|
14 |
+
๋ชฉํ_ํฐ๋ฐฉ๊ฐ์ง๋ฅ
|
15 |
+
๋ชฉํ_ํธ๋ณ๊ฝ์์ฌ๋น
|
16 |
+
๋ชฉํ_๋ฅ๊ทผ์๋ํ๊ฝ
|
17 |
+
๋ชฉํ_๋ฅ๊ทผ์๋ฏธ๊ตญ๋ํ๊ฝ
|
18 |
+
๋ชฉํ_๋ฅ๊ทผ์์ ํ์ด
|
19 |
+
๋ชฉํ_๋ฏธ๊ตญ๋ํ๊ฝ
|
20 |
+
๋ชฉํ_๋ณ๋ํ๊ฝ
|
21 |
+
๋ชฉํ_์ ๊ธฐ๋ํ๊ฝ
|
22 |
+
๋ชฉํ_๋์๋ช
์์ฃผ
|
23 |
+
๋ชฉํ_์๋ช
์์ฃผ
|
24 |
+
๋ชฉํ_์ข๋ช
์์ฃผ
|
25 |
+
๋ชฉํ_์ทจ๋ช
์์ฃผ
|
26 |
+
๋ชฉํ_ํฐ๋ช
์์ฃผ
|
27 |
+
๋ชฉํ_๊ฐ๋ํธ๋น๋ฆ
|
28 |
+
๋ชฉํ_๊ฐ์๋น๋ฆ
|
29 |
+
๋ชฉํ_๊ฐ๋น๋ฆ
|
30 |
+
๋ชฉํ_๊ธดํธ๋น๋ฆ
|
31 |
+
๋ชฉํ_๋ฏผํธ๋น๋ฆ
|
32 |
+
๋ชฉํ_์ฒญ๋น๋ฆ
|
33 |
+
๋ชฉํ_ํธ๋น๋ฆ
|
34 |
+
๋ชฉํ_๊ฐ๋๋ฏธ๊ตญ์ธํ
|
35 |
+
๋ชฉํ_๋๊ฐ๋ถ์ํ
|
36 |
+
๋ชฉํ_๋ฏธ๊ตญ์ธํ
|
37 |
+
๋ชฉํ_์ ๊ฐ๋ถ์ํ
|
38 |
+
๋ชฉํ_์ข๊ฐ๋ถ์ํ
|
39 |
+
๋ชฉํ_ํฐ๊ฐ๋ถ์ํ
|
config.yaml
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
#Dataset
|
3 |
+
"train_path": "./train.txt",
|
4 |
+
"test_path": "./test.txt",
|
5 |
+
"val_path": "./val.txt",
|
6 |
+
"n_data": 1,
|
7 |
+
|
8 |
+
|
9 |
+
#Model
|
10 |
+
"model": "EfficientNet", # [Alexnet, VGG, GoogleNet, ResNet, DenseNet, MobileNet, SqueezeNet, ShuffleNet, EfficientNet, SE-ResNet (not available)]
|
11 |
+
"pretrained": True,
|
12 |
+
"n_class": 40,
|
13 |
+
|
14 |
+
#Training
|
15 |
+
"B_sz": 4,
|
16 |
+
"Lr": 0.001,
|
17 |
+
"Epoch": 5,
|
18 |
+
"optimizer": "Adam" #[Adam, SGD]
|
19 |
+
}
|
model/epoch=08.ckpt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:0c21a880dfa8c41e3c1533f5e6ab5a12a2bc562d99db7c3504b042384a54de1d
|
3 |
+
size 244046718
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
pytorch-lightning
|
2 |
+
torch==1.8.1
|
3 |
+
torchvision==0.9.1
|