You are on page 1of 36

VISHWAKARMA GOVERNMENT ENGINEERING COLLEGE – CHANDKHEDA

Gujarat Technological University, Ahmadabad

Electronics & Communication Department

Winter 2021-22

SEMESTER – 7

Introduction of Artificial Intelligence


(Subject Code: 3171105)

Name of Student:

Enrollment Number:
Certificate

This is to certify that Mr./Mrs . of Branch ELECTRONICS

AND COMMUNICATION Semester 7th in Enrollment No has

satisfactorily completed the term work in subject code 3171105 subject

name INTRODUCTION OF ARTIFICIAL INTELLIGENCE within four walls of

Vishwakarma government Engineering College, Chandkheda, Ahmedabad.

Date of Submission:

Sign of Faculty Member:


LIST OF EXPERIMENTS

Exp.No. TITLE DATE SIGN REMARKS


1 Create a program using the Pandas, NumPy library that
implements grouping, filtering, sorting, merging
operations.
2 Create a program using a sample dataset(e.g.
Housing, finance) to implement a decision tree
algorithm.
3 Create a program to implement a backpropagation
algorithm in python
4 Create a program to implement a simple stock
market prediction based on historical datasets.
5 Create a program using NumPy to implement a simple
perceptron model
6 Create a program to perform sentiment analysis on a
textual dataset (Twitter feeds, E-commerce
reviews).
7 Create a program using any machine learning framework
like TensorFlow, Keras to implement a Linear regression
algorithm.
8 Create a program using any machine learning framework
like TensorFlow, Keras to implement a simple
convolutional neural network.
9 Create a program using a convolutional neural network
that identifies objects like water bottles, cap, books, etc
using the webcam.
10 Create a program using any machine learning framework
like TensorFlow, Keras to implement a Logistic
regression algorithm.
EXPERIMENT : 1
AIM : To create a program using the Pandas, Numpy library that implements grouping,
filtering, sorting, and merging operations.

Code:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv("Data/titanic.csv")
df1 = pd.read_csv("Data/family.csv")

# Filtering

df["Name"].head(5)
df[df["Sex"]=="female"].head()

# Sorting

df.sort_values("Age", ascending=False).head()

# Grouping

survived_group = df.groupby("Survived")
survived_group.mean()

# Merging

df_merge_col = pd.merge(df_row,df3,on='id')
df_merge_col

CONCLUSION : From this practical I have studied about the grouping , filtering, sorting and
merging operations using the pandas and numpy library.

Artificial intelligence
EXPERIMENT : 2

AIM : Create a program using a sample dataset(e.g. Housing, finance) to implement a


decision tree algorithm.

Code :

# Load libraries

import pandas as pd

from sklearn.tree import DecisionTreeClassifier

from sklearn.model_selection import train_test_split

from sklearn import metrics

import sklearn.datasets as datasets #For loading iris dataset # Loading the

from sklearn.tree import export_graphviz

from six import StringIO

from IPython.display import Image

import pydotplus

iris dataset

iris=datasets.load_iris()

# Forming the iris dataframe

df=pd.DataFrame(iris.data, columns=iris.feature_names) print(df.head(10))

#split dataset in features and target variable

X = df

y = iris.target print(y)

# Split dataset into training set and test set into ratio of 0.75:0.25

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1)


Artificial intelligence
# Create Decision Tree classifer object

classifier = DecisionTreeClassifier()

# Train Decision Tree Classifer

classifier = classifier.fit(X_train,y_train)

dot_data = StringIO()

export_graphviz(classifier,

out_file=dot_data,

filled=True,

rounded=True,

special_characters=True,

feature_names=iris.feature_names,

class_names=['0','1','2'])

graph = pydotplus.graph_from_dot_data(dot_data.getvalue())

graph.write_png('iris.png')

Image(graph.create_png())

# Predict the response for test dataset

y_pred = classifier.predict(X_test)

# Compare between predicted and actual class

df = pd.DataFrame({'Predicted Class':y_pred, 'Actual Class':y_test}) print(df.head(10))

Artificial intelligence
# Model Accuracy

print("Accuracy of Decision Tree Classifier:",metrics.accuracy_score(y_test, y_pred))

# Creating confusion matrix and report

from sklearn.metrics import classification_report, confusion_matrix print(confusion_matrix(y_test, y_pred))

print(classification_report(y_test, y_pred))

Output :

Artificial intelligence
Artificial intelligence
CONCLUSION : After using various functions, we were able to train a decision tree model
with 97.23% accuracy on the test data. Furthermore, we were able to verify it by using
different evaluation metrics.

Artificial intelligence
EXPERIMENT : 3
AIM : Create a program to implement a backpropagation algorithm in Python.

THEORY :

Artificial Neural Networks:

A neural network is a group of connected I/O units where each connection has a weight
associated with its computer programs. It helps you to build predictive models from large
databases. This model builds upon the human nervous system. It helps you to conduct image
understanding, human learning, computer speech, etc.

Backpropagation:

Backpropagation is the essence of neural network training. It is the method of fine-tuning the
weights of a neural network based on the error rate obtained in the previous epoch (i.e.,
iteration). Proper tuning of the weights allows you to reduce error rates and make the model
reliable by increasing its generalization.

Backpropagation in neural network is a short form for "backward propagation of errors." It is a


standard method of training artificial neural networks. This method helps calculate the
gradient of a loss function with respect to all the weights in the network.

How Backpropagation Algorithm Works

The Back propagation algorithm in neural network computes the gradient of the loss function
for a single weight by the chain rule. It efficiently computes one layer at a time, unlike a native
direct computation. It computes the gradient, but it does not define how the gradient is used.
It generalizes the computation in the delta rule.

Consider the following Back propagation neural network example diagram to understand:

Artificial intelligence
1. Inputs X, arrive through the preconnected path
2. Input is modeled using real weights W. The weights are usually randomly selected.
3. Calculate the output for every neuron from the input layer, to the hidden layers, to
the output layer.
4. Calculate the error in the outputs

ErrorB= Actual Output – Desired Output

5. Travel back from the output layer to the hidden layer to adjust the weights such that
the error is decreased.

Keep repeating the process until the desired output is achieved

Why We Need Backpropagation?

Most prominent advantages of Backpropagation are:


● Backpropagation is fast, simple and easy to program
● It has no parameters to tune apart from the numbers of input
● It is a flexible method as it does not require prior knowledge about the network
● It is a standard method that generally works well
● It does not need any special mention of the features of the function to be learned.
Artificial intelligence
Disadvantages of using Backpropagation

● The actual performance of backpropagation on a specific problem is dependent on the


input data.
● Back propagation algorithm in data mining can be quite sensitive to noisy data
● You need to use the matrix-based approach for backpropagation instead of mini-batch.

Code:

import numpy as np

# Define the sigmoid activation function and its derivative


def sigmoid(x):
return 1 / (1 + np.exp(-x))

def sigmoid_derivative(x):
return x * (1 - x)

# Define the neural network architecture


input_size = 2
hidden_size = 4
output_size = 1

# Initialize the weights and biases


np.random.seed(0)
input_layer = np.random.randn(input_size)
hidden_layer_weights = np.random.randn(input_size, hidden_size)
hidden_layer_biases = np.random.randn(1, hidden_size)
output_layer_weights = np.random.randn(hidden_size, output_size)
output_layer_biases = np.random.randn(1, output_size)

# Define the learning rate


learning_rate = 0.1

# Define the number of training epochs


epochs = 10000

# Training data
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])

Artificial intelligence
# Training loop
for epoch in range(epochs):
# Forward pass
hidden_layer_input = np.dot(X, hidden_layer_weights) + hidden_layer_biases
hidden_layer_output = sigmoid(hidden_layer_input)
output_layer_input = np.dot(hidden_layer_output, output_layer_weights) +
output_layer_biases
output_layer_output = sigmoid(output_layer_input)

# Calculate the loss


loss = 0.5 * np.sum((output_layer_output - y) ** 2)

# Backpropagation
output_layer_error = y - output_layer_output
output_layer_delta = output_layer_error * sigmoid_derivative(output_layer_output)

hidden_layer_error = output_layer_delta.dot(output_layer_weights.T)
hidden_layer_delta = hidden_layer_error * sigmoid_derivative(hidden_layer_output)

# Update the weights and biases


output_layer_weights += hidden_layer_output.T.dot(output_layer_delta) * learning_rate
output_layer_biases += np.sum(output_layer_delta, axis=0, keepdims=True) * learning_rate
hidden_layer_weights += X.T.dot(hidden_layer_delta) * learning_rate
hidden_layer_biases += np.sum(hidden_layer_delta, axis=0, keepdims=True) * learning_rate

