<jupyter_start><jupyter_text>Loading train and test data<jupyter_code>transform_ = transforms.Compose([Rescale(250),
                                    RandomCrop(224),
                                     Normalize(),
                                     ToTensor()])



train_dataset = FaceData(point_file=train_csv,image_folder=train_dir,transform=transform_)
print('Length of dataset: ', len(train_dataset))
test_dataset = FaceData(test_csv,test_dir,transform_)
print('Length of dataset: ', len(test_dataset))

for data in test_dataset:
  print(data['image'].shape)
  print(data['keypoints'].shape)
  break
for data in train_dataset:
  print(data['image'].shape)
  print(data['keypoints'].shape)
  break<jupyter_output>torch.Size([1, 224, 224])
torch.Size([68, 2])<jupyter_text>Loading in batches<jupyter_code># tr = int(0.8*len(face_dataset))
# val = len(face_dataset) - int(0.8*len(face_dataset))
# trainset, valset = random_split(face_dataset, [tr,val])

train_loader = DataLoader(dataset=train_dataset, batch_size=5, shuffle=True)
test_loader = DataLoader(dataset=test_dataset, batch_size=5)
for data in test_loader:
  print(data['image'].shape)
  print(data['keypoints'].shape)
  break<jupyter_output>torch.Size([5, 1, 224, 224])
torch.Size([5, 68, 2])<jupyter_text>Model Creation<jupyter_code>model = Net()
model = model.to(device)
model<jupyter_output><empty_output><jupyter_text>Loading saved model<jupyter_code>state_dict = torch.load('model_may14.pt')
model.load_state_dict(state_dict)<jupyter_output><empty_output><jupyter_text>Prediction and visualization<jupyter_code>model.eval()
data_ = iter(test_loader)
data = next(data_)
images = data['image']
images = images.to(device,dtype=torch.float)
out = model(images)


def image_kpconvert(image,kpts):
    kpts = kpts.view(68,-1)
    kpts = kpts.cpu().detach().numpy()
    kpts = (kpts*50) + 100 #denormalize keypoints
    image = image.cpu().detach().numpy()
    image = image.transpose((1,2,0))
    image = (image * 255).astype(np.uint8)
    return image,kpts


def plot_img(image,key_pts):
    plt.imshow(np.squeeze(image),cmap='gray')
    plt.scatter(key_pts[:, 0], key_pts[:, 1], s=20, marker='.', c='m')

plt.figure(figsize=(20,18))
for i in range(5):
    plt.subplot(1,5,i+1)
    img,kp = image_kpconvert(images[i],out[i])
    plot_img(img,kp)<jupyter_output><empty_output><jupyter_text>Feature visualization<jupyter_code>weights1 = model.conv1.weight.data

w = weights1.cpu().numpy()
# we can see that we have 32 feature outputs in first layer so we have shape of (32,1,5,5)
w.shape
# we will visua;ize these feature maps
plt.figure(figsize = (20,20))
for i in range(32):
   weight = w[i][0]
   plt.subplot(4,8,i+1)
   plt.imshow(weight,cmap='gray')<jupyter_output><empty_output><jupyter_text>Now we will choose one filter from your trained CNN and apply it to a validation image and will see what happens<jupyter_code>plt.figure(figsize=(10,10))

weight = w[3][0]
plt.subplot(1,3,1)
plt.imshow(weight,cmap='gray')


img = test_dataset[0]['image']
image = img.cpu().detach().numpy()
image = image.transpose((1,2,0))
image = (image * 255).astype(np.uint8)
image = np.squeeze(image)
plt.subplot(1,3,2)
plt.imshow(image,cmap='gray')

filtered = cv2.filter2D(image, -1, weight)
plt.subplot(1,3,3)
plt.imshow(filtered,cmap='gray')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15483677747828692, 'fraction_numerical': 0.03264450434261755, 'mean_word_length': 5.398467432950191, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '584178', 'license': 'MIT', 'n_tokens_mistral': 1319, 'n_tokens_neox': 1238, 'n_words': 214, 'path': 'part2-prediction and visualization.ipynb', 'repo_name': 'arunm8489/Face-keypoint-detection', 'size': '3339'}