Skip to content
Snippets Groups Projects
Commit 392f2ba8 authored by Hashim Sharif's avatar Hashim Sharif
Browse files

Keras: Creating top level class for all Benchmarks

parent 317b22f6
No related branches found
No related tags found
No related merge requests found
class Benchmark:
def __init__(self):
return
def buildModel(self):
return
def data_preprocess(self):
return
def trainModel(self):
return
def inference(self):
return
def run(self):
return
......@@ -18,176 +18,182 @@ import struct
import keras
import numpy as np
import os
from Benchmark import Benchmark
from frontend.approxhpvm_translator import translate_to_approxhpvm
from frontend.weight_utils import dumpCalibrationData
from frontend.weight_utils import dumpHPVMToKerasModel
def lr_schedule(epoch):
lrate = 0.001
if epoch > 20:
lrate = 0.0005
if epoch > 40:
lrate = 0.0003
if epoch > 60:
lrate = 0.0001
if epoch > 80:
lrate = 0.00005
return lrate
class AlexNet(Benchmark):
def buildModel():
def __init__(self):
self.name = "AlexNet"
def lr_schedule(self, epoch):
lrate = 0.001
if epoch > 20:
lrate = 0.0005
if epoch > 40:
lrate = 0.0003
if epoch > 60:
lrate = 0.0001
if epoch > 80:
lrate = 0.00005
activation_type = "tanh"
weight_decay = 1e-4
model = Sequential()
model.add(Conv2D(64, kernel_size=(11, 11), activation=activation_type,
input_shape=(3, 32, 32), padding = 'same',
kernel_regularizer=regularizers.l2(weight_decay) ))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2) ))
model.add(Dropout(0.2))
model.add(Conv2D(192, kernel_size=(5, 5), activation=activation_type, padding = 'same',
kernel_regularizer=regularizers.l2(weight_decay)))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2) ))
model.add(Dropout(0.3))
model.add(Conv2D(384, kernel_size=(3, 3), activation=activation_type, padding = 'same',
kernel_regularizer=regularizers.l2(weight_decay) ))
model.add(Conv2D(256, kernel_size=(3, 3), activation=activation_type, padding = 'same',
kernel_regularizer=regularizers.l2(weight_decay) ))
model.add(Conv2D(256, kernel_size=(3, 3), activation=activation_type, padding = 'same',
kernel_regularizer=regularizers.l2(weight_decay) ))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2) ))
model.add(Dropout(0.4))
model.add(Flatten())
#model.add(Flatten())
#model.add(Dense(256))
model.add(Dense(10))
model.add(Activation('softmax'))
return model
return lrate
def buildModel_old():
model = Sequential()
model.add(Conv2D(128, kernel_size=(3, 3), activation='tanh', input_shape=(3, 32, 32), padding = 'same'))
model.add(Conv2D(256, kernel_size=(3, 3), activation='tanh', padding = 'same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
#model.add(Dropout(0.25))
model.add(Conv2D(256, kernel_size=(3, 3), activation='tanh', padding = 'same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(256, kernel_size=(3, 3), activation='tanh', padding = 'same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(256, kernel_size=(3, 3), activation='tanh', padding = 'same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
#model.add(Dropout(0.25))
model.add(Flatten())
#model.add(Flatten())
model.add(Dense(4096, activation='tanh'))
#model.add(Dropout(0.5))
model.add(Dense(2048, activation='tanh'))
model.add(Dense(10, activation='tanh'))
model.add(Activation('softmax'))
return model
def buildModel(self):
activation_type = "tanh"
weight_decay = 1e-4
model = Sequential()
model.add(Conv2D(64, kernel_size=(11, 11), activation=activation_type,
input_shape=(3, 32, 32), padding = 'same',
kernel_regularizer=regularizers.l2(weight_decay) ))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2) ))
model.add(Dropout(0.2))
model.add(Conv2D(192, kernel_size=(5, 5), activation=activation_type, padding = 'same',
kernel_regularizer=regularizers.l2(weight_decay)))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2) ))
model.add(Dropout(0.3))
model.add(Conv2D(384, kernel_size=(3, 3), activation=activation_type, padding = 'same',
kernel_regularizer=regularizers.l2(weight_decay) ))
model.add(Conv2D(256, kernel_size=(3, 3), activation=activation_type, padding = 'same',
kernel_regularizer=regularizers.l2(weight_decay) ))
model.add(Conv2D(256, kernel_size=(3, 3), activation=activation_type, padding = 'same',
kernel_regularizer=regularizers.l2(weight_decay) ))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2) ))
model.add(Dropout(0.4))
model.add(Flatten())
#model.add(Flatten())
#model.add(Dense(256))
model.add(Dense(10))
model.add(Activation('softmax'))
return model
def buildModel_old():
model = Sequential()
model.add(Conv2D(128, kernel_size=(3, 3), activation='tanh', input_shape=(3, 32, 32), padding = 'same'))
model.add(Conv2D(256, kernel_size=(3, 3), activation='tanh', padding = 'same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
#model.add(Dropout(0.25))
model.add(Conv2D(256, kernel_size=(3, 3), activation='tanh', padding = 'same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(256, kernel_size=(3, 3), activation='tanh', padding = 'same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(256, kernel_size=(3, 3), activation='tanh', padding = 'same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
#model.add(Dropout(0.25))
model.add(Flatten())
#model.add(Flatten())
model.add(Dense(4096, activation='tanh'))
#model.add(Dropout(0.5))
model.add(Dense(2048, activation='tanh'))
model.add(Dense(10, activation='tanh'))
model.add(Activation('softmax'))
return model
def trainModel(model):
(X_train, Y_train), (X_test, Y_test) = cifar10.load_data()
test_labels = Y_test
train_labels = Y_train
#X_train = X_train.astype('float32')
#X_test = X_test.astype('float32')
X_train = X_train / 255.0
X_test = X_test / 255.0
mean = np.mean(X_train,axis=(0,1,2,3))
std = np.std(X_train,axis=(0,1,2,3))
X_train = (X_train-mean)/(std+1e-7)
X_test = (X_test-mean)/(std+1e-7)
dir_prefix = "/home/hsharif3/Gitlab/hpvm/llvm/projects/hpvm-tensor-rt/model_params/alexnet_cifar10/"
#opt_rms = keras.optimizers.rmsprop(lr=0.001,decay=1e-6)
# Compile the model
model.compile(loss='categorical_crossentropy',
optimizer=Adam(lr=0.0001, decay=1e-6),
#optimizer = opt_rms,
metrics=['accuracy'])
def trainModel(self, model):
#print to_categorical(Y_train, 10)
print (to_categorical(Y_train))
(X_train, Y_train), (X_test, Y_test) = cifar10.load_data()
test_labels = Y_test
train_labels = Y_train
#X_train = X_train.astype('float32')
#X_test = X_test.astype('float32')
X_train = X_train / 255.0
X_test = X_test / 255.0
datagen = ImageDataGenerator(
mean = np.mean(X_train,axis=(0,1,2,3))
std = np.std(X_train,axis=(0,1,2,3))
X_train = (X_train-mean)/(std+1e-7)
X_test = (X_test-mean)/(std+1e-7)
dir_prefix = "/home/hsharif3/Gitlab/hpvm/llvm/projects/hpvm-tensor-rt/model_params/alexnet_cifar10/"
#opt_rms = keras.optimizers.rmsprop(lr=0.001,decay=1e-6)
# Compile the model
model.compile(loss='categorical_crossentropy',
optimizer=Adam(lr=0.0001, decay=1e-6),
#optimizer = opt_rms,
metrics=['accuracy'])
#print to_categorical(Y_train, 10)
print (to_categorical(Y_train))
datagen = ImageDataGenerator(
rotation_range=15,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True,
)
datagen.fit(X_train)
)
datagen.fit(X_train)
model.fit(X_train, to_categorical(Y_train, 10),
batch_size=128,
shuffle=True,
epochs = 1,
#epochs=100,
validation_data=(X_test, to_categorical(Y_test, 10)), callbacks=[LearningRateScheduler(lr_schedule)])
# Evaluate the model
scores = model.evaluate(X_test, to_categorical(Y_test, 10))
print('Loss: %.3f' % scores[0])
print('Accuracy: %.3f' % scores[1])
print ("*** TRAINED MODEL ****\n")
#dumpCalibrationData("calibration_data/alexnet_calib.bin", X_train,
# "calibration_data/alexnet_train_labels.bin", train_labels)
model.fit(X_train, to_categorical(Y_train, 10),
batch_size=128,
shuffle=True,
epochs = 1,
#epochs=100,
validation_data=(X_test, to_categorical(Y_test, 10)), callbacks=[LearningRateScheduler(self.lr_schedule)])
# Evaluate the model
scores = model.evaluate(X_test, to_categorical(Y_test, 10))
print('Loss: %.3f' % scores[0])
print('Accuracy: %.3f' % scores[1])
print ("*** TRAINED MODEL ****\n")
def reloadKerasModel(model_path):
return model
model = load_model(model_path)
score = model.evaluate(X_test, to_categorical(Y_test, 10), verbose=0)
print('Test loss2:', score[0])
print('Test accuracy2:', score[1])
def data_preprocess():
def reloadKerasModel(model_path):
(X_train, Y_train), (X_test, Y_test) = cifar10.load_data()
model = load_model(model_path)
score = model.evaluate(X_test, to_categorical(Y_test, 10), verbose=0)
print('Test loss2:', score[0])
print('Test accuracy2:', score[1])
def data_preprocess(self):
(X_train, Y_train), (X_test, Y_test) = cifar10.load_data()
X_train = X_train / 255.0
X_test = X_test / 255.0
mean = np.mean(X_train,axis=(0,1,2,3))
std = np.std(X_train,axis=(0,1,2,3))
X_train = (X_train-mean)/(std+1e-7)
X_test = (X_test-mean)/(std+1e-7)
return X_train, Y_train, X_test, Y_test
X_train = X_train / 255.0
X_test = X_test / 255.0
mean = np.mean(X_train,axis=(0,1,2,3))
std = np.std(X_train,axis=(0,1,2,3))
X_train = (X_train-mean)/(std+1e-7)
X_test = (X_test-mean)/(std+1e-7)
return X_train, Y_train, X_test, Y_test
if __name__ == "__main__":
......@@ -199,9 +205,11 @@ if __name__ == "__main__":
# Changing to NCHW format
K.set_image_data_format('channels_first')
model = buildModel()
alexnet = AlexNet()
model = alexnet.buildModel()
X_train, Y_train, X_test, Y_test = data_preprocess()
X_train, Y_train, X_test, Y_test = alexnet.data_preprocess()
reload_dir = "/home/hsharif3/Gitlab/hpvm/llvm/projects/hpvm-tensor-rt/model_params/alexnet_cifar10/"
keras_model_file = "alexnet.h5"
......@@ -212,7 +220,7 @@ if __name__ == "__main__":
model = load_model(keras_model_file)
if sys.argv[1] == "train":
model = trainModel(model)
model = alexnet.trainModel(model)
num_classes = 10
score = model.evaluate(X_test, to_categorical(Y_test, num_classes), verbose=0)
......@@ -220,7 +228,7 @@ if __name__ == "__main__":
if len(sys.argv) > 2 and sys.argv[2] == "frontend":
if sys.argv[1] != "hpvm_reload":
if sys.argv[1] == "keras_reload":
print("ERROR: Must load HPVM model to invoke frontend")
sys.exit(1)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment