Skip to content

Commit 5f7c5f9

Browse files
committed
commit 1
0 parents  commit 5f7c5f9

12 files changed

+1255
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
transform_data/Untitled.ipynb
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import torch \n",
10+
"from torch import nn, optim\n",
11+
"import torch.nn.functional as F\n",
12+
"from torch.utils.data import DataLoader, Dataset\n",
13+
"from torch.utils.data.sampler import SubsetRandomSampler \n",
14+
"from torchvision import models, transforms\n",
15+
"from transform_data.face_keypoint_det import FacialKeyPointDataset, Normalize, Rescale, RandomCrop, ToTensor\n",
16+
"import numpy as np\n",
17+
"# dataset #\n",
18+
"data_transform = transforms.Compose([Rescale(250), RandomCrop(224), Normalize(), ToTensor()])\n",
19+
"\n",
20+
"transformed_train_dataset = FacialKeyPointDataset(csv_file = '/content/training_frames_keypoints.csv', root_dir = '/content/training/', transform = data_transform)\n",
21+
"\n",
22+
"transformed_test_dataset = FacialKeyPointDataset(csv_file = '/content/test_frames_keypoints.csv', root_dir = '/content/test/', transform = data_transform)\n",
23+
"\n",
24+
"print('length of dataset', len(transformed_train_dataset))\n",
25+
"print(type(transformed_train_dataset))\n",
26+
"\n",
27+
"# training indices used for validation #\n",
28+
"len_dataset = len(transformed_train_dataset)\n",
29+
"indices = list(range(len_dataset))\n",
30+
"np.random.shuffle(indices)\n",
31+
"split = int(np.floor(0.2*len_dataset))\n",
32+
"train_idx, test_idx = indices[split:], indices[:split]\n",
33+
"\n",
34+
"# trn_sampler #\n",
35+
"train_sampler = SubsetRandomSampler(train_idx)\n",
36+
"val_sampler = SubsetRandomSampler(test_idx)\n",
37+
"\n",
38+
"# training loader #\n",
39+
"num_workers = 4\n",
40+
"batch_size = 64\n",
41+
"\n",
42+
"train_loader = DataLoader(transformed_train_dataset, num_workers = num_workers, batch_size = batch_size , sampler = train_sampler)\n",
43+
"\n",
44+
"val_loader = DataLoader(transformed_train_dataset, num_workers = num_workers, batch_size = batch_size, sampler = val_sampler)\n",
45+
"\n",
46+
"test_loader = DataLoader(transformed_test_dataset, num_workers = num_workers, batch_size = batch_size , shuffle = True)\n",
47+
"\n",
48+
"# checking if gpu is available #\n",
49+
"device = torch.device('cuda' if torch.cuda.is_available else 'cpu')\n",
50+
"\n",
51+
"# defining model architecture\n",
52+
"model = models.vgg11_bn(pretrained=False)\n",
53+
"\n",
54+
"# redesigning the classifier part to output 136 features and the convolution part to accept grayscale image\n",
55+
"from collections import OrderedDict\n",
56+
"model.features[0] = nn.Conv2d(1,64, 3, stride=(1,2), padding=(1,1))\n",
57+
"print(model.features[0])\n",
58+
"classifier = nn.Sequential(OrderedDict([\n",
59+
" ('fc1', nn.Linear(25088, 4096)),\n",
60+
" ('relu', nn.ReLU()),\n",
61+
" ('dropout', nn.Dropout(0.5)),\n",
62+
" ('fc2', nn.Linear(4096, 4096)),\n",
63+
" ('relu2', nn.ReLU()),\n",
64+
" ('dropout2', nn.Dropout(0.5)),\n",
65+
" ('fc3', nn.Linear(4096, 1000)),\n",
66+
" ('relu3', nn.ReLU()),\n",
67+
" ('dropout3', nn.Dropout(0.5)),\n",
68+
" ('fc4', nn.Linear(1000, 136))\n",
69+
"]))\n",
70+
"model.classifier = classifier\n",
71+
"print(model)\n",
72+
"\n",
73+
"# defining loss funcion #\n",
74+
"criterion = nn.CrossEntropyLoss\n",
75+
"\n",
76+
"# defining optimizer #\n",
77+
"optimizer = optim.SGD(model.parameters(), lr = 0.01)\n",
78+
"\n",
79+
"if device == 'cuda':\n",
80+
" model = model.cuda()\n",
81+
"\n",
82+
"print(device)\n",
83+
"\n",
84+
"epochs = 50\n",
85+
"\n",
86+
"trn_loss_list = []\n",
87+
"val_loss_list = []\n",
88+
"test_loss_list = []\n",
89+
"val_min_loss = np.Inf\n",
90+
"\n",
91+
"for epoch in range(epochs):\n",
92+
" trn_loss = 0\n",
93+
" test_loss = 0\n",
94+
" val_loss = 0\n",
95+
"\n",
96+
" trn_running_loss = 0\n",
97+
" test_running_loss = 0\n",
98+
" val_running_loss = 0\n",
99+
"\n",
100+
" for trn_i, trn_sample in enumerate(train_loader):\n",
101+
"\n",
102+
" # move all images and labels to the gpu if the system has a gpu\n",
103+
" trn_img = trn_sample['image']\n",
104+
" trn_key_pts = trn_sample['key_points']\n",
105+
" # flatten key points\n",
106+
" trn_key_pts = trn_key_pts.view(trn_key_pts.shape[0], -1)\n",
107+
" if device == 'cuda':\n",
108+
" trn_key_pts = trn_key_pts.type(torch.cuda.FloatTensor)\n",
109+
" trn_img = trn_img.type(torch.cuda.FloatTensor)\n",
110+
" trn_img = trn_img.to(device)\n",
111+
" trn_key_pts = trn_key_pts.to(device)\n",
112+
"\n",
113+
" # sets optimizer to zero grad\n",
114+
" optimizer.zero_grad()\n",
115+
"\n",
116+
" # log probability\n",
117+
" trn_log_ps = model(trn_img)\n",
118+
"\n",
119+
" # computing loss\n",
120+
" loss_trn = criterion(trn_log_ps, trn_key_pts)\n",
121+
"\n",
122+
" # backward propagation\n",
123+
" loss_trn.backward()\n",
124+
"\n",
125+
" # optimize the loss\n",
126+
" optimizer.step()\n",
127+
"\n",
128+
" # adding training loss at each image\n",
129+
" trn_running_loss += loss_trn.item()\n",
130+
"\n",
131+
" else:\n",
132+
" with torch.no_grad:\n",
133+
" # setting model to evaluation mode so that dropout does not ocur while we train the model\n",
134+
" model.eval()\n",
135+
"\n",
136+
" for val_i, val_sample in enumerate(val_loader):\n",
137+
" val_img = val_sample['images']\n",
138+
" val_label = val_sample['key_points']\n",
139+
" # flatten key points\n",
140+
" val_label = val_label.view(val_label.shape[0], -1)\n",
141+
" if device == 'cuda':\n",
142+
" val_label = val_label.type(torch.cuda.FloatTensor)\n",
143+
" val_img = val_img.type(torch.cuda.FloatTensor)\n",
144+
" val_img = val_img.to(device)\n",
145+
" val_label = val_label.to(device)\n",
146+
"\n",
147+
" loss_val = criterion(val_img, val_label)\n",
148+
"\n",
149+
" val_running_loss += loss_val.item()\n",
150+
"\n",
151+
" for test_i, test_sample in test_loader:\n",
152+
" test_img = test_sample['images']\n",
153+
" test_label = test_sample['key_points']\n",
154+
" # flatten key points\n",
155+
" test_label = test_label.view(test_label.shape[0], -1)\n",
156+
" if device == 'cuda':\n",
157+
" test_label = test_label.type(torch.cuda.FloatTensor)\n",
158+
" test_img = test_img.type(torch.cuda.FloatTensor)\n",
159+
" test_img = test_img.to(device)\n",
160+
" test_label = test_label.to(device)\n",
161+
" \n",
162+
" loss_test = criterion(test_img, test_label)\n",
163+
"\n",
164+
" test_running_loss += loss_test.item()\n",
165+
"\n",
166+
" trn_loss = trn_running_loss / len(train_loader)\n",
167+
" val_loss = val_running_loss / len(val_loader)\n",
168+
" test_loss = test_running_loss / len(test_loader)\n",
169+
"\n",
170+
" trn_loss_list = trn_loss_list.append(trn_loss)\n",
171+
" val_loss_list = val_loss_list.append(val_loss)\n",
172+
" test_loss_list = test_loss_list.append(test_loss)\n",
173+
"\n",
174+
" print(f'epochs: {i+epoch} / {epochs}, training_loss: {trn_loss}, val_loss: {val_loss}, test_loss: {test_loss}')\n",
175+
"\n",
176+
" # setting model to training mode \n",
177+
" model.train()\n",
178+
"\n",
179+
" if val_loss <= val_min_loss:\n",
180+
" print(f'validation loss decreased {val_loss} ---> {val_min_loss}. Saving model...')\n",
181+
" torch.save(model.state_dict(), 'model.pth')\n",
182+
" val_min_loss = val_loss\n",
183+
"\n"
184+
]
185+
}
186+
],
187+
"metadata": {
188+
"kernelspec": {
189+
"display_name": "Python 3",
190+
"language": "python",
191+
"name": "python3"
192+
},
193+
"language_info": {
194+
"codemirror_mode": {
195+
"name": "ipython",
196+
"version": 3
197+
},
198+
"file_extension": ".py",
199+
"mimetype": "text/x-python",
200+
"name": "python",
201+
"nbconvert_exporter": "python",
202+
"pygments_lexer": "ipython3",
203+
"version": "3.6.7"
204+
}
205+
},
206+
"nbformat": 4,
207+
"nbformat_minor": 2
208+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
write this command in terminal to grab the dataset: wget -P ./data/ https://s3.amazonaws.com/video.udacity-data.com/topher/2018/May/5aea1b91_train-test-data/train-test-data.zip

0 commit comments

Comments
 (0)