diff --git a/llvm/projects/keras/src/lenet.py b/llvm/projects/keras/src/lenet.py index 3fa8123bd156c68183db3e43d24670c23990247d..c9588eef6c393457617b7fdda03c7b8222af5357 100644 --- a/llvm/projects/keras/src/lenet.py +++ b/llvm/projects/keras/src/lenet.py @@ -11,82 +11,87 @@ from frontend.approxhpvm_translator import translate_to_approxhpvm batch_size = 128 num_classes = 10 -epochs = 5 + # input image dimensions -img_rows, img_cols = 28, 28 - +img_rows, img_cols = 28, 28 if __name__ == "__main__": - + + # Changing Keras data format to NCHW - NHWC is default + # NOTE: ApproxHPVM requires NCHW format K.set_image_data_format('channels_first') - # the data, split between train and test sets + # Loads Mnist dataset (x_train, y_train), (x_test, y_test) = mnist.load_data() test_labels = y_test - - if K.image_data_format() == 'channels_first': - x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) - x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) - input_shape = (1, img_rows, img_cols) - else: - x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) - x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) - input_shape = (img_rows, img_cols, 1) + # Reshaping data to be NCHW format + x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) + x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) + input_shape = (1, img_rows, img_cols) - print(K.image_data_format()) - + # Data Normalization x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_test /= 255 - print('x_train shape:', x_train.shape) - print(x_train.shape[0], 'train samples') - print(x_test.shape[0], 'test samples') + - # convert class vectors to binary class matrices + # convert class vectors to binary class matrices - required by Keras y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) - activation_type = 'relu' - + + # Network Compostion: 3 Conv Layers, 2 Dense Layers model = Sequential() + + # ConvLayer1 model.add(Conv2D(32, kernel_size=(5, 5), - activation=activation_type, + activation='relu', padding = 'same', input_shape=input_shape)) model.add(MaxPooling2D(pool_size=(2, 2))) - model.add(Conv2D(64, (5, 5), activation=activation_type, padding = 'same')) + + # ConvLayer2 + model.add(Conv2D(64, (5, 5), activation='relu', padding = 'same')) + + # ConvLayer3 + # NOTE: ZeroPading needed for ConvLayer with strides > 1 model.add(ZeroPadding2D(padding = (1,1))) - model.add(Conv2D(64, (3, 3), strides = (2,2), activation=activation_type) ) + model.add(Conv2D(64, (3, 3), strides = (2,2), activation='relu', padding = 'valid') ) + model.add(Flatten()) - model.add(Dense(1024, activation=activation_type)) - model.add(Dense(num_classes, activation=activation_type)) + # DenseLayer1 + model.add(Dense(1024, activation='relu')) + # DenseLayer2 + model.add(Dense(num_classes, activation='relu')) + # Softmax Layer model.add(Activation('softmax')) - + # Configures model for training model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adadelta(), metrics=['accuracy']) + # Training model.fit(x_train, y_train, batch_size=batch_size, - epochs=1, + epochs=5, verbose=1, validation_data=(x_test, y_test)) + # Inference score = model.evaluate(x_test, y_test, verbose=0) print('Test loss:', score[0]) print('Test accuracy:', score[1]) - model.summary() - + # NOTE: Call to ApproxHPVM Translator - Dumps weights and ApproxHPVM C src translate_to_approxhpvm(model, "data/lenet_hpvm_batch/", x_test, test_labels, 10)