Skip to content

Instantly share code, notes, and snippets.

View deshwalmahesh's full-sized avatar
🎯
Focusing

Mahesh Deshwal deshwalmahesh

🎯
Focusing
  • Chegg
  • India
View GitHub Profile
# Get top-K accuracy from results. Given a query and its related results, it'll find if any of the Ground Truth are in results.
# Or How many of the results are in Ground Truth
import numpy as np
def get_relative_acc(test_result_indices:[np.ndarray,list],similar_image_indices:[np.ndarray,list])->float:
'''
Check HOW MANY similar images are in returned results for single test image
'''
# because then only it'll be relative. If an image has 2,3,4 similar images, it won't matter. It'll normalize relative acc
@deshwalmahesh
deshwalmahesh / generate_yolo_annot.py
Last active April 19, 2021 16:02
Generate Yolov4 Darknet type Annotations from Bounding Boxe given as (x,y,w,h) and Vice Versa. Also with Yolov4 weights and config file, it generates files for each image. You can use it to extend your data. Creates a classes.txt file in the same DIR as LabelImg can fetch that. Open LabelImg and open the DIR after executing code to verify. Calcu…
import cv2
from PIL import Image
import numpy as np
import glob
def select_box(results:np.ndarray,method:str)->int:
'''
Select a Single BB based on Max Probability or Max area logic
args:
results: Pass in the results by detection module in (classes, scores, boxes) format
@deshwalmahesh
deshwalmahesh / latex_cleaner.py
Last active January 8, 2022 10:39
Clean the latex. Use Dict with t tokenization method for faster execution
def process(sentence,use_lemmetization:False, use_stemming:False, add_pos: False, remove_length:bool = False):
if not isinstance(sentence,str):
return ''
# convert the characters into lower case
a = sentence.lower()
# remomve newline character
a = re.sub(r"\n+", " ", a)
@deshwalmahesh
deshwalmahesh / spam_the_spammer.py
Created September 12, 2023 04:33
Tired of people spamming you on WhatsApp? Don't block them from now on. Just Run this code and you Spam them. What's good? If it's a person, they'll be similarly annoyed and if they're a company, their WhatsApp API will go higher because they' are who they are and will reply via their bot :)
# pip install selenium
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
@deshwalmahesh
deshwalmahesh / losses.py
Last active March 8, 2024 10:07
EMD Loss with Confidence and Weights
import torch
import torch.nn as nn
class WeightedEMDLossWithConfidencePenalty(nn.Module):
"""
Original Idea of why this Loss:
1. If the actual class was "0" and model predicted class "4", then I want it to penalise more than if it would have predicted it class "2": Addressed by the Vanilla EMD Loss itself
2. Penalization based on classes. For example, if the weights are [2,2,1,1,1] then it penalizes twice if the wrong prediction is from Class "0" or "1" vs when the wrong predictions were from the remaining classes: Addressed by class_weights
3. If the model is correctly predicting the right class but with a Low confidence, penalise it. For example if the True class was class 0" and my model predicted class "0" BUT I want to penalise it more when it predicted with a probability of 0.6 than when it predicted with a probability of 0.9: Addressed by the `correct_pred_low_prob_penalty` logic
4. If the model is Wrongly predicting the class and that too with a high confidence, penalise it ev