EFI boot manager

Update EFI Boot Manager. Add new bootloaders for Windows OS and Linux Mint OS # Print available disks (drives) and partitions lsblk -o name,UUID,partuuid,mountpoint # Assumption ## Windows OS bootloader is installed on /dev/nvme0n1 ## Linux OS bootloader is installed on /dev/nvme1n1 # Add Windows bootloader entry sudo efibootmgr -c -d /dev/nvme0n1 -p 1 -l \\EFI\\Microsoft\\Boot\\bootmgfw.efi -L "Windows Boot Manager" # Add Linux Mint bootloader entry # shimx64.efi supports SecureBoot sudo efibootmgr -c -d /dev/nvme1n1 -p 1 -l \\efi\\debian\\shimx64.efi -L "Linux Mint" # Print the bootloader list sudo efibootmgr # Output: ## BootCurrent: 0000 ## Timeout: 2 seconds ## BootOrder: 0000,0001,0019,0003,0010,0011,0012,0013,0017,0018,001A,001B,001C,001D,001E ## Boot0000* Linux Mint ## Boot0001* Windows Boot Manager ## Boot0003* ubuntu # Optional: Remove unused bootloaders ## Example: Remove the Boot0003* ubuntu bootloader ## First, inactivate Boot0003 (ID 3) ## sudo efibootmgr -b 3 -A ## Finally, remove Boot0003 (ID 3) ## sudo efibootmgr -b 3 -B # Print the updated bootloader list ## sudo efibootmgr # Update the grub configuration sudo update-grub # Reboot and test

August 15, 2024 · 1 min

pipx Setup

Install pipx on Windows Run commands in Git Bash env PIP_REQUIRE_VIRTUALENV=0 python -m pip install --user pipx python -m pipx ensurepath --force Open a new Git Bash terminal pipx --version If getting an error during the installation of packages using pipx No Python at 'C:\Users\<username>\AppData\Local\Programs\Python\PythonX\python.exe' Then remove the following folder C:\Users\<username>\.local\pipx If still having the same issue Then remove the following folder as well C:\Users\<username>\AppData\Local\pipx

May 30, 2024 · 1 min

Keras Transfer Learning

Transfer learning with VGG and Keras for image classification task # Credits: # Author: Gabriel Cassimiro # Blog post: https://towardsdatascience.com/transfer-learning-with-vgg16-and-keras-50ea161580b4 # GitHub Repo: https://github.com/gabrielcassimiro17/object-detection # import tensorflow_datasets as tfds from tensorflow.keras import layers, models from tensorflow.keras.utils import to_categorical from tensorflow.keras.callbacks import EarlyStopping ## Loading images and labels (train_ds, train_labels), (test_ds, test_labels) = tfds.load( "tf_flowers", split=["train[:70%]", "train[:30%]"], ## Train test split batch_size=-1, as_supervised=True, # Include labels ) ## Resizing images train_ds = tf.image.resize(train_ds, (150, 150)) test_ds = tf.image.resize(test_ds, (150, 150)) ## Transforming labels to correct format train_labels = to_categorical(train_labels, num_classes=5) test_labels = to_categorical(test_labels, num_classes=5) from tensorflow.keras.applications.vgg16 import VGG16 from tensorflow.keras.applications.vgg16 import preprocess_input ## Loading VGG16 model base_model = VGG16(weights="imagenet", include_top=False, input_shape=train_ds[0].shape) base_model.trainable = False ## Not trainable weights ## Preprocessing input train_ds = preprocess_input(train_ds) test_ds = preprocess_input(test_ds) flatten_layer = layers.Flatten() dense_layer_1 = layers.Dense(50, activation='relu') dense_layer_2 = layers.Dense(20, activation='relu') prediction_layer = layers.Dense(5, activation='softmax') model = models.Sequential([ base_model, flatten_layer, dense_layer_1, dense_layer_2, prediction_layer ]) model.compile( optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'], ) es = EarlyStopping(monitor='val_accuracy', mode='max', patience=5, restore_best_weights=True) model.fit(train_ds, train_labels, epochs=50, validation_split=0.2, batch_size=32, callbacks=[es]) model.evaluate(test_ds, test_labels)

