def main(): assert tf.__version__.startswith('2')==True, 'Only Tensorflow-2.X API is supported.' config = helper.getProtoNNArgs() # Get hyper parameters DATA_DIR = config.data_dir OUT_DIR = config.output_dir # Load data train = np.load(DATA_DIR + '/train.npy') test = np.load(DATA_DIR + '/test.npy') x_train, y_train = train[:, 1:], train[:, 0] x_test, y_test = test[:, 1:], test[:, 0] # Convert y to one-hot minval = min(min(y_train), min(y_test)) numClasses = max(y_train) - min(y_train) + 1 numClasses = max(numClasses, max(y_test) - min(y_test) + 1) numClasses = int(numClasses) y_train = helper.to_onehot(y_train, numClasses, minlabel=minval) y_test = helper.to_onehot(y_test, numClasses, minlabel=minval) dataDimension = x_train.shape[1] W = np.load(OUT_DIR + '/W.npy') B = np.load(OUT_DIR + '/B.npy') Z = np.load(OUT_DIR + '/Z.npy') gamma = np.load(OUT_DIR + '/gamma.npy') n_dim = inputDimension = W.shape[0] projectionDimension = W.shape[1] numPrototypes = B.shape[1] numOutputLabels = Z.shape[0] errmsg = 'Dimensions mismatch! Should be W[d, d_cap], B[d_cap, m] and Z[L,m]' assert B.shape[0] == projectionDimension, errmsg assert Z.shape[1] == numPrototypes, errmsg dense = ProtoNNLayer( inputDimension, projectionDimension, numPrototypes, numOutputLabels, gamma ) model = keras.Sequential([ keras.Input(shape=(n_dim)), dense ]) dummy_tensor = tf.convert_to_tensor( np.zeros((1,n_dim), np.float32) ) out_tensor = model( dummy_tensor ) model.summary() dense.set_weights( [W, B, Z] ) print( 'gamma = ', gamma ) print( 'inputDim = ', inputDimension ) print( 'projectionDim = ', projectionDimension ) print( 'numPrototypes = ', numPrototypes ) print( 'numOutputLabels = ', numOutputLabels ) # Save the Keras model in tflite format converter = tf.lite.TFLiteConverter.from_keras_model(model) converter.optimizations = [tf.lite.Optimize.DEFAULT] tflite_model = converter.convert() # Save the TF Lite model as file out_tflite_model_file = OUT_DIR + '/protoNN_model.tflite' f = open(out_tflite_model_file, "wb") f.write(tflite_model) f.close() # Delete any reference to existing models in order to avoid conflicts del model del tflite_model # Prediction on an example input using tflite model we just saved x, y = x_train[0], y_train[0] x = x.astype(np.float32) x = np.expand_dims( x, 0 ) # Run inference with TensorFlow Lite interpreter = tf.lite.Interpreter(model_path=out_tflite_model_file) interpreter.allocate_tensors() interpreter.set_tensor(interpreter.get_input_details()[0]["index"], x) interpreter.invoke() output = interpreter.tensor(interpreter.get_output_details()[0]["index"])()[0] print('true y = ', np.argmax(y)) print('predicted y = ', np.argmax(output))
def main(): config = helper.getProtoNNArgs() # Get hyper parameters DATA_DIR = config.data_dir PROJECTION_DIM = config.projection_dim NUM_PROTOTYPES = config.num_prototypes REG_W = config.rW REG_B = config.rB REG_Z = config.rZ SPAR_W = config.sW SPAR_B = config.sB SPAR_Z = config.sZ LEARNING_RATE = config.learning_rate NUM_EPOCHS = config.epochs BATCH_SIZE = config.batch_size PRINT_STEP = config.print_step VAL_STEP = config.val_step OUT_DIR = config.output_dir # Load data train = np.load(DATA_DIR + '/train.npy') test = np.load(DATA_DIR + '/test.npy') x_train, y_train = train[:, 1:], train[:, 0] x_test, y_test = test[:, 1:], test[:, 0] # Convert y to one-hot minval = min(min(y_train), min(y_test)) numClasses = max(y_train) - min(y_train) + 1 numClasses = max(numClasses, max(y_test) - min(y_test) + 1) numClasses = int(numClasses) y_train = helper.to_onehot(y_train, numClasses, minlabel=minval) y_test = helper.to_onehot(y_test, numClasses, minlabel=minval) dataDimension = x_train.shape[1] W, B, gamma = helper.getGamma(config.gamma, PROJECTION_DIM, dataDimension, NUM_PROTOTYPES, x_train) # Setup input and train protoNN X = tf.placeholder(tf.float32, [None, dataDimension], name='X') Y = tf.placeholder(tf.float32, [None, numClasses], name='Y') protoNN = ProtoNN(dataDimension, PROJECTION_DIM, NUM_PROTOTYPES, numClasses, gamma, W=W, B=B) trainer = ProtoNNTrainer(protoNN, REG_W, REG_B, REG_Z, SPAR_W, SPAR_B, SPAR_Z, LEARNING_RATE, X, Y, lossType='xentropy') sess = tf.Session() trainer.train(BATCH_SIZE, NUM_EPOCHS, sess, x_train, x_test, y_train, y_test, printStep=PRINT_STEP, valStep=VAL_STEP) # Print some summary metrics acc = sess.run(protoNN.accuracy, feed_dict={X: x_test, Y: y_test}) # W, B, Z are tensorflow graph nodes W, B, Z, gamma = protoNN.getModelMatrices() matrixList = sess.run([W, B, Z]) gamma = sess.run(gamma) sparcityList = [SPAR_W, SPAR_B, SPAR_Z] nnz, size, sparse = helper.getModelSize(matrixList, sparcityList) print("Final test accuracy", acc) print("Model size constraint (Bytes): ", size) print("Number of non-zeros: ", nnz) nnz, size, sparse = helper.getModelSize(matrixList, sparcityList, expected=False) print("Actual model size: ", size) print("Actual non-zeros: ", nnz) print("Saving model matrices to: ", OUT_DIR) np.save(OUT_DIR + '/W.npy', matrixList[0]) np.save(OUT_DIR + '/B.npy', matrixList[1]) np.save(OUT_DIR + '/Z.npy', matrixList[2]) np.save(OUT_DIR + '/gamma.npy', gamma)
def main(): config = helper.getProtoNNArgs() # Get hyper parameters DATA_DIR = config.data_dir PROJECTION_DIM = config.projection_dim NUM_PROTOTYPES = config.num_prototypes REG_W = config.rW REG_B = config.rB REG_Z = config.rZ SPAR_W = config.sW SPAR_B = config.sB SPAR_Z = config.sZ LEARNING_RATE = config.learning_rate NUM_EPOCHS = config.epochs BATCH_SIZE = config.batch_size PRINT_STEP = config.print_step VAL_STEP = config.val_step # Load data out = helper.preprocessData(DATA_DIR) dataDimension = out[0] numClasses = out[1] x_train, y_train = out[2], out[3] x_test, y_test = out[4], out[5] W, B, gamma = helper.getGamma(config.gamma, PROJECTION_DIM, dataDimension, NUM_PROTOTYPES, x_train) # Setup input and train protoNN X = tf.placeholder(tf.float32, [None, dataDimension], name='X') Y = tf.placeholder(tf.float32, [None, numClasses], name='Y') protoNN = ProtoNN(dataDimension, PROJECTION_DIM, NUM_PROTOTYPES, numClasses, gamma, W=W, B=B) trainer = ProtoNNTrainer(protoNN, REG_W, REG_B, REG_Z, SPAR_W, SPAR_B, SPAR_Z, LEARNING_RATE, X, Y, lossType='xentropy') sess = tf.Session() trainer.train(BATCH_SIZE, NUM_EPOCHS, sess, x_train, x_test, y_train, y_test, printStep=PRINT_STEP, valStep=VAL_STEP) # Print some summary metrics acc = sess.run(protoNN.accuracy, feed_dict={X: x_test, Y: y_test}) # W, B, Z are tensorflow graph nodes W, B, Z, _ = protoNN.getModelMatrices() matrixList = sess.run([W, B, Z]) sparcityList = [SPAR_W, SPAR_B, SPAR_Z] nnz, size, sparse = helper.getModelSize(matrixList, sparcityList) print("Final test accuracy", acc) print("Model size constraint (Bytes): ", size) print("Number of non-zeros: ", nnz) nnz, size, sparse = helper.getModelSize(matrixList, sparcityList, expected=False) print("Actual model size: ", size) print("Actual non-zeros: ", nnz)
def main(): # change cuda:0 to cuda:gpu_id for using particular gpu device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") config = helper.getProtoNNArgs() # Get hyper parameters DATA_DIR = config.data_dir PROJECTION_DIM = config.projection_dim NUM_PROTOTYPES = config.num_prototypes REG_W = config.rW REG_B = config.rB REG_Z = config.rZ SPAR_W = config.sW SPAR_B = config.sB SPAR_Z = config.sZ LEARNING_RATE = config.learning_rate NUM_EPOCHS = config.epochs BATCH_SIZE = config.batch_size PRINT_STEP = config.print_step VAL_STEP = config.val_step OUT_DIR = config.output_dir # Load data train = np.load(DATA_DIR + '/train.npy') test = np.load(DATA_DIR + '/test.npy') x_train, y_train = train[:, 1:], train[:, 0] x_test, y_test = test[:, 1:], test[:, 0] # Convert y to one-hot minval = min(min(y_train), min(y_test)) numClasses = max(y_train) - min(y_train) + 1 numClasses = max(numClasses, max(y_test) - min(y_test) + 1) numClasses = int(numClasses) y_train = helper.to_onehot(y_train, numClasses, minlabel=minval) y_test = helper.to_onehot(y_test, numClasses, minlabel=minval) dataDimension = x_train.shape[1] W, B, gamma = helper.getGamma(config.gamma, PROJECTION_DIM, dataDimension, NUM_PROTOTYPES, x_train) # Setup input and train protoNN protoNN = ProtoNN(dataDimension, PROJECTION_DIM, NUM_PROTOTYPES, numClasses, gamma, W=W, B=B).to(device) trainer = ProtoNNTrainer(protoNN, REG_W, REG_B, REG_Z, SPAR_W, SPAR_B, SPAR_Z, LEARNING_RATE, lossType='xentropy', device=device) # Train the protoNN object trainer.train(BATCH_SIZE, NUM_EPOCHS, x_train, x_test, y_train, y_test, printStep=PRINT_STEP, valStep=VAL_STEP) # Print some summary metrics x_, y_ = (torch.Tensor(x_test)).to(device), ( torch.Tensor(y_test)).to(device) logits = protoNN.forward(x_) _, predictions = torch.max(logits, dim=1) _, target = torch.max(y_, dim=1) acc, count = trainer.accuracy(predictions, target) #Model needs to be on cpu for numpy operations below protoNN = protoNN.cpu() W, B, Z, gamma = protoNN.getModelMatrices() matrixList = [W, B, Z] matrixList = [x.detach().numpy() for x in matrixList] sparcityList = [SPAR_W, SPAR_B, SPAR_Z] nnz, size, sparse = helper.getModelSize(matrixList, sparcityList) print("Final test accuracy", acc) print("Model size constraint (Bytes): ", size) print("Number of non-zeros: ", nnz) nnz, size, sparse = helper.getModelSize(matrixList, sparcityList, expected=False) print("Actual model size: ", size) print("Actual non-zeros: ", nnz) print("Saving model matrices to: ", OUT_DIR) np.save(OUT_DIR + '/W.npy', matrixList[0]) np.save(OUT_DIR + '/B.npy', matrixList[1]) np.save(OUT_DIR + '/Z.npy', matrixList[2]) np.save(OUT_DIR + '/gamma.npy', gamma)