/
convnet1.py
169 lines (138 loc) · 5.05 KB
/
convnet1.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import numpy
import tkinter
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras import applications
from keras.preprocessing import image
from keras.constraints import maxnorm
from keras.optimizers import SGD
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
from keras import initializers
from keras import backend as K
K.set_image_dim_ordering('th')
import os
import numpy as np
import scipy.io
import scipy.misc
import matplotlib.pyplot as plt
import tensorflow as tf
def imread(path):
img = scipy.misc.imread(path).astype(np.float)
if len(img.shape) == 2:
img = np.transpose(np.array([img, img, img]), (2, 0, 1))
return img
cwd = os.getcwd()
path = cwd+"/data/categories"
valid_exts = [".jpg", ".gif", "png", ".jpeg"]
print("[%d] CATEGORIES ARE IN \n %s" % (len(os.listdir(path)), path))
categories = sorted(os.listdir(path))
ncategories = len(categories)
imgs = []
labels = []
#LOAD ALL IMAGES
for i, category in enumerate(categories):
iter = 0
for f in os.listdir(path + "/" + category ):
if iter == 0:
ext = os.path.splitext(f)[1]
if ext.lower() not in valid_exts:
continue
fullpath = os.path.join(path + "/" + category, f)
print(fullpath)
img = scipy.misc.imresize(imread(fullpath), [128,128, 3])
img = img.astype("float32")
img[:, :, 0] -= 123.68
img[:, :, 1] -= 116.78
img[:, :, 2] -= 103.94
imgs.append(img) # NORMALIZE IMAGE
label_curr = i
labels.append(label_curr)
# iter = (iter+1)%10;
print ("Num imgs: %d" % (len(imgs)))
print ("Num labels: %d" % (len(labels)) )
print (ncategories)
seed = 7
np.random.seed(seed)
import pandas as pd
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(imgs, labels, test_size = 0.1)
X_train = np.stack(X_train, axis=0)
y_train = np.stack(y_train, axis=0)
X_test = np.stack(X_test, axis=0)
y_test = np.stack(y_test, axis=0)
print ("Num train_imgs: %d" % (len(X_train)))
print ("Num test_imgs: %d" % (len(X_test)))
# # one hot encode outputs
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
num_classes= y_test.shape[1]
print(y_test.shape)
print(X_train[1,1,1,:])
print(y_train[1])
# normalize inputs from 0-255 to 0.0-1.0
print(X_train.shape)
print(X_test.shape)
X_train = X_train.transpose(0, 3, 1, 2)
X_test = X_test.transpose(0, 3, 1, 2)
print(X_train.shape)
print(X_test.shape)
import scipy.io as sio
data = {}
data['categories'] = categories
data['X_train'] = X_train
data['y_train'] = y_train
data['X_test'] = X_test
data['y_test'] = y_test
sio.savemat('caltech_del.mat', data)
from keras.regularizers import l1, l2
from keras.callbacks import EarlyStopping
earlyStopping = EarlyStopping(monitor='val_loss', patience=10, verbose=1, mode='auto')
# Create the model
#model = Sequential()
# model.add(Conv2D(32, (3, 3), padding='same', activation='relu', kernel_constraint=maxnorm(3)))
# model.add(Conv2D(32, (3, 3), activation='relu', padding='same', kernel_constraint=maxnorm(3)))
# model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
base_model = applications.VGG19(weights='imagenet', include_top=False, input_shape=(3, 128, 128))
for layer in base_model.layers:
layer.trainable = False
full_model = Sequential()
full_model.add(base_model)
# full_model.add(Dense(512, input_shape=(6, 6, 512), activation='relu'))
full_model.add(Flatten())
full_model.add(Dense(256, activation='relu'))
full_model.add(Dense(256, activation='relu'))
full_model.add(Dropout(0.5))
full_model.add(Dense(num_classes, activation='softmax'))
full_model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
hist = full_model.fit(X_train, y_train,validation_data=(X_test, y_test), epochs=20, batch_size=16)
#model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
#model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
print(full_model.summary())
np.random.seed(seed)
#hist = model.fit(X_train, y_train, validation_data=(X_test, y_test),
# epochs=epochs, batch_size=10, shuffle=True, callbacks=[earlyStopping])
#hist = model.load_weights('./64.15/model.h5');
# Final evaluation of the model
scores = full_model.evaluate(X_test, y_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))
plt.plot(hist.history['loss'])
plt.plot(hist.history['val_loss'])
plt.legend(['train','test'])
plt.title('loss')
plt.savefig("loss7.png",dpi=300,format="png")
plt.figure()
plt.plot(hist.history['acc'])
plt.plot(hist.history['val_acc'])
plt.legend(['train','test'])
plt.title('accuracy')
plt.savefig("accuracy7.png",dpi=300,format="png")
model_json = full_model.to_json()
with open("model7.json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
full_model.save_weights("model7.h5")
print("Saved model to disk")