Chetan Abbireddy 23WU0201049 Applied Analytics Using Python
Chetan Abbireddy 23WU0201049 Applied Analytics Using Python
Assignment
1
Provided Code:
import numpy as np
import cv2
from skimage.feature import hog
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
from tensorflow.keras.datasets import cifar10
import matplotlib.pyplot as plt
2
# Convert all images to HOG features
X_train_hog = np.array([extract_features(img) for img in X_train])
X_test_hog = np.array([extract_features(img) for img in X_test])
# Display image
plt.imshow(image)
plt.title(f"Predicted: {class_names[prediction[0]]}")
plt.axis('off')
plt.show()
# Example usage
predict_image(5) # Change index to test different images
3
Original Output:
4
Optimised Code using CNN:
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from tensorflow.keras.utils import to_categorical
# Flatten labels
y_train = y_train.flatten()
y_test = y_test.flatten()
5
X_train = X_train.astype("float32") / 255.0
X_test = X_test.astype("float32") / 255.0
6
# Function to predict and display image
def predict_image(index):
image = X_test[index]
prediction = model.predict(image.reshape(1, 32, 32, 3))
predicted_label = "Bird" if np.argmax(prediction) == 1 else "Automobile"
Optimised Output:
7
Conclusion:
There was a significant improvement in image recognition from the original being 43.40% to
the optimised and updated output which is 95.60% accurate. The CNN (Convolutional Neural
Network) is much more accurate than the HOG (Histogram of Oriented Gradients )and SVM
(Support Vector Machine) because it uses the raw pixels from the images rather than the
extracted HOG features. Another reason CNN was more accurate was because it used a deep
learning model, which although uses more computing power, will give a much more accurate
result.