PA8_1002116118

pdf

School

University of Texas, Arlington *

*We aren’t endorsed by this school

Course

5339

Subject

Industrial Engineering

Date

Feb 20, 2024

Type

pdf

Pages

7

Uploaded by MegaCrownKangaroo44

Report
Programming Assignment 8 Amitha Akepati 1002116118 Code: #Initial Setup import tensorflow as tf import random as rn import os import cv2 import numpy as np from keras.preprocessing.image import img_to_array, load_img from keras.utils import to_categorical from keras.models import Sequential from keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense from sklearn.model_selection import train_test_split # Set seeds for reproducibility os.environ[ 'PYTHONHASHSEED' ] = '0' np.random.seed( 37 ) rn.seed( 1254 ) tf.random.set_seed( 89 ) # TensorFlow session configuration config = tf.compat.v1.ConfigProto(intra_op_parallelism_threads= 1 , inter_op_parallelism_threads= 1 ) sess = tf.compat.v1.Session(graph=tf.compat.v1.get_default_graph(), config=config) tf.compat.v1.keras.backend.set_session(sess) # Paths to data directories train_coin_dir = '/content/drive/MyDrive/Training-20231125T160430Z- 001/Training/COIN' train_scrap_dir = '/content/drive/MyDrive/Training-20231125T160430Z- 001/Training/SCRAP' test_coin_dir = '/content/drive/MyDrive/Testing-20231125T160426Z- 001/Testing/COIN' test_scrap_dir = '/content/drive/MyDrive/Testing-20231125T160426Z- 001/Testing/SCRAP' #Data Preparation***************** def load_images_and_labels ( image_paths , label ): images = [] labels = [] for path in image_paths:
image = load_img(path, color_mode= 'grayscale' , target_size=( 200 , 200 )) image = img_to_array(image) image /= 255.0 images.append(image) labels.append(label) return np.array(images), np.array(labels) # Load and prepare training data coin_images = [os.path.join(train_coin_dir, img) for img in os.listdir(train_coin_dir)] scrap_images = [os.path.join(train_scrap_dir, img) for img in os.listdir(train_scrap_dir)] X_coin, y_coin = load_images_and_labels(coin_images, 0 ) X_scrap, y_scrap = load_images_and_labels(scrap_images, 1 ) X_train = np.concatenate((X_coin, X_scrap), axis= 0 ) y_train = np.concatenate((y_coin, y_scrap), axis= 0 ) y_train = to_categorical(y_train) # Split into training and validation sets X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size= 0.2 , random_state= 42 ) # Building the CNN model = Sequential() model.add(Conv2D( 64 , ( 3 , 3 ), strides=( 2 , 2 ), activation= 'relu' , input_shape=( 200 , 200 , 1 ))) model.add(MaxPooling2D(pool_size=( 2 , 2 ))) model.add(Dropout( 0.5 )) model.add(Flatten()) model.add(Dense( 128 , activation= 'relu' )) model.add(Dropout( 0.5 )) model.add(Dense( 2 , activation= 'softmax' )) # Compile the model model. compile (loss= 'categorical_crossentropy' , optimizer= 'adam' , metrics=[ 'accuracy' ]) # Training the Model # For training without data augmentation model.fit(X_train, y_train, batch_size= 32 , epochs= 20 , verbose= 1 , validation_data=(X_val, y_val))
Output: Epoch 1/20 32/32 [==============================] - 35s 1s/step - loss: 0.7708 - accuracy: 0.7292 - val_loss: 0.4167 - val_accuracy: 0.8419 Epoch 2/20 32/32 [==============================] - 23s 725ms/step - loss: 0.4360 - accuracy: 0.8202 - val_loss: 0.3277 - val_accuracy: 0.8735 Epoch 3/20 32/32 [==============================] - 26s 834ms/step - loss: 0.3851 - accuracy: 0.8419 - val_loss: 0.3209 - val_accuracy: 0.8814 Epoch 4/20 32/32 [==============================] - 23s 722ms/step - loss: 0.3573 - accuracy: 0.8597 - val_loss: 0.3474 - val_accuracy: 0.8696 Epoch 5/20 32/32 [==============================] - 25s 792ms/step - loss: 0.3302 - accuracy: 0.8686 - val_loss: 0.2771 - val_accuracy: 0.8854 Epoch 6/20 32/32 [==============================] - 24s 728ms/step - loss: 0.3142 - accuracy: 0.8696 - val_loss: 0.2724 - val_accuracy: 0.9091 Epoch 7/20 32/32 [==============================] - 23s 725ms/step - loss: 0.2963 - accuracy: 0.8765 - val_loss: 0.2559 - val_accuracy: 0.9091 Epoch 8/20 32/32 [==============================] - 25s 780ms/step - loss: 0.2824 - accuracy: 0.8962 - val_loss: 0.2467 - val_accuracy: 0.8972 Epoch 9/20 32/32 [==============================] - 23s 720ms/step - loss: 0.2730 - accuracy: 0.8943 - val_loss: 0.2356 - val_accuracy: 0.9091 Epoch 10/20 32/32 [==============================] - 25s 773ms/step - loss: 0.2566 - accuracy: 0.8982 - val_loss: 0.2352 - val_accuracy: 0.9091 Epoch 11/20 32/32 [==============================] - 23s 703ms/step - loss: 0.2361 - accuracy: 0.9111 - val_loss: 0.2105 - val_accuracy: 0.9051 Epoch 12/20 32/32 [==============================] - 23s 708ms/step - loss: 0.2170 - accuracy: 0.9121 - val_loss: 0.1968 - val_accuracy: 0.9170 Epoch 13/20 32/32 [==============================] - 25s 793ms/step - loss: 0.2066 - accuracy: 0.9190 - val_loss: 0.1978 - val_accuracy: 0.9368 Epoch 14/20 32/32 [==============================] - 22s 707ms/step - loss: 0.1866 - accuracy: 0.9269 - val_loss: 0.1810 - val_accuracy: 0.9249 Epoch 15/20 32/32 [==============================] - 22s 705ms/step - loss: 0.1801 - accuracy: 0.9229 - val_loss: 0.1592 - val_accuracy: 0.9209 Epoch 16/20 32/32 [==============================] - 24s 750ms/step - loss: 0.1531 - accuracy: 0.9318 - val_loss: 0.1725 - val_accuracy: 0.9091 Epoch 17/20 32/32 [==============================] - 21s 660ms/step - loss: 0.1629 - accuracy: 0.9368 - val_loss: 0.1409 - val_accuracy: 0.9289 Epoch 18/20
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
32/32 [==============================] - 22s 698ms/step - loss: 0.1396 - accuracy: 0.9476 - val_loss: 0.1331 - val_accuracy: 0.9328 Epoch 19/20 32/32 [==============================] - 26s 826ms/step - loss: 0.1310 - accuracy: 0.9536 - val_loss: 0.1329 - val_accuracy: 0.9328 Epoch 20/20 32/32 [==============================] - 22s 697ms/step - loss: 0.1302 - accuracy: 0.9496 - val_loss: 0.1437 - val_accuracy: 0.9289 <keras.src.callbacks.History at 0x7f17c85eae90> Code: from google.colab import drive drive.mount( '/content/drive' ) #Initial Setup import tensorflow as tf import random as rn import os import cv2 import numpy as np from keras.preprocessing.image import img_to_array, load_img from keras.utils import to_categorical from keras.models import Sequential from keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense from sklearn.model_selection import train_test_split # Set seeds for reproducibility os.environ[ 'PYTHONHASHSEED' ] = '0' np.random.seed( 37 ) rn.seed( 1254 ) tf.random.set_seed( 89 ) # TensorFlow session configuration config = tf.compat.v1.ConfigProto(intra_op_parallelism_threads= 1 , inter_op_parallelism_threads= 1 ) sess = tf.compat.v1.Session(graph=tf.compat.v1.get_default_graph(), config=config) tf.compat.v1.keras.backend.set_session(sess) # Paths to data directories train_coin_dir = '/content/drive/MyDrive/Training-20231125T160430Z- 001/Training/COIN' train_scrap_dir = '/content/drive/MyDrive/Training-20231125T160430Z- 001/Training/SCRAP'
test_coin_dir = '/content/drive/MyDrive/Testing-20231125T160426Z- 001/Testing/COIN' test_scrap_dir = '/content/drive/MyDrive/Testing-20231125T160426Z- 001/Testing/SCRAP' #Data Preparation***************** def load_images_and_labels ( image_paths , label ): images = [] labels = [] for path in image_paths: image = load_img(path, color_mode= 'grayscale' , target_size=( 200 , 200 )) image = img_to_array(image) image /= 255.0 images.append(image) labels.append(label) return np.array(images), np.array(labels) # Load and prepare training data # Load and prepare test data test_coin_images = [os.path.join(test_coin_dir, img) for img in os.listdir(test_coin_dir)] test_scrap_images = [os.path.join(test_scrap_dir, img) for img in os.listdir(test_scrap_dir)] X_test_coin, y_test_coin = load_images_and_labels(test_coin_images, 0 ) X_test_scrap, y_test_scrap = load_images_and_labels(test_scrap_images, 1 ) X_test = np.concatenate((X_test_coin, X_test_scrap), axis= 0 ) y_test = np.concatenate((y_test_coin, y_test_scrap), axis= 0 ) y_test = to_categorical(y_test) # Split into training and validation sets X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size= 0.2 , random_state= 42 ) # Building the CNN model = Sequential() model.add(Conv2D( 64 , ( 3 , 3 ), strides=( 2 , 2 ), activation= 'relu' , input_shape=( 200 , 200 , 1 ))) model.add(MaxPooling2D(pool_size=( 2 , 2 ))) model.add(Dropout( 0.5 )) model.add(Flatten()) model.add(Dense( 128 , activation= 'relu' )) model.add(Dropout( 0.5 ))
model.add(Dense( 2 , activation= 'softmax' )) model. compile (loss= 'categorical_crossentropy' , optimizer= 'adam' , metrics=[ 'accuracy' ]) # Training the Model # Choose one of the following based on whether you are using data augmentation or not # Without data augmentation: model.fit(X_train, y_train, batch_size= 32 , epochs= 20 , verbose= 1 , validation_data=(X_val, y_val)) # Evaluate the model test_loss, test_accuracy = model.evaluate(X_test, y_test) print ( f "Test Loss: {test_loss} , Test Accuracy: {test_accuracy} " ) Output: Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True). Epoch 1/20 26/26 [==============================] - 19s 699ms/step - loss: 1.0261 - accuracy: 0.6477 - val_loss: 0.5045 - val_accuracy: 0.7833 Epoch 2/20 26/26 [==============================] - 18s 691ms/step - loss: 0.4832 - accuracy: 0.7973 - val_loss: 0.4063 - val_accuracy: 0.8177 Epoch 3/20 26/26 [==============================] - 19s 729ms/step - loss: 0.4429 - accuracy: 0.8109 - val_loss: 0.3620 - val_accuracy: 0.8571 Epoch 4/20 26/26 [==============================] - 19s 724ms/step - loss: 0.3881 - accuracy: 0.8455 - val_loss: 0.3313 - val_accuracy: 0.8571 Epoch 5/20 26/26 [==============================] - 19s 717ms/step - loss: 0.3824 - accuracy: 0.8430 - val_loss: 0.3281 - val_accuracy: 0.8768 Epoch 6/20 26/26 [==============================] - 19s 721ms/step - loss: 0.3657 - accuracy: 0.8480 - val_loss: 0.3158 - val_accuracy: 0.8670 Epoch 7/20 26/26 [==============================] - 29s 1s/step - loss: 0.3489 - accuracy: 0.8616 - val_loss: 0.2939 - val_accuracy: 0.8670 Epoch 8/20 26/26 [==============================] - 19s 748ms/step - loss: 0.3279 - accuracy: 0.8727 - val_loss: 0.2848 - val_accuracy: 0.8867 Epoch 9/20 26/26 [==============================] - 20s 757ms/step - loss: 0.3276 - accuracy: 0.8714 - val_loss: 0.2770 - val_accuracy: 0.8768 Epoch 10/20 26/26 [==============================] - 19s 745ms/step - loss: 0.3150 - accuracy: 0.8739 - val_loss: 0.2832 - val_accuracy: 0.8768 Epoch 11/20 26/26 [==============================] - 19s 736ms/step - loss: 0.2962 - accuracy: 0.8863 - val_loss: 0.2463 - val_accuracy: 0.9015
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Epoch 12/20 26/26 [==============================] - 20s 743ms/step - loss: 0.2861 - accuracy: 0.8949 - val_loss: 0.2384 - val_accuracy: 0.9113 Epoch 13/20 26/26 [==============================] - 19s 732ms/step - loss: 0.2762 - accuracy: 0.8888 - val_loss: 0.2355 - val_accuracy: 0.8966 Epoch 14/20 26/26 [==============================] - 19s 736ms/step - loss: 0.2692 - accuracy: 0.8962 - val_loss: 0.2351 - val_accuracy: 0.9163 Epoch 15/20 26/26 [==============================] - 19s 736ms/step - loss: 0.2395 - accuracy: 0.9061 - val_loss: 0.2096 - val_accuracy: 0.9310 Epoch 16/20 26/26 [==============================] - 19s 741ms/step - loss: 0.2470 - accuracy: 0.9036 - val_loss: 0.2139 - val_accuracy: 0.9064 Epoch 17/20 26/26 [==============================] - 18s 711ms/step - loss: 0.2306 - accuracy: 0.9110 - val_loss: 0.1901 - val_accuracy: 0.9261 Epoch 18/20 26/26 [==============================] - 18s 705ms/step - loss: 0.2067 - accuracy: 0.9172 - val_loss: 0.1892 - val_accuracy: 0.9064 Epoch 19/20 26/26 [==============================] - 19s 739ms/step - loss: 0.2069 - accuracy: 0.9147 - val_loss: 0.2051 - val_accuracy: 0.9015 Epoch 20/20 26/26 [==============================] - 18s 709ms/step - loss: 0.2046 - accuracy: 0.9258 - val_loss: 0.1709 - val_accuracy: 0.9310 13/13 [==============================] - 2s 164ms/step - loss: 0.1702 - accuracy: 0.9444 Test Loss: 0.17021119594573975, Test Accuracy: 0.9444444179534912 CodeText