noshot 12.0.0__tar.gz → 13.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {noshot-12.0.0 → noshot-13.0.0}/PKG-INFO +1 -1
- {noshot-12.0.0 → noshot-13.0.0}/noshot.egg-info/PKG-INFO +1 -1
- noshot-13.0.0/noshot.egg-info/SOURCES.txt +35 -0
- {noshot-12.0.0 → noshot-13.0.0}/setup.py +1 -1
- noshot-13.0.0/src/noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/1.1 DNN (Pytorch).ipynb +164 -0
- noshot-13.0.0/src/noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/1.2 DNN (Tensorflow).ipynb +94 -0
- noshot-13.0.0/src/noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/1.3 DNN (Image Classification).ipynb +134 -0
- noshot-13.0.0/src/noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/2.1 DNN vs CNN.ipynb +127 -0
- noshot-13.0.0/src/noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/2.2 DNN vs CNN.ipynb +123 -0
- noshot-13.0.0/src/noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/4. FCNN (Image Segmentation).ipynb +108 -0
- noshot-13.0.0/src/noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/Lab Excercise (Training DNN).ipynb +646 -0
- noshot-13.0.0/src/noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/Load-Images.ipynb +553 -0
- noshot-13.0.0/src/noshot/data/DLE FSD BDA/DLE/DLE 3 (sonic boy)/Ex1.ipynb +216 -0
- noshot-13.0.0/src/noshot/data/DLE FSD BDA/DLE/DLE 3 (sonic boy)/Ex2.ipynb +195 -0
- noshot-13.0.0/src/noshot/data/DLE FSD BDA/DLE/DLE 3 (sonic boy)/Ex3.ipynb +427 -0
- noshot-13.0.0/src/noshot/data/DLE FSD BDA/DLE/DLE 3 (sonic boy)/Ex4.ipynb +186 -0
- noshot-13.0.0/src/noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp01/DNN Ex No 1.ipynb +398 -0
- noshot-13.0.0/src/noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp01/Ex No 1 Build in dataset.ipynb +171 -0
- noshot-13.0.0/src/noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp01/Exp1-Short-DL_ANN_ImageClassification.ipynb +401 -0
- noshot-13.0.0/src/noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp01/OR GATE .ipynb +8511 -0
- noshot-13.0.0/src/noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp02/Exp2-Short-DL_CNN_ImageClassification.ipynb +737 -0
- noshot-13.0.0/src/noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp03/DL-Ex3-RNN.ipynb +591 -0
- noshot-13.0.0/src/noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp04/Ex no 4.ipynb +551 -0
- noshot-12.0.0/noshot.egg-info/SOURCES.txt +0 -16
- {noshot-12.0.0 → noshot-13.0.0}/LICENSE.txt +0 -0
- {noshot-12.0.0 → noshot-13.0.0}/README.md +0 -0
- {noshot-12.0.0 → noshot-13.0.0}/noshot.egg-info/dependency_links.txt +0 -0
- {noshot-12.0.0 → noshot-13.0.0}/noshot.egg-info/not-zip-safe +0 -0
- {noshot-12.0.0 → noshot-13.0.0}/noshot.egg-info/top_level.txt +0 -0
- {noshot-12.0.0 → noshot-13.0.0}/setup.cfg +0 -0
- {noshot-12.0.0 → noshot-13.0.0}/src/noshot/__init__.py +0 -0
- {noshot-12.0.0/src/noshot/data/DLE FSD BDA/DLE → noshot-13.0.0/src/noshot/data/DLE FSD BDA/DLE/DLE 1 (Json)}/1. DNN (Image Classification).ipynb +0 -0
- {noshot-12.0.0/src/noshot/data/DLE FSD BDA/DLE → noshot-13.0.0/src/noshot/data/DLE FSD BDA/DLE/DLE 1 (Json)}/2. DNN vs CNN.ipynb +0 -0
- {noshot-12.0.0/src/noshot/data/DLE FSD BDA/DLE → noshot-13.0.0/src/noshot/data/DLE FSD BDA/DLE/DLE 1 (Json)}/3. CNN (Object Detecrion).ipynb +0 -0
- {noshot-12.0.0/src/noshot/data/DLE FSD BDA/DLE → noshot-13.0.0/src/noshot/data/DLE FSD BDA/DLE/DLE 1 (Json)}/4. FCN (Image Segmentaion).ipynb +0 -0
- {noshot-12.0.0 → noshot-13.0.0}/src/noshot/main.py +0 -0
- {noshot-12.0.0 → noshot-13.0.0}/src/noshot/utils/__init__.py +0 -0
- {noshot-12.0.0 → noshot-13.0.0}/src/noshot/utils/shell_utils.py +0 -0
@@ -0,0 +1,35 @@
|
|
1
|
+
LICENSE.txt
|
2
|
+
README.md
|
3
|
+
setup.py
|
4
|
+
noshot.egg-info/PKG-INFO
|
5
|
+
noshot.egg-info/SOURCES.txt
|
6
|
+
noshot.egg-info/dependency_links.txt
|
7
|
+
noshot.egg-info/not-zip-safe
|
8
|
+
noshot.egg-info/top_level.txt
|
9
|
+
src/noshot/__init__.py
|
10
|
+
src/noshot/main.py
|
11
|
+
src/noshot/data/DLE FSD BDA/DLE/DLE 1 (Json)/1. DNN (Image Classification).ipynb
|
12
|
+
src/noshot/data/DLE FSD BDA/DLE/DLE 1 (Json)/2. DNN vs CNN.ipynb
|
13
|
+
src/noshot/data/DLE FSD BDA/DLE/DLE 1 (Json)/3. CNN (Object Detecrion).ipynb
|
14
|
+
src/noshot/data/DLE FSD BDA/DLE/DLE 1 (Json)/4. FCN (Image Segmentaion).ipynb
|
15
|
+
src/noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/1.1 DNN (Pytorch).ipynb
|
16
|
+
src/noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/1.2 DNN (Tensorflow).ipynb
|
17
|
+
src/noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/1.3 DNN (Image Classification).ipynb
|
18
|
+
src/noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/2.1 DNN vs CNN.ipynb
|
19
|
+
src/noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/2.2 DNN vs CNN.ipynb
|
20
|
+
src/noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/4. FCNN (Image Segmentation).ipynb
|
21
|
+
src/noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/Lab Excercise (Training DNN).ipynb
|
22
|
+
src/noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/Load-Images.ipynb
|
23
|
+
src/noshot/data/DLE FSD BDA/DLE/DLE 3 (sonic boy)/Ex1.ipynb
|
24
|
+
src/noshot/data/DLE FSD BDA/DLE/DLE 3 (sonic boy)/Ex2.ipynb
|
25
|
+
src/noshot/data/DLE FSD BDA/DLE/DLE 3 (sonic boy)/Ex3.ipynb
|
26
|
+
src/noshot/data/DLE FSD BDA/DLE/DLE 3 (sonic boy)/Ex4.ipynb
|
27
|
+
src/noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp01/DNN Ex No 1.ipynb
|
28
|
+
src/noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp01/Ex No 1 Build in dataset.ipynb
|
29
|
+
src/noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp01/Exp1-Short-DL_ANN_ImageClassification.ipynb
|
30
|
+
src/noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp01/OR GATE .ipynb
|
31
|
+
src/noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp02/Exp2-Short-DL_CNN_ImageClassification.ipynb
|
32
|
+
src/noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp03/DL-Ex3-RNN.ipynb
|
33
|
+
src/noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp04/Ex no 4.ipynb
|
34
|
+
src/noshot/utils/__init__.py
|
35
|
+
src/noshot/utils/shell_utils.py
|
@@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as f:
|
|
5
5
|
|
6
6
|
setup(
|
7
7
|
name="noshot",
|
8
|
-
version="
|
8
|
+
version="13.0.0",
|
9
9
|
author="Tim Stan S",
|
10
10
|
description="Support library for Artificial Intelligence, Machine Learning and Data Science tools",
|
11
11
|
long_description=long_description,
|
@@ -0,0 +1,164 @@
|
|
1
|
+
{
|
2
|
+
"cells": [
|
3
|
+
{
|
4
|
+
"cell_type": "code",
|
5
|
+
"execution_count": null,
|
6
|
+
"id": "ab870464-a374-4292-9b59-0fe123e478df",
|
7
|
+
"metadata": {},
|
8
|
+
"outputs": [],
|
9
|
+
"source": [
|
10
|
+
"import os\n",
|
11
|
+
"import numpy as np\n",
|
12
|
+
"from PIL import Image\n",
|
13
|
+
"from sklearn.model_selection import train_test_split\n",
|
14
|
+
"from sklearn.metrics import classification_report, confusion_matrix\n",
|
15
|
+
"import torch\n",
|
16
|
+
"import torch.nn as nn\n",
|
17
|
+
"import torch.optim as optim\n",
|
18
|
+
"from torch.utils.data import Dataset, DataLoader\n",
|
19
|
+
"import torchvision.transforms as transforms\n",
|
20
|
+
"import matplotlib.pyplot as plt\n",
|
21
|
+
"\n",
|
22
|
+
"dataset_path = \"<filepath>\"\n",
|
23
|
+
"IMG_SIZE = 128\n",
|
24
|
+
"BATCH_SIZE = 32\n",
|
25
|
+
"EPOCHS = 10\n",
|
26
|
+
"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
|
27
|
+
"\n",
|
28
|
+
"class PetDataset(Dataset):\n",
|
29
|
+
" def __init__(self, images, labels, transform=None):\n",
|
30
|
+
" self.images = images\n",
|
31
|
+
" self.labels = labels\n",
|
32
|
+
" self.transform = transform\n",
|
33
|
+
" def __len__(self):\n",
|
34
|
+
" return len(self.images)\n",
|
35
|
+
" def __getitem__(self, idx):\n",
|
36
|
+
" img = self.images[idx]\n",
|
37
|
+
" if self.transform:\n",
|
38
|
+
" img = self.transform(img)\n",
|
39
|
+
" label = self.labels[idx]\n",
|
40
|
+
" return img, label\n",
|
41
|
+
"\n",
|
42
|
+
"def load_images(folder_path, image_size=IMG_SIZE):\n",
|
43
|
+
" images, labels, class_names = [], [], sorted(os.listdir(folder_path))\n",
|
44
|
+
" for label, class_name in enumerate(class_names):\n",
|
45
|
+
" class_dir = os.path.join(folder_path, class_name)\n",
|
46
|
+
" for file in os.listdir(class_dir):\n",
|
47
|
+
" img = Image.open(os.path.join(class_dir, file)).convert('RGB').resize((image_size,image_size))\n",
|
48
|
+
" images.append(np.array(img)/255.0)\n",
|
49
|
+
" labels.append(label)\n",
|
50
|
+
" return np.array(images, dtype=np.float32), np.array(labels), class_names\n",
|
51
|
+
"\n",
|
52
|
+
"X, y, class_names = load_images(dataset_path)\n",
|
53
|
+
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
|
54
|
+
"transform = transforms.Compose([transforms.ToTensor()])\n",
|
55
|
+
"train_dataset = PetDataset(X_train, y_train, transform=transform)\n",
|
56
|
+
"test_dataset = PetDataset(X_test, y_test, transform=transform)\n",
|
57
|
+
"train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True)\n",
|
58
|
+
"test_loader = DataLoader(test_dataset, batch_size=BATCH_SIZE)\n",
|
59
|
+
"\n",
|
60
|
+
"class SimpleCNN(nn.Module):\n",
|
61
|
+
" def __init__(self):\n",
|
62
|
+
" super().__init__()\n",
|
63
|
+
" self.conv = nn.Sequential(\n",
|
64
|
+
" nn.Conv2d(3,32,3,padding=1), nn.ReLU(), nn.MaxPool2d(2),\n",
|
65
|
+
" nn.Conv2d(32,64,3,padding=1), nn.ReLU(), nn.MaxPool2d(2)\n",
|
66
|
+
" )\n",
|
67
|
+
" self.fc = nn.Sequential(\n",
|
68
|
+
" nn.Flatten(),\n",
|
69
|
+
" nn.Linear(64*32*32,128), nn.ReLU(),\n",
|
70
|
+
" nn.Linear(128,len(class_names))\n",
|
71
|
+
" )\n",
|
72
|
+
" def forward(self,x):\n",
|
73
|
+
" return self.fc(self.conv(x))\n",
|
74
|
+
"\n",
|
75
|
+
"model = SimpleCNN().to(device)\n",
|
76
|
+
"criterion = nn.CrossEntropyLoss()\n",
|
77
|
+
"optimizer = optim.Adam(model.parameters(), lr=0.001)\n",
|
78
|
+
"\n",
|
79
|
+
"train_losses, val_losses, train_acc, val_acc = [], [], [], []\n",
|
80
|
+
"\n",
|
81
|
+
"for epoch in range(EPOCHS):\n",
|
82
|
+
" model.train()\n",
|
83
|
+
" running_loss, correct, total = 0,0,0\n",
|
84
|
+
" for imgs, labels in train_loader:\n",
|
85
|
+
" imgs, labels = imgs.to(device), labels.to(device)\n",
|
86
|
+
" optimizer.zero_grad()\n",
|
87
|
+
" outputs = model(imgs)\n",
|
88
|
+
" loss = criterion(outputs, labels)\n",
|
89
|
+
" loss.backward()\n",
|
90
|
+
" optimizer.step()\n",
|
91
|
+
" running_loss += loss.item()\n",
|
92
|
+
" _, preds = torch.max(outputs,1)\n",
|
93
|
+
" correct += (preds==labels).sum().item()\n",
|
94
|
+
" total += labels.size(0)\n",
|
95
|
+
" train_losses.append(running_loss/len(train_loader))\n",
|
96
|
+
" train_acc.append(correct/total)\n",
|
97
|
+
"\n",
|
98
|
+
" model.eval()\n",
|
99
|
+
" val_loss, correct, total = 0,0,0\n",
|
100
|
+
" with torch.no_grad():\n",
|
101
|
+
" for imgs, labels in test_loader:\n",
|
102
|
+
" imgs, labels = imgs.to(device), labels.to(device)\n",
|
103
|
+
" outputs = model(imgs)\n",
|
104
|
+
" loss = criterion(outputs, labels)\n",
|
105
|
+
" val_loss += loss.item()\n",
|
106
|
+
" _, preds = torch.max(outputs,1)\n",
|
107
|
+
" correct += (preds==labels).sum().item()\n",
|
108
|
+
" total += labels.size(0)\n",
|
109
|
+
" val_losses.append(val_loss/len(test_loader))\n",
|
110
|
+
" val_acc.append(correct/total)\n",
|
111
|
+
" print(f\"Epoch {epoch+1}/{EPOCHS} - Train Acc: {train_acc[-1]:.3f}, Val Acc: {val_acc[-1]:.3f}\")\n",
|
112
|
+
"\n",
|
113
|
+
"y_true, y_pred = [], []\n",
|
114
|
+
"model.eval()\n",
|
115
|
+
"with torch.no_grad():\n",
|
116
|
+
" for imgs, labels in test_loader:\n",
|
117
|
+
" imgs, labels = imgs.to(device), labels.to(device)\n",
|
118
|
+
" outputs = model(imgs)\n",
|
119
|
+
" preds = torch.argmax(outputs,1)\n",
|
120
|
+
" y_true.extend(labels.cpu().numpy())\n",
|
121
|
+
" y_pred.extend(preds.cpu().numpy())\n",
|
122
|
+
"\n",
|
123
|
+
"print(\"Classification Report:\")\n",
|
124
|
+
"print(classification_report(y_true, y_pred, target_names=class_names))\n",
|
125
|
+
"print(\"Confusion Matrix:\")\n",
|
126
|
+
"print(confusion_matrix(y_true, y_pred))\n",
|
127
|
+
"\n",
|
128
|
+
"plt.figure(figsize=(12,4))\n",
|
129
|
+
"plt.subplot(1,2,1)\n",
|
130
|
+
"plt.plot(train_acc,label='Train Accuracy')\n",
|
131
|
+
"plt.plot(val_acc,label='Val Accuracy')\n",
|
132
|
+
"plt.title('Accuracy')\n",
|
133
|
+
"plt.legend()\n",
|
134
|
+
"plt.subplot(1,2,2)\n",
|
135
|
+
"plt.plot(train_losses,label='Train Loss')\n",
|
136
|
+
"plt.plot(val_losses,label='Val Loss')\n",
|
137
|
+
"plt.title('Loss')\n",
|
138
|
+
"plt.legend()\n",
|
139
|
+
"plt.show()"
|
140
|
+
]
|
141
|
+
}
|
142
|
+
],
|
143
|
+
"metadata": {
|
144
|
+
"kernelspec": {
|
145
|
+
"display_name": "Python 3 (ipykernel)",
|
146
|
+
"language": "python",
|
147
|
+
"name": "python3"
|
148
|
+
},
|
149
|
+
"language_info": {
|
150
|
+
"codemirror_mode": {
|
151
|
+
"name": "ipython",
|
152
|
+
"version": 3
|
153
|
+
},
|
154
|
+
"file_extension": ".py",
|
155
|
+
"mimetype": "text/x-python",
|
156
|
+
"name": "python",
|
157
|
+
"nbconvert_exporter": "python",
|
158
|
+
"pygments_lexer": "ipython3",
|
159
|
+
"version": "3.12.4"
|
160
|
+
}
|
161
|
+
},
|
162
|
+
"nbformat": 4,
|
163
|
+
"nbformat_minor": 5
|
164
|
+
}
|
@@ -0,0 +1,94 @@
|
|
1
|
+
{
|
2
|
+
"cells": [
|
3
|
+
{
|
4
|
+
"cell_type": "code",
|
5
|
+
"execution_count": null,
|
6
|
+
"id": "272ad344-26ac-46a9-887a-823bc177175e",
|
7
|
+
"metadata": {},
|
8
|
+
"outputs": [],
|
9
|
+
"source": [
|
10
|
+
"import os\n",
|
11
|
+
"import numpy as np\n",
|
12
|
+
"from sklearn.model_selection import train_test_split\n",
|
13
|
+
"from sklearn.metrics import classification_report, confusion_matrix\n",
|
14
|
+
"import matplotlib.pyplot as plt\n",
|
15
|
+
"import tensorflow as tf\n",
|
16
|
+
"from tensorflow.keras.preprocessing.image import load_img, img_to_array\n",
|
17
|
+
"from tensorflow.keras.models import Sequential\n",
|
18
|
+
"from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense\n",
|
19
|
+
"\n",
|
20
|
+
"dataset_path = \"<filepath>\"\n",
|
21
|
+
"IMG_SIZE = (128, 128)\n",
|
22
|
+
"\n",
|
23
|
+
"def load_images(folder_path, image_size=IMG_SIZE):\n",
|
24
|
+
" images, labels, class_names = [], [], sorted(os.listdir(folder_path))\n",
|
25
|
+
" for label, class_name in enumerate(class_names):\n",
|
26
|
+
" class_dir = os.path.join(folder_path, class_name)\n",
|
27
|
+
" for file in os.listdir(class_dir):\n",
|
28
|
+
" img = load_img(os.path.join(class_dir, file), target_size=image_size)\n",
|
29
|
+
" images.append(img_to_array(img)/255.0)\n",
|
30
|
+
" labels.append(label)\n",
|
31
|
+
" return np.array(images), np.array(labels), class_names\n",
|
32
|
+
"\n",
|
33
|
+
"X, y, class_names = load_images(dataset_path)\n",
|
34
|
+
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
|
35
|
+
"\n",
|
36
|
+
"model = Sequential([\n",
|
37
|
+
" Conv2D(32, (3,3), activation='relu', input_shape=(*IMG_SIZE,3)),\n",
|
38
|
+
" MaxPooling2D(2,2),\n",
|
39
|
+
" Conv2D(64, (3,3), activation='relu'),\n",
|
40
|
+
" MaxPooling2D(2,2),\n",
|
41
|
+
" Flatten(),\n",
|
42
|
+
" Dense(128, activation='relu'),\n",
|
43
|
+
" Dense(len(class_names), activation='softmax')\n",
|
44
|
+
"])\n",
|
45
|
+
"\n",
|
46
|
+
"model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n",
|
47
|
+
"history = model.fit(X_train, y_train, validation_split=0.2, epochs=10, batch_size=32)\n",
|
48
|
+
"\n",
|
49
|
+
"test_loss, test_acc = model.evaluate(X_test, y_test)\n",
|
50
|
+
"print(f\"Test Accuracy: {test_acc*100:.2f}%\")\n",
|
51
|
+
"\n",
|
52
|
+
"y_pred = np.argmax(model.predict(X_test), axis=-1)\n",
|
53
|
+
"print(\"Classification Report:\")\n",
|
54
|
+
"print(classification_report(y_test, y_pred, target_names=class_names))\n",
|
55
|
+
"print(\"Confusion Matrix:\")\n",
|
56
|
+
"print(confusion_matrix(y_test, y_pred))\n",
|
57
|
+
"\n",
|
58
|
+
"plt.figure(figsize=(12,4))\n",
|
59
|
+
"plt.subplot(1,2,1)\n",
|
60
|
+
"plt.plot(history.history['accuracy'], label='Train Accuracy')\n",
|
61
|
+
"plt.plot(history.history['val_accuracy'], label='Val Accuracy')\n",
|
62
|
+
"plt.title('Accuracy')\n",
|
63
|
+
"plt.legend()\n",
|
64
|
+
"plt.subplot(1,2,2)\n",
|
65
|
+
"plt.plot(history.history['loss'], label='Train Loss')\n",
|
66
|
+
"plt.plot(history.history['val_loss'], label='Val Loss')\n",
|
67
|
+
"plt.title('Loss')\n",
|
68
|
+
"plt.legend()\n",
|
69
|
+
"plt.show()"
|
70
|
+
]
|
71
|
+
}
|
72
|
+
],
|
73
|
+
"metadata": {
|
74
|
+
"kernelspec": {
|
75
|
+
"display_name": "Python 3 (ipykernel)",
|
76
|
+
"language": "python",
|
77
|
+
"name": "python3"
|
78
|
+
},
|
79
|
+
"language_info": {
|
80
|
+
"codemirror_mode": {
|
81
|
+
"name": "ipython",
|
82
|
+
"version": 3
|
83
|
+
},
|
84
|
+
"file_extension": ".py",
|
85
|
+
"mimetype": "text/x-python",
|
86
|
+
"name": "python",
|
87
|
+
"nbconvert_exporter": "python",
|
88
|
+
"pygments_lexer": "ipython3",
|
89
|
+
"version": "3.12.4"
|
90
|
+
}
|
91
|
+
},
|
92
|
+
"nbformat": 4,
|
93
|
+
"nbformat_minor": 5
|
94
|
+
}
|
@@ -0,0 +1,134 @@
|
|
1
|
+
{
|
2
|
+
"cells": [
|
3
|
+
{
|
4
|
+
"cell_type": "code",
|
5
|
+
"execution_count": null,
|
6
|
+
"id": "8e5e0508-ead9-401c-bb0a-98e48722c959",
|
7
|
+
"metadata": {},
|
8
|
+
"outputs": [],
|
9
|
+
"source": [
|
10
|
+
"import torch\n",
|
11
|
+
"import torch.nn as nn\n",
|
12
|
+
"import torch.nn.functional as F\n",
|
13
|
+
"import torch.optim as optim\n",
|
14
|
+
"from torchvision import datasets, transforms\n",
|
15
|
+
"from torch.utils.data import DataLoader\n",
|
16
|
+
"from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score\n",
|
17
|
+
"import matplotlib.pyplot as plt\n",
|
18
|
+
"import seaborn as sns\n",
|
19
|
+
"\n",
|
20
|
+
"data_transforms = transforms.Compose([\n",
|
21
|
+
" transforms.Resize(256),\n",
|
22
|
+
" transforms.CenterCrop(224),\n",
|
23
|
+
" transforms.ToTensor(),\n",
|
24
|
+
" transforms.Normalize(mean=[0.485, 0.456, 0.406],\n",
|
25
|
+
" std=[0.229, 0.224, 0.225])\n",
|
26
|
+
"])\n",
|
27
|
+
"\n",
|
28
|
+
"data_dir = \"dataset\"\n",
|
29
|
+
"image_dataset = datasets.ImageFolder(root=data_dir, transform=data_transforms)\n",
|
30
|
+
"batch_size = 32\n",
|
31
|
+
"dataloader = DataLoader(image_dataset, batch_size=batch_size, shuffle=True)\n",
|
32
|
+
"\n",
|
33
|
+
"class FeedforwardNet(nn.Module):\n",
|
34
|
+
" def __init__(self, input_size, num_classes):\n",
|
35
|
+
" super(FeedforwardNet, self).__init__()\n",
|
36
|
+
" self.fc1 = nn.Linear(input_size, 512)\n",
|
37
|
+
" self.fc2 = nn.Linear(512, 256)\n",
|
38
|
+
" self.fc3 = nn.Linear(256, num_classes)\n",
|
39
|
+
" self.relu = nn.ReLU()\n",
|
40
|
+
"\n",
|
41
|
+
" def forward(self, x):\n",
|
42
|
+
" x = x.view(x.size(0), -1)\n",
|
43
|
+
" x = self.relu(self.fc1(x))\n",
|
44
|
+
" x = self.relu(self.fc2(x))\n",
|
45
|
+
" x = self.fc3(x)\n",
|
46
|
+
" return x\n",
|
47
|
+
"\n",
|
48
|
+
"input_size = 224 * 224 * 3\n",
|
49
|
+
"num_classes = len(image_dataset.classes)\n",
|
50
|
+
"model = FeedforwardNet(input_size, num_classes)\n",
|
51
|
+
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
|
52
|
+
"model.to(device)\n",
|
53
|
+
"criterion = nn.CrossEntropyLoss()\n",
|
54
|
+
"optimizer = optim.Adam(model.parameters(), lr=0.001)\n",
|
55
|
+
"num_epochs = 10\n",
|
56
|
+
"\n",
|
57
|
+
"for epoch in range(num_epochs):\n",
|
58
|
+
" model.train()\n",
|
59
|
+
" running_loss = 0.0\n",
|
60
|
+
" for images, labels in dataloader:\n",
|
61
|
+
" images, labels = images.to(device), labels.to(device)\n",
|
62
|
+
" outputs = model(images)\n",
|
63
|
+
" loss = criterion(outputs, labels)\n",
|
64
|
+
" optimizer.zero_grad()\n",
|
65
|
+
" loss.backward()\n",
|
66
|
+
" optimizer.step()\n",
|
67
|
+
" running_loss += loss.item() * images.size(0)\n",
|
68
|
+
" epoch_loss = running_loss / len(image_dataset)\n",
|
69
|
+
" print(f\"Epoch [{epoch+1}/{num_epochs}], Loss: {epoch_loss:.4f}\")\n",
|
70
|
+
"\n",
|
71
|
+
"model.eval()\n",
|
72
|
+
"with torch.no_grad():\n",
|
73
|
+
" correct = 0\n",
|
74
|
+
" total = 0\n",
|
75
|
+
" for images, labels in dataloader:\n",
|
76
|
+
" images, labels = images.to(device), labels.to(device)\n",
|
77
|
+
" outputs = model(images)\n",
|
78
|
+
" _, predicted = torch.max(outputs.data, 1)\n",
|
79
|
+
" total += labels.size(0)\n",
|
80
|
+
" correct += (predicted == labels).sum().item()\n",
|
81
|
+
" accuracy = 100 * correct / total\n",
|
82
|
+
"print(f\"Accuracy of the model on {total} images: {accuracy:.2f}%\")\n",
|
83
|
+
"\n",
|
84
|
+
"true_labels = []\n",
|
85
|
+
"predicted_labels = []\n",
|
86
|
+
"with torch.no_grad():\n",
|
87
|
+
" for images, labels in dataloader:\n",
|
88
|
+
" images, labels = images.to(device), labels.to(device)\n",
|
89
|
+
" outputs = model(images)\n",
|
90
|
+
" _, predicted = torch.max(outputs.data, 1)\n",
|
91
|
+
" true_labels.extend(labels.cpu().numpy())\n",
|
92
|
+
" predicted_labels.extend(predicted.cpu().numpy())\n",
|
93
|
+
"\n",
|
94
|
+
"precision = precision_score(true_labels, predicted_labels, average='weighted')\n",
|
95
|
+
"recall = recall_score(true_labels, predicted_labels, average='weighted')\n",
|
96
|
+
"f1 = f1_score(true_labels, predicted_labels, average='weighted')\n",
|
97
|
+
"print(f\"Precision: {precision:.4f}\")\n",
|
98
|
+
"print(f\"Recall: {recall:.4f}\")\n",
|
99
|
+
"print(f\"F1-score: {f1:.4f}\")\n",
|
100
|
+
"\n",
|
101
|
+
"cm = confusion_matrix(true_labels, predicted_labels)\n",
|
102
|
+
"plt.figure(figsize=(8, 6))\n",
|
103
|
+
"sns.heatmap(cm, annot=True, fmt='d', cmap='Blues',\n",
|
104
|
+
" xticklabels=image_dataset.classes,\n",
|
105
|
+
" yticklabels=image_dataset.classes)\n",
|
106
|
+
"plt.xlabel('Predicted Labels')\n",
|
107
|
+
"plt.ylabel('True Labels')\n",
|
108
|
+
"plt.title('Confusion Matrix')\n",
|
109
|
+
"plt.show()"
|
110
|
+
]
|
111
|
+
}
|
112
|
+
],
|
113
|
+
"metadata": {
|
114
|
+
"kernelspec": {
|
115
|
+
"display_name": "Python 3 (ipykernel)",
|
116
|
+
"language": "python",
|
117
|
+
"name": "python3"
|
118
|
+
},
|
119
|
+
"language_info": {
|
120
|
+
"codemirror_mode": {
|
121
|
+
"name": "ipython",
|
122
|
+
"version": 3
|
123
|
+
},
|
124
|
+
"file_extension": ".py",
|
125
|
+
"mimetype": "text/x-python",
|
126
|
+
"name": "python",
|
127
|
+
"nbconvert_exporter": "python",
|
128
|
+
"pygments_lexer": "ipython3",
|
129
|
+
"version": "3.12.4"
|
130
|
+
}
|
131
|
+
},
|
132
|
+
"nbformat": 4,
|
133
|
+
"nbformat_minor": 5
|
134
|
+
}
|
@@ -0,0 +1,127 @@
|
|
1
|
+
{
|
2
|
+
"cells": [
|
3
|
+
{
|
4
|
+
"cell_type": "code",
|
5
|
+
"execution_count": null,
|
6
|
+
"id": "335957c2-a530-4a12-8b04-1475513e138f",
|
7
|
+
"metadata": {},
|
8
|
+
"outputs": [],
|
9
|
+
"source": [
|
10
|
+
"import numpy as np\n",
|
11
|
+
"import tensorflow as tf\n",
|
12
|
+
"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
|
13
|
+
"from tensorflow.keras.models import Sequential\n",
|
14
|
+
"from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout\n",
|
15
|
+
"from tensorflow.keras.optimizers import Adam\n",
|
16
|
+
"import matplotlib.pyplot as plt\n",
|
17
|
+
"\n",
|
18
|
+
"data_dir = \"Bean_Dataset/Bean_Dataset\"\n",
|
19
|
+
"img_height, img_width = 128, 128\n",
|
20
|
+
"batch_size = 32\n",
|
21
|
+
"\n",
|
22
|
+
"datagen = ImageDataGenerator(\n",
|
23
|
+
" rescale=1./255,\n",
|
24
|
+
" validation_split=0.2,\n",
|
25
|
+
" rotation_range=20,\n",
|
26
|
+
" width_shift_range=0.2,\n",
|
27
|
+
" height_shift_range=0.2,\n",
|
28
|
+
" shear_range=0.2,\n",
|
29
|
+
" zoom_range=0.2,\n",
|
30
|
+
" horizontal_flip=True\n",
|
31
|
+
")\n",
|
32
|
+
"\n",
|
33
|
+
"train_gen = datagen.flow_from_directory(\n",
|
34
|
+
" data_dir, target_size=(img_height, img_width),\n",
|
35
|
+
" batch_size=batch_size, class_mode='categorical', subset='training'\n",
|
36
|
+
")\n",
|
37
|
+
"\n",
|
38
|
+
"val_gen = datagen.flow_from_directory(\n",
|
39
|
+
" data_dir, target_size=(img_height, img_width),\n",
|
40
|
+
" batch_size=batch_size, class_mode='categorical', subset='validation'\n",
|
41
|
+
")\n",
|
42
|
+
"\n",
|
43
|
+
"num_classes = len(train_gen.class_indices)\n",
|
44
|
+
"\n",
|
45
|
+
"def build_dnn():\n",
|
46
|
+
" model = Sequential([\n",
|
47
|
+
" Flatten(input_shape=(img_height, img_width, 3)),\n",
|
48
|
+
" Dense(512, activation='relu'),\n",
|
49
|
+
" Dropout(0.5),\n",
|
50
|
+
" Dense(256, activation='relu'),\n",
|
51
|
+
" Dropout(0.5),\n",
|
52
|
+
" Dense(num_classes, activation='softmax')\n",
|
53
|
+
" ])\n",
|
54
|
+
" model.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy'])\n",
|
55
|
+
" return model\n",
|
56
|
+
"\n",
|
57
|
+
"def build_cnn():\n",
|
58
|
+
" model = Sequential([\n",
|
59
|
+
" Conv2D(32, (3,3), activation='relu', input_shape=(img_height, img_width, 3)),\n",
|
60
|
+
" MaxPooling2D(2,2),\n",
|
61
|
+
" Conv2D(64, (3,3), activation='relu'),\n",
|
62
|
+
" MaxPooling2D(2,2),\n",
|
63
|
+
" Conv2D(128, (3,3), activation='relu'),\n",
|
64
|
+
" MaxPooling2D(2,2),\n",
|
65
|
+
" Flatten(),\n",
|
66
|
+
" Dense(512, activation='relu'),\n",
|
67
|
+
" Dropout(0.5),\n",
|
68
|
+
" Dense(num_classes, activation='softmax')\n",
|
69
|
+
" ])\n",
|
70
|
+
" model.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy'])\n",
|
71
|
+
" return model\n",
|
72
|
+
"\n",
|
73
|
+
"dnn_model = build_dnn()\n",
|
74
|
+
"dnn_history = dnn_model.fit(train_gen, epochs=20, validation_data=val_gen)\n",
|
75
|
+
"\n",
|
76
|
+
"cnn_model = build_cnn()\n",
|
77
|
+
"cnn_history = cnn_model.fit(train_gen, epochs=20, validation_data=val_gen)\n",
|
78
|
+
"\n",
|
79
|
+
"plt.figure(figsize=(12,5))\n",
|
80
|
+
"plt.subplot(1,2,1)\n",
|
81
|
+
"plt.plot(dnn_history.history['accuracy'], label='DNN Train')\n",
|
82
|
+
"plt.plot(dnn_history.history['val_accuracy'], label='DNN Val')\n",
|
83
|
+
"plt.plot(cnn_history.history['accuracy'], label='CNN Train')\n",
|
84
|
+
"plt.plot(cnn_history.history['val_accuracy'], label='CNN Val')\n",
|
85
|
+
"plt.title('Accuracy')\n",
|
86
|
+
"plt.xlabel('Epoch')\n",
|
87
|
+
"plt.ylabel('Accuracy')\n",
|
88
|
+
"plt.legend()\n",
|
89
|
+
"\n",
|
90
|
+
"plt.subplot(1,2,2)\n",
|
91
|
+
"plt.plot(dnn_history.history['loss'], label='DNN Train')\n",
|
92
|
+
"plt.plot(dnn_history.history['val_loss'], label='DNN Val')\n",
|
93
|
+
"plt.plot(cnn_history.history['loss'], label='CNN Train')\n",
|
94
|
+
"plt.plot(cnn_history.history['val_loss'], label='CNN Val')\n",
|
95
|
+
"plt.title('Loss')\n",
|
96
|
+
"plt.xlabel('Epoch')\n",
|
97
|
+
"plt.ylabel('Loss')\n",
|
98
|
+
"plt.legend()\n",
|
99
|
+
"plt.show()\n",
|
100
|
+
"\n",
|
101
|
+
"print(f\"DNN Train Acc: {dnn_history.history['accuracy'][-1]:.4f}, Val Acc: {dnn_history.history['val_accuracy'][-1]:.4f}\")\n",
|
102
|
+
"print(f\"CNN Train Acc: {cnn_history.history['accuracy'][-1]:.4f}, Val Acc: {cnn_history.history['val_accuracy'][-1]:.4f}\")"
|
103
|
+
]
|
104
|
+
}
|
105
|
+
],
|
106
|
+
"metadata": {
|
107
|
+
"kernelspec": {
|
108
|
+
"display_name": "Python 3 (ipykernel)",
|
109
|
+
"language": "python",
|
110
|
+
"name": "python3"
|
111
|
+
},
|
112
|
+
"language_info": {
|
113
|
+
"codemirror_mode": {
|
114
|
+
"name": "ipython",
|
115
|
+
"version": 3
|
116
|
+
},
|
117
|
+
"file_extension": ".py",
|
118
|
+
"mimetype": "text/x-python",
|
119
|
+
"name": "python",
|
120
|
+
"nbconvert_exporter": "python",
|
121
|
+
"pygments_lexer": "ipython3",
|
122
|
+
"version": "3.12.4"
|
123
|
+
}
|
124
|
+
},
|
125
|
+
"nbformat": 4,
|
126
|
+
"nbformat_minor": 5
|
127
|
+
}
|