0% found this document useful (0 votes)
9 views

PS4 - Ritesh Jaiswal - Ritesh - 054

Uploaded by

Ritesh Jaishwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

PS4 - Ritesh Jaiswal - Ritesh - 054

Uploaded by

Ritesh Jaishwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1/19/24, 9:39 AM PS4_Ritesh jaiswal_Ritesh_054 - Jupyter Notebook

In [ ]: !nvidia-smi

Thu Jan 18 12:06:59 2024


+-------------------------------------------------------------------------
--------------+
| NVIDIA-SMI 535.104.05 Driver Version: 535.104.05 CUDA Vers
ion: 12.2 |
|-----------------------------------------+----------------------+--------
--------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatil
e Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Uti
l Compute M. |
| | |
MIG M. |
|=========================================+======================+========
==============|
| 0 Tesla T4 Off | 00000000:00:04.0 Off |
0 |
| N/A 64C P8 12W / 70W | 0MiB / 15360MiB | 0%
Default |
| | |
N/A |
+-----------------------------------------+----------------------+--------
--------------+

+-------------------------------------------------------------------------
--------------+
| Processes:
|
| GPU GI CI PID Type Process name
GPU Memory |
| ID ID
Usage |
|=========================================================================
==============|
| No running processes found
|
+-------------------------------------------------------------------------
--------------+

In [ ]: !mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/

In [ ]: !kaggle competitions download -c sankalan-crack-detection-challenge

Warning: Your Kaggle API key is readable by other users on this system! To
fix this, you can run 'chmod 600 /root/.kaggle/kaggle.json'
Downloading sankalan-crack-detection-challenge.zip to /content
94% 140M/149M [00:01<00:00, 178MB/s]
100% 149M/149M [00:01<00:00, 144MB/s]

localhost:8888/notebooks/Downloads/Random/COMPETETION/PS4_Ritesh jaiswal_Ritesh_054.ipynb 1/8


1/19/24, 9:39 AM PS4_Ritesh jaiswal_Ritesh_054 - Jupyter Notebook

In [ ]: import zipfile
zip_ref=zipfile.ZipFile('/content/sankalan-crack-detection-challenge.zip','
zip_ref.extractall('/content')
zip_ref.close()

In [ ]: import numpy as np
import pandas as pd
import os
import cv2
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras
from PIL import Image
from sklearn.model_selection import train_test_split
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import Adam
from sklearn.metrics import accuracy_score
np.random.seed(42)

from matplotlib import style
style.use('fivethirtyeight')

In [ ]: train_path='/content/train'
# Resizing the images to 30x30x3
IMG_HEIGHT = 30
IMG_WIDTH = 30
channels = 3

In [ ]: NUM_CATEGORIES = len(os.listdir(train_path))
NUM_CATEGORIES

In [ ]: from google.colab.patches import cv2_imshow


from matplotlib.image import imread

In [ ]: img=plt.imread('/content/train/cracked/1002.jpg')
plt.imshow(img)
plt.grid(visible=None)

In [ ]: os.listdir(train_path)

In [ ]: folders=os.listdir(train_path)
d={}
for i in folders:
class_file=os.listdir(train_path+'/'+i)
d[i]=len(class_file)
d

In [ ]: a=pd.DataFrame(d.items()).rename(columns={0:'Category',1:'Count'})
a

localhost:8888/notebooks/Downloads/Random/COMPETETION/PS4_Ritesh jaiswal_Ritesh_054.ipynb 2/8


1/19/24, 9:39 AM PS4_Ritesh jaiswal_Ritesh_054 - Jupyter Notebook

In [ ]: import seaborn as sns


sns.set(rc={'figure.figsize':(10,8)})
sns.barplot(x='Category',y='Count',data=a)
plt.xticks(rotation=90);

In [ ]: imgs=os.listdir('/content/test')
imgs=imgs[:25]
imgs

In [ ]: plt.figure(figsize=(25,25))
for i in range(1,26):
plt.subplot(6,5,i)
img=imread('/content/test/'+imgs[i-1])
plt.imshow(img)
plt.grid(None)

In [ ]: a=cv2.imread('/content/train/cracked/10013.jpg')
a

In [ ]: a=Image.fromarray(a,'RGB')
a

In [ ]: lst=os.listdir('/content/train')
lst

In [ ]: # Here our prime goal is to read the png images in form of array convert th
image_data = []
image_labels = []
for i in lst:
images=os.listdir('/content/train/'+i)
for img in images:
try:
image=cv2.imread('/content/train/'+i+'/'+img)
image_fromarray=Image.fromarray(image,'RGB')
resize_image=image_fromarray.resize((30,30))
image_data.append(np.array(resize_image))
image_labels.append(i)
except:
print('Error in ',img)

In [ ]: zi={'cracked':1,'uncracked':0}
zi

In [ ]: img_labels=[]
for i in image_labels:
if i in zi.keys():
img_labels.append(zi[i])

localhost:8888/notebooks/Downloads/Random/COMPETETION/PS4_Ritesh jaiswal_Ritesh_054.ipynb 3/8


1/19/24, 9:39 AM PS4_Ritesh jaiswal_Ritesh_054 - Jupyter Notebook

In [ ]: # Changing the list to numpy array


image_data = np.array(image_data)
img_labels = np.array(img_labels)

In [ ]: #Shuffling the training data


shuffle_indexes = np.arange(image_data.shape[0])
np.random.shuffle(shuffle_indexes)
shuffle_indexes

In [ ]: image_data = image_data[shuffle_indexes]
img_labels = img_labels[shuffle_indexes]

In [ ]: #Splitting the data into train and validation set


X_train, X_val, y_train, y_val = train_test_split(image_data, img_labels, t

X_train = X_train/255
X_val = X_val/255

In [ ]: print("X_train.shape", X_train.shape)
print("X_valid.shape", X_val.shape)
print("y_train.shape", y_train.shape)
print("y_valid.shape", y_val.shape)

X_train.shape (10477, 30, 30, 3)


X_valid.shape (4491, 30, 30, 3)
y_train.shape (10477,)
y_valid.shape (4491,)

In [ ]: y_val

Out[27]: array([0, 1, 1, ..., 0, 0, 1])

In [ ]: # One hot encoding the labels


y_train = keras.utils.to_categorical(y_train,2)
y_val = keras.utils.to_categorical(y_val, 2)

In [ ]: print(y_train.shape)
print(y_val.shape)

(10477, 2)
(4491, 2)

In [ ]: #Making the model

localhost:8888/notebooks/Downloads/Random/COMPETETION/PS4_Ritesh jaiswal_Ritesh_054.ipynb 4/8


1/19/24, 9:39 AM PS4_Ritesh jaiswal_Ritesh_054 - Jupyter Notebook

In [ ]: model = keras.models.Sequential([
keras.layers.Conv2D(filters=16, kernel_size=(3,3), activation='relu', i
keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu'),
keras.layers.MaxPool2D(pool_size=(2, 2)),
keras.layers.BatchNormalization(axis=-1),

keras.layers.Conv2D(filters=64, kernel_size=(3,3), activation='relu'),
keras.layers.Conv2D(filters=128, kernel_size=(3,3), activation='relu'),
keras.layers.MaxPool2D(pool_size=(2, 2)),
keras.layers.BatchNormalization(axis=-1),

keras.layers.Flatten(),
keras.layers.Dense(512, activation='relu'),
keras.layers.BatchNormalization(),
keras.layers.Dropout(rate=0.5),

keras.layers.Dense(2, activation='sigmoid')
])

In [ ]: learning_rate = 0.001
epochs = 50
lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
initial_learning_rate=0.001,
decay_steps=10000,
decay_rate=learning_rate/(epochs))
opt = Adam(learning_rate=lr_schedule)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['acc

In [ ]: history = model.fit(X_train,y_train, batch_size=32, epochs=epochs, validati

Epoch 1/50
328/328 [==============================] - 11s 10ms/step - loss: 0.9056
- accuracy: 0.5582 - val_loss: 0.6821 - val_accuracy: 0.5736
Epoch 2/50
328/328 [==============================] - 2s 7ms/step - loss: 0.7632 -
accuracy: 0.5700 - val_loss: 0.6755 - val_accuracy: 0.5667
Epoch 3/50
328/328 [==============================] - 3s 9ms/step - loss: 0.7044 -
accuracy: 0.5950 - val_loss: 0.7260 - val_accuracy: 0.5849
Epoch 4/50
328/328 [==============================] - 2s 7ms/step - loss: 0.6742 -
accuracy: 0.6156 - val_loss: 0.7733 - val_accuracy: 0.5674
Epoch 5/50
328/328 [==============================] - 3s 8ms/step - loss: 0.6567 -
accuracy: 0.6352 - val_loss: 0.6802 - val_accuracy: 0.5896
Epoch 6/50
328/328 [==============================] - 2s 7ms/step - loss: 0.6480 -
accuracy: 0.6396 - val_loss: 0.7158 - val_accuracy: 0.5885
Epoch 7/50
328/328 [ ] 3s 8ms/step loss: 0 6303
In [ ]: model.save("model.h5")

/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103:
UserWarning: You are saving your model as an HDF5 file via `model.save()`.
This file format is considered legacy. We recommend using instead the nati
ve Keras format, e.g. `model.save('my_model.keras')`.
saving_api.save_model(

localhost:8888/notebooks/Downloads/Random/COMPETETION/PS4_Ritesh jaiswal_Ritesh_054.ipynb 5/8


1/19/24, 9:39 AM PS4_Ritesh jaiswal_Ritesh_054 - Jupyter Notebook

In [ ]: model.save_weights("/content/output")

In [ ]: pd.DataFrame(history.history).plot(figsize=(8, 5))
plt.grid(True)
plt.gca().set_ylim(0, 1)
plt.show()

In [ ]: cat=[i for i in zi.keys()]


vals=[i for i in zi.values()]
zi1=pd.DataFrame(data=[cat,vals]).T.rename(columns={0:'Cat',1:'Vals'})
zi1

Out[37]: Cat Vals

0 cracked 1

1 uncracked 0

In [ ]: data=[]
a=[]
image=cv2.imread('/content/test/1.jpg')
image_fromarray=Image.fromarray(image,'RGB')
resize_image=image_fromarray.resize((30,30))
data.append(np.array(resize_image))
x_test=np.array(data)
x_test=x_test/255
predict=model.predict(x_test)
class_x=np.argmax(predict,axis=1)
print(*class_x)
a.append(*class_x)

1/1 [==============================] - 0s 19ms/step


0

localhost:8888/notebooks/Downloads/Random/COMPETETION/PS4_Ritesh jaiswal_Ritesh_054.ipynb 6/8


1/19/24, 9:39 AM PS4_Ritesh jaiswal_Ritesh_054 - Jupyter Notebook

In [ ]: data=[]
pred=[]
for i in os.listdir('/content/test'):
image=cv2.imread('/content/test/'+i)
image_fromarray=Image.fromarray(image,'RGB')
resize_image=image_fromarray.resize((30,30))
data.append(np.array(resize_image))

--------------------------------------------------------------------------
-
NameError Traceback (most recent call las
t)
<ipython-input-59-d05fd46b17b6> in <cell line: 4>()
7 resize_image=image_fromarray.resize((30,30))
8 data.append(np.array(resize_image))
----> 9 label.append(i)

NameError: name 'label' is not defined

In [ ]: x_test=np.array(data)
x_test=x_test/255

In [ ]: predict=model.predict(x_test)
class_x=np.argmax(predict,axis=1)

63/63 [==============================] - 0s 5ms/step

In [ ]: class_x

Out[53]: array([0, 0, 1, ..., 1, 0, 0])

In [ ]: df=pd.DataFrame({'vals':class_x})

localhost:8888/notebooks/Downloads/Random/COMPETETION/PS4_Ritesh jaiswal_Ritesh_054.ipynb 7/8


1/19/24, 9:39 AM PS4_Ritesh jaiswal_Ritesh_054 - Jupyter Notebook

In [ ]: for i in os.listdir('/content/test'):
df['name']=

Out[55]: vals

0 0

1 0

2 1

3 0

4 1

... ...

1995 1

1996 0

1997 1

1998 0

1999 0

2000 rows × 1 columns

In [ ]: ​

localhost:8888/notebooks/Downloads/Random/COMPETETION/PS4_Ritesh jaiswal_Ritesh_054.ipynb 8/8

You might also like