<jupyter_start><jupyter_text>03 Happy Face ![](https://images.unsplash.com/photo-1472162072942-cd5147eb3902?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80)Photo by [<NAME>](https://unsplash.com/photos/4K2lIP0zc_k)<jupyter_code>import pickle
X, y = pickle.load(open("dataset.pkl", "rb"))
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state=0)
import matplotlib.pyplot as plt
import numpy as np
happiness = ["unhappy", "happy"]

idx = np.random.randint(X_train.shape[0])

plt.imshow(X_train[idx], cmap="gray")
plt.title(happiness[int(y_train[idx])])
X_train_flat = X_train.reshape(X_train.shape[0], np.prod(X_train.shape[1:]))
X_train_flat.shape
X_test_flat = X_test.reshape(X_test.shape[0], np.prod(X_test.shape[1:]))
X_test_flat.shape<jupyter_output><empty_output><jupyter_text>I. PCA<jupyter_code>from sklearn.decomposition import PCA

pca = PCA(n_components=0.99)

pca.fit(X_train_flat[:500])
X_train_pca = pca.transform(X_train_flat)
X_train_pca.shape
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()

X_train_pca = scaler.fit_transform(X_train_pca)
from sklearn.ensemble import RandomForestClassifier

rf = RandomForestClassifier()

rf.fit(X_train_pca, y_train)
X_test_pca = pca.transform(X_test_flat)

X_test_pca = scaler.transform(X_test_pca)

print("accuracy with random forest:", rf.score(X_test_pca, y_test))<jupyter_output>accuracy with random forest: 0.6313559322033898<jupyter_text>II. ConvNet<jupyter_code>X_train.shape
X_train = X_train/255.
X_test = X_test/255.
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], X_train.shape[2], 1)
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], X_test.shape[2], 1)
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import MaxPooling2D, Conv2D, Flatten, Dense


def my_model():
    
    model = Sequential()

    # Layer C1
    model.add(Conv2D(filters=8, kernel_size=(3, 3), activation='relu', input_shape=(350,350, 1)))
    # Layer S2
    model.add(MaxPooling2D(pool_size=(4, 4)))
    # Layer C3
    model.add(Conv2D(filters=16, kernel_size=(3, 3), activation='relu'))
    # Layer S4
    model.add(MaxPooling2D(pool_size=(4, 4)))
    # Before going into layer C5, we flatten our units
    model.add(Flatten())
    # Layer C5
    model.add(Dense(units=128, activation='relu'))
    # Layer F6
    model.add(Dense(units=64, activation='relu'))
    # Output layer
    model.add(Dense(units=1, activation = 'sigmoid'))
    
    return model
# Instantiate the model
model = my_model()
model.summary()
# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])


# Finally fit the model
model.fit(x=X_train, y=y_train, validation_data=(X_test, y_test), epochs=20, batch_size=32)
print("accuracy with CNN:", model.evaluate(X_test, y_test)[1])<jupyter_output>236/236 [==============================] - 3s 11ms/sample - loss: 0.1232 - acc: 0.9407
accuracy with CNN: 0.94067794<jupyter_text>III. Facial landmarks<jupyter_code>import face_recognition

landmarks_train = []
landmarks_test = []

for face in X_train:
    landmarks_train.append(face_recognition.face_landmarks(face))


for face in X_test:
    landmarks_test.append(face_recognition.face_landmarks(face))

X_train_landmarks = []
X_test_landmarks = []


for face in landmarks_train:
    face_feats = []
    try:
        for keys in face[0]:
            for x,y in face[0][keys]:
                face_feats.append(x)
                face_feats.append(y)
        X_train_landmarks.append(np.array(face_feats))
    except:
        X_train_landmarks.append(np.zeros(144))


for face in landmarks_test:
    face_feats = []
    try:
        for keys in face[0]:
            for x,y in face[0][keys]:
                face_feats.append(x)
                face_feats.append(y)
        X_test_landmarks.append(np.array(face_feats))
    except:
        X_test_landmarks.append(np.zeros(144))
        
X_train_landmarks = np.array(X_train_landmarks)
X_test_landmarks = np.array(X_test_landmarks)
X_train_landmarks = X_train_landmarks/350
X_test_landmarks = X_test_landmarks/350
from sklearn.ensemble import RandomForestClassifier

rf = RandomForestClassifier()

rf.fit(X_train_landmarks, y_train)
print("score with landmarks and Random Forest:", rf.score(X_test_landmarks, y_test))<jupyter_output>score with landmarks and Random Forest: 0.9449152542372882
{'fraction_non_alphanumeric': 0.13080357142857144, 'fraction_numerical': 0.04107142857142857, 'mean_word_length': 5.071815718157182, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '130975', 'license': 'MIT', 'n_tokens_mistral': 1840, 'n_tokens_neox': 1706, 'n_words': 287, 'path': 'exercises/04-face-recognition/03-Happy-Face/src/03-Happy-Face.ipynb', 'repo_name': 'eliaswalyba/2021-deep-learning-gift-course', 'size': '4483'}