neelimapreeti297 commited on
Commit
22f7a0e
·
verified ·
1 Parent(s): 7bbe260
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. main.ipynb +225 -0
  2. test/cats/cats_00091.jpg +0 -0
  3. test/cats/cats_00092.jpg +0 -0
  4. test/cats/cats_00093.jpg +0 -0
  5. test/cats/cats_00094.jpg +0 -0
  6. test/cats/cats_00095.jpg +0 -0
  7. test/cats/cats_00096.jpg +0 -0
  8. test/cats/cats_00097.jpg +0 -0
  9. test/cats/cats_00098.jpg +0 -0
  10. test/cats/cats_00099.jpg +0 -0
  11. test/cats/cats_00100.jpg +0 -0
  12. test/dogs/dogs_00091.jpg +0 -0
  13. test/dogs/dogs_00092.jpg +0 -0
  14. test/dogs/dogs_00093.jpg +0 -0
  15. test/dogs/dogs_00094.jpg +0 -0
  16. test/dogs/dogs_00095.jpg +0 -0
  17. test/dogs/dogs_00096.jpg +0 -0
  18. test/dogs/dogs_00097.jpg +0 -0
  19. test/dogs/dogs_00098.jpg +0 -0
  20. test/dogs/dogs_00099.jpg +0 -0
  21. test/dogs/dogs_00100.jpg +0 -0
  22. test/panda/panda_00091.jpg +0 -0
  23. test/panda/panda_00092.jpg +0 -0
  24. test/panda/panda_00093.jpg +0 -0
  25. test/panda/panda_00094.jpg +0 -0
  26. test/panda/panda_00095.jpg +0 -0
  27. test/panda/panda_00096.jpg +0 -0
  28. test/panda/panda_00097.jpg +0 -0
  29. test/panda/panda_00098.jpg +0 -0
  30. test/panda/panda_00099.jpg +0 -0
  31. test/panda/panda_00100.jpg +0 -0
  32. train/cats/cats_00001.jpg +0 -0
  33. train/cats/cats_00002.jpg +0 -0
  34. train/cats/cats_00003.jpg +0 -0
  35. train/cats/cats_00004.jpg +0 -0
  36. train/cats/cats_00005.jpg +0 -0
  37. train/cats/cats_00006.jpg +0 -0
  38. train/cats/cats_00007.jpg +0 -0
  39. train/cats/cats_00008.jpg +0 -0
  40. train/cats/cats_00009.jpg +0 -0
  41. train/cats/cats_00010.jpg +0 -0
  42. train/cats/cats_00011.jpg +0 -0
  43. train/cats/cats_00012.jpg +0 -0
  44. train/cats/cats_00013.jpg +0 -0
  45. train/cats/cats_00014.jpg +0 -0
  46. train/cats/cats_00015.jpg +0 -0
  47. train/cats/cats_00016.jpg +0 -0
  48. train/cats/cats_00017.jpg +0 -0
  49. train/cats/cats_00018.jpg +0 -0
  50. train/cats/cats_00019.jpg +0 -0
