Skip to content

Instantly share code, notes, and snippets.

@ruslangrimov
ruslangrimov / pytorch_repr.py
Created December 27, 2019 11:02
Reproducible pytorch cuda
np.random.seed(42)
random.seed(42)
cudnn.benchmark = False
cudnn.deterministic = True
torch.manual_seed(42)
torch.cuda.manual_seed_all(42)
torch.set_printoptions(precision=10)
@ruslangrimov
ruslangrimov / clear_notebook.sh
Created December 3, 2019 17:13
Clear large jupyter notebook
jupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace Notebook.ipynb
@ruslangrimov
ruslangrimov / intercept_ajax.js
Last active August 29, 2019 11:48
Intercept any ajax request
(function(open) {
XMLHttpRequest.prototype.open = function(method, URL, async, user, password) {
if (URL.indexOf("xxxx") > -1) {
// console.log(method, URL, async, user, password);
this.addEventListener('readystatechange', function () {
if(this.responseText !== '' && this.readyState == 4) {
var oldResponseText = this.responseText;
// ...
var newResponseText = 'blabla';
Object.defineProperty(this, 'responseText', {
for file in $(find ../../files/ -name '*.pkl')
do
name=${file##*/}
name=${name%.*}
name=${name/"_xxxx"/"_zzzzz"}
folder=$(dirname ${file})
python -u pkl_to_json.py -i "${file}" -o "${folder}/${name}.json"
done
@ruslangrimov
ruslangrimov / get_list_of_full_file_names.py
Created June 6, 2019 16:06
Get list of full file names
files = [os.path.join(r, f) for r, d, fs in os.walk(PATH) for f in fs if f.endswith('.png')]
conda uninstall --force pillow -y
# install libjpeg-turbo to $HOME/turbojpeg
git clone https://github.com/libjpeg-turbo/libjpeg-turbo
pushd libjpeg-turbo
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX:PATH=$HOME/turbojpeg
make
make install
@ruslangrimov
ruslangrimov / keras_split_last_layer.py
Last active August 28, 2018 18:11
Split the last layer of a keras model into a linear layer and an activation layer
import tempfile
from keras.models import load_model
from keras import activations
from keras.layers import Activation
def split_last_layer(model):
def apply_modifications(model, custom_objects=None):
model_path = os.path.join(tempfile.gettempdir(), next(tempfile._get_candidate_names()) + '.h5')
try:
model.save(model_path)
@ruslangrimov
ruslangrimov / keras-theano-to-tf.py
Last active July 16, 2018 19:56
Convert Keras Theano model to TensorFlow model
import os
import keras.backend as K
from keras.models import load_model, model_from_json
import numpy as np
import json
def switch_backend(backend):
if backend == 'theano':
# os.environ['KERAS_BACKEND'] = 'theano'
@ruslangrimov
ruslangrimov / calculate_receptive_field_size.py
Last active July 16, 2018 19:54
Get receptive field size of a neuron
'''
Code from https://medium.com/mlreview/a-guide-to-receptive-field-arithmetic-for-convolutional-neural-networks-e0f514068807
'''
import math
# [kernel_size, stride, pad_size], ...
convnet = [[8, 1, 0], [2, 2, 0],
[5, 1, 0], [2, 2, 0],
[3, 1, 0], [2, 2, 0],
[2, 1, 0], [2, 2, 0]
]
@ruslangrimov
ruslangrimov / get_gpu_info.py
Created June 16, 2018 21:25
GPU free memory
from subprocess import Popen, PIPE
import pandas as pd
SYMBOLS = {
'customary': ('B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'),
'customary_ext': ('byte', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa',
'zetta', 'iotta'),
'iec': ('Bi', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'),
'iec_60027_2': ('BiB', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB',
'YiB'),