Commit
·
33eeb70
1
Parent(s):
1e6d6e4
Upload 2 files
Browse files- app.py +102 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# streamlit_app.py
|
2 |
+
import streamlit as st
|
3 |
+
from fastai.vision.all import *
|
4 |
+
import shutil
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Function to get the label from the file name
|
8 |
+
def get_label(file_name):
|
9 |
+
return file_name.split('-')[0]
|
10 |
+
|
11 |
+
# Function to prepare data (similar to your code)
|
12 |
+
def prepare_data(food_path, label_a, label_b):
|
13 |
+
for img in get_image_files(food_path):
|
14 |
+
if label_a in str(img):
|
15 |
+
img.rename(f"{img.parent}/{label_a}-{img.name}")
|
16 |
+
elif label_b in str(img):
|
17 |
+
img.rename(f"{img.parent}/{label_b}-{img.name}")
|
18 |
+
else:
|
19 |
+
os.remove(img)
|
20 |
+
|
21 |
+
# Function to train the model
|
22 |
+
def train_model(food_path, label_func):
|
23 |
+
dls = ImageDataLoaders.from_name_func(
|
24 |
+
food_path, get_image_files(food_path), valid_pct=0.2, seed=420,
|
25 |
+
label_func=label_func, item_tfms=Resize(230)
|
26 |
+
)
|
27 |
+
|
28 |
+
learn = cnn_learner(dls, resnet34, metrics=error_rate, pretrained=True)
|
29 |
+
learn.fine_tune(epochs=1)
|
30 |
+
|
31 |
+
return learn
|
32 |
+
|
33 |
+
# ... (previous code)
|
34 |
+
|
35 |
+
# ... (previous code)
|
36 |
+
|
37 |
+
# Streamlit app
|
38 |
+
def main():
|
39 |
+
st.title("Food Classifier Streamlit App")
|
40 |
+
|
41 |
+
# Sidebar options
|
42 |
+
options = ["Train Model", "Upload Image", "Test Random Images", "Confusion Matrix"]
|
43 |
+
choice = st.sidebar.selectbox("Choose an option", options)
|
44 |
+
|
45 |
+
if choice == "Train Model":
|
46 |
+
st.subheader("Training the Model")
|
47 |
+
food_path = Path("~/.fastai/data/food-101/food-101").expanduser()
|
48 |
+
if not food_path.exists():
|
49 |
+
try:
|
50 |
+
food_path = untar_data(URLs.FOOD)
|
51 |
+
except FileExistsError:
|
52 |
+
st.warning("Data directory already exists. Skipping download.")
|
53 |
+
label_a = st.text_input("Enter label A:", "samosa")
|
54 |
+
label_b = st.text_input("Enter label B:", "hot_and_sour_soup")
|
55 |
+
|
56 |
+
prepare_data(food_path, label_a, label_b)
|
57 |
+
learn = train_model(food_path, get_label)
|
58 |
+
|
59 |
+
st.session_state.model = learn # Save the model to session state
|
60 |
+
st.success("Model trained successfully!")
|
61 |
+
|
62 |
+
# ... (rest of the code remains unchanged)
|
63 |
+
|
64 |
+
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
|
69 |
+
|
70 |
+
if uploaded_files:
|
71 |
+
for img in uploaded_files:
|
72 |
+
img = PILImage.create(img)
|
73 |
+
label, _, probs = st.session_state.model.predict(img)
|
74 |
+
|
75 |
+
st.image(img, caption=f"This is a {label}.")
|
76 |
+
st.write(f"{label_a}: {probs[1].item():.6f}")
|
77 |
+
st.write(f"{label_b}: {probs[0].item():.6f}")
|
78 |
+
|
79 |
+
elif choice == "Test Random Images":
|
80 |
+
st.subheader("Test Using Images in Dataset")
|
81 |
+
if "model" not in st.session_state:
|
82 |
+
st.warning("Please train the model first.")
|
83 |
+
else:
|
84 |
+
for i in range(0, 5): # Change 5 to the number of images you want to display
|
85 |
+
random_index = random.randint(0, len(get_image_files(food_path)) - 1)
|
86 |
+
img_path = get_image_files(food_path)[random_index]
|
87 |
+
img = mpimg.imread(img_path)
|
88 |
+
label, _, probs = st.session_state.model.predict(img)
|
89 |
+
|
90 |
+
st.image(img, caption=f"Predicted label: {label}")
|
91 |
+
|
92 |
+
elif choice == "Confusion Matrix":
|
93 |
+
st.subheader("Confusion Matrix")
|
94 |
+
if "model" not in st.session_state:
|
95 |
+
st.warning("Please train the model first.")
|
96 |
+
else:
|
97 |
+
interp = ClassificationInterpretation.from_learner(st.session_state.model)
|
98 |
+
st.pyplot(interp.plot_confusion_matrix())
|
99 |
+
|
100 |
+
# Run the Streamlit app
|
101 |
+
if __name__ == "__main__":
|
102 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
torch
|
3 |
+
fastai
|
4 |
+
Pillow
|
5 |
+
matplotlib
|