Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Machine learning submission #29

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions author.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"name": "",
"entry_number": ""
"name": "Aditya Saxena",
"entry_number": "2022AM11218",
"discord_id" : "adityaaa2511",
"phone no": "9711366368",
"email id": "[email protected]",
"email id": "[email protected]",
}
1 change: 1 addition & 0 deletions machine-learning/week1/FINAL.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions machine-learning/week1/final.ipynb

Large diffs are not rendered by default.

819 changes: 819 additions & 0 deletions machine-learning/week1/laptopPrice.csv

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added machine-learning/week2/PR_curve.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added machine-learning/week2/confusion_matrix.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions machine-learning/week2/data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
train: data_images/train
val: data_images/text
nc: 3
names: ['person',
'car',
'bus'
]
Binary file added machine-learning/week2/results.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added machine-learning/week2/val_batch0_labels.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added machine-learning/week2/val_batch0_pred.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added machine-learning/week2/yolo_pred.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
244 changes: 244 additions & 0 deletions machine-learning/week2/yolo_predictions.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 13,
"id": "1e2c1523",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: PyYAML in c:\\users\\adity\\onedrive\\desktop\\ml project week 2\\yolo_venv\\lib\\site-packages (6.0)\n"
]
}
],
"source": [
"!pip install PyYAML"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "f403f869",
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np\n",
"import os\n",
"import yaml\n",
"from yaml.loader import SafeLoader"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "6588072b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['person', 'car', 'bus']\n"
]
}
],
"source": [
"with open('data.yaml',mode='r') as f:\n",
" data_yaml=yaml.load(f,Loader=SafeLoader)\n",
"\n",
"label=data_yaml['names']\n",
"print(label)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "c4562cb7",
"metadata": {},
"outputs": [],
"source": [
"#Load Yolo Model\n",
"yolo=cv2.dnn.readNetFromONNX('./Model2/weights/best.onnx')\n",
"yolo.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)\n",
"yolo.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "1ee09254",
"metadata": {},
"outputs": [],
"source": [
"#Load the image\n",
"img=cv2.imread('./14474517749_600f20185e_c.jpg')\n",
"image=img.copy()\n",
"row,col,d=image.shape\n",
"\n",
"#get YOLO predictions\n",
"#step-1:-convert image into square image(array)\n",
"max_rc=max(row,col)\n",
"input_image=np.zeros((max_rc,max_rc,3),dtype=np.uint8)\n",
"input_image[0:row,0:col]=image\n",
"#step2:- get prediction from square array\n",
"INPUT_WH_YOLO=640\n",
"blob=cv2.dnn.blobFromImage(input_image,1/255,(INPUT_WH_YOLO,INPUT_WH_YOLO),swapRB=True,crop=False)\n",
"yolo.setInput(blob)\n",
"preds=yolo.forward()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "09278889",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1, 25200, 8)\n"
]
}
],
"source": [
"print(preds.shape)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "652c5418",
"metadata": {},
"outputs": [],
"source": [
"#Non Maximum Suppression\n",
"#step-1:- filter detection based on confidence(0.4) and probability(0.25)\n",
"detections=preds[0]\n",
"boxes=[]\n",
"confidences=[]\n",
"classes=[]\n",
"#width and height of input image\n",
"image_w,image_h=input_image.shape[:2]\n",
"x_factor=image_w/INPUT_WH_YOLO\n",
"y_factor=image_h/INPUT_WH_YOLO\n",
"\n",
"for i in range(len(detections)):\n",
" row=detections[i]\n",
" confidence=row[4]\n",
" if confidence>0.4:\n",
" class_score=row[5:].max()\n",
" class_id=row[5:].argmax()\n",
" \n",
" if class_score>0.25:\n",
" cx,cy,w,h=row[0:4]\n",
" left=int((cx-0.5*w)*x_factor)\n",
" top=int((cy-0.5*h)*y_factor)\n",
" width=int(w*x_factor)\n",
" height=int(h*y_factor)\n",
" \n",
" box=np.array([left,top,width,height])\n",
" \n",
" #append values to the list\n",
" confidences.append(confidence)\n",
" boxes.append(box)\n",
" classes.append(class_id)\n",
"\n",
"#clean\n",
"boxes_np=np.array(boxes).tolist()\n",
"confidences_np=np.array(confidences).tolist()\n",
"\n",
"#NMS\n",
"index=cv2.dnn.NMSBoxes(boxes_np,confidences_np,0.25,0.45).flatten()"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "040b6def",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 3, 16, 1, 13])"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"index"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "80d09058",
"metadata": {},
"outputs": [],
"source": [
"#Draw bounding box\n",
"for ind in index:\n",
" x,y,w,h=boxes_np[ind]\n",
" bb_conf=int(confidences_np[ind]*100)\n",
" classes_id=classes[ind]\n",
" class_name=label[classes_id]\n",
" \n",
" text=f'{class_name}:{bb_conf}%'\n",
" cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)\n",
" cv2.rectangle(image,(x,y-30),(x+w,y),(255,255,255),-1)\n",
" cv2.putText(image,text,(x,y-10),cv2.FONT_HERSHEY_PLAIN,0.7,(0,0,0),1)\n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "ec61c7be",
"metadata": {},
"outputs": [],
"source": [
"cv2.imshow('yolo_prediction',image)\n",
"cv2.waitKey(0)\n",
"cv2.destroyAllWindows()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "90114aee",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
1 change: 1 addition & 0 deletions machine-learning/week2/yolo_training.ipynb

Large diffs are not rendered by default.

80 changes: 80 additions & 0 deletions machine-learning/week3/Car detection using Haar Cascades.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "3b2db876",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"<>:17: SyntaxWarning: \"is\" with a literal. Did you mean \"==\"?\n",
"<>:17: SyntaxWarning: \"is\" with a literal. Did you mean \"==\"?\n",
"C:\\Users\\adity\\AppData\\Local\\Temp\\ipykernel_37336\\590239235.py:17: SyntaxWarning: \"is\" with a literal. Did you mean \"==\"?\n",
" if cars is ():\n"
]
}
],
"source": [
"import numpy as np\n",
"import cv2\n",
"\n",
"# We point OpenCV's CascadeClassifier function to where our \n",
"# classifier (XML file format) is stored\n",
"car_classifier = cv2.CascadeClassifier('Haarcascades/haarcascade_car.xml')\n",
"\n",
"# Load our image then convert it to grayscale\n",
"image = cv2.imread('lambo1.jpg')\n",
"gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
"\n",
"# Our classifier returns the ROI of the detected car as a tuple\n",
"# It stores the top left coordinate and the bottom right coordiantes\n",
"cars = car_classifier.detectMultiScale(gray, 1.4, 2)\n",
"\n",
"# When no cars detected, car_classifier returns and empty tuple\n",
"if cars is ():\n",
" print(\"No cars found\")\n",
"\n",
"# We iterate through our faces array and draw a rectangle\n",
"# over each car in cars\n",
"for (x,y,w,h) in cars:\n",
" cv2.rectangle(image, (x,y), (x+w,y+h), (0,255,0), 2)\n",
" cv2.imshow('Car Detection', image)\n",
" cv2.waitKey(0)\n",
" \n",
"cv2.destroyAllWindows()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "00a3561f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading