Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Gurucharan MK gurucharanmk

🎯
Focusing
  • Bangalore
View GitHub Profile
@gurucharanmk
gurucharanmk / torch_deterministic.py
Created Apr 2, 2020
Utility function to get deterministic results for PyToch executions
View torch_deterministic.py
import random
import os
import torch
import numpy as np
def set_seed(seed):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
@gurucharanmk
gurucharanmk / plantvillage_train.py
Created Mar 11, 2020
Code snippet for medium article
View plantvillage_train.py
learn = cnn_learner(dls, resnet34, pretrained=True, metrics=accuracy).to_fp16()
learn.fit_one_cycle(4)
learn.save('stage_1')
#Understand which classes are misclassified
interp = ClassificationInterpretation.from_learner(learn)
losses,idxs = interp.top_losses()
interp.plot_top_losses(9, figsize=(15,10))
interp.most_confused(min_val=3)
@gurucharanmk
gurucharanmk / plantvillage_dataloaders.py
Created Mar 11, 2020
Code snippet for medium article
View plantvillage_dataloaders.py
batch_tfms = [*aug_transforms(size=224, max_warp=0), Normalize.from_stats(*imagenet_stats)]
item_tfms = RandomResizedCrop(460, min_scale=0.75, ratio=(1.,1.))
bs=128
dls = ImageDataLoaders.from_folder(data_path, train='train', valid='val', batch_tfms=batch_tfms,
item_tfms=item_tfms, bs=bs)
@gurucharanmk
gurucharanmk / celeba_train.py
Created Mar 11, 2020
Code snippet for medium article
View celeba_train.py
#Mixed precision model to train
learn = cnn_learner(dls, resnet18, pretrained=True, metrics=[accuracy_multi]).to_fp16()
#Train only the classifier using one cycle policy, with default optimizer(ADAM) and learning rate.
learn.fit_one_cycle(4)
#Save the trained model
learn.save('stage_1')
#Unfreeze the model, now entire model is available for training
learn.unfreeze()
@gurucharanmk
gurucharanmk / celeba_dataloaders.py
Last active Mar 11, 2020
Code snippet for medium article
View celeba_dataloaders.py
get_x = lambda x:CELEBA_PATH/f'{x[0]}'
get_y = lambda x:[headers[index+1] for index,val in enumerate(x[1:]) if val == 1]
batch_tfms = [*aug_transforms(size=224, max_warp=0), Normalize.from_stats(*imagenet_stats)]
item_tfms = RandomResizedCrop(460, min_scale=0.75, ratio=(1.,1.))
bs=64
celleba_data = DataBlock(blocks=(ImageBlock, MultiCategoryBlock),
get_x=get_x,
splitter=RandomSplitter(),
get_y=get_y,
@gurucharanmk
gurucharanmk / import_fastai2.py
Last active Mar 11, 2020
Code snippet for medium article
View import_fastai2.py
from fastai2.data.all import *
from fastai2.vision.all import *
from fastai2.callback.all import *
View hackyIterWhile.js
let colors = [ "red", "green", "blue" ];
let iter = colors.entries();
let entry;
while (!(entry = iter.next()).done) {
console.log(entry.value);
}
View customIterator.js
let iterable = {
0: 'a',
1: 'b',
2: 'c',
length: 3,
[Symbol.iterator]() {
var keys = Object.keys(this).sort();
var index = 0;
return {
View for_of_builtinObject.js
console.log("======================[Array]======================");
for (let x of ['a', 'b', 'c']) {
console.log(x);
}
//String
console.log("======================[String]======================");
for (let x of 'Hello World!') {
console.log(x);
}
View forLoopES5.js
var Arr = [3, 6, 9, 12, 15, 18, 21];
Arr.author="Gurucharan";
//Using legacy for loop
console.log('==============[Legacy for loop]==============');
for (var i = 0; i < Arr.length; ++i) {
console.log("index is of type", typeof i, Arr[i]);
}
//Using forEach