-
Notifications
You must be signed in to change notification settings - Fork 16
/
utils2.py
109 lines (87 loc) · 3.6 KB
/
utils2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import math, keras, datetime, pandas as pd, numpy as np, keras.backend as K, threading, json, re, collections, tarfile, tensorflow as tf, matplotlib.pyplot as plt, operator, random, pickle, glob, os, bcolz, shutil, sklearn, functools, itertools
from PIL import Image
from concurrent.futures import ProcessPoolExecutor, as_completed, ThreadPoolExecutor
import matplotlib.patheffects as PathEffects
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.neighbors import NearestNeighbors, LSHForest
from IPython.display import display, Audio
from numpy.random import normal
#from gensim.models import word2vec
from keras.preprocessing.text import Tokenizer
#from nltk.tokenize import ToktokTokenizer, StanfordTokenizer
from functools import reduce
from itertools import chain
from tensorflow.python.framework import ops
#from tensorflow.contrib import rnn, legacy_seq2seq as seq2seq
#from keras_tqdm import TQDMNotebookCallback
#from keras import initializations
from keras.applications.resnet50 import ResNet50, decode_predictions, conv_block, identity_block
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.preprocessing.sequence import pad_sequences
from keras.models import Model, Sequential
from keras.layers import *
from keras.optimizers import Adam
from keras.regularizers import l2
from keras.utils.data_utils import get_file
from keras.applications.imagenet_utils import decode_predictions, preprocess_input
np.set_printoptions(threshold=50, edgeitems=20)
def beep(): return Audio(filename='/home/jhoward/beep.mp3', autoplay=True)
def dump(obj, fname): pickle.dump(obj, open(fname, 'wb'))
def load(fname): return pickle.load(open(fname, 'rb'))
def limit_mem():
cfg = K.tf.ConfigProto()
cfg.gpu_options.allow_growth = True
K.set_session(K.tf.Session(config=cfg))
def autolabel(plt, fmt='%.2f'):
rects = plt.patches
ax = rects[0].axes
y_bottom, y_top = ax.get_ylim()
y_height = y_top - y_bottom
for rect in rects:
height = rect.get_height()
if height / y_height > 0.95:
label_position = height - (y_height * 0.06)
else:
label_position = height + (y_height * 0.01)
txt = ax.text(rect.get_x() + rect.get_width()/2., label_position,
fmt % height, ha='center', va='bottom')
txt.set_path_effects([PathEffects.withStroke(linewidth=3, foreground='w')])
def column_chart(lbls, vals, val_lbls='%.2f'):
n = len(lbls)
p = plt.bar(np.arange(n), vals)
plt.xticks(np.arange(n), lbls)
if val_lbls: autolabel(p, val_lbls)
def save_array(fname, arr):
c=bcolz.carray(arr, rootdir=fname, mode='w')
c.flush()
def load_array(fname): return bcolz.open(fname)[:]
def load_glove(loc):
return (load_array(loc+'.dat'),
pickle.load(open(loc+'_words.pkl','rb'), encoding='latin1'),
pickle.load(open(loc+'_idx.pkl','rb'), encoding='latin1'))
def plot_multi(im, dim=(4,4), figsize=(6,6), **kwargs ):
plt.figure(figsize=figsize)
for i,img in enumerate(im):
plt.subplot(dim, i+1)
plt.imshow(img, **kwargs)
plt.axis('off')
plt.tight_layout()
def plot_train(hist):
h = hist.history
if 'acc' in h:
meas='acc'
loc='lower right'
else:
meas='loss'
loc='upper right'
plt.plot(hist.history[meas])
plt.plot(hist.history['val_'+meas])
plt.title('model '+meas)
plt.ylabel(meas)
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc=loc)
def fit_gen(gen, fn, eval_fn, nb_iter):
for i in range(nb_iter):
fn(*next(gen))
if i % (nb_iter//10) == 0: eval_fn()