forked from yangxue0827/RCNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fine_tune_RCNN.py
73 lines (66 loc) · 3.13 KB
/
fine_tune_RCNN.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
from __future__ import division, print_function, absolute_import
import os.path
import preprocessing_RCNN as prep
import config
import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression
# Use a already trained alexnet with the last layer redesigned
def create_alexnet(num_classes, restore=False):
# Building 'AlexNet'
network = input_data(shape=[None, config.IMAGE_SIZE, config.IMAGE_SIZE, 3])
network = conv_2d(network, 96, 11, strides=4, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)
network = conv_2d(network, 256, 5, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)
network = conv_2d(network, 384, 3, activation='relu')
network = conv_2d(network, 384, 3, activation='relu')
network = conv_2d(network, 256, 3, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)
network = fully_connected(network, 4096, activation='tanh')
network = dropout(network, 0.5)
network = fully_connected(network, 4096, activation='tanh')
network = dropout(network, 0.5)
# do not restore this layer
network = fully_connected(network, num_classes, activation='softmax', restore=restore)
network = regression(network, optimizer='momentum',
loss='categorical_crossentropy',
learning_rate=0.001)
return network
def fine_tune_Alexnet(network, X, Y, save_model_path, fine_tune_model_path):
# Training
model = tflearn.DNN(network, checkpoint_path='rcnn_model_alexnet',
max_checkpoints=1, tensorboard_verbose=2, tensorboard_dir='output_RCNN')
if os.path.isfile(fine_tune_model_path + '.index'):
print("Loading the fine tuned model")
model.load(fine_tune_model_path)
elif os.path.isfile(save_model_path + '.index'):
print("Loading the alexnet")
model.load(save_model_path)
else:
print("No file to load, error")
return False
model.fit(X, Y, n_epoch=1, validation_set=0.1, shuffle=True,
show_metric=True, batch_size=64, snapshot_step=200,
snapshot_epoch=False, run_id='alexnet_rcnnflowers2')
# Save the model
model.save(fine_tune_model_path)
if __name__ == '__main__':
data_set = config.FINE_TUNE_DATA
if len(os.listdir(config.FINE_TUNE_DATA)) == 0:
print("Reading Data")
prep.load_train_proposals(config.FINE_TUNE_LIST, 2, save=True, save_path=data_set)
print("Loading Data")
X, Y = prep.load_from_npy(data_set)
restore = False
if os.path.isfile(config.FINE_TUNE_MODEL_PATH + '.index'):
restore = True
print("Continue fine-tune")
# three classes include background
net = create_alexnet(config.FINE_TUNE_CLASS, restore=restore)
fine_tune_Alexnet(net, X, Y, config.SAVE_MODEL_PATH, config.FINE_TUNE_MODEL_PATH)