1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | # https://medium.com/analytics-vidhya/image-classification-using-machine-learning-support-vector-machine-svm-dc7a0ec92e01 import pandas as pd import os from skimage.transform import resize from skimage.io import imread import numpy as np import matplotlib.pyplot as plt Categories=['Cat', 'Dog'] flat_data_arr=[] target_arr=[] datadir=f'D:\PyTest\PetImages\\tkt' for i in Categories: print(f'loading ... category: {i}') path=os.path.join(datadir,i) for img in os.listdir(path): img_array=imread(os.path.join(path, img)) img_resized=resize(img_array, (150,150,3)) flat_data_arr.append(img_resized.flatten()) target_arr.append(Categories.index(i)) #print(Categories.index(i)) #print(os.path.join(path, img)) print(f'loaded category: {i} successfully!') flat_data=np.array(flat_data_arr) target=np.array(target_arr) df=pd.DataFrame(flat_data) df['Target']=target x=df.iloc[:,:-1] y=df.iloc[:,-1] from sklearn import svm from sklearn.model_selection import GridSearchCV param_grid={'C':[0.1, 1, 10, 100], 'gamma': [0.0001, 0.001, 0.1, 1], 'kernel': ['rbf', 'poly']} svc=svm.SVC(probability=True) model=GridSearchCV(svc, param_grid) from sklearn.model_selection import train_test_split x_train, x_test, y_train, y_test=train_test_split(x, y, test_size=0.20, random_state=77, stratify=y) print('Splitted Successfully!') model.fit(x_train, y_train) print('The model is trained successfully with the given images!') from sklearn.metrics import accuracy_score y_pred=model.predict(x_test) print('The predicted Data is:') print(y_pred) print('The actual data is:') print(np.array(y_test)) print(f"The model is {accuracy_score(y_test, y_pred)*100}% accurate.") |
Wednesday, January 24, 2024
ML: Image Classification Using Machine Learning-Support Vector Machine(SVM)
Labels:
ML
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment