-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw3p2.py
150 lines (122 loc) · 4.81 KB
/
hw3p2.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# Sara Beery
# EE 148
# HW3
# 4/21/17
# Python 2.7.13
import numpy as np
#import confusion_mat as cm
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import keras
from keras.models import Model
from keras.layers import Dense, Input, GlobalAveragePooling2D
from keras import backend as K
from keras.models import load_model
from sklearn.metrics import confusion_matrix
from keras.callbacks import ModelCheckpoint, CSVLogger
#from keras.callbacks import TensorBoard
#from keras.applications.inception_v3 import InceptionV3
from keras.applications.resnet50 import ResNet50
from keras.preprocessing.image import ImageDataGenerator
import pickle
def get_data_info(num_ims):
source_dir = 'CUB_200_2011/CUB_200_2011/'
class_file = source_dir + 'image_class_labels.txt'
image_file = source_dir + 'images.txt'
split_file = source_dir + 'train_test_split.txt'
x_train = []
y_train = []
x_test = []
y_test = []
classes = []
f1 = open(class_file, 'r')
f2 = open(image_file, 'r')
f3 = open(split_file, 'r')
count = 0
while count < num_ims:
line = f3.readline().split()
file_name = f2.readline().split()[1]
class_name = file_name.split('/')[0]
class_num = float(f1.readline().split()[1])-1
if class_name not in classes:
classes.append(class_name)
if float(line[1]):
x_train.append(file_name)
y_train.append(class_num)
else:
x_test.append(file_name)
y_test.append(class_num)
count += 1
y_train = np.asarray(y_train)
y_test = np.asarray(y_test)
return x_train, x_test, y_train, y_test, classes
batch_size = 32
train_all_classes = True
create_train_test_dirs = True
layers_to_train = 1
img_rows = 150
img_cols = 150
epochs = 10
if train_all_classes:
num_ims = 11788
num_classes = 200
else:
num_ims = 1115
num_classes = 20
image_folder = 'CUB_200_2011/CUB_200_2011/images'
test_folder = 'Test_Bbox'
train_folder = 'Train_Bbox'
filepath = "Bird_Model_2.h5"
x_train_names, x_test_names, y_train, y_test, classes = get_data_info(num_ims)
# convert class vectors to binary class matrices, subtract 1 to get correct classes
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
# create the base pre-trained model
base_model = ResNet50(weights='imagenet', include_top=False)
# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
# let's add a fully-connected layer
x = Dense(1024, activation='relu')(x)
# and a logistic layer -- let's say we have 200 classes
predictions = Dense(num_classes, activation='softmax')(x)
# this is the model we will train
model = Model(input=base_model.input, output=predictions)
# first: train only the top layers (which were randomly initialized)
# i.e. freeze all convolutional InceptionV3 layers
for layer in base_model.layers:
layer.trainable = False
#create checkpoints (after model has been compiled)
# filepath = "weights-improvement-{epoch:02d}-{val_acc:.2f}.hdf5"
# checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
# callbacks_list = [checkpoint]
#create Tensorboard Logs
#remember to pass this to your model while fitting!! model.fit(...inputs and parameters..., callbacks=[tbCallBack])
#tbCallBack = TensorBoard(log_dir='./logs', histogram_freq=0, write_graph=True, write_images=False)
#create callbacks
callbacks = [ModelCheckpoint('models/Bird_Model_2-{epoch:02d}-{val_acc:.4f}.hdf5'),CSVLogger('Bird_Model_2-history', separator=',', append=False)]
# compile the model (should be done *after* setting layers to non-trainable)
model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy'])
datagen = ImageDataGenerator()
#fit the model (should I specify classes? How do I split the training and test data)
history = model.fit_generator(datagen.flow_from_directory(directory=train_folder, target_size=(256,256),classes=classes),
validation_data=datagen.flow_from_directory(directory=test_folder, target_size=(256,256),classes=classes),
validation_steps=len(x_test_names)/batch_size,
epochs=epochs,
steps_per_epoch=len(x_train_names)/batch_size,
callbacks=callbacks,
verbose=1)
model.save(filepath)
# score = model.evaluate(x_test, y_test, verbose=0)
# print('Test loss:', score[0])
# print('Test accuracy:', score[1])
# score = model.evaluate(x_train, y_train, verbose=0)
# print('Training loss:', score[0])
# print('Training accuracy:', score[1])
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.xlabel('epochs')
plt.ylabel('accuracy')
plt.legend(['train','test'], loc = 'upper left')
plt.savefig('Bird_Model_2_accuracy.png', bbox_inches='tight')