Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyk committed May 29, 2020
1 parent ea160ff commit 67cfa2a
Show file tree
Hide file tree
Showing 68 changed files with 235 additions and 122 deletions.
2 changes: 1 addition & 1 deletion lab2/text_recognizer/datasets/emnist_lines_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
ESSENTIALS_FILENAME = Path(__file__).parents[0].resolve() / "emnist_lines_essentials.json"


class EmnistLinesDataset(Dataset):
class EmnistLinesDataset(Dataset): # pylint: disable=too-many-instance-attributes
"""
EmnistLinesDataset class.
Expand Down
2 changes: 1 addition & 1 deletion lab2/text_recognizer/networks/line_cnn_all_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def line_cnn_for_transformer(

# Because of MaxPooling, everything is divided by 2
new_height = image_height // 2
new_width = image_width // 2
# new_width = image_width // 2
new_window_width = window_width // 2
new_window_stride = window_stride // 2

Expand Down
2 changes: 1 addition & 1 deletion lab3/text_recognizer/datasets/emnist_lines_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
ESSENTIALS_FILENAME = Path(__file__).parents[0].resolve() / "emnist_lines_essentials.json"


class EmnistLinesDataset(Dataset):
class EmnistLinesDataset(Dataset): # pylint: disable=too-many-instance-attributes
"""
EmnistLinesDataset class.
Expand Down
10 changes: 6 additions & 4 deletions lab3/text_recognizer/models/line_model_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
sparse_categorical_crossentropy_ignoring_padding,
)
from text_recognizer.networks import line_cnn_for_transformer
from text_recognizer.networks.transformer import run_transformer_inference
from text_recognizer.networks.transformer.main import run_transformer_inference


class LineModelTransformer(Model):
Expand Down Expand Up @@ -81,6 +81,8 @@ def predict_on_image(self, image: np.ndarray) -> Tuple[str, float]:
"""Predict on a single image."""
if image.dtype == np.uint8:
image = (image / 255).astype(np.float32)
pred = run_inference(self.model, image, self.data.max_length, self.data.start_label, self.data.end_label)
string = "".join([dataset.mapping[ind] for ind in pred.numpy()]).strip()
return pred, 1.0 # NOTE: conference is always given as 1.0 for now...
pred = run_transformer_inference(
self.network, image, self.data.max_length, self.data.start_label, self.data.end_label
)
string_ = "".join([self.data.mapping[ind] for ind in pred.numpy()]).strip()
return string_, 1.0 # NOTE: conference is always given as 1.0 for now...
3 changes: 2 additions & 1 deletion lab3/text_recognizer/models/util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Utility functions."""
from tensorflow.python.keras import backend as K
from tensorflow.python.framework import ops
from tensorflow.python.ops import array_ops
Expand Down Expand Up @@ -42,7 +43,7 @@ def sparse_categorical_accuracy_ignoring_padding(y_true, y_pred, padding_label):
return math_ops.cast(math_ops.equal(y_true_masked, y_pred_masked), K.floatx())


class CustomSchedule(tf.keras.optimizers.schedules.LearningRateSchedule):
class CustomSchedule(tf.keras.optimizers.schedules.LearningRateSchedule): # pylint: disable=abstract-method
"""Learning rate scheduler from the Attention is all you need paper."""

def __init__(self, d_model, warmup_steps=4000):
Expand Down
2 changes: 1 addition & 1 deletion lab3/text_recognizer/networks/line_cnn_all_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def line_cnn_for_transformer(

# Because of MaxPooling, everything is divided by 2
new_height = image_height // 2
new_width = image_width // 2
# new_width = image_width // 2
new_window_width = window_width // 2
new_window_stride = window_stride // 2

Expand Down
8 changes: 5 additions & 3 deletions lab3/text_recognizer/networks/transformer/attention.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Code originally from https://colab.research.google.com/github/tensorflow/examples/blob/master/community/en/transformer_chatbot.ipynb
"""MultiHeadAttention class and supporting functions."""
# Code originally from https://colab.research.google.com/github/tensorflow/examples/blob/master
# /community/en/transformer_chatbot.ipynb

import tensorflow as tf


def scaled_dot_product_attention(query, key, value, mask):
"""Calculate the attention weights. """
"""Calculate the attention weights."""
matmul_qk = tf.matmul(query, key, transpose_b=True)

# scale matmul_qk
Expand Down Expand Up @@ -45,7 +47,7 @@ def split_heads(self, inputs, batch_size):
inputs = tf.reshape(inputs, shape=(batch_size, -1, self.num_heads, self.depth))
return tf.transpose(inputs, perm=[0, 2, 1, 3])

def call(self, inputs):
def call(self, inputs): # pylint: disable=arguments-differ
query, key, value, mask = inputs["query"], inputs["key"], inputs["value"], inputs.get("mask", None)
batch_size = tf.shape(query)[0]

Expand Down
4 changes: 3 additions & 1 deletion lab3/text_recognizer/networks/transformer/decoder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Code originally from https://colab.research.google.com/github/tensorflow/examples/blob/master/community/en/transformer_chatbot.ipynb
"""Transformer decoder layer."""
# Code originally from https://colab.research.google.com/github/tensorflow/examples/blob/master
# /community/en/transformer_chatbot.ipynb
import tensorflow as tf

from text_recognizer.networks.transformer.positional_encoding import PositionalEncoding
Expand Down
8 changes: 5 additions & 3 deletions lab3/text_recognizer/networks/transformer/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Transformer Keras model."""
# Code originally from https://colab.research.google.com/github/tensorflow/examples/blob/master/community/en/transformer_chatbot.ipynb
# Code originally from https://colab.research.google.com/github/tensorflow/examples/blob/master
# /community/en/transformer_chatbot.ipynb
from typing import Tuple

