Skip to content

Instantly share code, notes, and snippets.

View Tony607's full-sized avatar

Chengwei Zhang Tony607

View GitHub Profile
@Tony607
Tony607 / load_model.py
Created March 2, 2018 09:03
How to do Real Time Trigger Word Detection with Keras | DLology
model = load_model('./models/tr_model.h5')
@Tony607
Tony607 / has_new_triggerword.py
Created March 2, 2018 09:04
How to do Real Time Trigger Word Detection with Keras | DLology
def has_new_triggerword(predictions, chunk_duration, feed_duration, threshold=0.5):
"""
Function to detect new trigger word in the latest chunk of input audio.
It is looking for the rising edge of the predictions data belongs to the
last/latest chunk.
Argument:
predictions -- predicted labels from model
chunk_duration -- time in second of a chunk
feed_duration -- time in second of the input to model
@Tony607
Tony607 / demo.py
Last active March 2, 2018 09:12
How to do Real Time Trigger Word Detection with Keras | DLology
import pyaudio
from queue import Queue
from threading import Thread
import sys
import time
# Queue to communiate between the audio callback and main thread
q = Queue()
@Tony607
Tony607 / inceptionv3.py
Created March 20, 2018 01:29
Can you trust a Keras model to distinguish African elephant from Asian elephant? | DLology
from keras.applications import inception_v3 as inc_net
inet_model = inc_net.InceptionV3()
@Tony607
Tony607 / predict.py
Created March 20, 2018 01:31
Can you trust a Keras model to distinguish African elephant from Asian elephant? | DLology
images = transform_img_fn([os.path.join('data','asian-african-elephants.jpg')])
preds = inet_model.predict(images)
for x in decode_predictions(preds)[0]:
print(x)
@Tony607
Tony607 / explainer.py
Created March 20, 2018 01:32
Can you trust a Keras model to distinguish African elephant from Asian elephant? | DLology
import lime
from lime import lime_image
explainer = lime_image.LimeImageExplainer()
explanation = explainer.explain_instance(images[0], inet_model.predict,
top_labels=5,
hide_color=0,
num_samples=1000)
@Tony607
Tony607 / get_class_index.py
Created March 20, 2018 01:32
Can you trust a Keras model to distinguish African elephant from Asian elephant? | DLology
Indian_elephant = get_class_index("Indian_elephant")
@Tony607
Tony607 / Indian_elephant_mask.py
Created March 20, 2018 01:33
Can you trust a Keras model to distinguish African elephant from Asian elephant? | DLology
from skimage.segmentation import mark_boundaries
temp, mask = explanation.get_image_and_mask(Indian_elephant, positive_only=True, num_features=5, hide_rest=True)
plt.imshow(mark_boundaries(temp / 2 + 0.5, mask))
@Tony607
Tony607 / African_elephant_mask.py
Created March 20, 2018 01:34
Can you trust a Keras model to distinguish African elephant from Asian elephant? | DLology
African_elephant = get_class_index("African_elephant")
temp, mask = explanation.get_image_and_mask(African_elephant, positive_only=True, num_features=5, hide_rest=True)
plt.imshow(mark_boundaries(temp / 2 + 0.5, mask))
@Tony607
Tony607 / pros_cons.py
Created March 20, 2018 01:35
Can you trust a Keras model to distinguish African elephant from Asian elephant? | DLology
temp, mask = explanation.get_image_and_mask(Indian_elephant, positive_only=False, num_features=10, hide_rest=False)
plt.imshow(mark_boundaries(temp / 2 + 0.5, mask))