Shaurya DL file
Shaurya DL file
DEEP LEARNING
LAB
(ACSML0652)
(6th Semester)
24. W rite a program to Build Airplane vs Bird prediction model using transfer
learning.
25. Write a program to Build Airplane vs Bird prediction model using transfer learning.
Experiment 1
Code:
import pandas as pd
df = pd.read_csv("/content/sample_data/mnist_test.csv")
Output:
Experiment 2
Code:
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.datasets import load_iris
from sklearn.metrics import accuracy_score
# Training the model using the Support Vector Classification class of sklearn
svc = SVC()
svc.fit(X_train, Y_train)
Output:
Experiment 3
Code:
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_iris
from sklearn.metrics import accuracy_score
model.compile(optimizer ='adam',loss='binary_crossentropy',metrics=['accuracy'])
model.fit(X_train,y_train,epochs=50,batch_size=32,validation_split=0.2)
y_pred = model.predict(X_test)
y_pred_binary = (y_pred > 0.5).astype(int)
import numpy as np
Output:
Experiment 4
Code:
import pandas as pd
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.applications.vgg16 import preprocess_input
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.models import Model
from matplotlib import pyplot
from numpy import expand_dims
from matplotlib import pyplot
import warnings
warnings.filterwarnings('ignore')
model.summary()
for i in range(len(model.layers)):
if 'conv' not in model.layers[i].name:
continue
filters, biases = model.layers[i].get_weights()
print("layers number", i,model.layers[i].name, filters.shape)
import matplotlib
from matplotlib import pyplot
n_filters = 6
ix=1
fig = pyplot.figure(figsize = (15, 10))
for i in range(n_filters):
f = filters[:,:,:,i]
for j in range(3):
pyplot.subplot(n_filters,3,ix)
pyplot.imshow(f[:,:,j], cmap='gray')
ix+=1
pyplot.show() #Fig: 2
for i in range(len(model.layers)):
layer = model.layers[i]
if 'conv' not in layer.name:
continue
print(i,layer.name, layer.output.shape)
#calculating feature_map
features = model.predict(image)
fig = pyplot.figure(figsize=(20,15))
for i in range(1, features.shape[3]+1):
pyplot.subplot(8, 8, i)
pyplot.imshow(features[0,:,:,i-1], cmap='gray')
pyplot.show() #Fig: 2
Output:
Fig: 1
Fig: 2
Experiment 5
Code:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, classification_report
df = pd.DataFrame(data=iris_data.data, columns=iris_data.feature_names)
df['target'] = iris_data.target
X = df.drop('target', axis=1)
y = df['target']
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy}")
Code:
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import mnist
#Reshape the images into the format expected by the neural network.
train_images = train_images.reshape((60000, 28, 28, 1))
test_images = test_images.reshape((10000, 28, 28, 1))
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
#Compile the Model: Specify the loss function, optimizer, and metrics for model compilation.
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
#Train the Model: Train the model using the training data.
model.fit(train_images, train_labels, epochs=10, batch_size=64, validation_split=0.2)
#Evaluate the Model: Evaluate the trained model on the test data.
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f'Test accuracy: {test_acc}')
#Make Predictions: Use the trained model to make predictions on new data.
predictions = model.predict(test_images)
# Assuming you have already trained and compiled the model as mentioned in the previous
response
# Get the index of the class with the highest probability for each prediction
predicted_labels = predictions.argmax(axis=1)
plt.imshow(img, cmap=plt.cm.binary)
predicted_label = predictions_array.argmax()
color = 'blue' if predicted_label == true_label else 'red'
Code:
import tensorflow as tf
import keras
from keras import Sequential
from keras.layers import Dense, Flatten
print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)
x_train = x_train/255
x_test = x_test/255
model=Sequential()
model.add(Flatten(input_shape = (28,28)))
model.add(Dense(128, activation='relu'))
model.add(Dense(32,activation='relu'))
model.add(Dense(10,activation='softmax'))
model.summary()
import numpy as np
predicted_probabilities = model.predict(x_test)
predicted_classes = np.argmax(predicted_probabilities, axis=1)
plt.figure()
for i, correct in enumerate(correct_indices[:9]):
plt.subplot(3,3,i+1)
plt.imshow(x_test[correct].reshape(28,28), cmap='gray', interpolation='none')
plt.title("Predicted {}, Class {}".format(predicted_classes[correct], y_test[correct]))
plt.tight_layout()
plt.figure()
for i, incorrect in enumerate(incorrect_indices[:9]):
plt.subplot(3,3,i+1)
plt.imshow(x_test[incorrect].reshape(28,28), cmap='gray', interpolation='none')
plt.title("Predicted {}, Class {}".format(predicted_classes[incorrect], y_test[incorrect]))
plt.tight_layout()
Output:
Experiment 8
Code:
!mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/
!kaggle datasets download -d salader/dogs-vs-cats
import zipfile
zip_ref = zipfile.ZipFile('/content/dogs-vs-cats.zip', 'r')
zip_ref.extractall('/content')
zip_ref.close()
import tensorflow as tf
import keras
from keras import Sequential
from keras.layers import Dense, Flatten
from keras.applications.vgg16 import VGG16
conv_base = VGG16(
weights = 'imagenet',
include_top = False,
input_shape = (150, 150, 3)
)
conv_base.summary()
model=Sequential()
model.add(conv_base)
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(1,activation='sigmoid'))
model.summary()
conv_base.trainable = False
model.summary()
# generators
train_ds = keras.utils.image_dataset_from_directory(
directory = '/content/train',
labels = 'inferred',
label_mode = 'int',
batch_size=32,
image_size=(150,150)
)
validation_ds = keras.utils.image_dataset_from_directory(
directory = '/content/test',
labels = 'inferred',
label_mode = 'int',
batch_size=32,
image_size=(150,150)
)
#Normalize
def process(image,label):
image = tf.cast(image/255, tf.float32)
return image, label
train_ds = train_ds.map(process)
validation_ds = validation_ds.map(process)
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
history=model.fit(train_ds,epochs=10, validation_data=validation_ds)
Test_img.shape
Output:
Experiment 9
Code:
import numpy as np
docs = [
"go india",
"india india","hip hip hurray","jeetega bhai jeetega india jeetega","bharat mata ki jai","kholi
kholi",
"sachin sachin","dhoni dhoni","modi ji ki jai","inqualab jindabad"
]
tokenizer.word_counts
tokenizer.document_count
sequences = tokenizer.texts_to_sequences(docs)
sequences
model = Sequential()
model.add(SimpleRNN(32, input_shape=(50,1),return_sequences=False))
model.add(Dense(1,activation='sigmoid'))
model.summary()
Output:
Experiment 10
Code:
docs = [
"go india",
"india india","hip hip hurray","jeetega bhai jeetega india jeetega","bharat mata ki jai","kholi
kholi",
"sachin sachin","dhoni dhoni","modi ji ki jai","inqualab jindabad"
]
tokenizer.word_counts
tokenizer.document_count
sequences = tokenizer.texts_to_sequences(docs)
sequences
model.summary()
Output:
Experiment 11
Code:
# Reading Data
import pandas as pd
data = pd.read_csv('https://raw.githubusercontent.com/mohitgupta-omg/Kaggle-SMS-Spam-
Collection-Dataset-/master/spam.csv', encoding='latin-1')
data.head()
data.isna().sum()
import nltk
nltk.download('all')
text = list(data['text'])
import re
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
lematizer= WordNetLemmatizer()
corpus = []
for i in range(len(text)):
r = re.sub('[^a=zA-Z]',' ',text[i])
r = r.lower()
r = r.split()
r = [word for word in r if word not in stopwords.words('english')]
r = ' '.join(r)
corpus.append(r)
data['text'] = corpus
data.head()
X=data['text']
y=data['label']
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.33,random_state=123)
print("Training Data:",X_train.shape)
print("Testing Data:",X_test.shape)
lr.fit(X_train_cv, y_train)
Output:
Experiment 12
Code:
faqs = """About the Program
What is the course fee for Data Science Mentorship Program (DSMP 2023)
The course follows a monthly subscription model where you have to make monthly payments of Rs 799/month.
What is the total duration of the course?
The total duration of the course is 7 months. So the total course fee becomes 799*7 = Rs 5600(approx.)
What is the syllabus of the mentorship program?
We will be covering the following modules:
Python Fundamentals
Python libraries for Data Science
Data Analysis
SQL for Data Science
Maths for Machine Learning
ML Algorithms
Practical ML
MLOPs
Case studies
You can check the detailed syllabus here - https://learnwith.campusx.in/courses/CampusX-Data-Science-
Mentorship-Program-637339afe4b0615a1bbed390
Will Deep Learning and NLP be a part of this program?
No, NLP and Deep Learning both are not a part of this program’s curriculum.
What if I miss a live session? Will I get a recording of the session?
Yes all our sessions are recorded, so even if you miss a session you can go back and watch the recording.
Where can I find the class schedule?
Checkout this google sheet to see month by month time table of the course -
https://docs.google.com/spreadsheets/d/16OoTax_A6ORAeCg4emgexhqqPv3noQPYKU7RJ6ArOzk/edit?usp=s
haring.
What is the time duration of all the live sessions?
Roughly, all the sessions last 2 hours.
What is the language spoken by the instructor during the sessions?
Hinglish
How will I be informed about the upcoming class?
You will get a mail from our side before every paid session once you become a paid user.
Can I do this course if I am from a non-tech background?
Yes, absolutely.
I am late, can I join the program in the middle?
Absolutely, you can join the program anytime.
If I join/pay in the middle, will I be able to see all the past lectures?
Yes, once you make the payment you will be able to see all the past content in your dashboard.
Where do I have to submit the task?
You don’t have to submit the task. We will provide you with the solutions, you have to self evaluate the task
yourself.
Will we do case studies in the program?
Yes.
Where can we contact you?
You can mail us at [email protected]
Payment/Registration related questions
Where do we have to make our payments? Your YouTube channel or website?
You have to make all your monthly payments on our website. Here is the link for our website -
https://learnwith.campusx.in/
Can we pay the entire amount of Rs 5600 all at once?
Unfortunately no, the program follows a monthly subscription model.
What is the validity of monthly subscription? Suppose if I pay on 15th Jan, then do I have to pay again on 1st
Feb or 15th Feb
15th Feb. The validity period is 30 days from the day you make the payment. So essentially you can join
anytime you don’t have to wait for a month to end.
What if I don’t like the course after making the payment. What is the refund policy?
You get a 7 days refund period from the day you have made the payment.
I am living outside India and I am not able to make the payment on the website, what should I do?
You have to contact us by sending a mail at [email protected]
Post registration queries
Till when can I view the paid videos on the website?
This one is tricky, so read carefully. You can watch the videos till your subscription is valid. Suppose you have
purchased subscription on 21st Jan, you will be able to watch all the past paid sessions in the period of 21st Jan
to 20th Feb. But after 21st Feb you will have to purchase the subscription again.
But once the course is over and you have paid us Rs 5600(or 7 installments of Rs 799) you will be able to watch
the paid sessions till Aug 2024.
Why lifetime validity is not provided?
Because of the low course fee.
Where can I reach out in case of a doubt after the session?
You will have to fill a google form provided in your dashboard and our team will contact you for a 1 on 1 doubt
clearance session
If I join the program late, can I still ask past week doubts?
Yes, just select past week doubt in the doubt clearance google form.
I am living outside India and I am not able to make the payment on the website, what should I do?
You have to contact us by sending a mail at [email protected]
Certificate and Placement Assistance related queries
What is the criteria to get the certificate?
There are 2 criterias:
You have to pay the entire fee of Rs 5600
You have to attempt all the course assessments.
I am joining late. How can I pay payment of the earlier months?
You will get a link to pay fee of earlier months in your dashboard once you pay for the current month.
I have read that Placement assistance is a part of this program. What comes under Placement assistance?
This is to clarify that Placement assistance does not mean Placement guarantee. So we dont guarantee you any
jobs or for that matter even interview calls. So if you are planning to join this course just for placements, I am
afraid you will be disappointed. Here is what comes under placement assistance
Portfolio Building sessions
Soft skill sessions
Sessions with industry mentors
Discussion on Job hunting strategies
"""
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
tokenizer = Tokenizer()
tokenizer.fit_on_texts([faqs])
len(tokenizer.word_index)
input_sequences = []
for sentence in faqs.split('\n'):
tokenized_sentence = tokenizer.texts_to_sequences([sentence])[0]
for i in range(1,len(tokenized_sentence)):
input_sequences.append(tokenized_sentence[:i+1])
print(input_sequences)
x = padded_input_sequences[:,:-1]
y = padded_input_sequences[:,-1]
model.add(Dense(283, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
model.fit(x,y,epochs = 100)
import numpy as np
import time
text = "No, NLP and Deep"
for i in range(10):
#tokenize
token_text = tokenizer.texts_to_sequences([text])[0]
#padding
padded_token_text = pad_sequences([token_text],maxlen=56, padding='pre')
#predict
pos = np.argmax(model.predict(padded_token_text))
Code:
Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LSTM Model Deployment</title>
</head>
<body>
<h1>LSTM Model Deployment</h1>
<form action="/process" method="POST">
<label for="d1">Enter text:</label><br>
<textarea id="d1" name="d1" rows="4" cols="50"></textarea><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Result.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Result</title>
</head>
<body>
<h1>Result</h1>
<p>{{ result }}</p>
</body>
</html>
App.py
lstm_model.py
model.fit(x,y,epochs=10)
import numpy as np
_, accuracy = model.evaluate(x, y)
import time
def predict_seq(text):
for i in range(10):
token_text=tokenizer.texts_to_sequences([text])[0]
padded_token_text=pad_sequences([token_text],maxlen=56,padding='pre')
pos=np.argmax(model.predict(padded_token_text))
for word , index in tokenizer.word_index.items():
if index==pos:
text=text+" "+word
time.sleep(2)
return text
Output:
Experiment 14
Code:
faqs = """About the Program
What is the course fee for Data Science Mentorship Program (DSMP 2023)
The course follows a monthly subscription model where you have to make monthly payments of Rs 799/month.
What is the total duration of the course?
The total duration of the course is 7 months. So the total course fee becomes 799*7 = Rs 5600(approx.)
What is the syllabus of the mentorship program?
We will be covering the following modules:
Python Fundamentals
Python libraries for Data Science
Data Analysis
SQL for Data Science
Maths for Machine Learning
ML Algorithms
Practical ML
MLOPs
Case studies
You can check the detailed syllabus here - https://learnwith.campusx.in/courses/CampusX-Data-Science-
Mentorship-Program-637339afe4b0615a1bbed390
Will Deep Learning and NLP be a part of this program?
No, NLP and Deep Learning both are not a part of this program’s curriculum.
What if I miss a live session? Will I get a recording of the session?
Yes all our sessions are recorded, so even if you miss a session you can go back and watch the recording.
Where can I find the class schedule?
Checkout this google sheet to see month by month time table of the course -
https://docs.google.com/spreadsheets/d/16OoTax_A6ORAeCg4emgexhqqPv3noQPYKU7RJ6ArOzk/edit?usp=s
haring.
What is the time duration of all the live sessions?
Roughly, all the sessions last 2 hours.
What is the language spoken by the instructor during the sessions?
Hinglish
How will I be informed about the upcoming class?
You will get a mail from our side before every paid session once you become a paid user.
Can I do this course if I am from a non-tech background?
Yes, absolutely.
I am late, can I join the program in the middle?
Absolutely, you can join the program anytime.
If I join/pay in the middle, will I be able to see all the past lectures?
Yes, once you make the payment you will be able to see all the past content in your dashboard.
Where do I have to submit the task?
You don’t have to submit the task. We will provide you with the solutions, you have to self evaluate the task
yourself.
Will we do case studies in the program?
Yes.
Where can we contact you?
You can mail us at [email protected]
Payment/Registration related questions
Where do we have to make our payments? Your YouTube channel or website?
You have to make all your monthly payments on our website. Here is the link for our website -
https://learnwith.campusx.in/
Can we pay the entire amount of Rs 5600 all at once?
Unfortunately no, the program follows a monthly subscription model.
What is the validity of monthly subscription? Suppose if I pay on 15th Jan, then do I have to pay again on 1st
Feb or 15th Feb
15th Feb. The validity period is 30 days from the day you make the payment. So essentially you can join
anytime you don’t have to wait for a month to end.
What if I don’t like the course after making the payment. What is the refund policy?
You get a 7 days refund period from the day you have made the payment.
I am living outside India and I am not able to make the payment on the website, what should I do?
You have to contact us by sending a mail at [email protected]
Post registration queries
Till when can I view the paid videos on the website?
This one is tricky, so read carefully. You can watch the videos till your subscription is valid. Suppose you have
purchased subscription on 21st Jan, you will be able to watch all the past paid sessions in the period of 21st Jan
to 20th Feb. But after 21st Feb you will have to purchase the subscription again.
But once the course is over and you have paid us Rs 5600(or 7 installments of Rs 799) you will be able to watch
the paid sessions till Aug 2024.
Why lifetime validity is not provided?
Because of the low course fee.
Where can I reach out in case of a doubt after the session?
You will have to fill a google form provided in your dashboard and our team will contact you for a 1 on 1 doubt
clearance session
If I join the program late, can I still ask past week doubts?
Yes, just select past week doubt in the doubt clearance google form.
I am living outside India and I am not able to make the payment on the website, what should I do?
You have to contact us by sending a mail at [email protected]
Certificate and Placement Assistance related queries
What is the criteria to get the certificate?
There are 2 criterias:
You have to pay the entire fee of Rs 5600
You have to attempt all the course assessments.
I am joining late. How can I pay payment of the earlier months?
You will get a link to pay fee of earlier months in your dashboard once you pay for the current month.
I have read that Placement assistance is a part of this program. What comes under Placement assistance?
This is to clarify that Placement assistance does not mean Placement guarantee. So we dont guarantee you any
jobs or for that matter even interview calls. So if you are planning to join this course just for placements, I am
afraid you will be disappointed. Here is what comes under placement assistance
Portfolio Building sessions
Soft skill sessions
Sessions with industry mentors
Discussion on Job hunting strategies
"""
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
tokenizer = Tokenizer()
tokenizer.fit_on_texts([faqs])
len(tokenizer.word_index)
input_sequences = []
for sentence in faqs.split('\n'):
tokenized_sentence = tokenizer.texts_to_sequences([sentence])[0]
for i in range(1,len(tokenized_sentence)):
input_sequences.append(tokenized_sentence[:i+1])
print(input_sequences)
x = padded_input_sequences[:,:-1]
y = padded_input_sequences[:,-1]
model.fit(x,y,epochs = 100)
import numpy as np
import time
text = "No, NLP and Deep"
for i in range(10):
#tokenize
token_text = tokenizer.texts_to_sequences([text])[0]
#padding
padded_token_text = pad_sequences([token_text],maxlen=56, padding='pre')
#predict
pos = np.argmax(model.predict(padded_token_text))
Code:
Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GRU Model Deployment</title>
</head>
<body>
<h1>GRU Model Deployment</h1>
<form action="/process" method="POST">
<label for="d1">Enter text:</label><br>
<textarea id="d1" name="d1" rows="4" cols="50"></textarea><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Result.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Result</title>
</head>
<body>
<h1>Result</h1>
<p>{{ result }}</p>
</body>
</html>
App.py
GRU_model.py
model.fit(x, y, epochs=10)
import numpy as np
_, accuracy = model.evaluate(x, y)
import time
def predict_seq(text):
for i in range(10):
token_text=tokenizer.texts_to_sequences([text])[0]
padded_token_text=pad_sequences([token_text],maxlen=56,padding='pre')
pos=np.argmax(model.predict(padded_token_text))
for word , index in tokenizer.word_index.items():
if index==pos:
text=text+" "+word
time.sleep(2)
return text
Output:
Experiment 16
Code:
import tensorflow as tf
import pandas as pd
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import accuracy_score, precision_score, recall_score
from sklearn.model_selection import train_test_split
from keras import layers, losses
from keras.datasets import mnist
from keras.models import Model
#Loading the MNIST dataset and extracting training and testing data
(x_train, _), (x_test, _) = mnist.load_data()
class SimpleAutoencoder(Model):
def init (self, latent_dimensions, data_shape):
super(SimpleAutoencoder, self). init ()
self.latent_dimensions = latent_dimensions
self.data_shape = data_shape
simple_autoencoder.compile(optimizer='adam', loss=losses.MeanSquaredError())
simple_autoencoder.fit(x_train, x_train,
epochs =1,
shuffle=True,
validation_data=(x_test, x_test))
encoded_img = simple_autoencoder.encoder(x_test).numpy()
decoded_img = simple_autoencoder.decoder(encoded_img).numpy()
n=6
plt.figure(figsize=(8,4))
for i in range(n):
ax = plt.subplot(2,n,i+1)
plt.imshow(x_test[i])
plt.title("original")
plt.gray()
#display reconstruction
ax =plt.subplot(2,n,i+1+n)
plt.imshow(decoded_img[i])
plt.title("reconstructed")
plt.gray()
plt.show()
Output:
Experiment 17
Code:
import pandas as pd
import numpy as np
df = pd.read_pickle("/content/drive/MyDrive/CarPricesData")
df.head()
X=df[Predictors].values
y=df[TargetVariable].values
# Defining the Input layer and FIRST hidden layer, both are same!
model.add(Dense(units=5, input_dim=7, kernel_initializer='normal', activation='relu'))
TestingData=pd.DataFrame(data=Test_Data, columns=Predictors)
TestingData['Price']=y_test_orig
TestingData['PredictedPrice']=Predictions
TestingData.head()
Print(Testing Data)
Output:
Experiment 18
Code:
import tensorflow as tf
from tensorflow.keras.datasets import imdb
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding,SimpleRNN,Dense,LSTM,GRU
model = Sequential([
Embedding(10000,32,input_length=100),
SimpleRNN(5,return_sequences = True),
SimpleRNN(5),
Dense(1,activation='sigmoid')
])
model.summary()
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
history = model.fit(x_train, y_train, epochs = 5, batch_size=32, validation_split=0.2)
model = Sequential([
Embedding(10000,32,input_length=100),
LSTM(5,return_sequences = True),
LSTM(5),
Dense(1,activation='sigmoid')
])
model.summary()
Output:
Experiment 19
Code:
import cv2
import numpy as np
from google.colab.patches import cv2_imshow
# Load YOLO
net = cv2.dnn.readNet("/content/drive/MyDrive/yolov3.weights",
"/content/drive/MyDrive/yolov3.cfg")
classes = []
layer_names = net.getLayerNames()
output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]
# Load image
img = cv2.imread("/content/drive/MyDrive/image.jpg")
height, width, channels = img.shape
# Detecting objects
blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
net.setInput(blob)
outs = net.forward(output_layers)
font = cv2.FONT_HERSHEY_PLAIN
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
label = str(classes[class_ids[i]])
color = (255, 0, 0)
cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
cv2.putText(img, label, (x, y + 30), font, 2, color, 2)
cv2_imshow(img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
Experiment 20
Code:
import keras
from keras import layers
encoding_dim = 32 # 32 floats -> compression of factor 24.5, assuming the input is 784
floats
autoencoder.compile(optimizer='adam',loss='binary_crossentropy')
x_train= x_train.astype('float32')/255.
x_test=x_test.astype('float32')/255.
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:])))
print(x_train.shape)
print(x_test.shape)
autoencoder.fit(x_train, x_train,
epochs=50,
batch_size=256,
shuffle=True,
validation_data=(x_test, x_test))
encoded_imgs = encoder.predict(x_test)
decoded_imgs = decoder.predict(encoded_imgs)
# Display reconstruction
ax = plt.subplot(2, n, i + 1 + n)
plt.imshow(decoded_imgs[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
Output:
Epoch 1/50
235/235 [==============================] - 4s 13ms/step - loss: 0.2722 -
val_loss: 0.1855
Epoch 2/50
235/235 [==============================] - 2s 10ms/step - loss: 0.1691 -
val_loss: 0.1527
Epoch 3/50
235/235 [==============================] - 2s 10ms/step - loss: 0.1443 -
val_loss: 0.1344
Epoch 4/50
235/235 [==============================] - 2s 10ms/step - loss: 0.1291 -
val_loss: 0.1218
Epoch 5/50
235/235 [==============================] - 3s 11ms/step - loss: 0.1187 -
val_loss: 0.1136
Epoch 6/50
235/235 [==============================] - 3s 12ms/step - loss: 0.1117 -
val_loss: 0.1076
Epoch 7/50
235/235 [==============================] - 2s 10ms/step - loss: 0.1066 -
val_loss: 0.1033
Epoch 8/50
235/235 [==============================] - 2s 10ms/step - loss: 0.1027 -
val_loss: 0.0998
Epoch 9/50
235/235 [==============================] - 2s 10ms/step - loss: 0.0998 -
val_loss: 0.0974
Epoch 10/50
235/235 [==============================] - 2s 9ms/step - loss: 0.0976 -
val_loss: 0.0956
Epoch 11/50
235/235 [==============================] - 3s 14ms/step - loss: 0.0962 -
val_loss: 0.0945
Epoch 12/50
235/235 [==============================] - 2s 9ms/step - loss: 0.0954 -
val_loss: 0.0937
Epoch 13/50
...
Epoch 49/50
235/235 [==============================] - 2s 10ms/step - loss: 0.0927 -
val_loss: 0.0915
Epoch 50/50
235/235 [==============================] - 2s 9ms/step - loss: 0.0926 -
val_loss: 0.0916
Experiment 21
Write a program to implement text classification for spam-mails.
Code:
#reading datat
import pandas as pd
data = pd.read_csv('https://raw.githubusercontent.com/mohitgupta-omg/Kaggle-SMS-Spam-
Collection-Dataset-/master/spam.csv',encoding='latin-1')
data.head()
data.isna().sum()
#donwload nltk
import nltk
nltk.download('all')
#
text = list(data['text'])
# preprocessing loop
import re
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
corpus = []
for i in range(len(text)):
r = re.sub('[^a-zA-Z]', ' ', text[i])
r = r.lower()
r = r.split()
r =[word for word in r if word not in stopwords.words('english')]
r = [lemmatizer.lemmatize(word) for word in r]
r = ' '.join(r)
corpus.append(r)
x_train_cv = cv.fit_transform(x_train)
x_train_cv.shape
#generate predictions
predictions = lr.predict(x_test_cv)
predictions
Output
Experiment 22
Write a program to implement Transfer Learning Feature
Extraction on data.
Code:
img_path = 'bike-img.png'
img = cv2.imread(img_path)
results = model(img)
annotated_img = results[0].plot()
cv2_imshow(annotated_img)
boxes = results[0].boxes
cv2_imshow(img)
Output
Experiment 23
Write a program to implement face detection method .
Code:
detector = MTCNN()
img = cv2.imread('/content/lady.jpg')
output = detector.detect_faces(img)
#[{},{}...{}]
print(output)
for i in output:
x,y,widht,height = i['box']
left_eyeX,left_eyeY = i['keypoints']['left_eye']
right_eyeX,right_eyeY = i['keypoints']['right_eye']
noseX,noseY = i['keypoints']['nose']
mouth_leftX,mouth_leftY = i['keypoints']['mouth_left']
mouth_rightX,mouth_rightY = i['keypoints']['mouth_right']
cv2.circle(img,center=(left_eyeX,left_eyeY),color=(255,0,0),thickness=3,radius=2)
cv2.circle(img,center=(right_eyeX,right_eyeY),color=(255,0,0),thickness=3,radius=2)
cv2.circle(img,center=(noseX,noseY),color=(255,0,0),thickness=3,radius=2)
cv2.circle(img,center=(mouth_leftX,mouth_leftY),color=(255,0,0),thickness=3,radius=2)
cv2.circle(img,center=(mouth_rightX,mouth_rightY),color=(255,0,0),thickness=3,radius=2)
cv2.rectangle(img,pt1=(x,y),pt2=(x+widht,y+height),color=(255,0,0),thickness=3)
cv2_imshow(img) #use cv2_imshow instead of cv2.imshow
import cv2
import tensorflow as tf
# Load the pre-trained model (you'll need to download a car detection model)
# Example using a MobileNet SSD model (replace with your preferred model)
model = tf.saved_model.load("path/to/your/car_detection_model") # Replace with actual
path
# Preprocess the image (resize, normalize, etc.) - adapt this to match your model's input
requirements
input_tensor = tf.convert_to_tensor(img)
input_tensor = input_tensor[tf.newaxis, ...]
# Perform inference
detections = model(input_tensor)
Code:
!mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/
!kaggle datasets download -d persona/aeroplanes-vs-birds
import zipfile
zip_ref = zipfile.ZipFile('/content/aeroplanes-vs-birds.zip', 'r')
zip_ref.extractall('/content')
zip_ref.close()
import tensorflow as tf
import keras
from keras import Sequential
from keras.layers import Dense, Flatten
from keras.applications.vgg16 import VGG16
model = Sequential()
model.add(conv_base)
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.summary()
conv_base.trainable = False
model.summary()
train_ds = keras.utils.image_dataset_from_directory(directory='/content/train',
labels='inferred', label_mode='int', batch_size=32, image_size=(150,150))
validation_ds = keras.utils.image_dataset_from_directory(directory='/content/test',
labels='inferred', label_mode='int', batch_size=32, image_size=(150,150))
train_ds = train_ds.map(process)
validation_ds = validation_ds.map(process)
test_img = cv2.imread('/content/test_bird.jpg')
plt.imshow(cv2.cvtColor(test_img, cv2.COLOR_BGR2RGB))
plt.show()
model.predict(test_input)
Output
Experiment 25
Write a program to Build Airplane vs Bird prediction model
using transfer learning.
Code:
!mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/
!kaggle datasets download -d us236/oranges-vs-mangoes
import zipfile
zip_ref = zipfile.ZipFile('/content/oranges-vs-mangoes.zip', 'r')
zip_ref.extractall('/content')
zip_ref.close()
import tensorflow as tf
from tensorflow import keras
from keras import Sequential
from keras.layers import Dense, Flatten
from keras.applications.vgg16 import VGG16
conv_base = VGG16(
weights='imagenet',
include_top=False,
input_shape=(150,150,3)
)
conv_base.summary()
model = Sequential()
model.add(conv_base)
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.summary()
conv_base.trainable = False
model.summary()
train_ds = keras.utils.image_dataset_from_directory(
directory='/content/train',
labels='inferred',
label_mode='int',
batch_size=32,
image_size=(150,150)
)
validation_ds = keras.utils.image_dataset_from_directory(
directory='/content/test',
labels='inferred',
label_mode='int',
batch_size=32,
image_size=(150,150)
)
def process(image,label):
image = tf.cast(image/255, tf.float32)
return image,label
train_ds = train_ds.map(process)
validation_ds = validation_ds.map(process)
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
history = model.fit(train_ds,epochs=10,validation_data=validation_ds)
test_img = cv2.resize(test_img,(150,150))
test_input = test_img.reshape((1,150,150,3))
model.predict(test_input)
Output