Skip to content

Instantly share code, notes, and snippets.

@jasonsalas
jasonsalas / CommonWaterVolumesActivity.java
Last active August 29, 2015 14:12
Android Wear Message API (wearable-to-handheld communication)
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.wearable.view.WearableListView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;
@jasonsalas
jasonsalas / app.yaml
Created February 19, 2015 10:54
OAuth2 consumer for App Engine to access Pushbullet's API
application: YOUR_APP_NAME_HERE
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: oauth.application
@jasonsalas
jasonsalas / dcgan_model.py
Created September 7, 2019 11:11
Model definition for shallow DCGAN
''' SHALLOW MODEL '''
img_shape = (32, 32, 3)
z_dim = 100
init = initializers.RandomNormal(mean=0.0, stddev=0.02)
opt = Adam(lr=0.0002, beta_1=0.5)
def build_discriminator(in_shape=img_shape):
model = Sequential()
model.add(Conv2D(64, (5,5), input_shape=in_shape, kernel_initializer=init))
@jasonsalas
jasonsalas / selfie_dcgan_client.py
Created September 7, 2019 21:49
Produce DCGAN's generator images from latent space
''' client to produce generator images from latent space '''
import numpy as np
from keras.models import load_model
MODEL = './generator_model_2800.h5'
z_noise = np.random.randn(100*25)
z_noise = z_noise.reshape(25, 100)
model = load_model(MODEL)
@jasonsalas
jasonsalas / get_model.py
Created October 25, 2019 23:10
Download pre-trained VGG16 model on ImageNet weights
from keras.applications.vgg16 import VGG16
model = VGG16(weights='imagenet')
model.save('model_vgg16_imagenet.h5')
print('Pre-trained VGG16 model with ImageNet weights saved!')
@jasonsalas
jasonsalas / app_keras.py
Created October 25, 2019 23:16
Server for deployed ML model in Flask
import os
import numpy as np
import tensorflow as tf
from keras.applications.vgg16 import preprocess_input, decode_predictions
from keras.models import load_model
from keras.preprocessing.image import img_to_array, load_img
from flask import Flask, redirect, url_for, request, render_template
# define a Flask app
app = Flask(__name__)
@jasonsalas
jasonsalas / standalone_prediction_client.py
Last active November 5, 2019 12:38
NLP: predict the MPAA rating of a movie based solely on its plot summary
# Make predictions on unseen data
#
# follows my full demo/repo at
# https://github.com/jasonsalas/nlp_predict_movie_rating_via_description/
import pickle
from keras.preprocessing.text import Tokenizer
from keras.models import load_model
''' Cabin in the Woods (R) '''
@jasonsalas
jasonsalas / depeche_mode_neuralnet.py
Created November 20, 2019 03:40
Generate Depeche Mode lyrics automatically with an LSTM neural network
import numpy as np
import sys
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM
from keras.callbacks import ModelCheckpoint
from keras.utils import to_categorical
filename = 'dm_lyrics.txt'
raw_text = open(filename, 'r', encoding='utf-8').read()
raw_text = raw_text.lower()
@jasonsalas
jasonsalas / depeche_mode_lyric_generator.py
Created November 20, 2019 03:44
Using a trained LSTM-based neural network model to generate Depeche Mode-style lyrics
import numpy as np
import sys
from keras.models import Sequential
filename = 'trained_depechemode_lyrics_model.h5'
model = Sequential()
model.load_weights(filename)
model.compile(loss='categorical_crossentropy', optimizer='adam')
@jasonsalas
jasonsalas / workerpools.go
Last active July 5, 2020 07:07
Using worker pools & buffered channels in Go
/* using worker pools & buffered channels in Go */
// h/t: Naveen Ramanathan @ golangbot.com
// https://golangbot.com/buffered-channels-worker-pools/
package main
import (
"fmt"
"math/rand"
"sync"