import tensorflow as tf
Expand All @@ -15,7 +16,7 @@ def run_transformer_inference(
) -> tf.Tensor:
output = tf.expand_dims([start_label], 0)

for i in range(max_length):
for _ in range(max_length):
predictions = model(inputs=[image, output], training=False)

# select the last word from the seq_len dimension
Expand All @@ -26,12 +27,13 @@ def run_transformer_inference(
break

# concatenated the predicted_id to the output which is given to the decoder as input
# pylint: disable=unexpected-keyword-arg,no-value-for-parameter
output = tf.concat([output, predicted_id], axis=-1)

return tf.squeeze(output, axis=0)


def transformer(
def transformer( # pylint: disable=too-many-arguments
image_shape: Tuple[int, ...],
window_width: int,
window_stride: int,
Expand Down
4 changes: 3 additions & 1 deletion lab3/text_recognizer/networks/transformer/masking.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Code originally from https://colab.research.google.com/github/tensorflow/examples/blob/master/community/en/transformer_chatbot.ipynb
"""Masking code for transformers."""
# Code originally from https://colab.research.google.com/github/tensorflow/examples/blob/master
# /community/en/transformer_chatbot.ipynb
import tensorflow as tf


Expand Down
11 changes: 8 additions & 3 deletions lab3/text_recognizer/networks/transformer/positional_encoding.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
# Code originally from https://colab.research.google.com/github/tensorflow/examples/blob/master/community/en/transformer_chatbot.ipynb
"""Positional encoding for transformer."""
# Code originally from https://colab.research.google.com/github/tensorflow/examples/blob/master
# /community/en/transformer_chatbot.ipynb
import tensorflow as tf


MAX_POSITION = 1000


class PositionalEncoding(tf.keras.layers.Layer):
"""Positional encoding for transformer."""

def __init__(self, d_model, position=None):
super(PositionalEncoding, self).__init__()
if position is None:
position = MAX_POSITION
self.pos_encoding = self.positional_encoding(position, d_model)

def get_angles(self, position, i, d_model):
def get_angles(self, position, i, d_model): # pylint: disable=no-self-use
angles = 1 / tf.pow(10000, (2 * (i // 2)) / tf.cast(d_model, tf.float32))
return position * angles

Expand All @@ -27,9 +31,10 @@ def positional_encoding(self, position, d_model):
# apply cos to odd index in the array
cosines = tf.math.cos(angle_rads[:, 1::2])

# pylint: disable=unexpected-keyword-arg,no-value-for-parameter
pos_encoding = tf.concat([sines, cosines], axis=-1)
pos_encoding = pos_encoding[tf.newaxis, ...]
return tf.cast(pos_encoding, tf.float32)

def call(self, inputs):
def call(self, inputs): # pylint: disable=arguments-differ
return inputs + self.pos_encoding[:, : tf.shape(inputs)[1], :]
2 changes: 1 addition & 1 deletion lab4/text_recognizer/datasets/emnist_lines_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
ESSENTIALS_FILENAME = Path(__file__).parents[0].resolve() / "emnist_lines_essentials.json"


class EmnistLinesDataset(Dataset):
class EmnistLinesDataset(Dataset): # pylint: disable=too-many-instance-attributes
"""
EmnistLinesDataset class.
Expand Down
5 changes: 4 additions & 1 deletion lab4/text_recognizer/datasets/iam_lines_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from text_recognizer import util
from text_recognizer.datasets.dataset import Dataset, _parse_args
from text_recognizer.datasets.emnist_lines_dataset import EmnistLinesDataset
from text_recognizer.datasets.emnist_lines_dataset import add_start_and_end_labels, EmnistLinesDataset


PROCESSED_DATA_DIRNAME = Dataset.data_dirname() / "processed" / "iam_lines"
Expand Down Expand Up @@ -43,6 +43,9 @@ def __init__(
self.with_start_and_end_labels = with_start_and_end_labels
self.mapping = EmnistLinesDataset().mapping
self.inverse_mapping = {v: k for k, v in self.mapping.items()}
self.padding_label = self.inverse_mapping["_"]
self.start_label = self.inverse_mapping["<s>"]
self.end_label = self.inverse_mapping["<e>"]
self.num_classes = len(self.mapping)
self.input_shape = (28, 952)
self.output_shape = (97, self.num_classes)
Expand Down
10 changes: 6 additions & 4 deletions lab4/text_recognizer/models/line_model_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
sparse_categorical_crossentropy_ignoring_padding,
)
from text_recognizer.networks import line_cnn_for_transformer
from text_recognizer.networks.transformer import run_transformer_inference
from text_recognizer.networks.transformer.main import run_transformer_inference


class LineModelTransformer(Model):
Expand Down Expand Up @@ -81,6 +81,8 @@ def predict_on_image(self, image: np.ndarray) -> Tuple[str, float]:
"""Predict on a single image."""
if image.dtype == np.uint8:
image = (image / 255).astype(np.float32)
pred = run_inference(self.model, image, self.data.max_length, self.data.start_label, self.data.end_label)
string = "".join([dataset.mapping[ind] for ind in pred.numpy()]).strip()
return pred, 1.0 # NOTE: conference is always given as 1.0 for now...
pred = run_transformer_inference(
self.network, image, self.data.max_length, self.data.start_label, self.data.end_label
)
string_ = "".join([self.data.mapping[ind] for ind in pred.numpy()]).strip()
return string_, 1.0 # NOTE: conference is always given as 1.0 for now...
3 changes: 2 additions & 1 deletion lab4/text_recognizer/models/util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Utility functions."""
from tensorflow.python.keras import backend as K
from tensorflow.python.framework import ops
from tensorflow.python.ops import array_ops
Expand Down Expand Up @@ -42,7 +43,7 @@ def sparse_categorical_accuracy_ignoring_padding(y_true, y_pred, padding_label):
return math_ops.cast(math_ops.equal(y_true_masked, y_pred_masked), K.floatx())


class CustomSchedule(tf.keras.optimizers.schedules.LearningRateSchedule):
class CustomSchedule(tf.keras.optimizers.schedules.LearningRateSchedule): # pylint: disable=abstract-method
"""Learning rate scheduler from the Attention is all you need paper."""

def __init__(self, d_model, warmup_steps=4000):
Expand Down
2 changes: 1 addition & 1 deletion lab4/text_recognizer/networks/line_cnn_all_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def line_cnn_for_transformer(

# Because of MaxPooling, everything is divided by 2
new_height = image_height // 2
new_width = image_width // 2
# new_width = image_width // 2
new_window_width = window_width // 2
new_window_stride = window_stride // 2

Expand Down
8 changes: 5 additions & 3 deletions lab4/text_recognizer/networks/transformer/attention.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Code originally from https://colab.research.google.com/github/tensorflow/examples/blob/master/community/en/transformer_chatbot.ipynb
"""MultiHeadAttention class and supporting functions."""
# Code originally from https://colab.research.google.com/github/tensorflow/examples/blob/master
# /community/en/transformer_chatbot.ipynb

import tensorflow as tf


def scaled_dot_product_attention(query, key, value, mask):
"""Calculate the attention weights. """
"""Calculate the attention weights."""
matmul_qk = tf.matmul(query, key, transpose_b=True)

# scale matmul_qk
Expand Down Expand Up @@ -45,7 +47,7 @@ def split_heads(self, inputs, batch_size):
inputs = tf.reshape(inputs, shape=(batch_size, -1, self.num_heads, self.depth))
return tf.transpose(inputs, perm=[0, 2, 1, 3])

def call(self, inputs):
def call(self, inputs): # pylint: disable=arguments-differ
query, key, value, mask = inputs["query"], inputs["key"], inputs["value"], inputs.get("mask", None)
batch_size = tf.shape(query)[0]

Expand Down
4 changes: 3 additions & 1 deletion lab4/text_recognizer/networks/transformer/decoder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Code originally from https://colab.research.google.com/github/tensorflow/examples/blob/master/community/en/transformer_chatbot.ipynb
"""Transformer decoder layer."""
# Code originally from https://colab.research.google.com/github/tensorflow/examples/blob/master
# /community/en/transformer_chatbot.ipynb
import tensorflow as tf

from text_recognizer.networks.transformer.positional_encoding import PositionalEncoding
Expand Down
8 changes: 5 additions & 3 deletions lab4/text_recognizer/networks/transformer/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Transformer Keras model."""
# Code originally from https://colab.research.google.com/github/tensorflow/examples/blob/master/community/en/transformer_chatbot.ipynb
# Code originally from https://colab.research.google.com/github/tensorflow/examples/blob/master
# /community/en/transformer_chatbot.ipynb
from typing import Tuple

import tensorflow as tf
Expand All @@ -15,7 +16,7 @@ def run_transformer_inference(
) -> tf.Tensor:
output = tf.expand_dims([start_label], 0)

for i in range(max_length):
for _ in range(max_length):
predictions = model(inputs=[image, output], training=False)

# select the last word from the seq_len dimension
Expand All @@ -26,12 +27,13 @@ def run_transformer_inference(
break

# concatenated the predicted_id to the output which is given to the decoder as input
# pylint: disable=unexpected-keyword-arg,no-value-for-parameter
output = tf.concat([output, predicted_id], axis=-1)

return tf.squeeze(output, axis=0)


def transformer(
def transformer( # pylint: disable=too-many-arguments
image_shape: Tuple[int, ...],
window_width: int,
window_stride: int,
Expand Down
4 changes: 3 additions & 1 deletion lab4/text_recognizer/networks/transformer/masking.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Code originally from https://colab.research.google.com/github/tensorflow/examples/blob/master/community/en/transformer_chatbot.ipynb
"""Masking code for transformers."""
# Code originally from https://colab.research.google.com/github/tensorflow/examples/blob/master
# /community/en/transformer_chatbot.ipynb
import tensorflow as tf


Expand Down
11 changes: 8 additions & 3 deletions lab4/text_recognizer/networks/transformer/positional_encoding.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
# Code originally from https://colab.research.google.com/github/tensorflow/examples/blob/master/community/en/transformer_chatbot.ipynb
"""Positional encoding for transformer."""
# Code originally from https://colab.research.google.com/github/tensorflow/examples/blob/master
# /community/en/transformer_chatbot.ipynb
import tensorflow as tf


MAX_POSITION = 1000


class PositionalEncoding(tf.keras.layers.Layer):
"""Positional encoding for transformer."""

def __init__(self, d_model, position=None):
super(PositionalEncoding, self).__init__()
if position is None:
position = MAX_POSITION
self.pos_encoding = self.positional_encoding(position, d_model)

def get_angles(self, position, i, d_model):
def get_angles(self, position, i, d_model): # pylint: disable=no-self-use
angles = 1 / tf.pow(10000, (2 * (i // 2)) / tf.cast(d_model, tf.float32))
return position * angles

Expand All @@ -27,9 +31,10 @@ def positional_encoding(self, position, d_model):
# apply cos to odd index in the array
cosines = tf.math.cos(angle_rads[:, 1::2])

# pylint: disable=unexpected-keyword-arg,no-value-for-parameter
pos_encoding = tf.concat([sines, cosines], axis=-1)
pos_encoding = pos_encoding[tf.newaxis, ...]
return tf.cast(pos_encoding, tf.float32)

def call(self, inputs):
def call(self, inputs): # pylint: disable=arguments-differ
return inputs + self.pos_encoding[:, : tf.shape(inputs)[1], :]
2 changes: 1 addition & 1 deletion lab5/text_recognizer/datasets/emnist_lines_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
ESSENTIALS_FILENAME = Path(__file__).parents[0].resolve() / "emnist_lines_essentials.json"


class EmnistLinesDataset(Dataset):
class EmnistLinesDataset(Dataset): # pylint: disable=too-many-instance-attributes
"""
EmnistLinesDataset class.
Expand Down
5 changes: 4 additions & 1 deletion lab5/text_recognizer/datasets/iam_lines_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from text_recognizer import util
from text_recognizer.datasets.dataset import Dataset, _parse_args
from text_recognizer.datasets.emnist_lines_dataset import EmnistLinesDataset
from text_recognizer.datasets.emnist_lines_dataset import add_start_and_end_labels, EmnistLinesDataset


PROCESSED_DATA_DIRNAME = Dataset.data_dirname() / "processed" / "iam_lines"
Expand Down Expand Up @@ -43,6 +43,9 @@ def __init__(
self.with_start_and_end_labels = with_start_and_end_labels
self.mapping = EmnistLinesDataset().mapping
self.inverse_mapping = {v: k for k, v in self.mapping.items()}
self.padding_label = self.inverse_mapping["_"]
self.start_label = self.inverse_mapping["<s>"]
self.end_label = self.inverse_mapping["<e>"]
self.num_classes = len(self.mapping)
self.input_shape = (28, 952)
self.output_shape = (97, self.num_classes)
Expand Down
Loading

0 comments on commit 67cfa2a

Please sign in to comment.