Skip to content

Instantly share code, notes, and snippets.

View laurentmih's full-sized avatar

Laurent H. laurentmih

View GitHub Profile
@laurentmih
laurentmih / pytorchTutorials2PDF.py
Created July 4, 2019 14:27
Quick gist to convert all pyTorch tutorials to a nice PDF for offline use, using Chromium. Python3, POSIX-compliant systems only. Adapt for Windows by changing line 54.
import asyncio
from pyppeteer import launch
from PyPDF2 import PdfFileMerger
from pathlib import Path
merge_files = True
urls = [
'https://pytorch.org/tutorials/beginner/blitz/tensor_tutorial.html',
'https://pytorch.org/tutorials/beginner/blitz/autograd_tutorial.html#sphx-glr-beginner-blitz-autograd-tutorial-py',
@laurentmih
laurentmih / dummy_data.py
Last active June 12, 2019 14:22
Set up dummy data for a pytorch/fastai model
import torch
from torch.utils.data import TensorDataset, DataLoader
from fastai.basic_data import DataBunch
# This is a typical setup for images
# x images have 3 channels, 128x128
# y has 1 channel because I needed to
# predict segmentation
dataset_size = 4
x = torch.ones(dataset_size,3,128,128)#.cuda()
@laurentmih
laurentmih / replace_exif.py
Created September 14, 2018 20:47
Python function that replaces Exif data in an image based on the date-time passed to it. Relies on piexif and pillow packages.
def replace_exif(file, datetime_object):
"""
Replaces the createdTime Exif data of image with date parsed from the datetime_object
This function replaces the following three properties of the Exif data of
the image file that is passed to it:
Exif.Image.DateTime, at offset 306
Exif.Photo.DateTimeOriginal at offset 36867
Exif.Photo.DateTimeDigitized at offset 36868
This function expects that both PIL and piexif are imported
@laurentmih
laurentmih / urls.py
Created July 25, 2017 11:59
URLs for django-registration-redux simple backend
^account/ ^register/closed/$ [name='registration_disallowed']
^account/ ^register/$ [name='registration_register']
^account/ ^login/$ [name='auth_login']
^account/ ^logout/$ [name='auth_logout']
^account/ ^password/change/$ [name='auth_password_change']
^account/ ^password/change/done/$ [name='auth_password_change_done']
^account/ ^password/reset/$ [name='auth_password_reset']
^account/ ^password/reset/complete/$ [name='auth_password_reset_complete']
^account/ ^password/reset/done/$ [name='auth_password_reset_done']
^account/ ^password/reset/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$ [name='auth_password_reset_confirm']
@laurentmih
laurentmih / chartjs_categorical.js
Created June 21, 2017 22:06
ChartJS categorical bar chart: labels on both axes
// ChartJS charts
$(document).ready(function(){
// Coding/language barchart
var codingChart = $("#codinglevel");
var codingChartData = {
labels: ["Python ", "Octave/MATLAB ", "JavaScript "],
datasets: [{
data: [2.7, 2, 2],
backgroundColor: ["rgba(255, 99, 132, 0.2)", "rgba(255, 159, 64, 0.2)", "rgba(255, 205, 86, 0.2)"],
borderColor: ["rgb(255, 99, 132)", "rgb(255, 159, 64)", "rgb(255, 205, 86)"],
@laurentmih
laurentmih / UnityTransformerClass.md
Last active June 7, 2017 09:51
SKLearn Unity Transformer

I've been working on sklearn with the Kaggle Titanic set, and just discovered sklearn Pipelines and FeatureUnions. While following most of the content out there on how to work with the Titanic dataset, I started to rewrite all the usual functions into Transformers, that can be used in the FeatureUnion/Pipeline context.

Usually, you use FeatureUnions for (axis=1) concatenation of transformers output. Here, I created a UnityTransformer that effectively does nothing to the input, that way I could keep the original column, and concatenate it with a transformed version of itself.

The 'trick' here is in the transform method: just multiply the output by 1, a unity operation. I haven't extensively tested it, but it seems to work fine for strings as well as normal np.arrays. The whole thing feels a little hacky though, so I'll be happy to have feedback.

from sklearn.base import BaseEstimator, TransformerMixin

class IdentityTransformer(BaseEstimator, TransformerMixin):
@laurentmih
laurentmih / entity_overlap.py
Created March 17, 2017 15:35
Checking overlap in entities for RASA NLU with the MITIE backend
# This quick Python script should help you detect whether you have overlapping entities in your training data.
# These errors thrown by MITIE usually look like:
# Invalid range given to ner_training_instance.overlaps_any_entity(). It overlaps an entity given to a previous call to add_entity().
# The script assumes the form of the trainingdata is a JSON-encoded object.
# For an example of such a trainingset, see https://raw.githubusercontent.com/golastmile/rasa_nlu/master/data/examples/rasa/demo-rasa.json
# It should be trivial to adapt for other forms though.
# Tested on Python3