262 lines
No EOL
7.7 KiB
Python
262 lines
No EOL
7.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""AlexNet_pytorch.ipynb
|
|
|
|
Automatically generated by Colaboratory.
|
|
|
|
Original file is located at
|
|
https://colab.research.google.com/drive/1iafZNxt1THDq6WRYvmYPwGbDmSQFfj9m
|
|
|
|
# Dataset creation section
|
|
"""
|
|
|
|
from __future__ import print_function, division
|
|
import os
|
|
import torch
|
|
import pandas as pd
|
|
from skimage import io, transform
|
|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
from torch.utils.data import Dataset, DataLoader
|
|
import torchvision.transforms as transforms
|
|
import torchvision
|
|
import cv2
|
|
from sklearn.model_selection import train_test_split
|
|
|
|
|
|
# Ignore warnings
|
|
import warnings
|
|
warnings.filterwarnings("ignore")
|
|
|
|
plt.ion() # interactive mode
|
|
|
|
from google.colab import drive
|
|
drive.mount('/content/drive')
|
|
|
|
data = pd.read_csv('/content/drive/MyDrive/insa 5/Datasets/celebA/labels.csv',nrows=4999)
|
|
data = data[["image_id","Smiling"]]
|
|
data= data.replace(-1,0)
|
|
pd.set_option("display.max_rows", 20, "display.max_columns", 20)
|
|
train_set,test_set=train_test_split(data,test_size=0.25)
|
|
print(train_set)
|
|
|
|
class ImageDataset(Dataset):
|
|
def __init__(self,csv,img_folder,transform):
|
|
self.csv=csv
|
|
self.transform=transform
|
|
self.img_folder=img_folder
|
|
|
|
self.image_names=self.csv[:]['image_id']
|
|
self.labels=np.array(self.csv.drop(['image_id'], axis=1))
|
|
|
|
#The __len__ function returns the number of samples in our dataset.
|
|
def __len__(self):
|
|
return len(self.image_names)
|
|
|
|
def __getitem__(self,index):
|
|
|
|
image=cv2.imread(self.img_folder+self.image_names.iloc[index])
|
|
image=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
|
|
image=self.transform(image)
|
|
targets=self.labels[index]
|
|
|
|
sample = {'image': image,'labels':targets}
|
|
|
|
#return sample
|
|
return image,targets
|
|
|
|
train_transform = transforms.Compose([
|
|
transforms.ToPILImage(),
|
|
transforms.Resize((224, 224)),
|
|
transforms.ToTensor()])
|
|
#,transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
|
|
test_transform =transforms.Compose([
|
|
transforms.ToPILImage(),
|
|
transforms.Resize((224, 224)),
|
|
transforms.ToTensor()])
|
|
|
|
train_dataset=ImageDataset(train_set,'/content/drive/MyDrive/insa 5/Datasets/celebA/images/',train_transform)
|
|
test_dataset=ImageDataset(test_set,'/content/drive/MyDrive/insa 5/Datasets/celebA/images/',test_transform)
|
|
|
|
BATCH_SIZE=16
|
|
|
|
train_dataloader = DataLoader(
|
|
train_dataset,
|
|
batch_size=BATCH_SIZE,
|
|
shuffle=True
|
|
)
|
|
|
|
test_dataloader = DataLoader(
|
|
test_dataset,
|
|
batch_size=BATCH_SIZE,
|
|
shuffle=True
|
|
)
|
|
|
|
sample = next(iter(train_dataloader))
|
|
input, label = sample
|
|
input = input.view(BATCH_SIZE, -1)
|
|
#print(sample[input])
|
|
print(input.size())
|
|
|
|
def imshow(inp, title=None):
|
|
"""imshow for Tensor."""
|
|
inp = inp.numpy().transpose((1, 2, 0))
|
|
inp = np.clip(inp, 0, 1)
|
|
plt.imshow(inp)
|
|
|
|
|
|
# Get a batch of training data
|
|
images, labels = next(iter(test_dataloader))
|
|
|
|
# Make a grid from batch
|
|
output = torchvision.utils.make_grid(images)
|
|
|
|
imshow(output)
|
|
|
|
"""# Training section"""
|
|
|
|
import torch
|
|
import torch.nn as nn
|
|
import torch.nn.functional as F
|
|
AlexNet_model=torch.hub.load('pytorch/vision:v0.6.0', 'alexnet', pretrained=True)
|
|
AlexNet_model.eval()
|
|
print(AlexNet_model)
|
|
AlexNet_model.classifier[4]=nn.Linear(4096,1024)
|
|
AlexNet_model.classifier[6]=nn.Linear(1024,2)
|
|
AlexNet_model.eval()
|
|
print(AlexNet_model)
|
|
|
|
class Net(nn.Module):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.conv1 = nn.Conv2d(3, 96, 11, stride=4)
|
|
self.pool = nn.MaxPool2d(3, stride = 2)
|
|
self.conv2 = nn.Conv2d(96, 256, 5, padding=2)
|
|
self.conv3 = nn.Conv2d(256, 384, 3, padding=1)
|
|
self.conv4 = nn.Conv2d(384, 384, 3, padding=1)
|
|
self.conv5 = nn.Conv2d(384, 256, 3, padding=1)
|
|
self.fc1 = nn.Linear(6400, 4096)
|
|
self.fc2 = nn.Linear(4096, 4096)
|
|
self.fc3 = nn.Linear(4096, 1000)
|
|
self.fc4 = nn.Linear(1000, 2)
|
|
self.dropout = nn.Dropout(0.5)
|
|
|
|
def forward(self, x):
|
|
x = self.pool(F.relu(self.conv1(x)))
|
|
x = self.pool(F.relu(self.conv2(x)))
|
|
x = F.relu(self.conv3(x))
|
|
x = F.relu(self.conv4(x))
|
|
x = F.relu(self.conv5(x))
|
|
x = self.pool(x)
|
|
x = torch.flatten(x, 1) # flatten all dimensions except batch
|
|
x = self.dropout(F.relu(self.fc1(x)))
|
|
x = self.dropout(F.relu(self.fc2(x)))
|
|
x = self.dropout(F.relu(self.fc3(x)))
|
|
x = self.fc4(x)
|
|
return x
|
|
|
|
import torch
|
|
#print(torch.cuda.get_device_name(0))
|
|
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
|
print(device)
|
|
net = AlexNet_model
|
|
net.to(device)
|
|
|
|
import torch.optim as optim
|
|
#criterion = nn.BCELoss()
|
|
criterion = nn.CrossEntropyLoss()
|
|
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
|
|
|
|
for epoch in range(10): # loop over the dataset multiple times
|
|
#print(epoch)
|
|
running_loss = 0.0
|
|
for i, data in enumerate(train_dataloader, 0):
|
|
#print(i)
|
|
# get the inputs; data is a list of [inputs, labels]
|
|
inputs, labels = data
|
|
inputs, labels = inputs.cuda(), labels.cuda()
|
|
#print(inputs.size())
|
|
#print(labels.flatten())
|
|
# zero the parameter gradients
|
|
optimizer.zero_grad()
|
|
outputs = net(inputs)
|
|
loss = criterion(outputs, labels.flatten())
|
|
loss.backward()
|
|
optimizer.step()
|
|
|
|
|
|
#predicted=[]
|
|
# forward + backward + optimize
|
|
#outputs = net(inputs)
|
|
#predicted = torch.nn.functional.softmax(outputs, dim=0)
|
|
#outputs=outputs.float()
|
|
|
|
#predicted=torch.tensor(predicted)
|
|
#predicted=predicted.float()
|
|
#labels=labels.float()
|
|
#print(predicted.flatten())
|
|
#loss = criterion(predicted.flatten(), labels.flatten())
|
|
#loss.backward()
|
|
#optimizer.step()
|
|
# print statistics
|
|
running_loss += loss.item()
|
|
if i % 10 == 9: # print every 2000 mini-batches
|
|
print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss/10 ))
|
|
running_loss = 0.0
|
|
|
|
print('Finished Training')
|
|
|
|
|
|
|
|
PATH = '/content/drive/MyDrive/Models/saved_AlexNet.pth'
|
|
torch.save(net.state_dict(), PATH)
|
|
|
|
"""# Testing section"""
|
|
|
|
dataiter = iter(train_dataloader)
|
|
images, labels = dataiter.next()
|
|
classes = ("Not Smiling", "Smiling")
|
|
# print images
|
|
imshow(torchvision.utils.make_grid(images))
|
|
print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(16)))
|
|
|
|
PATH = '/content/drive/MyDrive/insa 5/Models/saved_AlexNet.pth'
|
|
net.load_state_dict(torch.load(PATH))
|
|
|
|
|
|
|
|
outputs = net(images.cuda())
|
|
print(outputs)
|
|
|
|
_, predicted = torch.max(outputs, 1)
|
|
|
|
print('Predicted: ', ' '.join('%5s' % classes[predicted[j]]
|
|
for j in range(16)))
|
|
|
|
correct = 0
|
|
total = 0
|
|
predicted = 0
|
|
# since we're not training, we don't need to calculate the gradients for our outputs
|
|
with torch.no_grad():
|
|
for data in test_dataloader:
|
|
images, labels = data
|
|
# calculate outputs by running images through the network
|
|
outputs = net(images.cuda())
|
|
# the class with the highest energy is what we choose as prediction
|
|
_, predicted = torch.max(outputs.data, 1)
|
|
#probabilities = torch.nn.functional.softmax(outputs[0], dim=0)
|
|
#print(probabilities)
|
|
#print("labels.size(0): ",labels.size(0))
|
|
#print("Batch size: ",BATCH_SIZE)
|
|
total += labels.size(0)
|
|
#if probabilities >= 0.5:
|
|
# predicted = 1
|
|
#else:
|
|
# predicted = 0
|
|
correct += (predicted == labels.flatten().cuda()).sum().item()
|
|
#print("Predicted: ", predicted)
|
|
#print("Labels: ", labels.flatten())
|
|
#print("Correct: ", correct)
|
|
print(correct)
|
|
print(total)
|
|
print('Accuracy of the network on the 750 test images: %d %%' % (
|
|
100 * correct / total)) |