Skip to main content

Common used code in Keras

Project description

simplified keras

Common used actions in keras

Table of contents

General info

This package is a set of common used actions in keras. At this moment includes:

Libraries

  • Keras - version 2.4.3
  • Matplotlib - version 3.3.3
  • NumPy - version 1.19.4
  • Tensorflow - version 2.4.0rc1
  • Pandas - version 1.1.5
  • Seaborn - version 0.11.1

Setup

  • Install from PyPi: pip install simplified-keras

Documentation

Main package

Generators

from keras.preprocessing.image import ImageDataGenerator
from simplified_keras.dir_flow_generators import get_train_val_generators

img_size = (48, 48)
img_datagen = ImageDataGenerator(rescale=1/255)

train_generator, validation_generator = get_train_val_generators(img_datagen, data_dir='../data/normal',
                                                                 color_mode='grayscale', target_size=img_size)

Signature:

def get_train_val_generators(img_datagen: ImageDataGenerator, data_dir='../data', color_mode='rgb', 
                             batch_size=128, class_mode='categorical', **kwargs)

Default callbacks

from simplified_keras.default_callbacks import get_default_callbacks

callbacks = get_default_callbacks('vgg16_calssifier')

hist = model.fit(train_generator, steps_per_epoch=train_steps, validation_data=validation_generator, 
                 validation_steps=valid_steps, epochs=100, callbacks=callbacks, verbose=2)

Signature:

def get_default_callbacks(model_name):
    return [
        clb.ReduceLROnPlateau(monitor='val_acc', factor=0.5, min_lr=1e-6, patience=3, verbose=1),
        clb.EarlyStopping(monitor='val_acc', patience=7, verbose=1),
        clb.ModelCheckpoint(monitor='val_acc', filepath=f'../models/{model_name}.h5',
                            save_best_only=True, verbose=1)
    ]

Plots

Accuracy and Loss plot

from simplified_keras.plots.history_plots import plot_acc_and_loss

history = model.fit(train_gen, teps_per_epoch=train_steps, epochs=5, validation_data=val_gen, 
                    validation_steps=val_steps, callbacks=callbacks)

fig = plot_acc_and_loss(history)

Result:

history.png

Predictions with image plot

from keras.models import load_model
from keras.preprocessing.image import ImageDataGenerator
from simplified_keras.dir_flow_generators import get_train_val_generators
from simplified_keras.plots import plot_predictions_with_img

img_size = (48, 48)
img_datagen = ImageDataGenerator(rescale=1/255)

_, validation_generator = get_train_val_generators(img_datagen, data_dir='../data/normal',
                                                   color_mode='grayscale', target_size=img_size)
model = load_model('../models/standard_model.h5')

batch, labels = validation_generator.next()
preds = model.predict(batch)

named_labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
fig = plot_predictions_with_img(1, preds, labels, batch, named_labels, grayscale=True)

Result:

p.png

Histogram with CDF and image plot

import cv2
from simplified_keras.plots import plot_gray_img_with_histogram

img = cv2.imread(f'{src_train_path}/0/241.png', 0)
fig1 = plot_gray_img_with_histogram(img)
img2 = stretch_histogram(img)
fig2 = plot_gray_img_with_histogram(img2)

Result:

history1.png history2.png

Confusion matrix plot

from simplified_keras.transformations import predictions_to_classes, one_hot_to_sparse
from simplified_keras.metrics import get_confusion_matrixes
from simplified_keras.plots import plot_confusion_matrix

predictions = model.predict(validation_images)
predicted_classes = predictions_to_classes(predictions)
sparse_labels = one_hot_to_sparse(validation_labels)

cm, cm_normalized = get_confusion_matrixes(predicted_classes, sparse_labels)
classes = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
f1 = plot_confusion_matrix(cm, classes)
f2 = plot_confusion_matrix(cm_normalized, classes, figsize=(10, 8))

Results:

Metrics

Confusion matrix

from simplified_keras.transformations import predictions_to_classes, one_hot_to_sparse
from simplified_keras.metrics import get_confusion_matrixes

predictions = model.predict(validation_images)
predicted_classes = predictions_to_classes(predictions)
sparse_labels = one_hot_to_sparse(validation_labels)

# Returns two numpy arrays: standard and normalized
cm, cm_normalized = get_confusion_matrixes(predicted_classes, sparse_labels)

Model Statistics

Calculates: self.FP self.FN self.TP self.TN

self.TPR # Sensitivity/true positive rate

self.TNR # Specificity/true negative rate

self.PPV # Precision/positive predictive value

self.NPV # Negative predictive value

self.FPR # Fall out or false positive rate

self.FNR # False negative rate

self.FDR # False discovery rate

self.ACC # Overall accuracy for each class

from simplified_keras.transformations import predictions_to_classes, one_hot_to_sparse
from simplified_keras.metrics import get_confusion_matrixes
from simplified_keras.metrics import get_model_statistics

predictions = model.predict(validation_images)
predicted_classes = predictions_to_classes(predictions)
sparse_labels = one_hot_to_sparse(validation_labels)

cm, cm_normalized = get_confusion_matrixes(predicted_classes, sparse_labels)

stats = get_model_statistics(cm)
classes = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
fig = stats.visualize(classes)
print(stats.TN) #[2890 3530 2874 2361 2591 3000 2661]

Visualization: stat-visualization.png

Folder statistics

from simplified_keras.metrics import get_folders_statistics

stat = get_folders_statistics('../data/normal/train')
print(stat.nr_of_elements, stat.info) # 28709 {'0': 3995, '1': 436, '2': 4097, '3': 7215, '4': 4830, '5': 3171, '6': 4965}
fig = stat.bar_plot()

Result:

drawing

Transformations

Convert predictions to classes array

from simplified_keras.transformations import predictions_to_classes

predictions = model.predict(validation_images)
predicted_classes = predictions_to_classes(predictions)
print(pedicted_classes) #[6 3 3 ... 6 2 0]

Convert one hot encoding to sparse

from simplified_keras.transformations import predictions_to_classes, one_hot_to_sparse

sparse_labels = one_hot_to_sparse(validation_labels)
print(sprase_labels) #[6 6 6 ... 6 2 0]

PyPi

simplified-keras

TODO

  • nothing :)

Development

Want to contribute? Great!

To fix a bug or enhance an existing module, follow these steps:

  • Fork the repo
  • Create a new branch (git checkout -b improve-feature)
  • Make the appropriate changes in the files
  • Verify if they are correct
  • Add changes to reflect the changes made
  • Commit changes
  • Push to the branch (git push origin improve-feature)
  • Create a Pull Request

Status

Library is: in progress

Contact

albert.lis.1996@gmail.com - feel free to contact me!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simplified_keras-0.0.4.tar.gz (7.7 kB view hashes)

Uploaded Source

Built Distribution

simplified_keras-0.0.4-py3-none-any.whl (9.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page