From 751dac2e1b8d12baed7b6561622ab6839f8e25a6 Mon Sep 17 00:00:00 2001 From: blocator23 Date: Mon, 4 Aug 2025 03:48:01 -0600 Subject: [PATCH] visual addings and improvements. I've implemented a classification model and Scikit-image to support the cursor's visual feedback. --- .../scikit-image | 343 ++++++++++++++++++ .../visual feedback implementation | 87 +++++ .../visual feedback implementation json | 17 + 3 files changed, 447 insertions(+) create mode 100644 Classification Model and Scikit-image/scikit-image create mode 100644 Classification Model and Scikit-image/visual feedback implementation create mode 100644 Classification Model and Scikit-image/visual feedback implementation json diff --git a/Classification Model and Scikit-image/scikit-image b/Classification Model and Scikit-image/scikit-image new file mode 100644 index 000000000..17745b749 --- /dev/null +++ b/Classification Model and Scikit-image/scikit-image @@ -0,0 +1,343 @@ +import skimage as ski +camera = ski.data.camera() +type(camera) + + +ccamera.shape +(512, 512) +camera.size +262144 + +camera.min(), camera.max() +(0, 255) +camera.mean() +118.31400299072266 + +# Get the value of the pixel at the 10th row and 20th column +camera[10, 20] +153 +# Set to black the pixel at the 3rd row and 10th column +camera[3, 10] = 0 + +# Set the first ten lines to "black" (0) +camera[:10] = 0 + +mask = camera < 87 +# Set to "white" (255) the pixels where mask is True +camera[mask] = 255 + +import numpy as np +inds_r = np.arange(len(camera)) +inds_c = 4 * inds_r % len(camera) +camera[inds_r, inds_c] = 0 + +nrows, ncols = camera.shape +row, col = np.ogrid[:nrows, :ncols] +cnt_row, cnt_col = nrows / 2, ncols / 2 +outer_disk_mask = ((row - cnt_row)**2 + (col - cnt_col)**2 > + (nrows / 2)**2) +camera[outer_disk_mask] = 0 + +lower_half = row > cnt_row +lower_half_disk = np.logical_and(lower_half, outer_disk_mask) +camera = data.camera() +camera[lower_half_disk] = 0 + +cat = ski.data.chelsea() +type(cat) + +cat.shape +(300, 451, 3) + +cat[10, 20] +array([151, 129, 115], dtype=uint8) +# Set the pixel at (50th row, 60th column) to "black" +cat[50, 60] = 0 +# set the pixel at (50th row, 61st column) to "green" +cat[50, 61] = [0, 255, 0] # [red, green, blue] + +import numpy as np +import scipy as sp +import skimage as ski +rng = np.random.default_rng() +im3d = rng.random((100, 1000, 1000)) +seeds = sp.ndimage.label(im3d < 0.1)[0] +ws = ski.segmentation.watershed(im3d, seeds) + +slics = ski.segmentation.slic(im3d, spacing=[5, 1, 1], channel_axis=None) + +edges = np.empty_like(im3d) +for pln, image in enumerate(im3d): + # Iterate over the leading dimension + edges[pln] = ski.filters.sobel(image) + +def in_order_multiply(arr, scalar): + for plane in list(range(arr.shape[0])): + arr[plane, :, :] *= scalar + +def out_of_order_multiply(arr, scalar): + for plane in list(range(arr.shape[2])): + arr[:, :, plane] *= scalar + +import time +rng = np.random.default_rng() +im3d = rng.random((100, 1024, 1024)) +t0 = time.time(); x = in_order_multiply(im3d, 5); t1 = time.time() +print("%.2f seconds" % (t1 - t0)) +0.14 seconds +s0 = time.time(); x = out_of_order_multiply(im3d, 5); s1 = time.time() +print("%.2f seconds" % (s1 - s0)) +1.18 seconds +print("Speedup: %.1fx" % ((s1 - s0) / (t1 - t0))) +Speedup: 8.6x + +for timepoint in image5d: + # Each timepoint is a 3D multichannel image + do_something_with(timepoint) + + import numpy as np +import skimage as ski +image = np.arange(0, 50, 10, dtype=np.uint8) +print(image.astype(float)) # These float values are out of range. +[ 0. 10. 20. 30. 40.] +print(ski.util.img_as_float(image)) +[ 0. 0.03921569 0.07843137 0.11764706 0.15686275] + +import skimage as ski +image = np.array([0, 0.5, 1], dtype=float) +ski.util.img_as_ubyte(image) +array([ 0, 128, 255], dtype=uint8) + +image = np.array([0, 0.5, 0.503, 1], dtype=float) +ski.util.img_as_ubyte(image) +array([ 0, 128, 128, 255], dtype=uint8) + +image = ski.data.coins() +image.dtype, image.min(), image.max(), image.shape +(dtype('uint8'), 1, 252, (303, 384)) +rescaled = ski.transform.rescale(image, 0.5) +(rescaled.dtype, np.round(rescaled.min(), 4), + np.round(rescaled.max(), 4), rescaled.shape) +(dtype('float64'), 0.0147, 0.9456, (152, 192)) +rescaled = ski.transform.rescale(image, 0.5, preserve_range=True) +(rescaled.dtype, np.round(rescaled.min()), + np.round(rescaled.max()), rescaled.shape) +(dtype('float64'), 4.0, 241.0, (152, 192)) + +out = ski.util.img_as_uint(sobel(image)) +plt.imshow(out) + +out = ski.util.img_as_uint(sobel(image)) +plt.imshow(out) + +image = image[:, :, ::-1] + +import skimage as ski +image = ski.util.img_as_float(any_opencv_image) + +import skimage as ski +cv_image = ski.util.img_as_ubyte(any_skimage_image) + +import skimage as ski +image = ski.util.img_as_float(func1(func2(image))) +processed_image = custom_func(image) + def custom_func(image): + image = ski.util.img_as_float(image) + # do something + +processed_image = custom_func(func1(func2(image))) + +import skimage as ski +image = ski.exposure.rescale_intensity(img10bit, in_range=(0, 2**10 - 1)) + +image = ski.exposure.rescale_intensity(img10bit, in_range='uint10') + +image = ski.exposure.rescale_intensity(img_int32, out_range=(0, 2**31 - 1)) +img_uint8 = ski.util.img_as_ubyte(image) + +skimage/io/_plugins/mpl.py +skimage/io/_plugins/mpl.ini + +[mpl] <-- name of the plugin, may be anything + +# This is mpl.py + +[mpl] +provides = imshow, _app_show + +import matplotlib.pyplot as plt + +def imshow(img): + plt.imshow(img) +description = Matplotlib image I/O plugin +provides = imshow <-- a comma-separated list, one or more of + imshow, imsave, imread, _app_show + +# This is mpl.py + +import matplotlib.pyplot as plt + +def imshow(img): + plt.imshow(img) + +def _app_show(): + plt.show() + + +import skimage as ski +ski.io.find_available_plugins() +{'gtk': ['imshow'], + 'matplotlib': ['imshow', 'imread', 'imread_collection'], + 'pil': ['imread', 'imsave', 'imread_collection'], + 'test': ['imsave', 'imshow', 'imread', 'imread_collection'],} + +ski.io.find_available_plugins(loaded=True) +{'matplotlib': ['imshow', 'imread', 'imread_collection'], + 'pil': ['imread', 'imsave', 'imread_collection']} + +ski.io.use_plugin('pil') # Use all capabilities provided by PIL + +ski.io.use_plugin('pil', 'imread') # Use only the imread capability of PIL + +ski.io.plugin_info('pil') + +{'description': 'Image reading via the Python Imaging Library', + 'provides': 'imread, imsave'} + + # bright saturated red +red_pixel_rgb = np.array([[[255, 0, 0]]], dtype=np.uint8) +color.rgb2hsv(red_pixel_rgb) +array([[[ 0., 1., 1.]]]) +# darker saturated blue +dark_blue_pixel_rgb = np.array([[[0, 0, 100]]], dtype=np.uint8) +color.rgb2hsv(dark_blue_pixel_rgb) +array([[[ 0.66666667, 1. , 0.39215686]]]) +# less saturated pink +pink_pixel_rgb = np.array([[[255, 100, 255]]], dtype=np.uint8) +color.rgb2hsv(pink_pixel_rgb) +array([[[ 0.83333333, 0.60784314, 1. ]]]) + +import skimage as ski +img_rgba = ski.data.logo() +img_rgb = ski.color.rgba2rgb(img_rgba) + +img = ski.data.astronaut() +img_gray = ski.color.rgb2gray(img) + +red_pixel = np.array([[[255, 0, 0]]], dtype=np.uint8) +ski.color.rgb2gray(red_pixel) +array([[ 0.2125]]) +green_pixel = np.array([[[0, 255, 0]]], dtype=np.uint8) +ski.color.rgb2gray(green_pixel) +array([[ 0.7154]]) + +import skimage as ski +img = ski.data.camera() +inverted_img = ski.util.invert(img) + +import numpy as np +import skimage as ski +image = np.array([[1, 3], [1, 1]]) +ski.exposure.histogram(image) +(array([3, 0, 1]), array([1, 2, 3])) + +import skimage as ski +text = ski.data.text() +text.min(), text.max() +(10, 197) +better_contrast = ski.exposure.rescale_intensity(text) +better_contrast.min(), better_contrast.max() +(0, 255) + +moon = ski.data.moon() +v_min, v_max = np.percentile(moon, (0.2, 99.8)) +v_min, v_max +(10.0, 186.0) +better_contrast = ski.exposure.rescale_intensity(moon, in_range=(v_min, v_max)) + +import skimage as ski +img = ski.data.astronaut() +top_left = img[:100, :100] + +from skimage import data, color +from skimage.transform import rescale, resize, downscale_local_mean + +image = color.rgb2gray(data.astronaut()) + +image_rescaled = rescale(image, 0.25, anti_aliasing=False) +image_resized = resize( + image, (image.shape[0] // 4, image.shape[1] // 4), anti_aliasing=True +) +image_downscaled = downscale_local_mean(image, (4, 3)) + +import numpy as np +import skimage as ski + +tform = ski.transform.EuclideanTransform( + rotation=np.pi / 12., + translation = (100, -20) + ) + + matrix = np.array([[np.cos(np.pi/12), -np.sin(np.pi/12), 100], + [np.sin(np.pi/12), np.cos(np.pi/12), -20], + [0, 0, 1]]) +tform = ski.transform.EuclideanTransform(matrix) + +img = ski.util.img_as_float(ski.data.chelsea()) +tf_img = ski.transform.warp(img, tform.inverse) + +text = ski.data.text() + +src = np.array([[0, 0], [0, 50], [300, 50], [300, 0]]) +dst = np.array([[155, 15], [65, 40], [260, 130], [360, 95]]) + +tform3 = ski.transform.ProjectiveTransform() +tform3.estimate(src, dst) +warped = ski.transform.warp(text, tform3, output_shape=(50, 300)) + +import skimage as ski + +def task(image): + """ + Apply some functions and return an image. + """ + image = ski.restoration.denoise_tv_chambolle( + image[0][0], weight=0.1, channel_axis=-1 + ) + fd, hog_image = ski.feature.hog( + ski.color.rgb2gray(image), + orientations=8, + pixels_per_cell=(16, 16), + cells_per_block=(1, 1), + visualize=True + ) + return hog_image + + +# Prepare images +hubble = ski.data.hubble_deep_field() +width = 10 +pics = ski.util.view_as_windows( + hubble, (width, hubble.shape[1], hubble.shape[2]), step=width +) + +def classic_loop(): + for image in pics: + task(image) + + +%timeit classic_loop() + +def comprehension_loop(): + [task(image) for image in pics] + +%timeit comprehension_loop() + +from joblib import Parallel, delayed +def joblib_loop(): + Parallel(n_jobs=4)(delayed(task)(i) for i in pics) + +%timeit joblib_loop() + +import skimage as ski +ski.util.lookfor('eigenvalue') \ No newline at end of file diff --git a/Classification Model and Scikit-image/visual feedback implementation b/Classification Model and Scikit-image/visual feedback implementation new file mode 100644 index 000000000..86271afb8 --- /dev/null +++ b/Classification Model and Scikit-image/visual feedback implementation @@ -0,0 +1,87 @@ +from sklearn.model_selection import train_test_split + +X = data.content +y = data.language + +data.dropna(inplace=True) +data.drop_duplicates(inplace=True) + +# Train/Test split +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) + +from nltk.tokenize.regexp import WhitespaceTokenizer +WhitespaceTokenizer().tokenize("This is a whitespace tokenizer!") # ['This', 'is', 'a', 'whitespace', 'tokenizer!'] + +import pandas as pd +data = pd.read_csv("file.csv") +from sklearn.preprocessing import FunctionTransformer + +def preprocess(x): + return pd.Series(x).replace(r'\b([A-Za-z])\1+\b', '', regex=True)\ + .replace(r'\b[A-Za-z]\b', '', regex=True) + +transformer = FunctionTransformer(preprocess) + +from sklearn.feature_extraction.text import TfidfVectorizer + +token_pattern = r"""([A-Za-z_]\w*\b|[!\#\$%\&\*\+:\-\./<=>\?@\\\^_\|\~]+|[ \t\(\),;\{\}\[\]"'`])""" + +vectorizer = TfidfVectorizer(token_pattern=token_pattern, max_features=3000) + +from sklearn.preprocessing import FunctionTransformer +from sklearn.pipeline import Pipeline +from sklearn.model_selection import GridSearchCV + +# Four Models to be compared +from sklearn.naive_bayes import GaussianNB +from sklearn.linear_model import LogisticRegression +from sklearn.ensemble import RandomForestClassifier +from sklearn.neural_network import MLPClassifier + + +clf_pipe = Pipeline([('preprocessing', transformer), ('vectorizer', vectorizer), ('clf', clf)]) +GNB_pipe = Pipeline([('transformer', FunctionTransformer(lambda x: x.todense(), accept_sparse=True)), ('GNB', GaussianNB())]) + +param_grid = dict(clf=[GNB_pipe, LogisticRegression(max_iter = 400), RandomForestClassifier(), MLPClassifier(max_iter = 400))]) +grid_search = GridSearchCV(clf_pipe, param_grid=param_grid, cv=3) +grid_search.fit(X_train, y_train) + +FunctionTransformer(lambda x: x.todense(), accept_sparse=True) + +final_clf = grid_search.best_estimator_ # Estimator that was chosen by the search (highest score) +best_score = grid_search.best_score_ # Mean cross-validated score of the best_estimator +comparaison_result = pd.DataFrame(grid_search.cv_results_).sort_values(by=['rank_test_score']) # Table detailing CV results for each model + +# Defining classifier + +best_params = grid_search_RF.best_params_ + +{ + 'clf__criterion': 'gini', + 'clf__max_features': 'sqrt', + 'clf__min_samples_split': 3, + 'clf__n_estimators': 300 +} +clf = RandomForestClassifier(n_jobs=4) + +# RandomForest parameters +param_grid_RF = {"clf__n_estimators" : [200, 300, 400] , + "clf__criterion" : ["gini", "entropy"], + "clf__min_samples_split" : [2, 3], + "clf__max_features" : ["sqrt", None, "log2"] + } + +#### Grid search on RandomForest +pipe_RF = Pipeline([('preprocessing', transformer), + ('vectorizer', vectorizer), + ('clf', clf)]) +grid_search_RF = GridSearchCV(pipe_RF, param_grid=param_grid_RF, cv=3) +grid_search_RF.fit(X_train, y_train) + +pipe_RF.set_params(**best_params) + +import re +from lime.lime_text import LimeTextExplainer + +explainer = LimeTextExplainer(class_names=model.classes_, split_expression=lambda x: re.findall(token_pattern, x)) + diff --git a/Classification Model and Scikit-image/visual feedback implementation json b/Classification Model and Scikit-image/visual feedback implementation json new file mode 100644 index 000000000..4af58a4b6 --- /dev/null +++ b/Classification Model and Scikit-image/visual feedback implementation json @@ -0,0 +1,17 @@ +import json + +file_path = "YOUR_JSON_FILE_PATH" +with open(file_path, 'r') as f: + lang_table = json.load(f) + +data["extension"] = data["sample_path"].apply(lambda x: x.split('.')[-1]) +data["language"] = data["extension"].replace({ext: lang for lang, exts in lang_table.items() for ext in exts}) + +import json + +file_path = "YOUR_JSON_FILE_PATH" +with open(file_path, 'r') as f: + lang_table = json.load(f) + +data["extension"] = data["sample_path"].apply(lambda x: x.split('.')[-1]) +data["language"] = data["extension"].replace({ext: lang for lang, exts in lang_table.items() for ext in exts}) \ No newline at end of file