if epoch % 1000 == 0:
print(f'Epoch {epoch}: Loss = {loss}')

# Print the final output


print("Final Output after training:")
print(output_layer_output)

Artificial intelligence
Output:

Epoch 0: Loss = 0.7913036436831435


Epoch 1000: Loss = 0.4093183264001299
Epoch 2000: Loss = 0.16365299427209434
Epoch 3000: Loss = 0.04678551762737469
Epoch 4000: Loss = 0.022930392277925968
Epoch 5000: Loss = 0.01450341437872332
Epoch 6000: Loss = 0.010413473607344691
Epoch 7000: Loss = 0.008048777275473224
Epoch 8000: Loss = 0.006524760757239399
Epoch 9000: Loss = 0.00546770303216257
Final Output after training:
[[0.02152526]
[0.95081329]
[0.9491524 ]
[0.06263166]]

CONCLUSION : As we can see in the output predicted output is depends on input values and
if we increase the range of test samples the difference between predicted output and actual
output will be decrease and value of predicted output we get will be near to actual output.

Artificial neural networks use backpropagation as a learning algorithm to compute a gradient


descent with respect to weights.

Artificial intelligence
EXPERIMENT : 4

AIM: Create a program to implement a simple stock market prediction.

THEORY :

Stocks are possibly the most popular financial instrument invented for building wealth and are
the centerpiece of any investment portfolio. The advances in trading technolo-gy has opened
up the markets so that nowadays nearly anybody can own stocks. From last few decades,
there seen explosive increase in the average person’s interest for stock market.

In a financially explosive market, as the stock market, it is important to have a very accurate
prediction of a future trend. Because of the financial crisis and re-cording profits, it is
compulsory to have a secure prediction of the values of the stocks.

This is a simple kernel in which we will forecast stock prices using Prophet (Facebook's
library for time series forecasting). However, historical prices are no indication whether a price
will go up or down. I'll rather use my own variables and use machine learning for stock price
prediction rather than just using historical prices as an indication of stock price increase.

About Prophet :

The analysts can produce high quality forecasting data that is rarely seen. This is one of the
reasons why Facebook's research team came to an easily approachable way for using
advanced concepts for time series forecasting and us Python users, can easily relate to this
library since it uses Scikit-Learn's api (Similar to Scikit-Learn).

There are several characteristics of Prophet

● hourly, daily, or weekly observations with at least a few months (preferably a year) of
history
● strong multiple “human-scale” seasonalities: day of week and time of year
● Important holidays that occur at irregular intervals that are known in advance (e.g. the
Super Bowl)
● A reasonable number of missing observations or large outliers
● Historical trend changes, for instance due to product launches or logging changes
● Trends that are non-linear growth curves, where a trend hits a natural limit or
saturates</ul>

Artificial intelligence
Code :

import streamlit as st
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import yfinance as yf
import matplotlib.pyplot as plt

# Function to download historical stock data


def download_stock_data(ticker, start_date, end_date):
df = yf.download(ticker, start=start_date, end=end_date)
return df

# Function to perform stock price prediction


def predict_stock_price(data, days_to_predict):
df = data[['Close']]
df['Prediction'] = df['Close'].shift(-days_to_predict)

X = np.array(df.drop(['Prediction'], 1))
X = X[:-days_to_predict]
y = np.array(df['Prediction'])
y = y[:-days_to_predict]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

model = LinearRegression()
model.fit(X_train, y_train)

# Predict the stock price


x_future = df.drop(['Prediction'], 1)[:-days_to_predict]
x_future = x_future.tail(days_to_predict)
x_future = np.array(x_future)

prediction = model.predict(x_future)

return prediction

# Streamlit UI
st.title("Stock Price Prediction")
st.sidebar.header("User Input")

# Input form
ticker = st.sidebar.text_input("Enter Stock Ticker Symbol (e.g., AAPL for Apple):", value="AAPL").upper()
start_date = st.sidebar.date_input("Start Date", pd.to_datetime('2020-01-01'))
end_date = st.sidebar.date_input("End Date", pd.to_datetime('2022-01-01'))
days_to_predict = st.sidebar.slider("Days to Predict", min_value=1, max_value=30, value=7)

