Delete UpsideDown.py
Browse files- UpsideDown.py +0 -328
UpsideDown.py
DELETED
@@ -1,328 +0,0 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
# 1. Deep Learning for Vision
|
4 |
-
|
5 |
-
**Upside down detector**: Train a model to detect if images are upside down
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
Synthetically turn some of images upside down. Create a training and test set.
|
10 |
-
"""
|
11 |
-
|
12 |
-
import os
|
13 |
-
def makedir(path):
|
14 |
-
if not os.path.exists(path):
|
15 |
-
os.makedirs(path)
|
16 |
-
|
17 |
-
makedir('./without_mask')
|
18 |
-
|
19 |
-
# from google.colab import drive
|
20 |
-
# drive.mount('/content/drive')
|
21 |
-
|
22 |
-
# Commented out IPython magic to ensure Python compatibility.
|
23 |
-
### WRITE YOUR CODE TO TRAIN THE MODEL HERE
|
24 |
-
|
25 |
-
#Imports
|
26 |
-
import os
|
27 |
-
import sys
|
28 |
-
from glob import glob
|
29 |
-
import torch
|
30 |
-
import torchvision
|
31 |
-
|
32 |
-
import numpy as np
|
33 |
-
import datetime as dt
|
34 |
-
|
35 |
-
|
36 |
-
import torch.nn as nn
|
37 |
-
import torch.nn.functional as F
|
38 |
-
import matplotlib.pyplot as plt
|
39 |
-
import cv2
|
40 |
-
|
41 |
-
from PIL import Image
|
42 |
-
from torch.utils.data import Dataset
|
43 |
-
from torch.autograd import Variable
|
44 |
-
from torch.optim import lr_scheduler
|
45 |
-
|
46 |
-
from torch.utils.data import Dataset, DataLoader
|
47 |
-
from torch.utils.data.sampler import SubsetRandomSampler
|
48 |
-
from torchvision import transforms, datasets, models
|
49 |
-
from os import listdir, makedirs, getcwd, remove
|
50 |
-
from os.path import isfile, join, abspath, exists, isdir, expanduser
|
51 |
-
from torchvision.transforms.functional import vflip
|
52 |
-
from PIL import Image
|
53 |
-
from torch.utils.data import random_split
|
54 |
-
from PIL import ImageFile
|
55 |
-
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
56 |
-
np.random.seed(42)
|
57 |
-
|
58 |
-
|
59 |
-
# %matplotlib inline
|
60 |
-
|
61 |
-
|
62 |
-
data_path = "./without_mask"
|
63 |
-
images = glob("./without_mask/*.png", recursive=True)
|
64 |
-
|
65 |
-
import shutil
|
66 |
-
!rm -rf ./train_data
|
67 |
-
!rm -rf ./test_data
|
68 |
-
makedir('./train_data')
|
69 |
-
makedir('./test_data')
|
70 |
-
split = int(np.floor(len(images) * .8))
|
71 |
-
np.random.seed(22)
|
72 |
-
N = len(images)
|
73 |
-
K = split
|
74 |
-
|
75 |
-
dst_dir = 'train_data'
|
76 |
-
for pngfile in images[:split]:
|
77 |
-
shutil.copy(pngfile, dst_dir)
|
78 |
-
dst_dir = 'test_data'
|
79 |
-
for pngfile in images[split:]:
|
80 |
-
shutil.copy(pngfile, dst_dir)
|
81 |
-
|
82 |
-
train_path = './content/train_data'
|
83 |
-
test_path = '/content/test_data'
|
84 |
-
|
85 |
-
# Transformations for both the training and testing data
|
86 |
-
mean=[0.5894, 0.4595, 0.3966]
|
87 |
-
std=[0.2404, 0.2020, 0.1959]
|
88 |
-
|
89 |
-
# Do data transforms here, Try many others
|
90 |
-
|
91 |
-
train_transforms = transforms.Compose([transforms.Resize(500),
|
92 |
-
transforms.ToTensor(),
|
93 |
-
transforms.Normalize(mean,std)])
|
94 |
-
|
95 |
-
test_transforms = transforms.Compose([ transforms.Resize(500),
|
96 |
-
transforms.ToTensor(),
|
97 |
-
transforms.Normalize(mean,std)])
|
98 |
-
|
99 |
-
class Dataset(Dataset):
|
100 |
-
def __init__(self, path, transform=None):
|
101 |
-
self.file_list = glob(path+"*.png", recursive=True)
|
102 |
-
print(self.file_list)
|
103 |
-
self.transform = transform
|
104 |
-
|
105 |
-
files = []
|
106 |
-
split = int(np.floor(len(self.file_list) * .5))
|
107 |
-
|
108 |
-
N = len(self.file_list)
|
109 |
-
K = split # K zeros, N-K ones
|
110 |
-
indices = np.array([0] * K + [1] * (N-K))
|
111 |
-
np.random.shuffle(indices)
|
112 |
-
print(indices)
|
113 |
-
|
114 |
-
final_pth = path
|
115 |
-
for idx,img_pth in enumerate(self.file_list):
|
116 |
-
if indices[idx]:
|
117 |
-
img = Image.open(img_pth)
|
118 |
-
img = vflip(img)
|
119 |
-
img_name = img_pth.split('/')[-1]
|
120 |
-
img.save(final_pth+img_name, format="png")
|
121 |
-
files.append([indices[idx],final_pth+img_name])
|
122 |
-
else:
|
123 |
-
img = Image.open(img_pth)
|
124 |
-
img_name = img_pth.split('/')[-1]
|
125 |
-
img.save(final_pth+img_name, format="png")
|
126 |
-
files.append([indices[idx],final_pth+img_name])
|
127 |
-
self.file_list = files
|
128 |
-
print(self.file_list)
|
129 |
-
files = None
|
130 |
-
|
131 |
-
|
132 |
-
def __len__(self):
|
133 |
-
return len(self.file_list)
|
134 |
-
|
135 |
-
def __getitem__(self, idx):
|
136 |
-
fileName = self.file_list[idx][1]
|
137 |
-
classCategory = self.file_list[idx][0]
|
138 |
-
im = Image.open(fileName)
|
139 |
-
if self.transform:
|
140 |
-
im = self.transform(im)
|
141 |
-
|
142 |
-
return im.view(3, 500, 500), classCategory
|
143 |
-
|
144 |
-
train_data = Dataset('./train_data/', transform=train_transforms)
|
145 |
-
test_data = Dataset('./test_data/', transform=test_transforms)
|
146 |
-
|
147 |
-
train_loader = torch.utils.data.DataLoader(train_data, batch_size=32)
|
148 |
-
|
149 |
-
test_loader = torch.utils.data.DataLoader(test_data, batch_size=32)
|
150 |
-
|
151 |
-
|
152 |
-
# Device configuration
|
153 |
-
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
154 |
-
|
155 |
-
# Define Models
|
156 |
-
|
157 |
-
# Define Models
|
158 |
-
|
159 |
-
class Classifier(nn.Module):
|
160 |
-
def __init__(self, num_classes):
|
161 |
-
super(Classifier, self).__init__()
|
162 |
-
# Block 1
|
163 |
-
self.conv1 = nn.Conv2d(in_channels = 3, out_channels = 32, kernel_size = 5, stride = 2, padding = 2)
|
164 |
-
self.relu1 = nn.ReLU()
|
165 |
-
self.maxpool1 = nn.MaxPool2d(kernel_size = 2)
|
166 |
-
|
167 |
-
#Block 2
|
168 |
-
self.conv2 = nn.Conv2d(in_channels = 32, out_channels = 64, kernel_size = 5, stride = 2, padding = 2)
|
169 |
-
self.relu2 = nn.ReLU()
|
170 |
-
self.maxpool2 = nn.MaxPool2d(kernel_size=2)
|
171 |
-
|
172 |
-
#Block 3
|
173 |
-
self.conv3 = nn.Conv2d(in_channels = 64, out_channels = 64, kernel_size = 3, stride = 2, padding = 2)
|
174 |
-
self.relu3 = nn.ReLU()
|
175 |
-
self.maxpool3 = nn.MaxPool2d(kernel_size=2)
|
176 |
-
|
177 |
-
#Block 4
|
178 |
-
self.conv4 = nn.Conv2d(in_channels = 64, out_channels = 64, kernel_size = 3, stride = 2, padding = 2)
|
179 |
-
self.relu4 = nn.ReLU()
|
180 |
-
self.maxpool4 = nn.MaxPool2d(kernel_size=2)
|
181 |
-
|
182 |
-
#Block 5
|
183 |
-
self.conv5 = nn.Conv2d(in_channels = 64, out_channels = 32, kernel_size = 3, stride = 2, padding = 2)
|
184 |
-
self.relu5 = nn.ReLU()
|
185 |
-
self.maxpool5 = nn.MaxPool2d(kernel_size=2)
|
186 |
-
|
187 |
-
# last fully-connected layer
|
188 |
-
self.fc = nn.Linear(32, num_classes)
|
189 |
-
self.dropout = nn.Dropout(.1)
|
190 |
-
|
191 |
-
|
192 |
-
def forward(self, input):
|
193 |
-
|
194 |
-
x = self.maxpool1(self.relu1(self.conv1(input)))
|
195 |
-
x = self.dropout(x)
|
196 |
-
x = self.maxpool2(self.relu2(self.conv2(x)))
|
197 |
-
x = self.dropout(x)
|
198 |
-
x = self.maxpool3(self.relu3(self.conv3(x)))
|
199 |
-
x = self.dropout(x)
|
200 |
-
x = self.maxpool4(self.relu4(self.conv4(x)))
|
201 |
-
x = self.dropout(x)
|
202 |
-
x = self.maxpool5(self.relu5(self.conv5(x)))
|
203 |
-
x = self.dropout(x)
|
204 |
-
|
205 |
-
x = x.view(x.size(0), -1)
|
206 |
-
x = self.fc(x)
|
207 |
-
return x
|
208 |
-
|
209 |
-
def train(model, criterion, data_loader, optimizer, num_epochs):
|
210 |
-
"""Simple training loop for a PyTorch model."""
|
211 |
-
|
212 |
-
# Make sure model is in training mode.
|
213 |
-
model.train()
|
214 |
-
|
215 |
-
# Move model to the device (CPU or GPU).
|
216 |
-
model.to(device)
|
217 |
-
|
218 |
-
# Exponential moving average of the loss.
|
219 |
-
ema_loss = None
|
220 |
-
|
221 |
-
print('----- Training Loop -----')
|
222 |
-
# Loop over epochs.
|
223 |
-
for epoch in range(num_epochs):
|
224 |
-
|
225 |
-
# Loop over data.
|
226 |
-
for batch_idx, (features, target) in enumerate(data_loader):
|
227 |
-
|
228 |
-
# Forward pass.
|
229 |
-
output = model(features.to(device))
|
230 |
-
output = output.squeeze()
|
231 |
-
target = target.float()
|
232 |
-
loss = criterion(output.to(device), target.to(device))
|
233 |
-
|
234 |
-
# Backward pass.
|
235 |
-
optimizer.zero_grad()
|
236 |
-
loss.backward()
|
237 |
-
optimizer.step()
|
238 |
-
|
239 |
-
# NOTE: It is important to call .item() on the loss before summing.
|
240 |
-
if ema_loss is None:
|
241 |
-
ema_loss = loss.item()
|
242 |
-
else:
|
243 |
-
ema_loss += (loss.item() - ema_loss) * 0.01
|
244 |
-
|
245 |
-
# Print out progress the end of epoch.
|
246 |
-
print('Epoch: {} \tLoss: {:.6f}'.format(epoch, ema_loss))
|
247 |
-
|
248 |
-
def test(model, data_loader):
|
249 |
-
"""Measures the accuracy of a model on a data set."""
|
250 |
-
# Make sure the model is in evaluation mode.
|
251 |
-
model.eval()
|
252 |
-
correct = 0
|
253 |
-
print('----- Model Evaluation -----')
|
254 |
-
# We do not need to maintain intermediate activations while testing.
|
255 |
-
with torch.no_grad():
|
256 |
-
|
257 |
-
# Loop over test data.
|
258 |
-
for features, target in data_loader:
|
259 |
-
|
260 |
-
# Forward pass.
|
261 |
-
output = model(features.to(device))
|
262 |
-
|
263 |
-
# Get the label corresponding to the highest predicted probability.
|
264 |
-
# pred = output.argmax(dim=1, keepdim=True)
|
265 |
-
pred = torch.where(output>0.5,torch.ones(output.shape),torch.zeros(output.shape))
|
266 |
-
# Count number of correct predictions.
|
267 |
-
correct += pred.cpu().eq(target.view_as(pred)).sum().item()
|
268 |
-
|
269 |
-
# Print test accuracy.
|
270 |
-
percent = 100. * correct / len(data_loader.dataset)
|
271 |
-
print(f'Test accuracy: {correct} / {len(data_loader.dataset)} ({percent:.0f}%)')
|
272 |
-
torch.save(model.state_dict(), 'model.ckpt')
|
273 |
-
return percent
|
274 |
-
|
275 |
-
num_epochs = 10
|
276 |
-
model = Classifier(1)
|
277 |
-
criterion = torch.nn.BCEWithLogitsLoss()
|
278 |
-
optimizer = torch.optim.Adam(model.parameters(), lr=1e-2)
|
279 |
-
|
280 |
-
train(model, criterion, train_loader, optimizer, num_epochs=num_epochs)
|
281 |
-
|
282 |
-
test(model, test_loader)
|
283 |
-
|
284 |
-
test(model, train_loader)
|
285 |
-
|
286 |
-
"""**Write up**:
|
287 |
-
* Link to the model on Hugging Face Hub:
|
288 |
-
* Include some examples of misclassified images. Please explain what you might do to improve your model's performance on these images in the future (you do not need to impelement these suggestions)
|
289 |
-
"""
|
290 |
-
|
291 |
-
# Commented out IPython magic to ensure Python compatibility.
|
292 |
-
|
293 |
-
# model1 = Classifier(1)
|
294 |
-
# model1.load_state_dict(torch.load('./model.ckpt'))
|
295 |
-
# model1.eval()
|
296 |
-
model.eval() # make sure the model is in evaluation mode
|
297 |
-
classes = ['no_flip','flip']
|
298 |
-
temp = []
|
299 |
-
def false_predictions(data):
|
300 |
-
for i in range(len(data)):
|
301 |
-
tensor, target = data[i]
|
302 |
-
|
303 |
-
if device == "cuda":
|
304 |
-
tensor = tensor.cuda()
|
305 |
-
|
306 |
-
prediction = model(tensor.unsqueeze(0)).item()
|
307 |
-
if prediction>.5:
|
308 |
-
prediction = 1
|
309 |
-
else:
|
310 |
-
prediction = 0
|
311 |
-
|
312 |
-
if prediction != target:
|
313 |
-
print(
|
314 |
-
"Img id=%d. Excpected class %s, but predicted class %s."
|
315 |
-
# % (
|
316 |
-
i,
|
317 |
-
classes[target],
|
318 |
-
classes[prediction],
|
319 |
-
)
|
320 |
-
)
|
321 |
-
img = Image.open(data.file_list[i][1])
|
322 |
-
img = transforms.Resize(224)(img)
|
323 |
-
display.display(img)
|
324 |
-
|
325 |
-
false_predictions(train_data)
|
326 |
-
|
327 |
-
false_predictions(test_data)
|
328 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|