April 9, 2023 · 1 min

Resize an image and bbox

Resize an image and bounding boxes: Reference: https://sheldonsebastian94.medium.com/resizing-image-and-bounding-boxes-for-object-detection-7b9d9463125a import albumentations from PIL import Image import numpy as np sample_img = Image.open("data/img1.jpg") sample_arr = np.asarray(sample_img) def resize_image(img_arr, bboxes, h, w): """ :param img_arr: original image as a numpy array :param bboxes: bboxes as numpy array where each row is 'x_min', 'y_min', 'x_max', 'y_max', "class_id" :param h: resized height dimension of image :param w: resized weight dimension of image :return: dictionary containing {image:transformed, bboxes:['x_min', 'y_min', 'x_max', 'y_max', "class_id"]} """ # create resize transform pipeline transform = albumentations.Compose( [albumentations.Resize(height=h, width=w, always_apply=True)], bbox_params=albumentations.BboxParams(format='pascal_voc')) transformed = transform(image=img_arr, bboxes=bboxes) return transformed transformed_dict = resize_image(sample_arr, bboxes_og, 224, 224) # contains the image as array transformed_arr = transformed_dict["image"] # contains the resized bounding boxes transformed_info = np.array(list(map(list, transformed_dict["bboxes"]))).astype(float)

November 17, 2022 · 1 min

Confusion matrix for cross validation

Confusion matrix for cross validation. Results are being saved as mlflow artifacts and retrieved later for evaluation purposes. import mlflow import numpy as np from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import precision_recall_fscore_support from sklearn.metrics import accuracy_score from sklearn.metrics import confusion_matrix from sklearn.datasets import load_breast_cancer from sklearn.model_selection import StratifiedKFold SEED = 42 mlflow.set_tracking_uri("http://127.0.0.1:5000") mlflow.set_experiment("cross-validation-average") # Data preparation dataset = load_breast_cancer() X = dataset.data y = dataset.target print(X.shape, y.shape) # Model training skf = StratifiedKFold(n_splits=5, random_state=SEED, shuffle=True) for train_index, test_index in skf.split(X, y): X_train, X_test = X[train_index], X[test_index] y_train, y_test = y[train_index], y[test_index] with mlflow.start_run(): mlflow.sklearn.autolog(exclusive=False) model = RandomForestClassifier(random_state=SEED) model.fit(X_train, y_train) y_pred = model.predict(X_test) mlflow.log_dict({"y_test": [int(x) for x in y_test], "y_pred": [int(x) for x in y_pred] }, "ytest-ypred.json") test_acc = accuracy_score(y_test, y_pred) mlflow.log_metric("test_accuracy", test_acc) print("test_accuracy:", test_acc) test_precision, test_recall, test_f1, _ = precision_recall_fscore_support( y_test, y_pred, average='binary' ) mlflow.log_metric("test_precision", test_precision) mlflow.log_metric("test_recall", test_recall) mlflow.log_metric("test_f1_score", test_f1) tn, fp, fn, tp = confusion_matrix(y_test, y_pred).ravel() mlflow.log_metric("tn", tn) mlflow.log_metric("fp", fp) mlflow.log_metric("fn", fn) mlflow.log_metric("tp", tp) tn, fp, fn, tp = confusion_matrix( y_test, y_pred, normalize="true" ).ravel() mlflow.log_metric("tn_normalized", tn) mlflow.log_metric("fp_normalized", fp) mlflow.log_metric("fn_normalized", fn) mlflow.log_metric("tp_normalized", tp) mlflow.sklearn.autolog(disable=True) # Results evaluation runs = mlflow.search_runs(experiment_ids=["2"]) columns = [ 'metrics.tn_normalized', 'metrics.fp_normalized', 'metrics.fn_normalized', 'metrics.tp_normalized' ] mean_confusion_matrix = runs[columns].mean() print(mean_confusion_matrix) Output: ...

November 2, 2022 · 1 min