Skip to content

Instantly share code, notes, and snippets.

View rahulremanan's full-sized avatar

Rahul Remanan rahulremanan

View GitHub Profile
!cat ./message.priv.enc | base64 -d | openssl rsautl -verify -pubin -inkey ./self_signed_cert.pub.key -in - > ./message.priv.dec
@rahulremanan
rahulremanan / encrypt_hash_decrypt--encrypt_private_key.py
Created May 31, 2023 18:21
Encrypt a message using the end-point server private key
!cat ./message.txt | openssl rsautl -sign -inkey ./self_signed_cert.key -in - -out - | base64 > ./message.priv.enc
@rahulremanan
rahulremanan / encrypt_hash_decrypt--decrypt_private_key.py
Created May 31, 2023 18:03
Decrypt the end-point server public key encrypted file using the end-point server private key
!openssl rsautl -inkey ./self_signed_cert.key -decrypt -in ./message.enc -out ./message.dec
@rahulremanan
rahulremanan / encrypt_hash_decrypt--file_hash.py
Created May 31, 2023 17:58
Compute the hash value for a file using OpenSSL
!openssl dgst -sha512 -binary ./message.enc | openssl base64 -A
@rahulremanan
rahulremanan / encrypt_hash_decrypt--encrypt_pub_key.py
Created May 31, 2023 17:53
Encrypt a small text file using the end-point server public key
!openssl rsautl -pubin -inkey ./self_signed_cert.pub.key -encrypt -in ./message.txt -out ./message.enc
@rahulremanan
rahulremanan / encrypt_hash_decrypt--pub_key.py
Created May 31, 2023 17:51
Extract public key from the end-point server certificate file
!openssl x509 -pubkey -noout -in ./self_signed_cert.crt -out ./self_signed_cert.pub.key
@rahulremanan
rahulremanan / Post-training_dynamic_range_quantization--inference_API--03.py
Created May 5, 2023 08:34
A simple inference API for the post-training dynamic range model quantization
class TfLiteModel():
def __init__(self, model):
self.model = model
def predict(self, X, **kwargs):
_tflite_model = dynamic_range_quantize_model(self.model)
_x = np.asarray(X); assert len(X.shape)>=2
_tflite_out_preds = list(map(lambda i : tflite_preds(np.expand_dims(_x[i,:],axis=0), _tflite_model), range(_x.shape[0]) ))
return np.asarray(_tflite_out_preds)
@rahulremanan
rahulremanan / Post-training_dynamic_range_quantization--model_inference--02.py
Last active May 5, 2023 08:36
Model inference using a post-training dynamic range quantized model
def tflite_preds(X, tflite_model):
_interpreter = tf.lite.Interpreter(model_content=tflite_model)
_interpreter.allocate_tensors()
_input_details = _interpreter.get_input_details()
_output_details = _interpreter.get_output_details()
_interpreter.set_tensor(_input_details[0]['index'], tf.cast(X, dtype=tf.float32))
del X; _interpreter.invoke()
_out_pred = _interpreter.get_tensor(_output_details[0]['index'])
return np.squeeze(np.asarray(_out_pred), axis=0)
def dynamic_range_quantize_model(model):
_converter = tf.lite.TFLiteConverter.from_keras_model(model)
del model
_converter.optimizations = [tf.lite.Optimize.DEFAULT]
_converter.target_spec.supported_ops = [
tf.lite.OpsSet.TFLITE_BUILTINS, # enable TensorFlow Lite ops.
tf.lite.OpsSet.SELECT_TF_OPS # enable TensorFlow ops.
]
_tflite_model = _converter.convert()
del _converter
@rahulremanan
rahulremanan / Python_generators--DICOM_reader_seqeunce--04.py
Last active April 2, 2023 11:48
A DICOM reader sequence object using Tensorflow Keras Sequence API
class DicomGenerator(tf.keras.utils.Sequence):
def __init__(self, dicom_path, batch_size=1, dtype='float32',
shuffle=False, drop_remainder=False,
preserve_batch_size=False, **kwargs):
self.i = 0
self.batch_size = batch_size
self.dtype = dtype
self.n = len(dicom_path)
self.dicom_path = dicom_path
self.drop_remainder = drop_remainder