Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13345 from Carreau/no-sys-path-append
Don't add ipython unconditionally to sys.path
- Loading branch information
Showing
1 changed file
with
29 additions
and
15 deletions.
There are no files selected for viewing
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
a2ae81e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import cv2
def generate_dataset():
face_classifier = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
def face_cropped(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray, 1.3, 5)
#generate_dataset()
import numpy as np # pip install numpy
def my_label(image_name):
name = image_name.split('.')[-3]
# if you have two person in your dataset
if name=="Ishwar":
return np.array([1,0])
elif name=="Manish":
return np.array([0,1])
import os
from random import shuffle
from tqdm import tqdm
def my_data():
data = []
for img in tqdm(os.listdir("data")):
path=os.path.join("data",img)
img_data = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
img_data = cv2.resize(img_data, (50,50))
data.append([np.array(img_data), my_label(img)])
shuffle(data)
return data
data = my_data()
train = data[:2400]
test = data[2400:]
X_train = np.array([i[0] for i in train]).reshape(-1,50,50,1)
print(X_train.shape)
y_train = [i[1] for i in train]
X_test = np.array([i[0] for i in test]).reshape(-1,50,50,1)
print(X_test.shape)
y_test = [i[1] for i in test]
import warnings
warnings.filterwarnings('ignore')
import tensorflow as tf
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
tf.reset_default_graph()
convnet = input_data(shape=[50,50,1])
convnet = conv_2d(convnet, 32, 5, activation='relu')
32 filters and stride=5 so that the filter will move 5 pixel or unit at a time
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 128, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)
convnet = fully_connected(convnet, 3, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate = 0.001, loss='categorical_crossentropy')
model = tflearn.DNN(convnet, tensorboard_verbose=1)
model.fit(X_train, y_train, n_epoch=12, validation_set=(X_test, y_test), show_metric = True, run_id="FRS" )
def data_for_visualization():
Vdata = []
for img in tqdm(os.listdir("Images for visualization")):
path = os.path.join("Images for visualization", img)
img_num = img.split('.')[0]
img_data = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
img_data = cv2.resize(img_data, (50,50))
Vdata.append([np.array(img_data), img_num])
shuffle(Vdata)
return Vdata
Vdata = data_for_visualization()
import matplotlib.pyplot as plt # pip install matplotlib
fig = plt.figure(figsize=(20,20))
for num, data in enumerate(Vdata[:20]):
img_data = data[0]
y = fig.add_subplot(5,5, num+1)
image = img_data
data = img_data.reshape(50,50,1)
model_out = model.predict([data])[0]
plt.show()