# Download and display stock data


try:
st.write(f"Displaying historical stock data for {ticker}")
data = download_stock_data(ticker, start_date, end_date)
st.write(data)

Artificial intelligence
# Perform stock price prediction
prediction = predict_stock_price(data, days_to_predict)

st.subheader(f"Predicted Stock Prices for the Next {days_to_predict} Days:")


st.write(prediction)

# Plot historical and predicted stock prices


fig, ax = plt.subplots()
ax.plot(data.index, data['Close'], label='Historical Price')
future_dates = pd.date_range(start=data.index[-1] + pd.DateOffset(1), periods=days_to_predict)
ax.plot(future_dates, prediction, label='Predicted Price')
ax.set_xlabel("Date")
ax.set_ylabel("Stock Price")
ax.set_title(f"{ticker} Stock Price Prediction")
ax.legend()
st.pyplot(fig)

except Exception as e:
st.error("Error fetching data. Please check the stock ticker symbol and date range.")

# Additional information
st.write("Note: This is a simple linear regression-based prediction model and should not be used for actual trading
decisions."

Output :

Artificial intelligence
CONCLUSION :

A stock market web application using Streamlit for stock prediction combines user-friendliness, data
visualization, real-time data, and machine learning. It empowers users with valuable insights for
making informed financial decisions, though it also requires vigilance in model accuracy and data
management.

Artificial intelligence
EXPERIMENT: 5

AIM : Create a program using NumPy to implement a simple perceptron model.

THEORY :

Artificial Neural Networks(ANNs) are the newfound love for all data scientists. From classical
machine learning techniques, it is now shifted towards deep learning. Neural networks mimic
the human brain which passes information through neurons. Perceptron is the first neural
network to be created. It was designed by Frank Rosenblatt in 1957. Perceptron is a single
layer neural network. This is the only neural network without any hidden layer. Perceptron is
used in supervised learning generally for binary classification.

Code :

import numpy as np
import matplotlib.pyplot as plt

# Generate some random data for two classes


np.random.seed(0)
num_samples = 100
class_1 = np.random.randn(num_samples, 2) + np.array([2, 2])
class_2 = np.random.randn(num_samples, 2) + np.array([-2, -2])
data = np.vstack((class_1, class_2))
labels = np.hstack((np.ones(num_samples), -1 * np.ones(num_samples)))

# Initialize weights and bias


weights = np.random.rand(2)
bias = np.random.rand(1)

# Learning rate
learning_rate = 0.1

# Training the perceptron


num_epochs = 100
errors = []

for epoch in range(num_epochs):


total_error = 0
for i in range(len(data)):
x = data[i]
y = labels[i]
prediction = np.dot(x, weights) + bias
if y * prediction <= 0: # Misclassification
weights += learning_rate * y * x
bias += learning_rate * y
Artificial intelligence
total_error += 1
errors.append(total_error)

# Plot the errors over epochs


plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.plot(errors)
plt.xlabel('Epoch')
plt.ylabel('Total Misclassifications')
plt.title('Training Errors')

# Plot the decision boundary


plt.subplot(1, 2, 2)
plt.scatter(class_1[:, 0], class_1[:, 1], marker='o', label='Class 1')
plt.scatter(class_2[:, 0], class_2[:, 1], marker='x', label='Class 2')
x_boundary = np.linspace(-5, 5, 100)
y_boundary = (-weights[0] * x_boundary - bias) / weights[1]
plt.plot(x_boundary, y_boundary, 'r', label='Decision Boundary')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.legend()
plt.title('Decision Boundary')

plt.show()

Output :

CONCLUSION : A basic implementation of the perceptron algorithm in Python to classify the


flowers in the iris dataset.

Artificial intelligence
EXPERIMENT: 6

AIM : Create a program to perform sentiment analysis on a textual dataset.

THEORY :

What is sentiment analysis?

Sentiment analysis is the automated process of identifying and classifying subjective


information in text data. This might be an opinion, a judgment, or a feeling about a particular
topic or product feature. It’s also known as opinion mining, deriving the opinion or attitude of a
speaker.

The most common type of sentiment analysis is ‘polarity detection’ and involves classifying
statements as Positive, Negative or Neutral.

Why sentiment analysis?

● Business: In marketing field companies use it to develop their strategies, to understand


customers’ feelings towards products or brand, how people respond to their campaigns
or product launches and why consumers don’t buy some products.
● Politics: In political field, it is used to keep track of political view, to detect consistency
and inconsistency between statements and actions at the government level. It can be
used to predict election results as well!
● Public Actions: Sentiment analysis also is used to monitor and analyse social
phenomena, for the spotting of potentially dangerous situations and determining the
general mood of the blogosphere.

Some of the common examples of Sentiment Analysis are

● Customer Feedback
● Product Analysis
● Social Media Monitoring
● Emotion Recognition
● Chatbot reactions
● Threat Detection etc.

Artificial intelligence
Code:

Artificial intelligence
Artificial intelligence
Artificial intelligence
Output :

CONCLUSION : Sentiment analysis using API is a good option but we can make our own
LSTM or classic RNN to get better results on our data by changing hyperparameters and
modelarchitectur

Artificial intelligence
EXPERIMENT: 7

AIM : Create a program using any machine learning framework like TensorFlow, Keras to
implement a Linear regression algorithm.

THEORY :

Linear regression attempts to model the relationship between two variables by fitting a linear
equation to observed data. One variable is considered to be an explanatory variable, and the
other is considered to be a dependent variable. For example, a modeler might want to relate
the weights of individuals to their heights using a linear regression model.
Before attempting to fit a linear model to observed data, a modeler should first determine
whether or not there is a relationship between the variables of interest. This does not
necessarily imply that one variable causes the other (for example, higher SAT scores do not
cause higher college grades), but that there is some significant association between the two
variables. A scatterplot can be a helpful tool in determining the strength of the relationship
between two variables. If there appears to be no association between the proposed
explanatory and dependent variables (i.e., the scatterplot does not indicate any increasing or
decreasing trends), then fitting a linear regression model to the data probably will not provide
a useful model. A valuable numerical measure of association between two variables is the
correlation coefficient, which is a value between -1 and 1 indicating the strength of the
association of the observed data for the two variables.
A linear regression line has an equation of the form Y = a + b*X, where X is the explanatory
variable and Y is the dependent variable. The slope of the line is b, and a is the intercept (the
value of y when x = 0).
In higher dimensions when we have more than one input (x), the line is called a plane or a
hyper-plane. The representation therefore is the form of the equation and the specific values
used for the coefficients (e.g., a and b in the above example).
It is common to talk about the complexity of a regression model like linear regression. This
refers to the number of coefficients used in the model.
When a coefficient becomes zero, it effectively removes the influence of the input variable on
the model and therefore from the prediction made from the model (0*X=0). This becomes
relevant if you look at regularization methods that change the learning algorithm to reduce the
complexity of regression models by putting pressure on the absolute size of the coefficients,

Artificial intelligence
driving some to zero.

The linear regression is explained using this equation in the following practical as a jupyter
notebook script.

Code:

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

# Generate some random data for demonstration


np.random.seed(0)
X = np.random.rand(100, 1)
y = 2 * X + 1 + 0.1 * np.random.rand(100, 1)

# Create a simple linear regression model


model = keras.Sequential()
model.add(layers.Input(shape=(1,)))
model.add(layers.Dense(1))

# Compile the model


model.compile(optimizer='sgd', loss='mean_squared_error')

# Train the model


model.fit(X, y, epochs=1000, verbose=0)

# Make predictions
y_pred = model.predict(X)

# Print the learned parameters (slope and intercept)


weights = model.get_weights()
slope, intercept = weights[0][0][0], weights[1][0]
print(f"Slope (weight): {slope}")
print(f"Intercept: {intercept}")

# Evaluate the model's performance


mse = np.mean((y - y_pred) ** 2)
Artificial intelligence
print(f"Mean Squared Error: {mse}")

# Plot the original data and the linear regression line


plt.scatter(X, y, label='Original Data')
plt.plot(X, y_pred, color='red', linewidth=2, label='Linear Regression Line')
plt.xlabel('X')
plt.ylabel('y')
plt.legend()
plt.title('Linear Regression')
plt.show()

CONCLUSION : Despite its simplicity, linear regression is one of the most commonly used
machine learning algorithms in the industry, and some companies test how well you
understand it.

Artificial intelligence
EXPERIMENT: 8

AIM : Create a program using any machine learning framework like Tensorflow, Keras to
implement a simple convolution neural network.

THEORY :

In the past few years, Deep Learning has been proved that its a very powerful tool due to its
ability to handle huge amounts of data. The use of hidden layers exceeds traditional techniques,
especially for pattern recognition. One of the most popular Deep Neural Networks is
Convolutional Neural Networks(CNN).A convolutional neural network(CNN) is a type of Artificial
Neural Network(ANN) used in image recognition and processing which is specially designed for
processing data(pixels).

Tensorflow is an open source artificial intelligence library, using data flow graphs to build
models. It allows developers to create large-scale neural networks with many layers. TensorFlow
is mainly used for: Classification, Perception, Understanding, Discovering, Prediction and
Creation.

Keras is the high-level API of TensorFlow 2: an approachable, highly-productive interface for


solving machine learning problems, with a focus on modern deep learning. It provides essential
abstractions and building blocks for developing and shipping machine learning solutions with
high iteration velocity.

matplotlib. pyplot is a collection of functions that make matplotlib work like MATLAB. Each
pyplot function makes some changes to a figure: e.g., creates a figure, creates a plotting area in
a figure, plots some lines in a plotting area, decorates the plot with labels, etc. In matplotlib.

NumPy is the fundamental package for scientific computing in Python. NumPy arrays facilitate
advanced mathematical and other types of operations on large numbers of data. Typically, such
operations are executed more efficiently and with less code than is possible using Python's
built-in sequences.

CIFAR10 is a dataset of 50,000 32x32 color training images and 10,000 test images, labeled over 10
categories.

Artificial intelligence
Code:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from visualkeras import layered_view

# Create a simple CNN model


model = keras.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 3)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])

