Skip to content

Instantly share code, notes, and snippets.

# Call the vision API to extract text from the image
imageresponse = vision_client.document_text_detection(image=image)
# Lump all the text from the image in a single string
text = ' - '.join(imageresponse.text_annotations[0].description.split('\n'))
# Call the natural language API to extract entities for our text
document = language.types.Document(content=text, type=language.enums.Document.Type.PLAIN_TEXT)
languageresponse = language_client.analyze_entities(document=document)
from keras.callbacks import EarlyStopping # use as base class
class MyCallBack(EarlyStopping):
def __init__(self, threshold, min_epochs, **kwargs):
super(MyCallBack, self).__init__(**kwargs)
self.threshold = threshold # threshold for validation loss
self.min_epochs = min_epochs # min number of epochs to run
def on_epoch_end(self, epoch, logs=None):
current = logs.get(self.monitor)
# Siamese network: two input sequences through same embedding layer
sequence_one = layers.Input((10, )) # Input of arbitrary shape
sequence_two = layers.Input((10, ))
embedding_layer = layers.Embedding(input_dim=1000,
output_dim=128, input_length=10)
embedded_seq_1 = embedding_layer(sequence_one)
embedded_seq_2 = embedding_layer(sequence_two)
sequence_input = keras.layers.Input((100, 5)) # 100 timesteps, 5 features
image_input = keras.layers.Input((128, 128, 3)) # 128x128 pixels, 3 channels
auxiliary_input = keras.layers.Input((10,)) # Additional vector input
sequence_module = keras.layers.LSTM(128)(sequence_input)
image_module = keras.layers.Conv2D(32, 1)(image_input)
image_features = keras.layers.Flatten()(image_module)
concat = keras.layers.Concatenate()([sequence_module, image_features, auxiliary_input])
# Sequential API: define a model, add linear stack of layers
sequential_network = models.Sequential()
sequential_network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28, ), name='input'))
sequential_network.add(layers.Dense(256, activation='relu', name='dense_layer'))
sequential_network.add(layers.Dense(10, activation='softmax', name='output'))
# Functional API: tie layers together in a potentially complex DAG, wrap in a keras Model
input_layer = keras.layers.Input((28*28, ))
dense_layer = keras.layers.Dense(512, activation='relu')(input_layer)
# Saving a model
network.save(filepath='./network.h5') # architecture + weights
network_json = network.to_json() # only the architecture
network.save_weights() # only the weights
# Restoring a saved model
new_network = keras.models.load_model('./another_model.h5')
# Using the ModelCheckpoint callback during training
callbacks = [
from keras.utils import Sequence
class DataSequence(Sequence):
"""
Keras Sequence object to train a model on larger-than-memory data.
"""
def __init__(self, df, batch_size, mode='train'):
...
def get_batch_images(self, idx):
from keras.utils import Sequence
import numpy as np
class MySequence(Sequence):
"""Custom Sequence"""
def __init__(self, x, y, batch_size):
self.x, self.y = x, y
self.batch_size = batch_size
def __len__(self):
from keras import callbacks
callbacks = [
callbacks.EarlyStopping(monitor='val_loss', patience=2, verbose=1)
]
history = network.fit(train_images,
train_labels,
epochs=500,
batch_size=128,
predictions = network.predict(train_images, verbose=1)