Posts

Showing posts from August, 2023

7. Optimization of Training in Deep Learning (Design Robust Bi-Tempered Logistic Loss)

 AIM: Design a Deep learning Network for Robust Bi-Tempered Logistic Loss Theory: The Robust Bi-Tempered Logistic Loss is a loss function designed for training deep neural networks, particularly in the context of classification tasks. It addresses some of the limitations of traditional loss functions like the standard cross-entropy loss, which can suffer from issues like vanishing gradients and sensitivity to outliers. The Robust Bi-Tempered Logistic Loss aims to provide better convergence properties and improved robustness to noisy or mislabeled data. Program:  import tensorflow as tf import numpy as np # Define the Robust Bi-Tempered Logistic Loss def robust_bi_tempered_logistic_ loss ( y_true , y_pred , t1 = 0.8 , t2 = 1.2 , label_smoothing = 0.1 ):     y_true = tf.cast(y_true, dtype=tf.float32)     y_pred = tf.math.softmax(y_pred, axis= -1 )     temp1 = ( 1 - y_true) * tf.math.maximum(y_pred - t1, 0 )     temp2 = ( 1 - y_true)...

8.2 Testing Alexnet Model

 Program: import numpy as np import tensorflow as tf from tensorflow.keras.models import load_model from tensorflow.keras.preprocessing.image import load_img, img_to_array #from tensorflow.keras.applications.resnet50 import preprocess_input import matplotlib.pyplot as plt # Load the saved model model = load_model( '/content/drive/MyDrive/8/alexnet_cifar10.h5' ) d={ 0 : "Airplane" , 1 : "Automobile" , 2 : "Bird" , 3 : "Cat" , 4 : "Deer" , 5 : "Dog" , 6 : "Frog" ,     7 : "Horse" , 8 : "Ship" , 9 : "Truck" } # Load and preprocess an image from a local path for prediction local_image_path = '/content/drive/MyDrive/8/a.jpg'   # Replace with the path to your local image local_image = load_img(local_image_path, target_size=( 32 , 32 )) local_image1 = load_img(local_image_path) local_image_array = img_to_array(local_image) local_image_array = np.expand_dims(local_imag...

8. Advanced CNN (Build AlexNet using Advanced CNN)

  Building and training an AlexNet-like model using the CIFAR-10 dataset in TensorFlow/Keras: Program to build the model: import tensorflow as tf from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout from tensorflow.keras.datasets import cifar10 from tensorflow.keras.utils import to_categorical from sklearn.model_selection import train_test_split # Load and preprocess CIFAR-10 dataset (x_train, y_train), (x_test, y_test) = cifar10.load_data() x_train = x_train.astype( 'float32' ) / 255.0 x_test = x_test.astype( 'float32' ) / 255.0 y_train = to_categorical(y_train, num_classes= 10 ) y_test = to_categorical(y_test, num_classes= 10 ) # Split into training and validation sets x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size= 0.1 , random_state= 42 ) # Define the model architecture '''model = tf.keras.Sequential([     Conv2D(96, (11, 11), strides=(4, 4), activation='relu', input_shape=(32...

6. Advanced Deep Learning Architectures

AIM:   Implement Object Detection Using YOLO. PROGRAM: import cv2 import numpy as np # Load YOLO model net = cv2.dnn.readNet( "/content/drive/MyDrive/6-3/yolov3.weights" , "/content/drive/MyDrive/6-3/yolov3.cfg" ) classes = [] with open ( "/content/drive/MyDrive/6-3/classes.txt" , "r" ) as f:     classes = [line.strip() for line in f.readlines()] #layer_names = net.getLayerNames() #output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()] layer_names = net.getLayerNames() # Get the names of the output layers (unconnected layers) output_layers = [layer_names[i - 1 ] for i in net.getUnconnectedOutLayers()] # Load image image = cv2.imread( "/content/drive/MyDrive/6/test/road1.jpg" ) height, width, channels = image.shape # Preprocess image for YOLO model blob = cv2.dnn.blobFromImage(image, scalefactor= 0.00392 , size=( 416 , 416 ), mean=( 0 , 0 , 0 ), swapRB= True , crop= False ) # Set the input to the YOLO net...

5. Removing noise from the images

 AIM:  Implement Multi-Layer Perceptron algorithm for Image denoising hyperparameter tuning. PROGRAM: import numpy as np import tensorflow as tf from tensorflow.keras.layers import Input, Dense from tensorflow.keras.models import Model from tensorflow.keras.datasets import mnist # Load the MNIST dataset (x_train, _), (x_test, _) = mnist.load_data() # Normalize and reshape the data x_train = x_train.astype( 'float32' ) / 255.0 x_test = x_test.astype( 'float32' ) / 255.0 x_train = x_train.reshape(( len (x_train), np.prod(x_train.shape[ 1 :]))) x_test = x_test.reshape(( len (x_test), np.prod(x_test.shape[ 1 :]))) # Add noise to the training and test data noise_factor = 0.5 x_train_noisy = x_train + noise_factor * np.random.normal(loc= 0.0 , scale= 1.0 , size=x_train.shape) x_test_noisy = x_test + noise_factor * np.random.normal(loc= 0.0 , scale= 1.0 , size=x_test.shape) # Clip the noisy images to ensure values are between 0 and 1 x_train_noisy = np.clip(x_train_n...