Commit 55e7bfe8 authored by Chamindu Madushan's avatar Chamindu Madushan

completed unauthorized animal identification by scikit-learn

parent c55b3161
from bing_image_downloader import downloader
downloader.download("civet",limit =30,output_dir='images',
adult_filter_off=True)
from bing_image_downloader import downloader
downloader.download("cat",limit =30,output_dir='images',
adult_filter_off=True)
from bing_image_downloader import downloader
downloader.download("rat",limit =40,output_dir='images',
adult_filter_off=True)
# prepocess
import os
import matplotlib.pyplot as plt
import numpy as np
from skimage.io import imread
from skimage.transform import resize
target = []
images = []
flat_data = []
DATADIR = '/content/images'
CATEGORIES = ['civet', 'cat', 'snakes', 'rat']
for category in CATEGORIES:
class_num = CATEGORIES.index(category) # label encoding
path = os.path.join(DATADIR, category) # create path to use all images
for img in os.listdir(path):
img_array = imread(os.path.join(path, img))
# print(img_array.shape)
# plt.imshow(img_array)
img_resized = resize(img_array, (150, 150, 3)) # Normalizes to 0 to 1
flat_data.append(img_resized.flatten())
images.append(img_resized)
target.append(class_num)
flat_data = np.array(flat_data)
target = np.array(target)
images = np.array(images)
# split data into Training and Testing
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(flat_data, target,
test_size=0.3, random_state=109)
from sklearn.model_selection import GridSearchCV
from sklearn import svm # support vector machine for a model
param_grid = [
{'C': [1, 10, 100, 1000], 'kernel': ['linear']},
{'C': [1, 10, 100, 1000], 'gamma': [0.001, 0.0001], 'kernel': ['rbf']},
]
svc = svm.SVC(probability=True)
clf = GridSearchCV(svc, param_grid)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
y_pred
y_test
from sklearn.metrics import accuracy_score, confusion_matrix
accuracy_score(y_pred, y_test) # check Accuracy
confusion_matrix(y_pred, y_test)
# save the model using pickle library
import pickle
pickle.dump(clf, open('img_model.p', 'wb'))
model = pickle.load(open('img_model.p', 'rb'))
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment