-
Notifications
You must be signed in to change notification settings - Fork 2
/
AdvancedLinearRegression.py
143 lines (125 loc) · 4.89 KB
/
AdvancedLinearRegression.py
1
2
3
4
5
6
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import os
import torch
import numpy as np
from PIL import Image
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
os.environ["CUDA_VISIBLE_DEVICES"] = "1,2,3"
device = 'cuda:0'
datasetFolder = '/home/cqiuac/Downloads/img_align_celeba'
mySmileDataPath = './mysmiledataPooling.t7'
loader = transforms.Compose([transforms.ToTensor()])
unloader = transforms.ToPILImage()
dtype = torch.float
trainsetPercent = 3 / 4
poolingSize = [100, 100]
def image_loader(image_name):
image = Image.open(image_name).convert('L')
image = loader(image).unsqueeze(0)
return image.to(device, dtype)
def pca(X, k=10000):
X_mean = torch.mean(X, 1).unsqueeze(1)
X = X - X_mean
U, S, V = torch.svd(X)
S[k:] = 0
return [email protected](S)@V.t()
print('===> Loading Data...')
if os.path.exists(mySmileDataPath):
data = torch.load(mySmileDataPath)
A = data['A']
bTest = data['bTest']
XTest = data['XTest']
lossTrain = data['lossTrain']
imgNameTestList = data['imgNameTestList']
print('Loading successful')
else:
f = open("list_attr_celeba.txt")
Number_Of_Image = len(f.readlines())
Number_Of_Used_Image = int(Number_Of_Image / 10)
f.seek(0)
line = f.readline()
line = f.readline()
Smile_Indicator_Index = line.split().index('Smiling')
line = f.readline()
array = line.split()
total_Pixel_Number_Per_Img = np.prod(poolingSize)
b = torch.zeros(1, Number_Of_Used_Image, dtype=dtype, device=device)
X = torch.zeros(
total_Pixel_Number_Per_Img,
Number_Of_Used_Image,
dtype=dtype,
device=device)
imgNameList = []
lineInd = 0
Number_Of_Smile_Image_Choosen = 0
Number_Of_None_Smile_Image_Choosen = 0
lastlineInd = -1
poolingOp = torch.nn.AdaptiveMaxPool2d((100, 100))
while line and Number_Of_Smile_Image_Choosen + \
Number_Of_None_Smile_Image_Choosen < Number_Of_Used_Image:
array = line.split()
if int(array[Smile_Indicator_Index]
) == 1 and Number_Of_Smile_Image_Choosen < Number_Of_Used_Image / 2:
Number_Of_Smile_Image_Choosen += 1
img = image_loader(os.path.join(datasetFolder, array[0]))
img = poolingOp(img)
imgNameList.append(array[0])
b[0, lineInd] = int(array[Smile_Indicator_Index])
X[:, lineInd] = img.flatten()
lineInd += 1
if int(array[Smile_Indicator_Index]) == - \
1 and Number_Of_None_Smile_Image_Choosen < Number_Of_Used_Image / 2:
Number_Of_None_Smile_Image_Choosen += 1
img = image_loader(os.path.join(datasetFolder, array[0]))
img = poolingOp(img)
imgNameList.append(array[0])
b[0, lineInd] = int(array[Smile_Indicator_Index])
X[:, lineInd] = img.flatten()
lineInd += 1
line = f.readline()
if lineInd % 1000 == 0 and lastlineInd != lineInd:
print('processed:', lineInd, '/', Number_Of_Used_Image)
lastlineInd = lineInd
print('===> Doing PCA...')
X = pca(X)
bTrain = b[0, :int(trainsetPercent * Number_Of_Used_Image)].t()
XTrain = X[:, :int(trainsetPercent * Number_Of_Used_Image)].t()
bTest = b[0, int(trainsetPercent * Number_Of_Used_Image):].t()
XTest = X[:, int(trainsetPercent * Number_Of_Used_Image):].t()
A, LU = torch.lstsq(bTrain.cpu(), XTrain.cpu())
A, LU = [x.cuda() for x in [A, LU]]
A = A[:np.prod(poolingSize), 0]
bPredictedOri = XTrain @ A
lossTrain = torch.mean(torch.abs(bPredictedOri - bTrain))
imgNameTestList = imgNameList[int(trainsetPercent * Number_Of_Used_Image):]
print('===> Saving models...')
torch.save({
'A': A,
'bTest': bTest,
'XTest': XTest,
'lossTrain': lossTrain,
'imgNameTestList': imgNameTestList,
}, './mysmiledataPooling.t7')
bPredicted = XTest@A
loss = torch.mean(torch.abs(bPredicted - bTest))
lossDiscrete = torch.mean(((bPredicted / bTest) > 0).float())
choosenIndexList = (torch.rand(5) * XTest.size()[0]).int()
print()
for i in range(choosenIndexList.size()[0]):
choosenIndex = choosenIndexList[i]
img = Image.open(os.path.join(datasetFolder,
imgNameTestList[choosenIndex.item()]))
plt.figure()
plt.imshow(img)
plt.show()
if bPredicted[choosenIndex.item()] >= 0:
print('Yes, I guess he is smiling.')
else:
print('No, I guess he is not smiling.')
if bTest[choosenIndex.item()] / bPredicted[choosenIndex.item()] >= 0:
print('You guess correctly.\n')
else:
print('You did not guess correctly.\n')
print('Accuracy on backtesting:', lossDiscrete.item() * 100, '%')
print('Loss on backtesting:', loss.item())
print('Loss on Trainingset:', lossTrain.item())