<jupyter_start><jupyter_text>Face landmark- This is an ipynb file created in Kaggle environment.- CNN Dataset- Face Images with Marked Landmark Points, https://www.kaggle.com/drgilermo/face-images-with-marked-landmark-points  0. Import Packages<jupyter_code>import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import tensorflow as tf
import pandas as pd
import numpy as np
from tqdm.keras import TqdmCallback
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Flatten, Dense, Dropout, LeakyReLU, BatchNormalization
from tensorflow.keras.initializers import glorot_uniform
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.optimizers import SGD, Adam<jupyter_output><empty_output><jupyter_text>1. Make dataset<jupyter_code># let's deal with .npz type of image file
img = np.load('/kaggle/input/face-images-with-marked-landmark-points/face_images.npz')  # class
face = img.get(img.files[0])  # image to array (96, 96, 7049)
face = np.moveaxis(face,-1,0)  # (7049, 96, 96)
face = face.reshape(face.shape[0],face.shape[1],face.shape[1], 1)

print(face.shape)
key_pts = pd.read_csv('/kaggle/input/face-images-with-marked-landmark-points/facial_keypoints.csv')
key_pts.head()
print(key_pts.shape)
key_pts = key_pts.fillna(0)
key_pts.index.values
features = face[key_pts.index.values, :, :, :]
features = features/255  # normalize
key_pts.reset_index(inplace=True,drop=True)
features.shape
# visualization
ind = 10
plt.imshow(features[ind,:,:,0],cmap='gray')
plt.scatter(key_pts.iloc[ind][0:-1:2], key_pts.iloc[ind][1::2], c='y')
plt.axis('off')
plt.show()
x_train, x_test, y_train, y_test = train_test_split(features, key_pts, test_size = 0.2)<jupyter_output><empty_output><jupyter_text>2. Model (CNN)<jupyter_code># params
img_size = 96
batch_size = 32
epochs = 200
model = Sequential()

model.add(Input(shape=(img_size, img_size, 1)))

model.add(BatchNormalization())
model.add(Conv2D(32, (3,3), padding="same"))
model.add(LeakyReLU(alpha = 0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

model.add(BatchNormalization())
model.add(Conv2D(64, (3,3), padding="same"))
model.add(LeakyReLU(alpha = 0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

model.add(BatchNormalization())
model.add(Conv2D(128, (3,3), padding="same"))
model.add(LeakyReLU(alpha = 0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

model.add(Flatten())
model.add(Dense(256))
model.add(LeakyReLU(alpha = 0.1))
model.add(Dropout(0.5))

model.add(Dense(64))
model.add(LeakyReLU(alpha = 0.1))
model.add(Dense(30))
model.summary()
model.compile(loss = 'mean_squared_error', optimizer = Adam(), 
              metrics=['mean_squared_error'])<jupyter_output><empty_output><jupyter_text>3. Training<jupyter_code>%%time
hist = model.fit(x_train, y_train,batch_size = batch_size, epochs = epochs, 
                 validation_data = (x_test, y_test), shuffle = True)
# Save model
model.save("/kaggle/working/landmark.h5")

landmark_json = model.to_json()

with open("/kaggle/working/landmark.json", "w") as json_file:
    json_file.write(landmark_json)
model = load_model('/kaggle/working/landmark.h5')
plt.plot(hist.history['loss'][3:50])
plt.plot(hist.history['val_loss'][3:50])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()<jupyter_output><empty_output><jupyter_text>4. Prediction<jupyter_code>pred = model.predict(x_test)
index = 100
pred_pt = np.array(y_test)[index]

plt.imshow(x_test[index,:,:,:], cmap='gray')
plt.scatter(pred_pt[0:-1:2], pred_pt[1::2], c='r')
plt.scatter(pred[index,:][0:-1:2],pred[index,:][1::2],c='b')
plt.axis('off')
plt.show()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup