Skip to content

Commit 2bd2321

Browse files
committed
top5accuracy
1 parent a50e461 commit 2bd2321

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

gierlach/build_model_top5.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import pandas as pd
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
import matplotlib.image as mpimg
5+
from keras.models import Sequential
6+
from keras.layers import Conv2D
7+
from keras.layers import MaxPooling2D
8+
from keras.preprocessing.image import ImageDataGenerator
9+
from keras.layers.core import Dense, Dropout, Activation, Flatten
10+
from keras.metrics import top_k_categorical_accuracy
11+
12+
def top_5_accuracy(y_true, y_pred):
13+
return top_k_categorical_accuracy(y_true, y_pred, k=5)
14+
15+
def get_data():
16+
train_data = ImageDataGenerator(rescale = 1./255, shear_range = 0.2,zoom_range = 0.2,rotation_range=40,fill_mode='nearest', horizontal_flip = True)
17+
test_data = ImageDataGenerator(rescale = 1./255)
18+
validation_data = ImageDataGenerator(rescale = 1./255)
19+
training_set = train_data.flow_from_directory('Training',target_size = (64, 64),color_mode='rgb',class_mode = 'categorical')
20+
test_set = test_data.flow_from_directory('Testing',target_size = (64,64),color_mode='rgb',class_mode = 'categorical')
21+
validation_set = validation_data.flow_from_directory('Validation',target_size = (64,64),color_mode='rgb',class_mode = 'categorical')
22+
plt.figure()
23+
plt.hist(training_set.classes, bins=62)
24+
plt.title("Histogram of training dataset")
25+
plt.savefig('Histogram of training dataset.png')
26+
plt.figure()
27+
plt.hist(test_set.classes, bins=62)
28+
plt.title("Histogram of testing dataset")
29+
plt.savefig("Histogram of testing dataset.png")
30+
plt.figure()
31+
plt.hist(validation_set.classes, bins=62)
32+
plt.title("Histogram of validation dataset")
33+
plt.savefig("Histogram of validation dataset.png")
34+
35+
classifier = Sequential()
36+
classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))
37+
classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))
38+
classifier.add(MaxPooling2D(pool_size = (2, 2)))
39+
classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
40+
classifier.add(MaxPooling2D(pool_size = (2, 2)))
41+
classifier.add(Flatten(input_shape=classifier.output_shape[1:]))
42+
classifier.add(Dense(256, activation='relu'))
43+
classifier.add(Dropout(0.5))
44+
classifier.add(Dense(62, activation='softmax'))
45+
classifier.compile(optimizer = 'Nadam', loss = 'mean_squared_error', metrics = [top_5_accuracy])
46+
47+
48+
nr_epoch = 20
49+
his = classifier.fit_generator(training_set,steps_per_epoch = 110,epochs = nr_epoch,validation_data = validation_set)
50+
train_loss = his.history['loss']
51+
val_loss = his.history['val_loss']
52+
train_acc = his.history['top_5_accuracy']
53+
val_acc = his.history['val_top_5_accuracy']
54+
xc = range(nr_epoch)
55+
56+
plt.figure()
57+
plt.plot(xc, val_loss, label='val_loss')
58+
plt.plot(xc, train_loss, label='train_loss')
59+
plt.ylabel("mean_squared_error")
60+
plt.xlabel("number of epoch")
61+
plt.legend()
62+
plt.savefig("Loss_top5.png")
63+
plt.figure()
64+
plt.plot(xc, val_acc, label='val_acc')
65+
plt.plot(xc ,train_acc, label='train_acc')
66+
plt.legend(loc=4)
67+
plt.ylabel("accuracy")
68+
plt.xlabel("number of epoch")
69+
plt.savefig("Accuracy_top5.png")
70+
classifier.save('classifier_20epoch.h5')
71+
72+
73+
get_data()
74+
75+

gierlach/draw_model.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from keras.models import load_model
2+
from keras.utils import plot_model
3+
4+
model = load_model('classifier_augmentation1_20epoch.h5')
5+
plot_model(model, to_file='model.png')
6+
7+
8+
9+
10+

gierlach_acc_top5.png

30.1 KB
Loading

0 commit comments

Comments
 (0)