forked from mynktwri/Kuzushiji_Character_Classification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprelim_model.py
109 lines (95 loc) · 3.84 KB
/
prelim_model.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
import numpy as np
from keras.models import Model, Sequential
from keras.layers import Input, Dense, Dropout, Flatten
from keras import metrics
from keras.layers.convolutional import Conv2D, AveragePooling2D
import keras
import matplotlib.pyplot as plt
# New imports
import datetime
from keras.callbacks import TensorBoard as TBCallback
# custom resnet.py
from my_resnet import ResNet
# UNCOMMENT BELOW FOR KMNIST
# KMNIST LOADING
# classes = 10
# with np.load("KMNIST/kmnist-test-imgs.npz") as data:
# xtest_imgs = data['arr_0']
# with np.load("KMNIST/kmnist-test-labels.npz") as data:
# test_labels = data['arr_0']
# print("Test imgs and labels loaded.")
# with np.load("KMNIST/kmnist-train-imgs.npz") as data:
# xtrain_imgs = data['arr_0']
# with np.load("KMNIST/kmnist-train-labels.npz") as data:
# train_labels = data['arr_0']
# print("Training imgs and labels loaded.")
# K49 LOADING
classes = 49
with np.load("K49/k49-test-imgs.npz") as data:
xtest_imgs = data['arr_0']
with np.load("K49/k49-test-labels.npz") as data:
test_labels = data['arr_0']
print("Test imgs and labels loaded.")
with np.load("K49/k49-train-imgs.npz") as data:
xtrain_imgs = data['arr_0']
with np.load("K49/k49-train-labels.npz") as data:
train_labels = data['arr_0']
print("Training imgs and labels loaded.")
# Data Preprocessing (unravel the image to a 1D vector)
# train_imgs = np.ndarray(shape=(len(xtrain_imgs), 784))
# test_imgs = np.ndarray(shape=(len(xtest_imgs), 784))
# for i in range(0, len(xtrain_imgs)):
# train_imgs[i]=xtrain_imgs[i].ravel()
# for i in range(0, len(xtest_imgs)):
# test_imgs[i]=xtest_imgs[i].ravel()
train_imgs = xtrain_imgs.reshape(xtrain_imgs.shape[0], 28, 28, 1).astype('float32')
test_imgs = xtest_imgs.reshape(xtest_imgs.shape[0], 28, 28, 1).astype('float32')
train_labels = keras.utils.to_categorical(train_labels, classes)
test_labels = keras.utils.to_categorical(test_labels, classes)
# All arrays are saved as ndarrays
# Input: 28x28 grayscale images each with value 0-255
# Output: Probability of image being one of 'classes' classes.
# 0 | Hidden Layer | 0
# 1 | | 1
# 2 | | 2
# . | | .
# . | | .
# 783 | | 9
model = Sequential()
# new tutorial implementation
stages = [3,4,6]
filters = [64, 128, 256, 512]
model = ResNet.build(28, 28, 1, classes, stages, filters)
#original
model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=[metrics.categorical_accuracy])
#From GitHub
#model.compile(loss="categorical_crossentropy", optimizer="sgd")
model.summary()
# what I did to test epoch for last progress report
history = model.fit(x=train_imgs, y=train_labels, epochs=5, batch_size=16, verbose=1, validation_split=.1)
'''
Objective: Build a Keras Layer
'''
'''
# using custom layer from keras_layer
# Fix argument value. Maybe fix how layers are made in keras_layer.py
singleX = single_layer( Conv2D(10, kernel_size=(3,5), padding="same", input_shape=(28,28,1), activation = 'relu') )
# new.
now = datetime.datetime.now()
logdir = f"logs/%d-%d-%d-%d" %(now.month, now.day, now.hour, now.minute)
#For test purpose, use 3 epochs only
history = model.fit(x=train_imgs, y=train_labels, epochs=3, batch_size=128, verbose=1, validation_split=.1,
callbacks=[TBCallback(log_dir=logdir)])
#history = model.fit(x=train_imgs, y=train_labels, epochs=10, batch_size=128, verbose=1, validation_split=.1,
# callbacks=[TBCallback(log_dir=logdir)])
'''
loss, accuracy = model.evaluate(test_imgs, test_labels)
plt.plot(history.history['categorical_accuracy'])
plt.plot(history.history['val_categorical_accuracy'])
plt.title('model accuracy')
plt.ylabel('categorical_accuracy')
plt.xlabel('epoch')
plt.legend(['training', 'validation'], loc='best')
print("Test loss:", loss)
print("Test accuracy:", accuracy)
plt.show()