Skip to content

Instantly share code, notes, and snippets.

View Praveen76's full-sized avatar
🎯
Focusing

Praveen Kumar Anwla Praveen76

🎯
Focusing
View GitHub Profile
#Write a function to choose one image randomly from your dataset and predict using Trained model.
def show_image_with_predictions(df, threshold=0.6):
# choose a random image
row = df.sample()
filepath = row['fileName'].values[0]
print("filepath:", filepath)
# get all rows for this image
df2 = df[df['fileName'] == filepath]
im = np.array(Image.open(filepath))
from glob import glob
model_paths = glob('snapshots/resnet50_csv_0*.h5')
latest_path = sorted(model_paths)[-1]
print("path:", latest_path)
from keras_retinanet import models
model = models.load_model(latest_path, backbone_name='resnet50')
model = models.convert_model(model)
python keras_retinanet/bin/train.py --freeze-backbone
--random-transform \
--weights {PRETRAINED_MODEL}
--batch-size 8
--steps 500
--epochs 15
csv maskDetectorData.csv maskDetectorClasses.csv
#Put your training data path & file that has labels for your training data
!keras_retinanet/bin/train.py
--freeze-backbone
--random-transform
--weights {PRETRAINED_MODEL}
--batch-size 8
--steps 500
--epochs 15
csv maskDetectorData.csv
maskDetectorClasses.csv
URL_MODEL = 'https://github.com/fizyr/keras-retinanet/releases/download/0.5.1/resnet50_coco_best_v2.1.0.h5'
urllib.request.urlretrieve(URL_MODEL, PRETRAINED_MODEL)
#Define labels & write them in a file
classes = ['mask','noMask']
with open('../maskDetectorClasses.csv', 'w') as f:
for i, class_name in enumerate(classes):
f.write(f'{class_name},{i}\n')
if not os.path.exists('snapshots'):
os.mkdir('snapshots')
# pick a random image
filepath = df.sample()['fileName'].values[0]
# get all rows for this image
df2 = df[df['fileName'] == filepath]
im = np.array(Image.open(filepath))
# if there's a PNG it will have alpha channel
im = im[:,:,:3]
pngPath='C:/Users/PraveenKumar/RetinaNet//maskDetectorJPEGImages/'
annotPath='C:/Users/PraveenKumar/RetinaNet//maskDetectorXMLfiles/'
data=pd.DataFrame(columns=['fileName','xmin','ymin','xmax','ymax','class'])
os.getcwd()
#read All files
allfiles = [f for f in listdir(annotPath) if isfile(join(annotPath, f))]
#Read all pdf files in images and then in text and store that in temp folder
import numpy as np
import shutil
import pandas as pd
import os, sys, random
import xml.etree.ElementTree as ET
import pandas as pd
from os import listdir
from os.path import isfile, join
import matplotlib.pyplot as plt
from PIL import Image
import os
print(os.getcwd())
git clone https://github.com/fizyr/keras-retinanet.git
%cd keras-retinanet/
!pip install .
!python setup.py build_ext --inplace