Skip to content

Instantly share code, notes, and snippets.

View sukhitashvili's full-sized avatar

Soso Sukhitashvili sukhitashvili

View GitHub Profile
@sukhitashvili
sukhitashvili / private_fork.md
Created March 23, 2024 20:32 — forked from 0xjac/private_fork.md
Create a private fork of a public repository

The repository for the assignment is public and Github does not allow the creation of private forks for public repositories.

The correct way of creating a private frok by duplicating the repo is documented here.

For this assignment the commands are:

  1. Create a bare clone of the repository. (This is temporary and will be removed so just do it wherever.)

git clone --bare [email protected]:usi-systems/easytrace.git

from typing import Optional, List
import numpy as np
from skimage.measure import label, regionprops, find_contours
def mask_to_border(mask):
h, w = mask.shape
border = np.zeros((h, w))
contours = find_contours(mask, 128)
@sukhitashvili
sukhitashvili / binary_otsu.py
Created March 22, 2023 18:47
Does Otsu binarization on an input array of floats
def binary_otsu(input_vector: list) -> float:
"""
Otsu's method to find the best threshold for a binary image or vector
"""
pixel_number = len(input_vector)
mean_weight = 1.0 / pixel_number
his, bins = np.histogram(input_vector, bins=50)
final_thresh = -1
final_value = -1
intensity_arr = bins[:-1]
@sukhitashvili
sukhitashvili / tip_for_multi_GPU_training.md
Last active January 26, 2023 13:54
Tip for multi GPU training

As a tip for multi GPU training, I want to say that there is rule called Linear Scaling Rule which claims that: When the minibatch size is multiplied by k, multiply the learning rate by k. . Here is the paper for full explanation and my shorter explanation is that with k number of GPUs your batch size is increased by k and thus you do k times less number of training iterations with given number of epochs. So, they find that you can simply scale your LR to overcome this Issue and train model K times faster to achieve the same accuracy performance as you could on single GPU machine with given number of epochs (but K times slower).

@sukhitashvili
sukhitashvili / draw_dashed_lines
Created January 19, 2023 11:09
draws polygon of [[x1, y1], ...] coordinated in dashed lines
from typing import Tuple, List
import numpy as np
def drawline(img, pt1, pt2, color, thickness=1, gap=20):
dist = ((pt1[0] - pt2[0]) ** 2 + (pt1[1] - pt2[1]) ** 2) ** .5
pts = []
if dist == 0:
return img
for i in np.arange(0, dist, gap):
@sukhitashvili
sukhitashvili / resize_coords.pu
Created January 13, 2023 13:30
Resized coordinates
def resize_coordinates(coords: np.ndarray, old_size: tuple, new_size: tuple):
"""
:param coords: coordinates as numpy array of shape (N, 2) [x, y] format
:param old_size: old size as (width, height)
:param new_size: new size as (width, height)
:return: resized coordinates as numpy array of shape (N, 2)
"""
# Calculate the scale factor
scale_factor = np.array(new_size) / np.array(old_size)
@sukhitashvili
sukhitashvili / generate_n_colors.pt
Created January 5, 2023 11:10
Generates N color tuples for opencv-python
import colorsys
def hsv_to_rgb(h, s, v):
r, g, b = colorsys.hsv_to_rgb(h, s, v)
return [int(255 * i) for i in [r, g, b]]
def get_n_colors(n: int):
"""
Generates n distinct colors
@sukhitashvili
sukhitashvili / image_to_json_and_json_to_image.py
Created December 1, 2022 09:33
Image to json and json to image util functions
import base64
import numpy as np
def img_to_json(img):
return {
'data': base64.b64encode(img.tobytes()).decode('utf8'),
'shape': list(img.shape)
}
@sukhitashvili
sukhitashvili / non_maximum_suppression.py
Last active September 5, 2023 11:59
non maximum suppression
import torch
def get_iou(pred1: torch.Tensor, pred2: torch.Tensor):
"""
prediction: [x1, y1, x2, y2, conf, cls]
"""
ar1 = abs((pred1[2] - pred1[0]) * (pred1[3] - pred1[1]))
ar2 = abs((pred2[2] - pred2[0]) * (pred2[3] - pred2[1]))
x1 = torch.max(pred1[0], pred2[0])
y1 = torch.max(pred1[1], pred2[1])
@sukhitashvili
sukhitashvili / resize_bbox.py
Last active October 10, 2022 14:03
Resize bbox to new image size
import numpy as np
def rescale_bounding_boxes(original_shape: tuple, new_shape: tuple,
bounding_boxes: list) -> list:
"""
Rescales bounding box coords according to new image size
:param original_shape: (W, H)
:param new_shape: (W, H)