-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrunner.py
64 lines (51 loc) · 2.42 KB
/
runner.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
# This Spatial Pyramid Pooling Layer is for keras 2.2.4+ running over TensorFlow 2.0
import os
import numpy as np
import tensorflow
from tensorflow.keras import optimizers
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Convolution2D, Activation, MaxPooling2D, Dense, Dropout
from SpatialPyramidPooling import SpatialPyramidPooling
# Minimizes Tensorflow Logging
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
BATCH_SIZE = 64
NUM_CHANNELS = 1
NUM_CLASSES = 10
def makeModel():
model = Sequential()
# MODEL 1
# uses tensorflow ordering. Note that we leave the image size as None to allow multiple image sizes
model.add(Convolution2D(32, 3, NUM_CHANNELS, padding='same', input_shape=(None, None, NUM_CHANNELS)))
model.add(Activation('relu'))
model.add(Convolution2D(32, 3, NUM_CHANNELS, padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), padding='same'))
model.add(Convolution2D(64, 3, NUM_CHANNELS, padding='same'))
model.add(Activation('relu'))
model.add(Convolution2D(64, 3, NUM_CHANNELS, padding='same'))
model.add(Activation('relu'))
model.add(SpatialPyramidPooling([1, 2, 4]))
model.add(Dense(NUM_CLASSES))
model.add(Activation('softmax'))
return model
def main():
model=makeModel()
model.summary()
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images.reshape(train_images.shape[0], 28, 28, 1).astype('float32')
train_images = (train_images - 127.5) / 127.5 # Normalize the images to [-1, 1]
test_images = test_images.reshape(test_images.shape[0], 28, 28, 1).astype('float32')
test_images = (test_images - 127.5) / 127.5 # Normalize the images to [-1, 1]
adam=optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False)
model.compile(loss='sparse_categorical_crossentropy', optimizer=adam, metrics = ["accuracy"])
model.fit(train_images, train_labels)
# results = model.evaluate(test_images, test_labels, batch_size=128)
# print('test loss, test acc:', results)
model.compile(loss='categorical_crossentropy', optimizer=adam, metrics = ["accuracy"])
# train on 64x64x3 random images
model.fit(np.random.rand(BATCH_SIZE, 64, 64, NUM_CHANNELS), np.zeros((BATCH_SIZE, NUM_CLASSES)))
# train on 32x32x3 random images
model.fit(np.random.rand(BATCH_SIZE, 32, 32, NUM_CHANNELS), np.zeros((BATCH_SIZE, NUM_CLASSES)))
if __name__ == '__main__':
main()