Skip to content

Instantly share code, notes, and snippets.

View pythonlessons's full-sized avatar

Rokas Liuberskis pythonlessons

View GitHub Profile
# celebA dataset path
dataset_path = "Dataset/img_align_celeba"
# Set the input shape and size for the generator and discriminator
batch_size = 128
img_shape = (64, 64, 3) # The shape of the input image, input to the discriminator
noise_dim = 128 # The dimension of the noise vector, input to the generator
model_path = 'Models/02_WGANGP_faces'
os.makedirs(model_path, exist_ok=True)
# Load MNIST dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# Normalize pixel values to [-1, 1]
x_train = (x_train.astype('float32') - 127.5) / 127.5
# Set the input shape and size for the generator and discriminator
img_shape = (28, 28, 1) # The shape of the input image, input to the discriminator
noise_dim = 100 # The dimension of the noise vector, input to the generator
model_path = 'Models/01_GANs_introduction'
class ResultsCallback(tf.keras.callbacks.Callback):
"""A callback that saves generated images after each epoch."""
def __init__(
self,
noise_dim: int,
results_path: str,
examples_to_generate: int=16,
grid_size: tuple=(4, 4),
spacing: int=5,
gif_size: tuple=(416, 416),
class GAN(tf.keras.models.Model):
"""A Generative Adversarial Network (GAN) implementation.
This class inherits from `tf.keras.models.Model` and provides a
straightforward implementation of the GAN algorithm.
"""
def __init__(
self,
discriminator: tf.keras.models.Model,
generator: tf.keras.models.Model,
def discriminator_loss(real_output, fake_output):
real_loss = tf.keras.losses.BinaryCrossentropy(from_logits=True)(tf.ones_like(real_output), real_output)
fake_loss = tf.keras.losses.BinaryCrossentropy(from_logits=True)(tf.zeros_like(fake_output), fake_output)
total_loss = real_loss + fake_loss
return total_loss
def generator_loss(fake_output):
return tf.keras.losses.BinaryCrossentropy(from_logits=True)(tf.ones_like(fake_output), fake_output)
import tensorflow as tf
from keras import layers
# Define the generator model
def build_generator(noise_dim):
inputs = layers.Input(shape=noise_dim, name="input")
x = layers.Dense(7*7*256, use_bias=False)(inputs)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)
import tensorflow as tf
from tensorflow.keras.datasets import mnist
# Load MNIST dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# Normalize pixel values to [-1, 1]
x_train = (x_train.astype('float32') - 127.5) / 127.5
@pythonlessons
pythonlessons / handwriting_recognition_pytorch_16.html
Created March 20, 2023 13:53
handwriting_recognition_pytorch
...
Image: Datasets/IAM_Words/words/b05/b05-017/b05-017-03-03.png, Label: won't, Prediction: won't, CER: 0.0
Image: Datasets/IAM_Words/words/a01/a01-049u/a01-049u-07-06.png, Label: session, Prediction: sessicn, CER: 0.14285714285714285
Image: Datasets/IAM_Words/words/a02/a02-000/a02-000-07-00.png, Label: but, Prediction: but, CER: 0.0
Image: Datasets/IAM_Words/words/m02/m02-087/m02-087-06-02.png, Label: as, Prediction: as, CER: 0.0
Image: Datasets/IAM_Words/words/g06/g06-037j/g06-037j-07-01.png, Label: his, Prediction: his, CER: 0.0
Image: Datasets/IAM_Words/words/g06/g06-047i/g06-047i-02-09.png, Label: human, Prediction: human, CER: 0.0
Image: Datasets/IAM_Words/words/c03/c03-094c/c03-094c-08-03.png, Label: gaudy, Prediction: gaudy, CER: 0.0
Image: Datasets/IAM_Words/words/e04/e04-132/e04-132-01-04.png, Label: on, Prediction: on, CER: 0.0
Image: Datasets/IAM_Words/words/k02/k02-018/k02-018-04-01.png, Label: surprised, Prediction: supised, CER: 0.2222222222222222
@pythonlessons
pythonlessons / handwriting_recognition_pytorch_15.html
Created March 20, 2023 13:53
handwriting_recognition_pytorch
===================================================================================
Kernel Shape Output Shape Params \
Layer
0_rb1.convb1.Conv2d_conv [3, 16, 3, 3] [1, 16, 32, 128] 448.0
1_rb1.convb1.BatchNorm2d_bn [16] [1, 16, 32, 128] 32.0
2_rb1.LeakyReLU_act1 - [1, 16, 32, 128] -
3_rb1.convb2.Conv2d_conv [16, 16, 3, 3] [1, 16, 32, 128] 2.32k
4_rb1.convb2.BatchNorm2d_bn [16] [1, 16, 32, 128] 32.0
5_rb1.Conv2d_shortcut [3, 16, 1, 1] [1, 16, 32, 128] 64.0
6_rb1.LeakyReLU_act2 - [1, 16, 32, 128] -
@pythonlessons
pythonlessons / handwriting_recognition_pytorch_14.py
Created March 20, 2023 13:53
handwriting_recognition_pytorch
import cv2
import typing
import numpy as np
from mltu.inferenceModel import OnnxInferenceModel
from mltu.utils.text_utils import ctc_decoder, get_cer
class ImageToWordModel(OnnxInferenceModel):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)