-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathORL.py
119 lines (61 loc) · 2.74 KB
/
ORL.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
'''
Load data from ORL dataset
Please rename the pictures in the form:
'si_j.bmp'
where i is the label,j the number of faces
'''
from PIL import Image
from keras.preprocessing.image import load_img, save_img, img_to_array
import numpy as np
from six.moves import range
from keras import backend as K
'''
def load_data(img_nrows=92, img_ncols=92):
if K.image_data_format() == 'channels_first':
x_train = np.zeros(shape=(360, 1, img_nrows, img_ncols), dtype='float32')
x_test = np.zeros(shape=(40, 1, img_nrows, img_ncols), dtype='float32')
else:
x_train = np.zeros(shape=(360, img_nrows, img_ncols, 1), dtype='float32')
x_test = np.zeros(shape=(40, img_nrows, img_ncols, 1), dtype='float32')
y_train = np.zeros(shape=(360,), dtype='int32')
y_test = np.zeros(shape=(40,), dtype='int32')
for i in range(40):
for j in range(9):
img_path = '.\ORL\s{:}_{:}.bmp'.format(i+1,j+1)
img = Image.open(img_path)
img = img.resize((img_nrows, img_ncols),Image.ANTIALIAS)
img = img.convert('L')
img = img_to_array(img)
x_train[i*9+j, :, :, :] = img
y_train[i*9+j] = i
img_path = '.\ORL\s{:}_{:}.bmp'.format(i+1,10)
img = Image.open(img_path)
img = img.resize((img_nrows, img_ncols),Image.ANTIALIAS)
img = img.convert('L')
img = img_to_array(img)
x_test[i, :, :, :] = img
y_test[i] = i
return (x_train, y_train), (x_test, y_test)
'''
def load_data(img_nrows=92, img_ncols=92):
if K.image_data_format() == 'channels_first':
x_train = np.zeros(shape=(360, 3, img_nrows, img_ncols), dtype='float32')
x_test = np.zeros(shape=(40, 3, img_nrows, img_ncols), dtype='float32')
else:
x_train = np.zeros(shape=(360, img_nrows, img_ncols, 3), dtype='float32')
x_test = np.zeros(shape=(40, img_nrows, img_ncols, 3), dtype='float32')
y_train = np.zeros(shape=(360,), dtype='int32')
y_test = np.zeros(shape=(40,), dtype='int32')
for i in range(40):
for j in range(9):
img_path = '.\ORL\s{:}_{:}.bmp'.format(i+1,j+1)
img = load_img(img_path, target_size=(img_nrows, img_ncols))
img = img_to_array(img)
x_train[i*9+j, :, :, :] = img
y_train[i*9+j] = i
img_path = '.\ORL\s{:}_{:}.bmp'.format(i+1,10)
img = load_img(img_path, target_size=(img_nrows, img_ncols))
img = img_to_array(img)
x_test[i, :, :, :] = img
y_test[i] = i
return (x_train, y_train), (x_test, y_test)