Skip to content

Instantly share code, notes, and snippets.

View tyommik's full-sized avatar

tyommik

View GitHub Profile
@tyommik
tyommik / flask_mjpeg.py
Created April 5, 2021 16:05
Flask MJPEG
import cv2
from flask import Flask, Response
from PIL import Image
from io import StringIO,BytesIO
import time
app = Flask(__name__)
frame_buffer = None
@tyommik
tyommik / sss_k_fold
Created March 10, 2021 17:15
Stratified Cross Validation
from collections import OrderedDict
import pandas as pd
from sklearn.model_selection import train_test_split, StratifiedShuffleSplit
import numpy as np
def one_hot_encoding(df, column, drop_column=True):
"""Do one hot encoding for DataFrame using particular column
@tyommik
tyommik / ProtonLaunch.sh
Created March 7, 2021 15:23 — forked from smo0z/ProtonLaunch.sh
Proton Launch Script
#!/bin/sh
# Application path
APP_PATH="$(dirname "${BASH_SOURCE[0]}")"
cd "$APP_PATH"
# Executable file
APP_EXEC="$APP_PATH/.exe"
# Steam / IDs
@tyommik
tyommik / README.md
Created November 18, 2020 19:28 — forked from M0r13n/README.md
Logging with Loguru in Flask

This is a simple example of how to use loguru in your flask application

Just create a new InterceptHandler and add it to your app. Different settings should be configured in your config file, so that it is easy to change settings.

Logging is then as easy as:

from loguru import logger

logger.info("I am logging from loguru!")

@tyommik
tyommik / focal_loss_label_smoothing
Created August 15, 2020 14:32
FocalLoss with LabelSmoothing
class LabelSmoothingLoss(nn.Module):
def __init__(self, classes, smoothing=0.0, dim=-1):
super(LabelSmoothingLoss, self).__init__()
self.confidence = 1.0 - smoothing
self.smoothing = smoothing
self.cls = classes
self.dim = dim
def forward(self, pred, target):
pred = pred.log_softmax(dim=self.dim)
@tyommik
tyommik / label_smoothing_CE
Created July 27, 2020 06:51
Fastai/PyTorch Implementation of Label Smoothing Cross Entropy loss
# Helper functions from fastai
def reduce_loss(loss, reduction='mean'):
return loss.mean() if reduction=='mean' else loss.sum() if reduction=='sum' else loss
# Implementation from fastai https://github.com/fastai/fastai2/blob/master/fastai2/layers.py#L338
class LabelSmoothingCrossEntropy(nn.Module):
def __init__(self, ε:float=0.1, reduction='mean'):
super().__init__()
self.ε,self.reduction = ε,reduction
@tyommik
tyommik / conver_dataparallel
Created July 22, 2020 17:36
Convert model from DataParallel
# original saved file with DataParallel
state_dict = torch.load('myfile.pth.tar')
# create new OrderedDict that does not contain `module.`
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in state_dict.items():
name = k[7:] # remove `module.`
new_state_dict[name] = v
# load params
model.load_state_dict(new_state_dict)
@tyommik
tyommik / average_best_checkpoints
Last active July 23, 2020 07:10
NN weights averaging
def average_ckh(models_paths: list):
params_dict = collections.OrderedDict()
params_keys = None
new_state = None
num_models = len(models_paths)
for f in models_paths:
state = torch.load(f, map_location='cpu')
if new_state is None:
new_state = state
@tyommik
tyommik / Threadsafe_iter.py
Created March 2, 2020 18:23 — forked from platdrag/Threadsafe_iter.py
An example generic wrapper for making any iterator / generator thread-safe compatible with python 3
import threading
'''
A generic iterator and generator that takes any iterator and wrap it to make it thread safe.
This method was introducted by Anand Chitipothu in http://anandology.com/blog/using-iterators-and-generators/
but was not compatible with python 3. This modified version is now compatible and works both in python 2.8 and 3.0
'''
class threadsafe_iter:
"""Takes an iterator/generator and makes it thread-safe by
serializing call to the `next` method of given iterator/generator.
"""
@tyommik
tyommik / iou.py
Created February 28, 2020 05:29 — forked from meyerjo/iou.py
Python code to compute the intersection of two boundingboxes
def bb_intersection_over_union(boxA, boxB):
# determine the (x, y)-coordinates of the intersection rectangle
xA = max(boxA[0], boxB[0])
yA = max(boxA[1], boxB[1])
xB = min(boxA[2], boxB[2])
yB = min(boxA[3], boxB[3])
# compute the area of intersection rectangle
interArea = abs(max((xB - xA, 0)) * max((yB - yA), 0))
if interArea == 0: