Skip to content

Instantly share code, notes, and snippets.

from matplotlib import pyplot as plt
import numpy as np
%matplotlib inline
#show and tell
fig = plt.figure(figsize=(16,12))
n_images_per_row=4
choices=np.random.choice(len(imgs),16)
#choices=range(16)
for i,id in enumerate(choices):
@shgidi
shgidi / XGBoost.py
Last active May 7, 2017 08:53
This is generic XGBoost
import xgboost as xgb
clf = xgb.XGBClassifier(max_depth=10, n_estimators=1500,min_child_weight=9,learning_rate=0.01,
nthread=8, subsample=0.80,colsample_bytree=0.80,seed=4242)
clf.fit(trn , y,eval_set=[(val, y_val)], eval_metric='mlogloss', verbose=True, early_stopping_rounds=50)
#multiclass logloss
#genertor
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True) #many other augmentations are avialbile
#flow
datagen.flow(x, batch_size=1, save_to_dir='preview', save_prefix='cat', save_format='jpeg')
import bcolz
import pickle
def save_array(fname, arr):
c=bcolz.carray(arr, rootdir=fname, mode='w')
c.flush()
def load_array(fname):
return bcolz.open(fname)[:]
plt.imshow(np.array(Image.open(g[0]))[y-30:y+30,x-30:x+30])
currentAxis = plt.gca()
coords=[10,10],20,20
currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor=color, linewidth=2))
from keras.applications.resnet50 import ResNet50
from scipy.misc import imresize
resnet=ResNet50()
imgs_new=[]
for i,img in enumerate(imgs_new_rand):
imgs_new[i]=imresize(img,(224,224,3))
resnet_preds=resnet.predict(imgs_new)
from keras.applications.vgg16 import VGG16
from keras.layers import Conv2D
from keras.models import Sequential
from keras.layers import BatchNormalization
from keras.optimizers import Adam
vgg=VGG16()
p=0.4 #dropout
label_count=17
#simple model
model = Sequential([
#Embedding(vocab_size, vec_size, input_length=seq_len,weights=[emb]),
#Flatten(),
Dense(100, input_dim=test_data.shape[1], init='uniform', activation='relu'),
#Dropout(0.7),
Dense(1, activation='sigmoid')])
model.compile(loss='binary_crossentropy', optimizer=Adam(), metrics=['accuracy'])
from keras.models import model_from_json
def save_keras_model(model,path):
model_json = model.to_json()
with open(path+"json", "w") as json_file:
json_file.write(model_json)
model.save_weights(path+'.hdf5')
def load_keras_model(path):
@shgidi
shgidi / plot_loss+sample.py
Created July 12, 2017 10:30
plots loss in keras, additionally plots segmentation in image
#https://gist.github.com/stared/dfb4dfaf6d9a8501cd1cc8b8cb806d2e
class PlotLosses(keras.callbacks.Callback):
def __init__(self,imgs):
super(PlotLosses, self).__init__()
self.imgs=imgs
def on_train_begin(self, logs={}):
self.i = 0
self.x = []