Skip to content

Instantly share code, notes, and snippets.

View ajayarunachalam's full-sized avatar
💭
I may be slow to respond.

AjayAru ajayarunachalam

💭
I may be slow to respond.
View GitHub Profile
@ajayarunachalam
ajayarunachalam / step2.py
Created August 5, 2021 13:44
DATA LABELING ALGORITHM
def _autoLabel(self, img, rects, masks):
with open(self._via_export_json, 'r') as fr:
json_file = json.load(fr)
regions = []
N = len(rects)
for i in range(N):
mask = masks[..., i]
mask = np.where(mask, 255, 0).astype(np.uint8)
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
@ajayarunachalam
ajayarunachalam / step1.py
Created August 5, 2021 13:42
Detection Model
# Object Detection using MaskRCNN
def MaskRCNNDetection(self, frame, epsilon=None, auto_mask=True):
'''
Detects objects in frame by masking them.
:param frame: An image in BGR format.
:type frame: :class:`numpy.ndarray`
:param None epsilon: Used only to have consistency.
Not used in the algorithm.
:param bool auto_mask: Should the system perform auto-masking
display_FP_FN(model, test_loader, criterion = nn.CrossEntropyLoss(), display = 'fn')
get_confusion_matrix(y_truth, y_proba, labels = [0, 1])
plot_ROCAUC_curve(y_truth, y_proba, (8, 8))
# performance on test data
def test(model, test_loader, criterion):
'''
Model testing
Args:
model: model used during training and validation
test_loader: data loader object containing testing data
criterion: loss function used
# Save model
PATH = ".\ViTModel.pt" # Use your own path
torch.save(model.state_dict(), PATH)
efficient_transformer = Linformer(
dim=128,#128
seq_len=49+1, # 7x7 patches + 1 cls-token
depth=12,
heads=8,
k=64
)
model = ViT(
dim=128,
# ### Image Augmentation
#Tune the transforms
ORIG_RES = False
if ORIG_RES:
resize = 512
else:
resize = 224
device = 'cpu'
os.makedirs('lung_ct_scan_data', exist_ok=True)
train_dir = 'lung_ct_scan_data/train'
test_dir = 'lung_ct_scan_data/test'
train_list = glob.glob(os.path.join(train_dir,'*.png')) + glob.glob(os.path.join(train_dir,'*.jpg'))
test_list = glob.glob(os.path.join(test_dir, '*.png')) + glob.glob(os.path.join(test_dir, '*.jpg'))
### Training settings
batch_size = 64
epochs = 20
lr = 3e-5
gamma = 0.7
seed = 142
IMG_SIZE = 224
from __future__ import print_function
import glob
from itertools import chain
import os
import random
import zipfile
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import torch