main.ipynb ADDED
@@ -0,0 +1,225 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 2,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "#import libraries\n",
10
+ "import torch \n",
11
+ "from torchvision import datasets, transforms \n",
12
+ "import torch.nn as nn\n",
13
+ "import torch.optim as optim\n",
14
+ "from torch.utils.data import DataLoader\n",
15
+ "from torchvision.datasets import ImageFolder\n",
16
+ "\n"
17
+ ]
18
+ },
19
+ {
20
+ "cell_type": "code",
21
+ "execution_count": 3,
22
+ "metadata": {},
23
+ "outputs": [],
24
+ "source": [
25
+ "#define the data transforms\n",
26
+ "\n",
27
+ "transform = transforms.Compose([\n",
28
+ " transforms.Resize((224,224)),\n",
29
+ " transforms.ToTensor(),\n",
30
+ " transforms.Normalize((0.485,0.456,0.406),(0.229,0.224,0.225))\n",
31
+ " ])"
32
+ ]
33
+ },
34
+ {
35
+ "cell_type": "code",
36
+ "execution_count": 5,
37
+ "metadata": {},
38
+ "outputs": [],
39
+ "source": [
40
+ "#insert the datasets\n",
41
+ "\n",
42
+ "train_dataset = ImageFolder('./data/train', transform=transform)\n",
43
+ "test_dataset =ImageFolder('./data/test', transform=transform)\n"
44
+ ]
45
+ },
46
+ {
47
+ "cell_type": "code",
48
+ "execution_count": 11,
49
+ "metadata": {},
50
+ "outputs": [],
51
+ "source": [
52
+ "# make cnn model\n",
53
+ "\n",
54
+ "class CNN(nn.Module):\n",
55
+ " def __init__(self):\n",
56
+ " super(CNN, self).__init__()\n",
57
+ " self.conv1 = nn.Conv2d(3, 6, 5)\n",
58
+ " self.conv2 = nn.Conv2d(6, 16, 5)\n",
59
+ " self.pool = nn.MaxPool2d(2, 2)\n",
60
+ " self.fc1 = nn.Linear(16 * 53 * 53, 120)\n",
61
+ " self.fc2 = nn.Linear(120, 84)\n",
62
+ " self.fc3 = nn.Linear(84, 3)\n",
63
+ "\n",
64
+ " def forward(self, x):\n",
65
+ " x = self.conv1(x)\n",
66
+ " x = self.pool(x)\n",
67
+ " x = self.conv2(x)\n",
68
+ " x = self.pool(x)\n",
69
+ " x = x.view(-1, 16 * 53 * 53)\n",
70
+ " x = self.fc1(x)\n",
71
+ " x = self.fc2(x)\n",
72
+ " x = self.fc3(x)\n",
73
+ " return x\n",
74
+ "\n",
75
+ " \n",
76
+ "\n"
77
+ ]
78
+ },
79
+ {
80
+ "cell_type": "code",
81
+ "execution_count": 12,
82
+ "metadata": {},
83
+ "outputs": [],
84
+ "source": [
85
+ "batch_size = 8\n"
86
+ ]
87
+ },
88
+ {
89
+ "cell_type": "code",
90
+ "execution_count": 13,
91
+ "metadata": {},
92
+ "outputs": [],
93
+ "source": [
94
+ "train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)\n",
95
+ "test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=True)\n"
96
+ ]
97
+ },
98
+ {
99
+ "cell_type": "code",
100
+ "execution_count": 14,
101
+ "metadata": {},
102
+ "outputs": [],
103
+ "source": [
104
+ "model = CNN()\n",
105
+ "loss_function = nn.CrossEntropyLoss()\n",
106
+ "optimizer = optim.Adam(model.parameters(), lr=0.001)"
107
+ ]
108
+ },
109
+ {
110
+ "cell_type": "code",
111
+ "execution_count": 15,
112
+ "metadata": {},
113
+ "outputs": [
114
+ {
115
+ "name": "stdout",
116
+ "output_type": "stream",
117
+ "text": [
118
+ "Epoch [1/10], Step [1/34], Loss: 1.0981\n",
119
+ "Epoch [2/10], Step [1/34], Loss: 1.2921\n",
120
+ "Epoch [3/10], Step [1/34], Loss: 0.4883\n",
121
+ "Epoch [4/10], Step [1/34], Loss: 0.3408\n",
122
+ "Epoch [5/10], Step [1/34], Loss: 0.1063\n",
123
+ "Epoch [6/10], Step [1/34], Loss: 0.0406\n",
124
+ "Epoch [7/10], Step [1/34], Loss: 0.0009\n",
125
+ "Epoch [8/10], Step [1/34], Loss: 0.0066\n",
126
+ "Epoch [9/10], Step [1/34], Loss: 0.0009\n",
127
+ "Epoch [10/10], Step [1/34], Loss: 0.0012\n"
128
+ ]
129
+ }
130
+ ],
131
+ "source": [
132
+ "#Train the model\n",
133
+ "\n",
134
+ "for epoch in range(10):\n",
135
+ " for i, (images, labels) in enumerate(train_loader):\n",
136
+ "\n",
137
+ " outputs = model(images)\n",
138
+ "\n",
139
+ " loss = loss_function(outputs, labels)\n",
140
+ "\n",
141
+ " optimizer.zero_grad()\n",
142
+ " loss.backward()\n",
143
+ " optimizer.step()\n",
144
+ "\n",
145
+ " if i % 200 == 0:\n",
146
+ " print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch + 1, 10, i + 1, len(train_loader), loss.item()))"
147
+ ]
148
+ },
149
+ {
150
+ "cell_type": "code",
151
+ "execution_count": 16,
152
+ "metadata": {},
153
+ "outputs": [],
154
+ "source": [
155
+ "#iterate over the test data \n",
156
+ "\n",
157
+ "correct = 0\n",
158
+ "total = 0\n",
159
+ "for i, (images, labels) in enumerate(test_loader):\n",
160
+ " outputs = model(images)\n",
161
+ " \n",
162
+ " _, predicted = torch.max(outputs.data, 1)\n",
163
+ " correct += (predicted == labels).sum().item()\n",
164
+ " total += labels.size(0)\n"
165
+ ]
166
+ },
167
+ {
168
+ "cell_type": "code",
169
+ "execution_count": 17,
170
+ "metadata": {},
171
+ "outputs": [
172
+ {
173
+ "name": "stdout",
174
+ "output_type": "stream",
175
+ "text": [
176
+ "Accuracy: 53.333333333333336%\n"
177
+ ]
178
+ }
179
+ ],
180
+ "source": [
181
+ "#calculate the accuracy\n",
182
+ "accuracy = 100 * correct / total\n",
183
+ "print('Accuracy: {}%' .format(accuracy))"
184
+ ]
185
+ },
186
+ {
187
+ "cell_type": "code",
188
+ "execution_count": 19,
189
+ "metadata": {},
190
+ "outputs": [],
191
+ "source": [
192
+ "model_scripted = torch.jit.script(model)\n",
193
+ "model_scripted.save('./models/cat_dog_cnn.pt')"
194
+ ]
195
+ },
196
+ {
197
+ "cell_type": "code",
198
+ "execution_count": null,
199
+ "metadata": {},
200
+ "outputs": [],
201
+ "source": []
202
+ }
203
+ ],
204
+ "metadata": {
205
+ "kernelspec": {
206
+ "display_name": "base",
207
+ "language": "python",
208
+ "name": "python3"
209
+ },
210
+ "language_info": {
211
+ "codemirror_mode": {
212
+ "name": "ipython",
213
+ "version": 3
214
+ },
215
+ "file_extension": ".py",
216
+ "mimetype": "text/x-python",
217
+ "name": "python",
218
+ "nbconvert_exporter": "python",
219
+ "pygments_lexer": "ipython3",
220
+ "version": "3.9.12"
221
+ }
222
+ },
223
+ "nbformat": 4,
224
+ "nbformat_minor": 2
225
+ }
test/cats/cats_00091.jpg ADDED
test/cats/cats_00092.jpg ADDED
test/cats/cats_00093.jpg ADDED
test/cats/cats_00094.jpg ADDED
test/cats/cats_00095.jpg ADDED
test/cats/cats_00096.jpg ADDED
test/cats/cats_00097.jpg ADDED
test/cats/cats_00098.jpg ADDED
test/cats/cats_00099.jpg ADDED
test/cats/cats_00100.jpg ADDED
test/dogs/dogs_00091.jpg ADDED
test/dogs/dogs_00092.jpg ADDED
test/dogs/dogs_00093.jpg ADDED
test/dogs/dogs_00094.jpg ADDED
test/dogs/dogs_00095.jpg ADDED
test/dogs/dogs_00096.jpg ADDED
test/dogs/dogs_00097.jpg ADDED
test/dogs/dogs_00098.jpg ADDED
test/dogs/dogs_00099.jpg ADDED
test/dogs/dogs_00100.jpg ADDED
test/panda/panda_00091.jpg ADDED
test/panda/panda_00092.jpg ADDED
test/panda/panda_00093.jpg ADDED
test/panda/panda_00094.jpg ADDED
test/panda/panda_00095.jpg ADDED
test/panda/panda_00096.jpg ADDED
test/panda/panda_00097.jpg ADDED
test/panda/panda_00098.jpg ADDED
test/panda/panda_00099.jpg ADDED
test/panda/panda_00100.jpg ADDED
train/cats/cats_00001.jpg ADDED
train/cats/cats_00002.jpg ADDED
train/cats/cats_00003.jpg ADDED
train/cats/cats_00004.jpg ADDED
train/cats/cats_00005.jpg ADDED
train/cats/cats_00006.jpg ADDED
train/cats/cats_00007.jpg ADDED
train/cats/cats_00008.jpg ADDED
train/cats/cats_00009.jpg ADDED
train/cats/cats_00010.jpg ADDED
train/cats/cats_00011.jpg ADDED
train/cats/cats_00012.jpg ADDED
train/cats/cats_00013.jpg ADDED
train/cats/cats_00014.jpg ADDED
train/cats/cats_00015.jpg ADDED
train/cats/cats_00016.jpg ADDED
train/cats/cats_00017.jpg ADDED
train/cats/cats_00018.jpg ADDED
train/cats/cats_00019.jpg ADDED