-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_training.py
72 lines (54 loc) · 1.45 KB
/
data_training.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
import os
import numpy as np
import cv2
import tensorflow
from tensorflow.keras.utils import to_categorical
from keras.layers import Input,Dense
from keras.models import Model
is_init=False
size=-1
label=[]
dictionary={}
c=0
for i in os.listdir():
if i.split('.')[-1]=='npy' and not(i.split('.')[0] == 'labels'):
if not(is_init):
is_init=True
X=np.load(i)
size=X.shape[0]
y=np.array([i.split('.')[0]]*size).reshape(-1,1)
else:
X=np.concatenate((X,np.load(i)))
y=np.concatenate((y,np.array([i.split('.')[0]]*size).reshape(-1,1)))
label.append(i.split('.')[0])
dictionary[i.split('.')[0]]=c
c=c+1
#print(dictionary)
#print(label)
for i in range(y.shape[0]):
y[i,0]=dictionary[y[i,0]]
y=np.array(y,dtype='int32')
#print(y)
### hello=0 nope=1 ---> [0,1] ....[1,0]
y=to_categorical(y)
X_new=X.copy()
y_new=y.copy()
counter=0
cnt=np.arange(X.shape[0])
np.random.shuffle(cnt)
for i in cnt:
X_new[counter]=X[i]
y_new[counter]=y[i]
counter=counter+1
#print(y)
#########
#print(y_new)
ip=Input(shape=X.shape[1])
m=Dense(512,activation='relu')(ip)
m=Dense(256,activation='relu')(m)
op=Dense(y.shape[1],activation='softmax')(m)
model=Model(inputs=ip,outputs=op)
model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['acc'])
model.fit(X_new,y_new,epochs=50)
model.save('model.h5')
np.save ('labels.npy', np.array(label))