Skip to content

Instantly share code, notes, and snippets.

View phizaz's full-sized avatar
😀

Konpat phizaz

😀
View GitHub Profile
@phizaz
phizaz / async.py
Last active April 3, 2024 15:44
Python turn sync functions to async (and async to sync)
import functools
def force_async(fn):
'''
turns a sync function to async function using threads
'''
from concurrent.futures import ThreadPoolExecutor
import asyncio
pool = ThreadPoolExecutor()
@phizaz
phizaz / inspect-trained-model.ipynb
Last active September 12, 2023 20:31
Pytorch: Inspect Activations Layer-by-layer in Trained Model
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@phizaz
phizaz / keybindings.json
Created February 24, 2017 14:25
VSCode Settings
// Place your key bindings in this file to overwrite the defaults
[{
"key": "ctrl+a",
"command": "editor.action.selectAll"
},
{
"key": "ctrl+x",
"command": "editor.action.clipboardCutAction"
},
{
@phizaz
phizaz / mimic_img_text.py
Created March 24, 2021 06:28
MIMIC's chest x-ray for pair image and text dataset
import os
import random
import cv2
import pandas as pd
import torch
from torch.utils.data import Dataset
from transformers import (BertTokenizerFast, PreTrainedTokenizerFast,
RobertaTokenizerFast)
@phizaz
phizaz / li2018.py
Last active January 25, 2021 14:15
li2018
from segmentation_models_pytorch.encoders import get_encoder
from .mil import MILPool
def make_net_li2018(
backbone,
n_out,
n_in=1,
n_dec_ch=512,
out_size=20,
@phizaz
phizaz / filelock.py
Created October 5, 2020 15:28
filelock.py
import errno
import os
import time
from collections import defaultdict
from contextlib import ContextDecorator, contextmanager
from itertools import count
import torch
CUDA_ALLOC_FILE = os.path.expanduser('~/mlkit.alloc')
@phizaz
phizaz / program.cs
Created June 21, 2017 10:50
C# Producer-Consumer Pattern with Timeout and Return Value
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ProducerConsumer
{
@phizaz
phizaz / til.py
Last active June 4, 2020 14:44
Blocks until a given time (Python)
"""
blocks until a specific time,
useful for planning to run a script at a specific time
usage:
python til.py 13:00 tomorrow && python script.py
"""
import dateparser
from datetime import datetime, timedelta
@phizaz
phizaz / live-output.py
Created January 20, 2018 17:56
python subprocess's Popen live-output with "select"
from select import select
import os
import time
import subprocess
from contextlib import contextmanager
@contextmanager
def pipe():
r, w = os.pipe()
@phizaz
phizaz / catchableprocess.py
Last active May 19, 2019 00:35
Python CatchableProcess: Catch exceptions in spawned processes
from multiprocessing import Process
class CatchableProcess(Process):
def __init__(self, target, args=[], kwargs=dict()):
from multiprocessing import Queue
self.error = Queue(1)
self.result = Queue(1)
def wrapper(target, args, kwargs):
try: