help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
tensorflow-deeplearning
/
src
RSK World
tensorflow-deeplearning
Deep learning with TensorFlow and Keras
src
  • utils
  • __init__.py330 B
  • autoencoders.py8 KB
  • cnns.py6.7 KB
  • custom_layers.py8.3 KB
  • data_generator.py14.2 KB
  • data_preprocessing.py9.9 KB
  • gans.py7 KB
  • model_deployment.py8.7 KB
  • model_evaluation.py10.5 KB
  • model_training.py10.1 KB
  • neural_networks.py4.7 KB
  • rnns.py6.8 KB
  • transfer_learning.py5.4 KB
  • transformers.py7.8 KB
  • visualization.py9.6 KB
README.mdgans.py
README.md
Raw Download

README.md

# TensorFlow Deep Learning

<!--
Project: TensorFlow Deep Learning
Author: RSK World
Website: https://rskworld.in
Email: help@rskworld.in
Phone: +91 93305 39277
Category: Deep Learning
Difficulty: Advanced
-->

Deep learning with TensorFlow including neural networks, CNNs, RNNs, and building custom models for various applications.

## Description

This project provides a comprehensive guide to TensorFlow, Google's deep learning framework. It covers neural network construction, convolutional neural networks (CNNs), recurrent neural networks (RNNs), custom layers, model training, and deployment. Perfect for building deep learning applications.

## Features

### Core Deep Learning Models
- **Neural Networks**: Feedforward networks, deep networks with batch normalization
- **CNNs**: Simple CNNs, deep CNNs, ResNet-style architectures
- **RNNs**: Simple RNN, LSTM, GRU, Bidirectional LSTM, Sequence-to-sequence models
- **Transformers**: Multi-head attention, encoder-decoder architectures
- **Transfer Learning**: Pre-trained models (VGG16, ResNet50, MobileNet, InceptionV3, etc.)
- **GANs**: Generative Adversarial Networks (DCGAN implementation)
- **Autoencoders**: Simple, Convolutional, and Variational Autoencoders

### Advanced Features
- **Custom Layers**: Custom dense, attention, and residual layers
- **Model Training**: Advanced training techniques, callbacks, data augmentation, mixed precision
- **Model Evaluation**: Comprehensive metrics, confusion matrices, ROC curves
- **Data Preprocessing**: Image, text, and tabular data preprocessing pipelines
- **Visualization**: Training history, model architecture, feature importance, layer activations
- **Model Deployment**: SavedModel, H5, TFLite, TensorFlow.js, REST API
- **Docker Support**: Containerized deployment with Docker and Docker Compose

## Technologies

- **Deep Learning**: TensorFlow, Keras
- **Data Processing**: NumPy, Pandas, Scikit-learn
- **Visualization**: Matplotlib, Seaborn
- **Development**: Jupyter Notebook, Python 3.8+
- **Deployment**: Flask, Docker, TensorFlow Serving
- **Utilities**: Pillow, TensorFlow.js

## Project Structure

```
tensorflow-deeplearning/
├── README.md
├── requirements.txt
├── setup.py
├── main.py
├── config.yaml
├── env.example
├── Dockerfile
├── docker-compose.yml
├── notebooks/
│ ├── 01_neural_networks.ipynb
│ ├── 02_cnns.ipynb
│ ├── 03_rnns.ipynb
│ └── 04_custom_models.ipynb
├── src/
│ ├── __init__.py
│ ├── neural_networks.py
│ ├── cnns.py
│ ├── rnns.py
│ ├── transformers.py
│ ├── transfer_learning.py
│ ├── gans.py
│ ├── autoencoders.py
│ ├── custom_layers.py
│ ├── model_training.py
│ ├── model_deployment.py
│ ├── model_evaluation.py
│ ├── data_preprocessing.py
│ ├── visualization.py
│ └── utils/
│ ├── __init__.py
│ └── helpers.py
├── api/
│ ├── server.py
│ └── requirements.txt
├── examples/
│ ├── train_custom_model.py
│ └── transfer_learning_example.py
├── tests/
│ ├── test_neural_networks.py
│ └── test_cnns.py
├── models/
│ └── .gitkeep
└── data/
└── .gitkeep
```

## Installation

1. Clone the repository
2. Install dependencies:
```bash
pip install -r requirements.txt
```

## Installation

1. Clone the repository
2. Install dependencies:
```bash
pip install -r requirements.txt
```

3. (Optional) For API server:
```bash
pip install -r api/requirements.txt
```

## Usage

### Running Python Scripts

```bash
# Using main entry point
python main.py --module neural_networks
python main.py --module cnns
python main.py --module rnns
python main.py --module transfer_learning
python main.py --module gans
python main.py --module autoencoders

# Direct module execution
python src/neural_networks.py
python src/cnns.py
python src/rnns.py
```

### Running Example Scripts

```bash
python examples/train_custom_model.py
python examples/transfer_learning_example.py
```

### Running Jupyter Notebooks

```bash
jupyter notebook notebooks/
```

### Running Tests

```bash
python -m pytest tests/
# or
python -m unittest discover tests
```

### Running API Server

```bash
# Using Python directly
python api/server.py

# Using Docker
docker-compose up tensorflow-api

# The API will be available at http://localhost:5000
```

### Docker Deployment

```bash
# Build and run with Docker Compose
docker-compose up -d

# Run Jupyter notebook in Docker
docker-compose up jupyter
```

## API Endpoints

- `GET /health` - Health check
- `POST /predict` - Single prediction
- `POST /predict/batch` - Batch predictions
- `GET /model/info` - Model information

## Configuration

Edit `config.yaml` or create `.env` file from `env.example` to customize settings.

## Contact

**RSK World**
- Website: https://rskworld.in
- Email: help@rskworld.in
- Phone: +91 93305 39277

## License

This project is for educational purposes.
src/gans.py
Raw Download
Find: Go to:
"""
Generative Adversarial Networks (GANs) with TensorFlow
Author: RSK World
Website: https://rskworld.in
Email: help@rskworld.in
Phone: +91 93305 39277

This module demonstrates GAN implementations including DCGAN.
"""

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers, Model
import numpy as np
import matplotlib.pyplot as plt

def build_generator(latent_dim=100):
    """
    Build a generator model for GAN.
    
    Args:
        latent_dim: Dimension of latent space
    
    Returns:
        Generator model
    """
    model = keras.Sequential([
        layers.Dense(7 * 7 * 256, use_bias=False, input_shape=(latent_dim,)),
        layers.BatchNormalization(),
        layers.LeakyReLU(),
        
        layers.Reshape((7, 7, 256)),
        
        layers.Conv2DTranspose(128, (5, 5), strides=(1, 1), padding='same', use_bias=False),
        layers.BatchNormalization(),
        layers.LeakyReLU(),
        
        layers.Conv2DTranspose(64, (5, 5), strides=(2, 2), padding='same', use_bias=False),
        layers.BatchNormalization(),
        layers.LeakyReLU(),
        
        layers.Conv2DTranspose(1, (5, 5), strides=(2, 2), padding='same', use_bias=False, activation='tanh')
    ])
    
    return model

def build_discriminator(input_shape=(28, 28, 1)):
    """
    Build a discriminator model for GAN.
    
    Args:
        input_shape: Shape of input images
    
    Returns:
        Discriminator model
    """
    model = keras.Sequential([
        layers.Conv2D(64, (5, 5), strides=(2, 2), padding='same', input_shape=input_shape),
        layers.LeakyReLU(),
        layers.Dropout(0.3),
        
        layers.Conv2D(128, (5, 5), strides=(2, 2), padding='same'),
        layers.LeakyReLU(),
        layers.Dropout(0.3),
        
        layers.Flatten(),
        layers.Dense(1, activation='sigmoid')
    ])
    
    return model

def build_dcgan(generator, discriminator):
    """
    Build a DCGAN model combining generator and discriminator.
    
    Args:
        generator: Generator model
        discriminator: Discriminator model
    
    Returns:
        Combined GAN model
    """
    # Freeze discriminator during generator training
    discriminator.trainable = False
    
    # Create GAN
    gan_input = keras.Input(shape=(100,))
    generated_image = generator(gan_input)
    gan_output = discriminator(generated_image)
    
    gan = Model(gan_input, gan_output)
    gan.compile(
        optimizer=keras.optimizers.Adam(learning_rate=0.0002, beta_1=0.5),
        loss='binary_crossentropy'
    )
    
    return gan

def train_gan(generator, discriminator, gan, dataset, epochs=50, batch_size=128, latent_dim=100):
    """
    Train a GAN model.
    
    Args:
        generator: Generator model
        discriminator: Discriminator model
        gan: Combined GAN model
        dataset: Training dataset
        epochs: Number of training epochs
        batch_size: Batch size
        latent_dim: Dimension of latent space
    
    Returns:
        Training history
    """
    real_label = 1.0
    fake_label = 0.0
    
    history = {'d_loss': [], 'g_loss': []}
    
    for epoch in range(epochs):
        epoch_d_loss = []
        epoch_g_loss = []
        
        for batch in dataset:
            batch_size_actual = batch.shape[0]
            
            # Train discriminator
            noise = tf.random.normal([batch_size_actual, latent_dim])
            generated_images = generator(noise, training=False)
            
            # Combine real and fake images
            real_images = batch
            combined_images = tf.concat([real_images, generated_images], axis=0)
            
            # Create labels
            labels = tf.concat([
                tf.ones((batch_size_actual, 1)) * real_label,
                tf.ones((batch_size_actual, 1)) * fake_label
            ], axis=0)
            
            # Add noise to labels (label smoothing)
            labels += 0.05 * tf.random.uniform(labels.shape)
            
            # Train discriminator
            d_loss = discriminator.train_on_batch(combined_images, labels)
            epoch_d_loss.append(d_loss)
            
            # Train generator
            noise = tf.random.normal([batch_size_actual, latent_dim])
            misleading_labels = tf.ones((batch_size_actual, 1)) * real_label
            
            g_loss = gan.train_on_batch(noise, misleading_labels)
            epoch_g_loss.append(g_loss)
        
        avg_d_loss = np.mean(epoch_d_loss)
        avg_g_loss = np.mean(epoch_g_loss)
        
        history['d_loss'].append(avg_d_loss)
        history['g_loss'].append(avg_g_loss)
        
        if (epoch + 1) % 10 == 0:
            print(f"Epoch {epoch + 1}/{epochs} - D Loss: {avg_d_loss:.4f}, G Loss: {avg_g_loss:.4f}")
            
            # Generate sample images
            generate_and_save_images(generator, epoch + 1, latent_dim)
    
    return history

def generate_and_save_images(generator, epoch, latent_dim, num_images=16):
    """
    Generate and save sample images from generator.
    
    Args:
        generator: Generator model
        epoch: Current epoch number
        latent_dim: Dimension of latent space
        num_images: Number of images to generate
    """
    noise = tf.random.normal([num_images, latent_dim])
    generated_images = generator(noise, training=False)
    
    fig = plt.figure(figsize=(4, 4))
    for i in range(generated_images.shape[0]):
        plt.subplot(4, 4, i + 1)
        plt.imshow(generated_images[i, :, :, 0] * 127.5 + 127.5, cmap='gray')
        plt.axis('off')
    
    plt.tight_layout()
    plt.savefig(f'generated_images_epoch_{epoch}.png')
    plt.close()

def example_usage():
    """
    Example usage of GAN functions.
    """
    # Build models
    generator = build_generator(latent_dim=100)
    discriminator = build_discriminator(input_shape=(28, 28, 1))
    
    # Compile discriminator
    discriminator.compile(
        optimizer=keras.optimizers.Adam(learning_rate=0.0002, beta_1=0.5),
        loss='binary_crossentropy',
        metrics=['accuracy']
    )
    
    # Build GAN
    gan = build_dcgan(generator, discriminator)
    
    print("Generator Model:")
    generator.summary()
    
    print("\nDiscriminator Model:")
    discriminator.summary()
    
    # Generate dummy dataset for demonstration
    dataset = tf.random.normal([1000, 28, 28, 1])
    dataset = tf.data.Dataset.from_tensor_slices(dataset).batch(128)
    
    # Train GAN (short training for demo)
    print("\nTraining GAN...")
    history = train_gan(
        generator, discriminator, gan,
        dataset, epochs=5, batch_size=128, latent_dim=100
    )
    
    return generator, discriminator, gan, history

if __name__ == '__main__':
    print("Generative Adversarial Networks with TensorFlow")
    print("Author: RSK World - https://rskworld.in")
    generator, discriminator, gan, history = example_usage()
233 lines•7 KB
python

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer