Commit
·
dfee1e5
1
Parent(s):
cb4591a
Upload 2 files
Browse files- eda.py +98 -7
- prediction.py +6 -44
eda.py
CHANGED
@@ -7,9 +7,6 @@ import matplotlib.pyplot as plt
|
|
7 |
import plotly.express as px
|
8 |
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
9 |
from tensorflow.keras.preprocessing import image
|
10 |
-
from datasets import load_dataset
|
11 |
-
from torchvision import transforms
|
12 |
-
from torch.utils.data import DataLoader
|
13 |
|
14 |
st.set_page_config(page_title='Fast Food Classification Dataset Analysis', layout='wide', initial_sidebar_state='expanded')
|
15 |
|
@@ -30,11 +27,105 @@ def run():
|
|
30 |
|
31 |
|
32 |
st.markdown('---')
|
|
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
|
40 |
|
|
|
7 |
import plotly.express as px
|
8 |
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
9 |
from tensorflow.keras.preprocessing import image
|
|
|
|
|
|
|
10 |
|
11 |
st.set_page_config(page_title='Fast Food Classification Dataset Analysis', layout='wide', initial_sidebar_state='expanded')
|
12 |
|
|
|
27 |
|
28 |
|
29 |
st.markdown('---')
|
30 |
+
main_path= 'D:\\tugas_andrew_DS\\phase_2\\m2\\food'
|
31 |
|
32 |
+
# Define batch size and image size
|
33 |
+
batch_size = 256
|
34 |
+
img_size = (64, 64)
|
35 |
+
# Define paths to the data folders
|
36 |
+
train_path = os.path.join(main_path, 'Train')
|
37 |
+
valid_path = os.path.join(main_path, 'Valid')
|
38 |
+
test_path = os.path.join(main_path, 'Test')
|
39 |
+
# Create data generators for training, validation, and testing
|
40 |
+
train_datagen = ImageDataGenerator(
|
41 |
+
rescale=1./255,
|
42 |
+
horizontal_flip=True
|
43 |
+
)
|
44 |
+
|
45 |
+
valid_datagen = ImageDataGenerator(
|
46 |
+
rescale=1./255
|
47 |
+
)
|
48 |
+
test_datagen = ImageDataGenerator(
|
49 |
+
rescale=1./255
|
50 |
+
)
|
51 |
+
|
52 |
+
train_generator = train_datagen.flow_from_directory(
|
53 |
+
train_path,
|
54 |
+
target_size=img_size,
|
55 |
+
batch_size=batch_size,
|
56 |
+
class_mode='categorical'
|
57 |
+
)
|
58 |
+
|
59 |
+
valid_generator = valid_datagen.flow_from_directory(
|
60 |
+
valid_path,
|
61 |
+
target_size=img_size,
|
62 |
+
batch_size=batch_size,
|
63 |
+
class_mode='categorical'
|
64 |
+
)
|
65 |
+
|
66 |
+
test_generator = test_datagen.flow_from_directory(
|
67 |
+
test_path,
|
68 |
+
target_size=img_size,
|
69 |
+
batch_size=batch_size,
|
70 |
+
class_mode='categorical'
|
71 |
+
)
|
72 |
+
|
73 |
+
st.write('## Showing Random Samples')
|
74 |
+
class_names = list(train_generator.class_indices.keys())
|
75 |
+
train_classes = pd.Series(train_generator.classes)
|
76 |
+
test_classes = pd.Series(test_generator.classes)
|
77 |
+
valid_classes = pd.Series(valid_generator.classes)
|
78 |
+
# Plot some samples from each class
|
79 |
+
fig, ax = plt.subplots(nrows=2, ncols=5, figsize=(10, 6), subplot_kw={'xticks': [], 'yticks': []})
|
80 |
+
for i, axi in enumerate(ax.flat):
|
81 |
+
img = plt.imread(f'{train_path}/{class_names[i]}/{os.listdir(train_path+"/"+class_names[i])[0]}')
|
82 |
+
axi.imshow(img)
|
83 |
+
axi.set_title(class_names[i])
|
84 |
+
plt.tight_layout()
|
85 |
+
st.pyplot(fig)
|
86 |
+
|
87 |
+
|
88 |
+
st.markdown('---')
|
89 |
+
|
90 |
+
st.write('## Balance Classification')
|
91 |
+
|
92 |
+
# Create a pandas dataframe to show the distribution of classes in train, test, and validation data
|
93 |
+
df = pd.concat([train_classes.value_counts(), test_classes.value_counts(), valid_classes.value_counts()], axis=1)
|
94 |
+
df.columns = ['Training Data', 'Test Data', 'Validation Data']
|
95 |
+
df.index = class_names
|
96 |
+
|
97 |
+
fig, ax = plt.subplots(figsize=(12, 6))
|
98 |
+
df.plot(kind='bar', stacked=False, ax=ax, width=0.8)
|
99 |
+
plt.xlabel('Class')
|
100 |
+
plt.ylabel('Data Distribution')
|
101 |
+
plt.title('Data Distribution for each class')
|
102 |
+
plt.xticks(rotation=45, ha='right')
|
103 |
+
st.pyplot(fig)
|
104 |
+
|
105 |
+
|
106 |
+
st.markdown('---')
|
107 |
+
|
108 |
+
st.write('## Mean Pixel Value')
|
109 |
+
|
110 |
+
# Plot the mean of pixel mean of each channel for each class (unstacked bar chart)
|
111 |
+
means = []
|
112 |
+
for i in range(len(class_names)):
|
113 |
+
class_name = class_names[i]
|
114 |
+
img_path = os.path.join(train_path, class_name, os.listdir(os.path.join(train_path, class_name))[0])
|
115 |
+
img = image.load_img(img_path, target_size=img_size)
|
116 |
+
img_array = image.img_to_array(img)
|
117 |
+
means.append(np.mean(img_array, axis=(0, 1)))
|
118 |
+
means_df = pd.DataFrame(means, columns=['Red', 'Green', 'Blue'])
|
119 |
+
means_df.index = class_names
|
120 |
+
fig, ax = plt.subplots(figsize=(12, 6))
|
121 |
+
means_df.plot(kind='bar', stacked=False, ax=ax, width=0.8)
|
122 |
+
plt.xlabel('Class')
|
123 |
+
plt.ylabel('Mean pixel value')
|
124 |
+
plt.title('Mean pixel value of each channel for each class')
|
125 |
+
plt.xticks(rotation=45, ha='right')
|
126 |
+
st.pyplot(fig)
|
127 |
+
|
128 |
+
st.markdown('---')
|
129 |
|
130 |
|
131 |
|
prediction.py
CHANGED
@@ -5,9 +5,6 @@ from PIL import Image
|
|
5 |
import os
|
6 |
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
7 |
from tensorflow.keras.models import load_model
|
8 |
-
from datasets import load_dataset
|
9 |
-
from torchvision import transforms
|
10 |
-
from torch.utils.data import DataLoader
|
11 |
|
12 |
|
13 |
# Load the Models
|
@@ -23,50 +20,15 @@ def preprocess_input_image(img_path):
|
|
23 |
x /= 255.
|
24 |
return x, img1
|
25 |
|
26 |
-
|
27 |
|
28 |
-
|
29 |
-
import torch
|
30 |
-
import torchvision.transforms as transforms
|
31 |
-
from torch.utils.data import DataLoader
|
32 |
-
from datasets import load_dataset
|
33 |
-
|
34 |
-
# Define the path to the dataset
|
35 |
-
dataset_path = 'andrewsunanda/fast_food_image_classification'
|
36 |
-
|
37 |
-
# Load the dataset from Hugging Face
|
38 |
-
dataset = load_dataset(dataset_path)
|
39 |
-
|
40 |
-
# Define the batch size and image size
|
41 |
batch_size = 256
|
42 |
img_size = (64, 64)
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
test_path = os.path.join(dataset_path, 'Test')
|
48 |
-
|
49 |
-
# Define the transforms for the dataset
|
50 |
-
transform = transforms.Compose([
|
51 |
-
transforms.Resize(img_size),
|
52 |
-
transforms.ToTensor(),
|
53 |
-
])
|
54 |
-
|
55 |
-
# Load the training dataset
|
56 |
-
train_dataset = dataset['train']
|
57 |
-
train_dataset = train_dataset.map(lambda x: {'image': transform(x['image']), 'label': x['label']})
|
58 |
-
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
|
59 |
-
|
60 |
-
# Load the validation dataset
|
61 |
-
valid_dataset = dataset['validation']
|
62 |
-
valid_dataset = valid_dataset.map(lambda x: {'image': transform(x['image']), 'label': x['label']})
|
63 |
-
valid_loader = DataLoader(valid_dataset, batch_size=batch_size, shuffle=False)
|
64 |
-
|
65 |
-
# Load the testing dataset
|
66 |
-
test_dataset = dataset['test']
|
67 |
-
test_dataset = test_dataset.map(lambda x: {'image': transform(x['image']), 'label': x['label']})
|
68 |
-
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
|
69 |
-
|
70 |
|
71 |
# Create data generators for training, validation, and testing
|
72 |
train_datagen = ImageDataGenerator(
|
|
|
5 |
import os
|
6 |
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
7 |
from tensorflow.keras.models import load_model
|
|
|
|
|
|
|
8 |
|
9 |
|
10 |
# Load the Models
|
|
|
20 |
x /= 255.
|
21 |
return x, img1
|
22 |
|
23 |
+
main_path= 'D:\\tugas_andrew_DS\\phase_2\\m2\\food'
|
24 |
|
25 |
+
# Define batch size and image size
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
batch_size = 256
|
27 |
img_size = (64, 64)
|
28 |
+
# Define paths to the data folders
|
29 |
+
train_path = os.path.join(main_path, 'Train')
|
30 |
+
valid_path = os.path.join(main_path, 'Valid')
|
31 |
+
test_path = os.path.join(main_path, 'Test')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
# Create data generators for training, validation, and testing
|
34 |
train_datagen = ImageDataGenerator(
|