Skip to content

Instantly share code, notes, and snippets.

View phizaz's full-sized avatar
😀

Konpat phizaz

😀
View GitHub Profile
@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 / 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 / inspect-trained-model.ipynb
Last active September 12, 2023 20:31
Pytorch: Inspect Activations Layer-by-layer in Trained Model
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@phizaz
phizaz / tensorflow_summary_v2_eager.py
Created July 8, 2018 14:32
Tensorflow summary v2 eager mode
import tensorflow as tf
import numpy as np
tf.enable_eager_execution()
save_path = 'logs/test10'
graph = tf.Graph()
with graph.as_default():
global_step = tf.train.create_global_step()
writer = tf.contrib.summary.create_file_writer(save_path)
@phizaz
phizaz / tensorflow_summary_v2_graph.py
Created July 8, 2018 14:20
Tensorflow summary v2 graph
import tensorflow as tf
import numpy as np
save_path = 'summary_path'
graph = tf.Graph()
with graph.as_default():
global_step = tf.train.create_global_step()
writer = tf.contrib.summary.create_file_writer(save_path)
with writer.as_default():
tf.contrib.summary.always_record_summaries()
import tensorflow.contrib.summary as summary
global_step = tf.train.get_or_create_global_step()
optimizer = tf.train.AdamOptimizer(lr).minimize(..., global_step=global_step)
writer = summary.create_file_writer('log')
writer.set_as_default()
summary.always_record_summaries()
with summary.record_summaries_every_n_global_steps(1):
@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 / keras_2dconv_layer.py
Last active December 6, 2017 15:09
Keras 2D Conv Layer without Conv2D
class MyConv(Layer):
'''
Implemeting a Conv2D with strides=1, and 'valid' padding
'''
def __init__(self, filters, kernel, **kwargs):
self.filters = filters
self.k_h, self.k_w = kernel
super(MyConv, self).__init__(**kwargs)