noshot 12.0.0__py3-none-any.whl → 14.0.0__py3-none-any.whl
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/data/DLE FSD BDA/BDA/BDA Lab/Ex1/input.txt +1 -0
- noshot/data/DLE FSD BDA/BDA/BDA Lab/Ex1/mapper.py +6 -0
- noshot/data/DLE FSD BDA/BDA/BDA Lab/Ex1/reducer.py +22 -0
- noshot/data/DLE FSD BDA/BDA/BDA Lab/Ex2/Weatherdataset.csv +200 -0
- noshot/data/DLE FSD BDA/BDA/BDA Lab/Ex2/mapper.py +20 -0
- noshot/data/DLE FSD BDA/BDA/BDA Lab/Ex2/reducer.py +32 -0
- noshot/data/DLE FSD BDA/BDA/BDA Lab/Ex3/BF_Map.py +11 -0
- noshot/data/DLE FSD BDA/BDA/BDA Lab/Ex3/BF_Red.py +30 -0
- noshot/data/DLE FSD BDA/BDA/BDA Lab/Ex3/bloom_filter.py +71 -0
- noshot/data/DLE FSD BDA/BDA/BDA Lab/Ex3/bloom_filter_mapper.py +71 -0
- noshot/data/DLE FSD BDA/BDA/BDA Lab/Ex3/bloom_filter_reducer.py +71 -0
- noshot/data/DLE FSD BDA/BDA/BDA Lab/Ex3/weblog.csv +100 -0
- noshot/data/DLE FSD BDA/BDA/BDA Lab/Ex4/FMA_mapper.py +14 -0
- noshot/data/DLE FSD BDA/BDA/BDA Lab/Ex4/FMA_reducer.py +14 -0
- noshot/data/DLE FSD BDA/BDA/BDA Lab/Ex4/Tweets.csv +92 -0
- noshot/data/DLE FSD BDA/BDA/BDA Lab/Instructions.txt +56 -0
- noshot/data/DLE FSD BDA/BDA/BDA Lab.iso +0 -0
- noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/1.1 DNN (Pytorch).ipynb +164 -0
- noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/1.2 DNN (Tensorflow).ipynb +94 -0
- noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/1.3 DNN (Image Classification).ipynb +134 -0
- noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/2.1 DNN vs CNN.ipynb +127 -0
- noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/2.2 DNN vs CNN.ipynb +123 -0
- noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/3 Bounding Boxes.ipynb +109 -0
- noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/4. FCNN (Image Segmentation).ipynb +108 -0
- noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/Lab Excercise (Training DNN).ipynb +646 -0
- noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/Load-Images.ipynb +553 -0
- noshot/data/DLE FSD BDA/DLE/DLE 3 (sonic boy)/Ex1.ipynb +216 -0
- noshot/data/DLE FSD BDA/DLE/DLE 3 (sonic boy)/Ex2.ipynb +195 -0
- noshot/data/DLE FSD BDA/DLE/DLE 3 (sonic boy)/Ex3.ipynb +427 -0
- noshot/data/DLE FSD BDA/DLE/DLE 3 (sonic boy)/Ex4.ipynb +186 -0
- noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp01/DNN Ex No 1.ipynb +398 -0
- noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp01/Ex No 1 Build in dataset.ipynb +171 -0
- noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp01/Exp1-Short-DL_ANN_ImageClassification.ipynb +401 -0
- noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp01/OR GATE .ipynb +8511 -0
- noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp02/Exp2-Short-DL_CNN_ImageClassification.ipynb +737 -0
- noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp03/DL-Ex3-RNN.ipynb +591 -0
- noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp04/Ex no 4.ipynb +551 -0
- {noshot-12.0.0.dist-info → noshot-14.0.0.dist-info}/METADATA +1 -1
- noshot-14.0.0.dist-info/RECORD +50 -0
- noshot-12.0.0.dist-info/RECORD +0 -13
- /noshot/data/DLE FSD BDA/DLE/{1. DNN (Image Classification).ipynb → DLE 1 (Json)/1. DNN (Image Classification).ipynb} +0 -0
- /noshot/data/DLE FSD BDA/DLE/{2. DNN vs CNN.ipynb → DLE 1 (Json)/2. DNN vs CNN.ipynb} +0 -0
- /noshot/data/DLE FSD BDA/DLE/{3. CNN (Object Detecrion).ipynb → DLE 1 (Json)/3. CNN (Object Detecrion).ipynb} +0 -0
- /noshot/data/DLE FSD BDA/DLE/{4. FCN (Image Segmentaion).ipynb → DLE 1 (Json)/4. FCN (Image Segmentaion).ipynb} +0 -0
- {noshot-12.0.0.dist-info → noshot-14.0.0.dist-info}/WHEEL +0 -0
- {noshot-12.0.0.dist-info → noshot-14.0.0.dist-info}/licenses/LICENSE.txt +0 -0
- {noshot-12.0.0.dist-info → noshot-14.0.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,14 @@
|
|
1
|
+
#usr/bin/env python
|
2
|
+
import sys
|
3
|
+
|
4
|
+
max_trail = 0
|
5
|
+
for line in sys.stdin :
|
6
|
+
line = line.strip()
|
7
|
+
if not line : continue
|
8
|
+
try :
|
9
|
+
trail = int(line)
|
10
|
+
if max_trail < trail :
|
11
|
+
max_trail = trail
|
12
|
+
except : continue
|
13
|
+
else:
|
14
|
+
print("Total number of unique uids are : ",2**max_trail)
|
@@ -0,0 +1,92 @@
|
|
1
|
+
tweet_id,airline_sentiment,airline_sentiment_confidence,negativereason,negativereason_confidence,airline,airline_sentiment_gold,name,negativereason_gold,retweet_count,text,tweet_coord,tweet_created,tweet_location,user_timezone
|
2
|
+
570306133677760513,neutral,1,,,Virgin America,,cairdin,,0,@VirginAmerica What @dhepburn said.,,2/24/2015 11:35,,Eastern Time (US & Canada)
|
3
|
+
570301130888122368,positive,0.3486,,0,Virgin America,,jnardino,,0,@VirginAmerica plus you've added commercials to the experience... tacky.,,2/24/2015 11:15,,Pacific Time (US & Canada)
|
4
|
+
570301083672813571,neutral,0.6837,,,Virgin America,,yvonnalynn,,0,@VirginAmerica I didn't today... Must mean I need to take another trip!,,2/24/2015 11:15,Lets Play,Central Time (US & Canada)
|
5
|
+
570301031407624196,negative,1,Bad Flight,0.7033,Virgin America,,jnardino,,0,"@VirginAmerica it's really aggressive to blast obnoxious ""entertainment"" in your guests' faces & they have little recourse",,2/24/2015 11:15,,Pacific Time (US & Canada)
|
6
|
+
570300817074462722,negative,1,Can't Tell,1,Virgin America,,jnardino,,0,@VirginAmerica and it's a really big bad thing about it,,2/24/2015 11:14,,Pacific Time (US & Canada)
|
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
|
+
,,,,,,,,,,,,,,
|
40
|
+
,,,,,,,,,,,,,,
|
41
|
+
,,,,,,,,,,,,,,
|
42
|
+
,,,,,,,,,,,,,,
|
43
|
+
,,,,,,,,,,,,,,
|
44
|
+
,,,,,,,,,,,,,,
|
45
|
+
,,,,,,,,,,,,,,
|
46
|
+
,,,,,,,,,,,,,,
|
47
|
+
,,,,,,,,,,,,,,
|
48
|
+
,,,,,,,,,,,,,,
|
49
|
+
,,,,,,,,,,,,,,
|
50
|
+
,,,,,,,,,,,,,,
|
51
|
+
,,,,,,,,,,,,,,
|
52
|
+
,,,,,,,,,,,,,,
|
53
|
+
,,,,,,,,,,,,,,
|
54
|
+
,,,,,,,,,,,,,,
|
55
|
+
,,,,,,,,,,,,,,
|
56
|
+
,,,,,,,,,,,,,,
|
57
|
+
,,,,,,,,,,,,,,
|
58
|
+
,,,,,,,,,,,,,,
|
59
|
+
,,,,,,,,,,,,,,
|
60
|
+
,,,,,,,,,,,,,,
|
61
|
+
,,,,,,,,,,,,,,
|
62
|
+
,,,,,,,,,,,,,,
|
63
|
+
,,,,,,,,,,,,,,
|
64
|
+
,,,,,,,,,,,,,,
|
65
|
+
,,,,,,,,,,,,,,
|
66
|
+
,,,,,,,,,,,,,,
|
67
|
+
,,,,,,,,,,,,,,
|
68
|
+
,,,,,,,,,,,,,,
|
69
|
+
,,,,,,,,,,,,,,
|
70
|
+
,,,,,,,,,,,,,,
|
71
|
+
,,,,,,,,,,,,,,
|
72
|
+
,,,,,,,,,,,,,,
|
73
|
+
,,,,,,,,,,,,,,
|
74
|
+
,,,,,,,,,,,,,,
|
75
|
+
,,,,,,,,,,,,,,
|
76
|
+
,,,,,,,,,,,,,,
|
77
|
+
,,,,,,,,,,,,,,
|
78
|
+
,,,,,,,,,,,,,,
|
79
|
+
,,,,,,,,,,,,,,
|
80
|
+
,,,,,,,,,,,,,,
|
81
|
+
,,,,,,,,,,,,,,
|
82
|
+
,,,,,,,,,,,,,,
|
83
|
+
,,,,,,,,,,,,,,
|
84
|
+
,,,,,,,,,,,,,,
|
85
|
+
,,,,,,,,,,,,,,
|
86
|
+
,,,,,,,,,,,,,,
|
87
|
+
,,,,,,,,,,,,,,
|
88
|
+
,,,,,,,,,,,,,,
|
89
|
+
,,,,,,,,,,,,,,
|
90
|
+
,,,,,,,,,,,,,,
|
91
|
+
,,,,,,,,,,,,,,
|
92
|
+
,,,,,,,,,,,,,,
|
@@ -0,0 +1,56 @@
|
|
1
|
+
Go to cloudera's Home
|
2
|
+
Create a folder named Lab
|
3
|
+
Copy the mapper.py & reducer.py into the folder
|
4
|
+
==========================================================
|
5
|
+
Run The Commands In The Folder Containing Mapper & Reducer
|
6
|
+
==========================================================
|
7
|
+
|
8
|
+
=====================
|
9
|
+
To Create & Edit File
|
10
|
+
=====================
|
11
|
+
gedit input.txt
|
12
|
+
gedit mapper.py
|
13
|
+
gedit reducer.py
|
14
|
+
|
15
|
+
==========================
|
16
|
+
To View Contents Of A File
|
17
|
+
==========================
|
18
|
+
cat input.txt
|
19
|
+
|
20
|
+
======================
|
21
|
+
To Demonstarte Locally
|
22
|
+
======================
|
23
|
+
cat input.txt | python3 mapper.py | sort | python3 reducer.py
|
24
|
+
|
25
|
+
=============================================================
|
26
|
+
Add Files To Hadoop FS (use different name if already exists)
|
27
|
+
=============================================================
|
28
|
+
hdfs fs -put input.txt
|
29
|
+
|
30
|
+
=================
|
31
|
+
To List The Files
|
32
|
+
=================
|
33
|
+
hdfs fs -ls
|
34
|
+
|
35
|
+
|
36
|
+
(Note: Change the paths for mapper, reducer, input, output as in your system)
|
37
|
+
(output path should be unique)
|
38
|
+
================
|
39
|
+
To Execute A Job
|
40
|
+
================
|
41
|
+
hadoop jar /usr/lib/hadoop-0.20-mapreduce/contrib/streaming/hadoop-streaming-2.6.0-mr1-cdh5.4.2.jar \
|
42
|
+
-file /home/cloudera/Lab/mapper.py \
|
43
|
+
-file /home/cloudera/Lab/reducer.py \
|
44
|
+
-mapper "python mapper.py" \
|
45
|
+
-reducer "python reducer.py" \
|
46
|
+
-input /user/cloudera/input.txt \
|
47
|
+
-output /user/cloudera/laboutput
|
48
|
+
|
49
|
+
hadoop fs -ls /user/cloudera/laboutput
|
50
|
+
|
51
|
+
hadoop fs -cat /user/cloudera/laboutput/part-00000
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
|
Binary file
|
@@ -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
|
+
}
|