-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_data.py
138 lines (99 loc) · 3.36 KB
/
load_data.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
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
from tqdm import tqdm
import random
import pickle
DATADIR = "train"
CATEGORIES = ["ابيض" , "احمر","اخضر","ازرق","اسود","اصفر","برتقاني","بمبي","بني","بيج"]
#print( CATEGORIES.index("Eswed"))
training_data = []
def create_training_data():
for category in CATEGORIES:
path = os.path.join(DATADIR,category) # create path to dogs and cats
class_num = CATEGORIES.index(category) # get the class
for img in os.listdir(path): # iterate over each image
try:
keyp_arr=np.load(os.path.join(path,img))
training_data.append([keyp_arr, class_num]) # add this to our training_data
except Exception as e: # in the interest in keeping the output clean...
pass
#except OSError as e:
# print("OSErrroBad img most likely", e, os.path.join(path,img))
#except Exception as e:
# print("general exception", e, os.path.join(path,img))
create_training_data()
print(len(training_data))
#shuffle data
random.shuffle(training_data)
for sample in training_data[:10]:
print(sample[1])
X = []
y = []
for features,label in training_data:
X.append(features)
y.append(label)
X = np.array(X)
y=np.array(y)
print(X.shape)
#(160, 1, 21, 3)
print(y.shape)
#(160,)
#Let's save this data, so that we don't need to keep calculating it every time we want to play with the neural network model:
pickle_out = open("X.pickle","wb")
pickle.dump(X, pickle_out)
pickle_out.close()
pickle_out = open("y.pickle","wb")
pickle.dump(y, pickle_out)
pickle_out.close()
pickle_in = open("X.pickle","rb")
X = pickle.load(pickle_in)
pickle_in = open("y.pickle","rb")
y = pickle.load(pickle_in)
X = np.reshape(X, (-1, 21,3, 1))
#Same for test
DATADIR = "test"
testing_data = []
def create_testing_data():
for category in CATEGORIES:
path = os.path.join(DATADIR,category) # create path to classes
# print(path)
class_num = CATEGORIES.index(category) # get the class
for img in os.listdir(path): # iterate over each image
try:
keyp_arr=np.load(os.path.join(path,img))
testing_data.append([keyp_arr, class_num]) # add this to our training_data
except Exception as e: # in the interest in keeping the output clean...
pass
#except OSError as e:
# print("OSErrroBad img most likely", e, os.path.join(path,img))
#except Exception as e:
# print("general exception", e, os.path.join(path,img))
create_testing_data()
print(len(testing_data))
#40
random.shuffle(testing_data)
for sample in testing_data[:10]:
print(sample[1])
X_test = []
y_test = []
for features,label in testing_data:
X_test.append(features)
y_test.append(label)
X_test = np.array(X_test).reshape(-1, 21,3, 1)
y_test=np.array(y_test)
print(X_test.shape)
#(40, 21, 3, 1)
print(y_test.shape)
#(40,)
pickle_out = open("X_test.pickle","wb")
pickle.dump(X_test, pickle_out)
pickle_out.close()
pickle_out = open("y_test.pickle","wb")
pickle.dump(y_test, pickle_out)
pickle_out.close()
pickle_in = open("X_test.pickle","rb")
X_test = pickle.load(pickle_in)
pickle_in = open("y_test.pickle","rb")
y_test = pickle.load(pickle_in)