Skip to content

Instantly share code, notes, and snippets.

import numpy as np, pandas as pd, os
import matplotlib.pyplot as plt,
import cv2
import tensorflow as tf, re, math
PATH = 'data/train/'
IMGS = os.listdir(PATH)
SIZE = (len(IMGS) // 5) + 1 # split images into 5 files
IMAGE_SIZE = [256, 256]
#Reference: https://www.tensorflow.org/tutorials/text/nmt_with_attention
class BahdanauAttention(tf.keras.layers.Layer):
def __init__(self, units):
super(BahdanauAttention, self).__init__()
self.W1 = tf.keras.layers.Dense(units)
self.W2 = tf.keras.layers.Dense(units)
self.V = tf.keras.layers.Dense(1)
def call(self, query, values):
#Reference: https://www.tensorflow.org/tutorials/text/nmt_with_attention
class BahdanauAttention(tf.keras.layers.Layer):
def __init__(self, units):
super(BahdanauAttention, self).__init__()
self.W1 = tf.keras.layers.Dense(units)
self.W2 = tf.keras.layers.Dense(units)
self.V = tf.keras.layers.Dense(1)
def call(self, query, values):
def evaluate(sentence):
sentence = preprocess_sentence(sentence)
inputs = [inp_lang.word_index[i] for i in sentence.split(' ')]
inputs = tf.keras.preprocessing.sequence.pad_sequences([inputs],
maxlen=max_length_inp,
padding='post')
inputs = tf.convert_to_tensor(inputs)
optimizer = tf.keras.optimizers.Adam()
loss_object = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True, reduction='none')
def loss_function(real, pred):
mask = tf.math.logical_not(tf.math.equal(real, 0))
loss_ = loss_object(real, pred)
mask = tf.cast(mask, dtype=loss_.dtype)
loss_ *= mask
@tf.function
def train_step(inp, targ, enc_hidden):
loss = 0
with tf.GradientTape() as tape:
enc_output, enc_hidden = encoder(inp, enc_hidden)
dec_hidden = enc_hidden
#initial decoder input - SOS token
dec_input = tf.expand_dims([targ_lang.word_index['<start>']] * BATCH_SIZE, 1)
# Teacher forcing - feeding the target as the next input
class Decoder(tf.keras.Model):
def __init__(self, vocab_size, embedding_dim, dec_units, batch_sz):
super(Decoder, self).__init__()
self.batch_sz = batch_sz
self.dec_units = dec_units
self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)
self.gru = tf.keras.layers.GRU(self.dec_units,
return_sequences=True,
return_state=True,
recurrent_initializer='glorot_uniform')
class Encoder(tf.keras.Model):
def __init__(self, vocab_size, embedding_dim, enc_units, batch_sz):
'''
vocab_size: number of unique words
embedding_dim: dimension of your embedding output
enc_units: how many units of RNN cell
batch_sz: batch of data passed to the training in each epoch
'''
super(Encoder, self).__init__()
self.batch_sz = batch_sz
from rouge_score import rouge_scorer
def read_input(filename = 'evaluation_input.txt'):
input_pair = []
# read evaluation_input.txt
# append each line to input_pair
return input_pair
'''
Reusable set of functions to convert a tuple of strings (pair) to tensors
Reference: https://pytorch.org/tutorials/intermediate/seq2seq_translation_tutorial.html
'''
import torch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def indexesFromSentence(lang, sentence):