# Compile the model


model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Visualize the model architecture using visualkeras


layered_view(model).show()

CONCLUSION : After using various libraries, we were able to classify the CIFAR10 image
dataset with 78.36% accuracy on the test data. And learned about Convolution Neural
Network.

Artificial intelligence
EXPERIMENT-9

AIM : Create A Program using a Convolutional Neural Network that


identifies objects using the webcam.

Code:
# clone YOLOv5 repository
!git clone https://github.com/ultralytics/yolov5 # clone repo
%cd yolov5

# install dependencies as necessary


!pip install -qr requirements.txt # install dependencies (ignore errors)
import torch

from IPython.display import Image, clear_output # to display images


from utils.google_utils import gdrive_download # to download models/datasets

# clear_output()
print('Setup complete. Using torch %s %s' % (torch.__version__, torch.cuda.get_device_properties(0) if
torch.cuda.is_available() else 'CPU'))

#follow the link below to get your download code from from Roboflow
!pip install roboflow

from roboflow import Roboflow


rf = Roboflow(api_key="4sJ5Iefp4V0kEetf9FuN")
project = rf.workspace().project("deadly-animal-detection")
dataset = project.version(1).download("yolov5")

%cat {dataset.location}/data.yaml

# define number of classes based on YAML


import yaml
with open(dataset.location + "/data.yaml", 'r') as stream:
num_classes = str(yaml.safe_load(stream)['nc'])

