-
Notifications
You must be signed in to change notification settings - Fork 13
/
matrix_test+train.py
112 lines (80 loc) · 3.14 KB
/
matrix_test+train.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
import os
from PIL import Image
import numpy as np
#Directory containing images you wish to convert
input_dir = "/Users/14/Documents/deep-learning-project/data/output_images/"
directories = os.listdir(input_dir)
index = 0
index2 = 0
index_test = 0
index2_test = 0
for folder in directories[0:500]:
#Ignoring .DS_Store dir
if folder == '.DS_Store':
pass
else:
print folder
folder2 = os.listdir(input_dir + '/' + folder)
index += 1
len_images = len(folder2)
len_images80 = len_images * 80 #Getting index of image on the 80% mark
len_images20 = len_images * 20 #Getting index of image on the 20% mark
#Iterating through first 80% of images in folder for train data
for image in folder2[0:int(len_images80)]:
if image == ".DS_Store":
pass
else:
index2 += 1
im = Image.open(input_dir+"/"+folder+"/"+image) #Opening image
im = (np.array(im)) #Converting to numpy array
try:
r = im[:,:,0] #Slicing to get R data
g = im[:,:,1] #Slicing to get G data
b = im[:,:,2] #Slicing to get B data
if index2 != 1:
new_array = np.array([[r] + [g] + [b]], np.uint8) #Creating array with shape (3, 100, 100)
out = np.append(out, new_array, 0) #Adding new image to array shape of (x, 3, 100, 100) where x is image number
elif index2 == 1:
out = np.array([[r] + [g] + [b]], np.uint8) #Creating array with shape (3, 100, 100)
if index == 1 and index2 == 1:
index_array = np.array([[index]])
else:
new_index_array = np.array([[index]], np.int8)
index_array = np.append(index_array, new_index_array, 0)
except Exception as e:
print e
print "Removing image" + image
os.remove(input_dir+"/"+folder+"/"+image)
#Iterating throught last 20% of image in folder for test data
for image in folder2[len_images-int(len_images20):len_images]:
if image == ".DS_Store":
pass
else:
index2_test += 1
im = Image.open(input_dir+"/"+folder+"/"+image) #Opening image
im = (np.array(im)) #Converting to numpy array
try:
r = im[:,:,0] #Slicing to get R data
g = im[:,:,1] #Slicing to get G data
b = im[:,:,2] #Slicing to get B data
if index2_test != 1:
new_array_test = np.array([[r] + [g] + [b]], np.uint8) #Creating array with shape (3, 100, 100)
out_test = np.append(out_test, new_array_test, 0) #Adding new image to array shape of (x, 3, 100, 100) where x is image number
elif index2_test == 1:
out_test = np.array([[r] + [g] + [b]], np.uint8) #Creating array with shape (3, 100, 100)
if index == 1 and index2_test == 1:
index_array_test = np.array([[index]])
else:
new_index_array_test = np.array([[index]], np.int8)
index_array_test = np.append(index_array_test, new_index_array_test, 0)
except Exception as e:
print e
print "Removing image" + image
os.remove(input_dir+"/"+folder+"/"+image)
print index
print index2
print index2_test
np.save('X_train.npy', out) #Saving train image arrays
np.save('Y_train.npy', index_array) #Saving train labels
np.save('X_test.npy', out_test) #Saving test image arrays
np.save('Y_test.npy', index_array_test) #Saving test labels