-
Notifications
You must be signed in to change notification settings - Fork 0
/
keras_top.py
89 lines (62 loc) · 2.68 KB
/
keras_top.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
from keras.applications.inception_resnet_v2 import InceptionResNetV2
from keras.preprocessing import image
from keras.models import Model, Sequential
from keras.layers import Dense, GlobalAveragePooling2D,Dropout, Flatten
from keras import backend as K
from keras.preprocessing.image import ImageDataGenerator
from keras.metrics import top_k_categorical_accuracy, sparse_top_k_categorical_accuracy
from keras import applications
import keras
import numpy as np
import tensorflow as tf
#arch = 'resnet'
arch = 'desnet'
top_model_weights_path = 'top_model_'+arch+'_365_test03.h5'
train_data = np.load(arch+'.npy')
#validation_labels = np.load('validation_labels.npy')
nb_train_samples = len(train_data[:,0])
nb_validation_samples = 7120
epochs = 20
batch_size = 200
def to_one_hot(array):
array=array.astype(np.int)
n_values = np.max(array) + 1
return np.eye(n_values)[array]
def top_3_categorical_accuracy(y_true, y_pred, k=3):
return K.mean(K.in_top_k(y_pred, K.argmax(y_true, axis=-1), k))
def train_top_model():
np.random.seed(1)
global train_data
#global validation_labels
#train_data = np.load(arch+'.npy')
#train_labels = np.array([0] * (nb_train_samples / 2) + [1] * (nb_train_samples / 2))
#train_labels have been defined globally
validation_data = np.load(arch+'v.npy')
#validation_labels = np.array([0] * (nb_validation_samples / 2) + [1] * (nb_validation_samples / 2))
# array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
model = Sequential()
#model.add(Flatten(input_shape=train_data.shape[1:]))
model.add(Dropout(0.3, input_shape=(validation_data.shape[1]-1,)))
#model.add(Dense(1000, activation='relu'))
#model.add(Dropout(0.2))
model.add(Dense(80, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy',top_3_categorical_accuracy])
#keras.optimizers.RMSprop(lr=0.0005)
np.random.shuffle(train_data)
np.random.shuffle(validation_data)
train_labels=train_data[:,0]
train_data=train_data[:,1:]
validation_labels=validation_data[:,0]
validation_data=validation_data[:,1:]
#validation_labels=K.one_hot(validation_labels,80)
validation_labels=to_one_hot(validation_labels.flatten())
train_labels=to_one_hot(train_labels.flatten())
model.load_weights(top_model_weights_path)
model.fit(train_data, train_labels,
epochs=epochs,
batch_size=batch_size,
validation_data=(validation_data, validation_labels))
model.save_weights(top_model_weights_path)
model.save('model_'+top_model_weights_path.replace(".h5",".pth"))
#save_features()
train_top_model()