Skip to content

Instantly share code, notes, and snippets.

@mkroutikov
mkroutikov / profiler.py
Created April 22, 2019 13:05
profile snippet
if __name__ == '__main__':
import cProfile
cProfile.run('unittest.main()', 'b1')
import pstats
p = pstats.Stats('b1')
p.sort_stats('cumulative').print_stats(20)
print()
@mkroutikov
mkroutikov / io.py
Created April 21, 2019 22:25
Write PyTorch models to cloud locations (requires tensorflow)
'''
Utilities to save/load torch models.
Same as built-in torch.save/torch.load, but support cloud URLs
'''
import tensorflow.gfile as gio
import io
import torch
@mkroutikov
mkroutikov / summary.py
Last active April 21, 2019 22:36
Hack to teach tensorboardX to write summaries to cloud locations
'''
Super dirty trick to replace io of tensorboardX.SummaryWriter with cloud-capable one (from tensorflow.gfile)
'''
import tensorboardX
import tensorflow.gfile as gio
from unittest import mock
import logging
import time
def make_overlay(imagename, box, outname):
'''creates a PNG image of base with overlayed colored boxes. Useful for human review'''
if 'size' not in box:
return
img = Image.open(imagename).convert('RGB')
draw = ImageDraw.Draw(img, 'RGBA')
for key,color in [
('title', (255, 0, 0, 128)),
@mkroutikov
mkroutikov / toylang.py
Created November 21, 2018 22:30
Generates Toy language sentences
import random
class Rule:
def __init__(self, name):
self.name = name
GRAMMAR = {
'op': ['+', '-', '/', '%', '*'],
'var': ['x', 'y', 'z', '1', '2', '3'],
import unittest
import queue
class BlockPipe:
'''a file-like object that implements Pipe protocol
using blocking queue
'''
def __init__(self, maxsize=0):
@mkroutikov
mkroutikov / Jenkinsfile.groovy
Last active October 25, 2017 15:52
Provision for training
pipeline {
// use only nodes marked as 'tensorflow'
agent { node { label 'tensorflow' } }
// build parameters - these are prompted interactively
parameters {
string(defaultValue: '', description: 'Problem Name', name: 'problem')
}
import socket
import json
from urllib import request
import time
from izutil.progress import NULL_PROGRESS_METER
class ILabsEngine:
URL_UPLOAD = 'https://api.innodatalabs.com/v1/documents/input/'
URL_PREDICT = 'https://api.innodatalabs.com/v1/reference/{domain}/{name}'
class DomainMap:
_map = None
@classmethod
def _get(cls):
raise NotImplementedError('heavy work done here (someday)')
@classmethod
def get(cls):
@mkroutikov
mkroutikov / snippet.py
Created July 12, 2017 11:31
Lazy-loading domain map
class DomainMap:
_domain_map = None
@classmethod
def _get(cls):
# do heavy lifting here
# result = ...
return result
@classmethod