#this is the model configuration we will use for our tutorial


%cat /content/yolov5/models/yolov5s.yaml

#customize iPython writefile so we can write variables


from IPython.core.magic import register_line_cell_magic

@register_line_cell_magic
def writetemplate(line, cell):
with open(line, 'w') as f:
f.write(cell.format(**globals()))

%%writetemplate /content/yolov5/models/custom_yolov5s.yaml

Artificial intelligence
# parameters
nc: {num_classes} # number of classes
depth_multiple: 0.33 # model depth multiple
width_multiple: 0.50 # layer channel multiple

# anchors
anchors:
- [10,13, 16,30, 33,23] # P3/8
- [30,61, 62,45, 59,119] # P4/16
- [116,90, 156,198, 373,326] # P5/32

# YOLOv5 backbone
backbone:
# [from, number, module, args]
[[-1, 1, Focus, [64, 3]], # 0-P1/2
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
[-1, 3, BottleneckCSP, [128]],
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
[-1, 9, BottleneckCSP, [256]],
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
[-1, 9, BottleneckCSP, [512]],
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
[-1, 1, SPP, [1024, [5, 9, 13]]],
[-1, 3, BottleneckCSP, [1024, False]], # 9
]

# YOLOv5 head
head:
[[-1, 1, Conv, [512, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 6], 1, Concat, [1]], # cat backbone P4
[-1, 3, BottleneckCSP, [512, False]], # 13

[-1, 1, Conv, [256, 1, 1]],


[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 4], 1, Concat, [1]], # cat backbone P3
[-1, 3, BottleneckCSP, [256, False]], # 17 (P3/8-small)

