-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathw4code_example.py
More file actions
125 lines (105 loc) · 3.87 KB
/
Copy pathw4code_example.py
File metadata and controls
125 lines (105 loc) · 3.87 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Flatten
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K
from keras.utils import plot_model
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import TensorBoard
import matplotlib.pyplot as plt
train_data_dir='/home/mcv/datasets/MIT_split/train'
val_data_dir='/home/mcv/datasets/MIT_split/test'
test_data_dir='/home/mcv/datasets/MIT_split/test'
img_width = 224
img_height=224
batch_size=32
number_of_epoch=20
validation_samples=807
def preprocess_input(x, dim_ordering='default'):
if dim_ordering == 'default':
dim_ordering = K.image_data_format()
assert dim_ordering in {'channels_first', 'channels_last'}
if dim_ordering == 'channels_first':
# 'RGB'->'BGR'
x = x[ ::-1, :, :]
# Zero-center by mean pixel
x[ 0, :, :] -= 103.939
x[ 1, :, :] -= 116.779
x[ 2, :, :] -= 123.68
else:
# 'RGB'->'BGR'
x = x[:, :, ::-1]
# Zero-center by mean pixel
x[:, :, 0] -= 103.939
x[:, :, 1] -= 116.779
x[:, :, 2] -= 123.68
return x
# create the base pre-trained model
base_model = VGG16(weights='imagenet')
plot_model(base_model, to_file='modelVGG16a.png', show_shapes=True, show_layer_names=True)
x = base_model.layers[-2].output
x = Dense(8, activation='softmax',name='predictions')(x)
model = Model(input=base_model.input, output=x)
#plot_model(model, to_file='modelVGG16b.png', show_shapes=True, show_layer_names=True)
#for layer in base_model.layers:
# layer.trainable = False
model.compile(loss='categorical_crossentropy',optimizer='adadelta', metrics=['accuracy'])
for layer in model.layers:
print(layer.name, layer.trainable)
#preprocessing_function=preprocess_input,
datagen = ImageDataGenerator(featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
preprocessing_function=preprocess_input,
rotation_range=0.,
width_shift_range=0.,
height_shift_range=0.,
shear_range=0.,
zoom_range=0.,
channel_shift_range=0.,
fill_mode='nearest',
cval=0.,
horizontal_flip=False,
vertical_flip=False,
rescale=None)
train_generator = datagen.flow_from_directory(train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical')
test_generator = datagen.flow_from_directory(test_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical')
validation_generator = datagen.flow_from_directory(val_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical')
tbCallBack = TensorBoard(log_dir='/home/ramon/work/mcv3/outs/4', histogram_freq=10, write_graph=True, profile_batch=0)
history=model.fit_generator(train_generator,
steps_per_epoch=(int(400//batch_size)+1),
nb_epoch=number_of_epoch,
validation_data=validation_generator,
validation_steps= (int(validation_samples//batch_size)+1), callbacks=[tbCallBack])
result = model.evaluate_generator(test_generator, val_samples=validation_samples)
print( result)
# list all data in history
if False:
# summarize history for accuracy
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.savefig('accuracy.jpg')
plt.close()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.savefig('loss.jpg')