DigiNsure Inc. is an innovative insurance company focused on enhancing the efficiency of processing claims and customer service interactions. Their newest initiative is digitizing all historical insurance claim documents, which includes improving the labeling of some IDs scanned from paper documents and identifying them as primary or secondary IDs.
To help them in their effort, you'll be using multi-modal learning to train an Optical Character Recognition (OCR) model. To improve the classification, the model will use images of the scanned documents as input and their insurance type (home, life, auto, health, or other). Integrating different data modalities (such as image and text) enables the model to perform better in complex scenarios, helping to capture more nuanced information. The labels that the model will be trained to identify are of two types: a primary and a secondary ID, for each image-insurance type pair.
! pip install torchvision
! pip install torchmetrics# Import the necessary libraries
import matplotlib.pyplot as plt
import numpy as np
from project_utils import ProjectDataset
import pickle
import torch
import torch.nn as nn
from sklearn.model_selection import train_test_split
from torch.utils.data import DataLoader, Subset
from torchmetrics import Accuracy
import numpy as np
Load Data
# Load the data
dataset = pickle.load(open('ocr_insurance_dataset.pkl', 'rb'))# Define a function to visualize codes with their corresponding types and labels
def show_dataset_images(dataset, num_images=5):
fig, axes = plt.subplots(1, min(num_images, len(dataset)), figsize=(20, 4))
for ax, idx in zip(axes, np.random.choice(len(dataset), min(num_images, len(dataset)), False)):
img, lbl = dataset[idx]
ax.imshow((img[0].numpy() * 255).astype(np.uint8).reshape(64,64), cmap='gray'), ax.axis('off')
ax.set_title(f"Type: {list(dataset.type_mapping.keys())[img[1].tolist().index(1)]}\nLabel: {list(dataset.label_mapping.keys())[list(dataset.label_mapping.values()).index(lbl)]}")
plt.show()# Inspect 5 codes images from the dataset
show_dataset_images(dataset)Exploring the data
print(type(dataset))# Inspect the available methods and attributes of the dataset
print(dir(dataset))# Check the length of the dataset
print(f"Number of samples in the dataset: {len(dataset)}")# Inspect a single data sample to understand its structure
sample = dataset[0]
print(f"Sample structure: {sample}")# Inspect shape of data
data_array = np.array(dataset.data)
print(data_array.shape)# Get the first sample from the dataset
image_tensor, insurance_type_tensor = dataset.data[0]
# Print the shape of the image tensor
print(f"Shape of the image tensor: {image_tensor.shape}")