From dda75cebdbfa2c9d64038be36d60d7dc040f8e32 Mon Sep 17 00:00:00 2001 From: Hashim Sharif <hsharif3@tyler.cs.illinois.edu> Date: Thu, 20 Aug 2020 16:52:27 -0500 Subject: [PATCH] Adding VGG16_CIFAR100 to new Benchmark structure --- llvm/projects/keras/src/vgg16_cifar100.py | 111 ++++++++-------------- 1 file changed, 39 insertions(+), 72 deletions(-) diff --git a/llvm/projects/keras/src/vgg16_cifar100.py b/llvm/projects/keras/src/vgg16_cifar100.py index 66fe6be669..41d0816ecd 100644 --- a/llvm/projects/keras/src/vgg16_cifar100.py +++ b/llvm/projects/keras/src/vgg16_cifar100.py @@ -1,6 +1,7 @@ from __future__ import print_function import os +import sys import keras from keras.datasets import cifar100 from keras.preprocessing.image import ImageDataGenerator @@ -12,27 +13,20 @@ import numpy as np from keras.layers.core import Lambda from keras import backend as K from keras import regularizers -from approxhpvm_translator import translate_to_approxhpvm -import sys -from weight_utils import dumpCalibrationData +from Benchmark import Benchmark +from frontend.weight_utils import dumpCalibrationData +from frontend.approxhpvm_translator import translate_to_approxhpvm -class cifar100vgg: - def __init__(self,train=True): - self.num_classes = 100 - self.weight_decay = 0.0005 - self.x_shape = [3,32,32] +class VGG16_CIFAR100(Benchmark): - self.model = self.build_model() - if train: - self.model = self.train(self.model) - else: - self.model.load_weights('cifar100vgg.h5') + def buildModel(self): - def build_model(self): - # Build the network of vgg for 10 classes with massive dropout and weight decay as described in the paper. + # Build the network of vgg for 100 classes + self.weight_decay = 0.0005 + self.x_shape = [3,32,32] model = Sequential() weight_decay = self.weight_decay @@ -118,35 +112,21 @@ class cifar100vgg: return model - def normalize(self,X_train,X_test): - #this function normalize inputs for zero mean and unit variance - # it is used when training a model. - # Input: training set and test set - # Output: normalized training set and test set according to the trianing set statistics. - mean = np.mean(X_train,axis=(0,1,2,3)) - std = np.std(X_train, axis=(0, 1, 2, 3)) - print(mean) - print(std) - X_train = (X_train-mean)/(std+1e-7) - X_test = (X_test-mean)/(std+1e-7) - return X_train, X_test - def normalize_production(self,x): - #this function is used to normalize instances in production according to saved training set statistics - # Input: X - a training set - # Output X - a normalized training set according to normalization constants. + def data_preprocess(self): - #these values produced during first training and are general for the standard cifar10 training set normalization - mean = 121.936 - std = 68.389 - return (x-mean)/(std+1e-7) + (X_train, Y_train), (X_test, Y_test) = cifar100.load_data() - def predict(self,x,normalize=True,batch_size=50): - if normalize: - x = self.normalize_production(x) - return self.model.predict(x,batch_size) + 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) - def train(self,model): + return X_train, Y_train, X_test, Y_test + + + + def trainModel(self,model): #training parameters batch_size = 128 @@ -166,12 +146,12 @@ class cifar100vgg: y_train = keras.utils.to_categorical(y_train, self.num_classes) y_test = keras.utils.to_categorical(y_test, self.num_classes) - + def lr_scheduler(epoch): return learning_rate * (0.5 ** (epoch // lr_drop)) + reduce_lr = keras.callbacks.LearningRateScheduler(lr_scheduler) - #data augmentation datagen = ImageDataGenerator( featurewise_center=False, # set input mean to 0 over the dataset @@ -188,56 +168,43 @@ class cifar100vgg: datagen.fit(x_train) - #optimization details sgd = optimizers.SGD(lr=learning_rate, decay=lr_decay, momentum=0.9, nesterov=True) model.compile(loss='categorical_crossentropy', optimizer=sgd,metrics=['accuracy']) - + # training process in a for loop with learning rate drop every 25 epoches. - historytemp = model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size), steps_per_epoch=x_train.shape[0] // batch_size, epochs=maxepoches, validation_data=(x_test, y_test),callbacks=[reduce_lr],verbose=2) - model.save_weights('cifar100vgg.h5') + + ##### model.save_weights('cifar100vgg.h5') return model -if __name__ == '__main__': - K.set_image_data_format('channels_first') - os.environ["CUDA_VISIBLE_DEVICES"] = "1" - (x_train, y_train), (x_test, y_test) = cifar100.load_data() - test_labels = y_test - train_labels = y_train - x_train = x_train.astype('float32') - x_test = x_test.astype('float32') - y_train = keras.utils.to_categorical(y_train, 100) - y_test = keras.utils.to_categorical(y_test, 100) - model = cifar100vgg() + +if __name__ == "__main__": - predicted_x = model.predict(x_test) + + os.environ["CUDA_VISIBLE_DEVICES"] = "0" + # Changing to NCHW format + K.set_image_data_format('channels_first') - norm_test = model.normalize_production(x_test) - x_train = model.normalize_production(x_train) - - dumpCalibrationData("calibration_data/vgg16_cifar100_calib.bin", x_train, - "calibration_data/vgg16_cifar100_train_labels.bin", train_labels) - sys.exit(0) - - - translate_to_approxhpvm(model.model, "vgg16_cifar100_test/", norm_test, test_labels, - "vgg16_cifar100_front", y_test) + ### Parameters specific to each benchmark + reload_dir = "/home/hsharif3/Gitlab/hpvm/llvm/projects/hpvm-tensor-rt/model_params/vgg16_cifar100/" + keras_model_file = "vgg16_cifar100.h5" + hpvm_dir = "data/vgg16_cifar100/" + num_classes = 100 + vgg16_cifar100 = VGG16_CIFAR100("vgg16_cifar100", reload_dir, keras_model_file, hpvm_dir, num_classes) - residuals = (np.argmax(predicted_x,1)!=np.argmax(y_test,1)) - loss = sum(residuals)/len(residuals) - print("the validation 0/1 loss is: ",loss) - + vgg16_cifar100.run(sys.argv) + -- GitLab