-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransferlearning.py
More file actions
62 lines (51 loc) · 1.95 KB
/
Copy pathtransferlearning.py
File metadata and controls
62 lines (51 loc) · 1.95 KB
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
import numpy as np
import keras
from keras import applications
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Input
from keras.layers import Activation, Dropout, Flatten, Dense, LSTM, Reshape
from keras.layers.normalization import BatchNormalization
from keras.models import Model
from keras.applications.inception_v3 import InceptionV3
import cv2
batch_size = 128
epochs = 120
data_x_train = np.load('rehsaped_tx_learning_data.npy')
data_y = np.load('new_training_data_y.npy')
print(data_x_train.shape)
print(data_y.shape)
data_y_val = data_y[50000:]
data_y_train = data_y[:50000]
data_x_val = data_x_train[50000:]
data_x_train = data_x_train[:50000]
original_model = InceptionV3()
bottleneck_input = original_model.get_layer(index=0).input
bottleneck_output = original_model.get_layer(index=-2).output
bottleneck_model = Model(inputs=bottleneck_input, outputs=bottleneck_output)
for layer in bottleneck_model.layers:
layer.trainable = False
new_model = Sequential()
new_model.add(bottleneck_model)
new_model.add(Dense(1024, activation='relu', input_dim=2048))
new_model.add(Dropout(0.5))
new_model.add(Dense(256, activation='relu'))
new_model.add(Dropout(0.5))
new_model.add(Dense(128, activation='relu'))
new_model.add(Dropout(0.5))
new_model.add(Dense(64, activation='relu'))
new_model.add(Dropout(0.5))
new_model.add(BatchNormalization())
new_model.add(Dense(32, activation='relu'))
new_model.add(Dropout(0.5))
new_model.add(Dense(9, activation='softmax'))
new_model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
new_model.fit(data_x_train,
data_y_train,
epochs=epochs,
batch_size=batch_size)
score = new_model.evaluate(data_x_val, data_y_val, verbose=1)
print('Test loss: ', score[0])
print('Test accuracy: ', score[1])
new_model.save('TransferLearningModel.h5')