[-1, 1, Conv, [256, 3, 2]],


[[-1, 14], 1, Concat, [1]], # cat head P4
[-1, 3, BottleneckCSP, [512, False]], # 20 (P4/16-medium)

[-1, 1, Conv, [512, 3, 2]],


[[-1, 10], 1, Concat, [1]], # cat head P5
[-1, 3, BottleneckCSP, [1024, False]], # 23 (P5/32-large)

[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)


]

Artificial intelligence
# train yolov5s on custom data for 100 epochs

%cd /content/yolov5/
!python train.py
--img 416
--batch 16
--epochs 100
--data {dataset.location}/data.yaml
--cfg ./models/custom_yolov5s.yaml
--weights ''
--name yolov5s_results
--cache

!python detect.py --weights weights/best.pt --conf 0.4 --source "/content/yolov5/data/images/test.jpg"

Conclusion : YOLO v5 is the one of the fastest among all the other versions and it uses logistic
regression instead of softmax to perform multi class classification within one anchor box.

Artificial intelligence
EXPERIMENT : 10

AIM : To implement logistic regression algorithm using Tensorflow ans keras libraries

THEORY :

Logistic regression is basically a supervised classification algorithm. In a classification problem, the


target variable(or output), y, can take only discrete values for given set of features(or inputs), X.

Contrary to popular belief, logistic regression IS a regression model. The model builds a regression
model to predict the probability that a given data entry belongs to the category numbered as “1”. Just
like Linear regression assumes that the data follows a linear function, Logistic regression models the
data using the sigmoid function.

Code:

import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.preprocessing import LabelEncoder

messages = [...]
labels = [...]

# Convert text messages to numerical features using CountVectorizer


vectorizer = CountVectorizer()
X = vectorizer.fit_transform(messages)

# Convert labels to binary values


encoder = LabelEncoder()
y = encoder.fit_transform(labels)

# Split the data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Define a logistic regression model


input_dim = X_train.shape[1]
learning_rate = 0.01

# Define weights and bias


W = tf.Variable(tf.zeros([input_dim, 1], dtype=tf.float32))
b = tf.Variable(tf.zeros([1], dtype=tf.float32))

# Define the logistic regression model


def logistic_regression(x):
return tf.sigmoid(tf.matmul(x, W) + b)

Artificial intelligence
# Define the loss function
def loss_fn(y_true, y_pred):
return tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=y_pred,
labels=y_true))

# Define the optimizer


optimizer = tf.optimizers.SGD(learning_rate)

y_pred = logistic_regression(tf.cast(X_train.toarray(), dtype=tf.float32))

# Training loop
epochs = 1000
for epoch in range(epochs):
with tf.GradientTape() as tape:
y_pred = logistic_regression(tf.cast(X_train.toarray(), dtype=tf.float32))
current_loss = loss_fn(tf.convert_to_tensor(y_train.reshape(-1, 1), dtype=tf.float32),
y_pred)

grads = tape.gradient(current_loss, [W, b])


optimizer.apply_gradients(zip(grads, [W, b]))

if (epoch + 1) % 100 == 0:
print(f"Epoch {epoch + 1}, Loss: {current_loss.numpy():.4f}")

# Evaluate the model on the test data


y_pred_test = logistic_regression(tf.cast(X_test.toarray(), dtype=tf.float32))
test_loss = loss_fn(tf.convert_to_tensor(y_test.reshape(-1, 1), dtype=tf.float32), y_pred_test)
print(f"Test Loss: {test_loss.numpy():.4f}")

# Calculate accuracy
accuracy = np.mean(np.round(y_pred_test) == y_test.reshape(-1, 1))
print(f"Test Accuracy: {accuracy * 100:.2f}%")

Artificial intelligence
Conclusion: When you have more than one outputs from one input you should use logistic regression
instead of softmax and also logistic regression is well optimized and faster than softmax.

Artificial intelligence

You might also like