-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Day-7_SUMMER_TRAINING_AI/ML IMAGE REC. 🤔
- Loading branch information
1 parent
4df7cd7
commit 6e5929b
Showing
6 changed files
with
2,728 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,272 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Day-7_DHRUVDHAYAL_AI/ML.ipynb | ||
Automatically generated by Colab. | ||
Original file is located at | ||
https://colab.research.google.com/drive/1K26xv32kLaOTY8qLVpCVxFylXPeMLQx7 | ||
#Now, we directly acess the data values from the Drive Directly! | ||
""" | ||
|
||
from google.colab import drive; | ||
drive.mount('/gdrive'); | ||
|
||
!unzip '/content/drive/MyDrive/Colab Notebooks/orl_face/orl_face.zip' -d '/content/drive/MyDrive/Colab Notebooks/orl_face' | ||
|
||
#Now, Importing the Images data from the unzip files. | ||
import matplotlib.pyplot as plt; | ||
import matplotlib.image as mimg; | ||
import numpy as np; | ||
import pandas as pd; | ||
|
||
#Now, we need to read the values of the Data from a Drive. | ||
user_name=24; #Labels. | ||
samples_no=6; #Define the Variable Sample_Numbers. | ||
path='/content/drive/MyDrive/Colab Notebooks/orl_face/orl_face/u%d/%d.png'%(user_name,samples_no); | ||
|
||
#Now, we need to read the data from images. | ||
imag=mimg.imread(path); | ||
print("\n 1. Type of the Image is: ",type(imag)); | ||
print("\n 2. Length of the Image is: ",imag.shape); | ||
print("\n"); | ||
|
||
#Now, we need to plot the Image. | ||
plt.figure(1,figsize=(5,10)); | ||
plt.imshow(imag,cmap='gray'); | ||
plt.axis("off"); | ||
plt.show(); | ||
|
||
#Now, convert 2-D Images it into 1-D Images by using the Flatten. | ||
feat=imag.reshape(1,-1); | ||
print("\n 1. Length of the Images: ",imag.shape); | ||
print("\n 2. Length of the Features: ",feat.shape); | ||
print("\n --> Range: ",imag.min()," - ",imag.max()); | ||
|
||
#Logic to acess all the samples of the User. | ||
#Of the Valued Users. | ||
total_sample=400; | ||
user_name=26; | ||
sample_no=6; | ||
data=np.zeros((total_sample,imag.shape[0]*imag.shape[1])); | ||
label=np.zeros((total_sample)); | ||
images=np.zeros((total_sample,imag.shape[0],imag.shape[1])); | ||
index=-1; | ||
for i in range(1,41,1): | ||
for j in range(1,11,1): | ||
index=index+1; | ||
#acess any of the single image. | ||
user_name=i; | ||
sample_no=j; | ||
path='/content/drive/MyDrive/Colab Notebooks/orl_face/orl_face/u%d/%d.png'%(user_name,sample_no); | ||
#reading the Image. | ||
im=mimg.imread(path); | ||
data[index,:]=feat | ||
label[index]=i | ||
images[index,:,:]=im | ||
print("user num ",i,'samp no',j,'processed...'); | ||
|
||
#Now, displaying the values of the Image. | ||
plt.imshow(images[397,:,:],cmap='gray'); | ||
plt.axis('off'); | ||
plt.show(); | ||
|
||
"""#Now, we need to represent the values of the (5*5) Matrix.""" | ||
|
||
#Importing all the Built-in Libraries. | ||
import numpy as np | ||
import pandas as pd | ||
import matplotlib.pyplot as plt # Import matplotlib.pyplot | ||
|
||
#Now, we need to show all the (5*5) Matrix Values. | ||
plt.figure(1,figsize=(10,10)); | ||
for i in range(5): | ||
for j in range(5): | ||
num=np.random.randint(0,400); | ||
samples=images[num,:,:]; | ||
la=label[num]; | ||
plt.subplot(5,5,i*5+j+1) # Use plt to access subplot | ||
plt.imshow(samples,cmap='gray'); | ||
plt.axis('off'); | ||
plt.title(" "+str(int(la))); | ||
#showing the Image (5*5) Matrix. | ||
plt.show(); | ||
|
||
from sklearn import svm | ||
import pandas as pd | ||
X = data # Extract the data from the 'data' key | ||
y =label # Use the target variable from the Iris dataset | ||
|
||
|
||
# split the data into 70:30 ratio | ||
Xtrain,Xtest,ytrain,ytest = model_selection.train_test_split(X,y,test_size=0.3,random_state=5) | ||
print(Xtrain.shape,ytrain.shape) | ||
print(Xtest.shape,ytest.shape) | ||
|
||
ker = ['poly','linear','rbf'] | ||
c_value = [1,2,3] | ||
|
||
# pre allocation of the result variable | ||
result = np.zeros((len(ker),len(c_value))) | ||
for i in range(len(ker)): | ||
for j in range(len(c_value)): | ||
# create the svm classifier | ||
orl_svm_model = svm.SVC(kernel=ker[i],gamma='scale',C=c_value[j]) | ||
|
||
# train the model | ||
orl_svm_model = orl_svm_model.fit(Xtrain,ytrain) | ||
|
||
# predict the labels | ||
ypred = orl_svm_model.predict(Xtest) | ||
|
||
# accuracy | ||
acc = metrics.accuracy_score(ypred,ytest) | ||
#print("accuracy:", acc) | ||
result[i,j]=acc | ||
print(result) | ||
|
||
ResultDF = pd.DataFrame(result,index=ker,columns=["C=1","C=2","C=3"]) | ||
ResultDF | ||
print("\n Showing in the form of the Graphical Methods!"); | ||
ResultDF.plot(kind='bar',figsize=(4,4)) | ||
|
||
#Saving our trained model in the Google Drive. | ||
#We, use the Joblib to save the models. | ||
import joblib | ||
# final best model | ||
# kernel function - linear , C =1 | ||
|
||
orl_svm_model = svm.SVC(kernel='linear',gamma='scale',C=1) | ||
# train the model | ||
orl_svm_model = orl_svm_model.fit(Xtrain,ytrain) | ||
|
||
# save the trained model | ||
joblib.dump(orl_svm_model,'/content/drive/MyDrive/Colab Notebooks/orl_face_model.pkl') | ||
|
||
#Show the random image from 1-10 , asked by the user to take as input form of parameter. | ||
#Ask the user as ID to show that location image randomly. | ||
import matplotlib.pyplot as plt; | ||
import matplotlib.image as mimg; | ||
import pandas as pd; | ||
import numpy as np; | ||
|
||
#Now, we simply ask from the User. | ||
user_name=int(input("Enter the User ID: ")); | ||
sample_no=np.random.randint(8,10); | ||
path='/content/drive/MyDrive/Colab Notebooks/orl_face/orl_face/u%d/%d.png'%(user_name,sample_no); | ||
|
||
#Now, we need to show the value of the Image. | ||
imag=mimg.imread(path); | ||
print("\n"); | ||
plt.figure(1,figsize=(5,10)); | ||
plt.imshow(imag,cmap='gray'); | ||
plt.axis("off"); | ||
plt.title('Query Image'); | ||
plt.show(); | ||
|
||
#We, make the trained model and save in the drive by using the joblib. | ||
from sklearn import datasets, svm, metrics, model_selection; | ||
import joblib; | ||
import numpy as np; | ||
import pandas as pd; | ||
|
||
feat=imag.reshape(1,-1); | ||
|
||
#Set the value of the Path_model where, we have to store the model in pkl. file on Drive. | ||
path_model='/content/drive/MyDrive/Colab Notebooks/orl_face_model.pkl'; | ||
|
||
#Train the Model by the help of Classifications. | ||
face_model=joblib.load(path_model); | ||
|
||
#predict the image model by datasets. | ||
result=face_model.predict(feat); | ||
|
||
print("Prediction: ",int(result[0])); | ||
|
||
#set the location of the zip image file. | ||
path='/content/drive/MyDrive/Colab Notebooks/orl_face/orl_face/u%d/%d.png'%(result[0],1); | ||
|
||
im_pred = mimg.imread(path) | ||
|
||
plt.figure(1,(8,5)) | ||
plt.subplot(1,2,1) | ||
plt.imshow(im,cmap='gray') | ||
plt.axis('off') | ||
plt.title("Query image") | ||
|
||
plt.subplot(1,2,2) | ||
plt.imshow(im_pred,cmap='gray') | ||
plt.axis('off') | ||
plt.title("predicted user") | ||
|
||
"""#Example of FP-(FALSE - POSITIVE), Very, Dangerous I check but I don't know where is the problem where all snippet is found to be correct, I refer to my friend's colab notebook for reviewing.""" | ||
|
||
import matplotlib.image as mimg | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
# load the query image | ||
usrId = int(input("Enter the user number:")) | ||
samp = np.random.randint(1,10) # selecting any random sample number | ||
path = "/content/drive/MyDrive/Colab Notebooks/orl_face/orl_face/u%d/%d.png"%(usrId,samp) | ||
|
||
im = mimg.imread(path) | ||
|
||
plt.figure(1,(5,5)) | ||
plt.imshow(im,cmap='gray') | ||
plt.axis('off') | ||
plt.title("Query image") | ||
|
||
import joblib | ||
from sklearn import svm ,metrics | ||
# calcualte the features of the query image | ||
feat_query = im.reshape(1,-1) | ||
|
||
# model path | ||
path_model = '/content/drive/MyDrive/Colab Notebooks/orl_face_model.pkl'; | ||
# load the trained model | ||
face_model = joblib.load(path_model) | ||
|
||
|
||
# predict the id of the query image | ||
id = face_model.predict(feat_query) | ||
print("User id predicted is :",int(id[0])) | ||
|
||
|
||
path = "/content/drive/MyDrive/Colab Notebooks/orl_face/orl_face/u%d/%d.png"%(id[0],1) | ||
|
||
im_pred = mimg.imread(path) | ||
|
||
plt.figure(1,(8,5)) | ||
plt.subplot(1,2,1) | ||
plt.imshow(im,cmap='gray') | ||
plt.axis('off') | ||
plt.title("Query image") | ||
|
||
plt.subplot(1,2,2) | ||
plt.imshow(im_pred,cmap='gray') | ||
plt.axis('off') | ||
plt.title("predicted user") | ||
|
||
"""#False-Positive! | ||
#Now, working on Generating an Computer Applications to analyze the Brain Tumor Problem. | ||
""" | ||
|
||
#Now, unzip the tumor files. | ||
!unzip '/content/drive/MyDrive/Colab Notebooks/orl_face/new_train-20240708T093236Z-001.zip' -d '/content/drive/MyDrive/Colab Notebooks' | ||
|
||
#Finding the Tumor in the Brain with the White-Spot. | ||
import matplotlib.pyplot as plt; | ||
import matplotlib.image as mimg; | ||
import numpy as np; | ||
plt.figure(1,figsize=(10,10)); | ||
for i in range(1,62,1): | ||
path='/content/drive/MyDrive/Colab Notebooks/new_train/d(%d).png'%(i); | ||
imag=mimg.imread(path); | ||
plt.subplot(8,8,i); | ||
plt.imshow(imag,cmap='gray'); | ||
plt.title(i); | ||
plt.tight_layout(); | ||
plt.axis("off"); | ||
#plt.show(); |
Oops, something went wrong.