<jupyter_start><jupyter_text>The recommended dataset is the Pascal VOC dataset, that is available here: http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar<jupyter_code>!pip install xmltodict
!pip install selectivesearch
import matplotlib.pyplot as plt
%matplotlib inline
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth=True
sess = tf.Session(config=config)
import json, scipy, os
import numpy as np, pandas as pd
import argparse, time, sys, gc, cv2

import xmltodict
from copy import deepcopy
import selectivesearch
xmls_root ="..."  # Path to data folder
annotations = xmls_root + "Annotations/"
jpegs = xmls_root + "JPEGImages/"
XMLs = os.listdir(annotations)
print(XMLs[:10]); print(len(XMLs))
pre_xtrain = []
y_train = []
xml_filepath=annotations
mylist = os.listdir(xml_filepath)
anchors = [[0.83612969, 0.90107317],
       [0.16291552, 0.30092524],
       [0.47087648, 0.86898735],
       [0.27149698, 0.6013433 ],
       [0.57903691, 0.59789919]]
num_grids = 5
def find_closest_anchor(label,img_width, img_height):
  label_width = (label[2]-label[0])/img_width
  label_height = (label[3]-label[1])/img_height  
  label_width_height_array = np.array([label_width, label_height])  
  distance = np.sum(np.square(np.array(anchors) - label_width_height_array), axis=1)  
  closest_anchor = anchors[np.argmin(distance)]  
  return closest_anchor
def closest_anchor_corrections(a, b, anchor, label, img_width, img_height):  
  label_center = [(label[0]+label[2])/(2),(label[1]+label[3])/(2)]  
  anchor_center = [a*img_width/num_grids , b*img_height/num_grids ]  
  dx = (label_center[0] - anchor_center[0])/img_width  
  dy = (label_center[1] - anchor_center[1])/img_height
  dw = ((label[2] - label[0])/img_width) / (anchor[0])
  dh = ((label[3] - label[1])/img_height) / (anchor[1])  
  return dx, dy, dw, dh  
def positive_grid_cell(label,img_width = 224, img_height = 224):
  
  label_center = [(label[0]+label[2])/(2),(label[1]+label[3])/(2)]
  
  a = int(label_center[0]/(img_width/num_grids))
  
  b = int(label_center[1]/(img_height/num_grids))
  
  return a, b
def extract_iou(candidate, current_y,img_shape):
  boxA = deepcopy(candidate)
  boxB = deepcopy(current_y)
  boxA[2] += boxA[0]
  boxA[3] += boxA[1]
  iou_img1 = np.zeros(img_shape)
  iou_img1[boxA[1]:boxA[3],boxA[0]:boxA[2]]=1

  iou_img2 = np.zeros(img_shape)
  iou_img2[int(boxB[1]):int(boxB[3]),int(boxB[0]):int(boxB[2])]=1
  iou = np.sum(iou_img1*iou_img2)/(np.sum(iou_img1)+np.sum(iou_img2)-np.sum(iou_img1*iou_img2))
  return iou
classes = {'person':0,
'cat':1,
'dog':2,
'bird':3,
'train':4,
'aeroplane':5,
'car':6,
'bicycle':7,
'motorbike':8,
'horse':9,
'cow':10,
'tvmonitor':11,
'chair':12,
'boat':13,
'bus':14,
'sheep':15,
'bottle':16,
'pottedplant':17,
'sofa':18,
'diningtable':19}
from keras.applications import vgg16
from keras.utils.vis_utils import plot_model
vgg16_model = vgg16.VGG16(include_top=False, weights='imagenet')
y_train = []

for i in mylist[:10000]:
    xml_file = xml_filepath +i
    arg1=i.split('.')[0]
    with open(xml_file, "rb") as f:    # notice the "rb" mode
        d = xmltodict.parse(f, xml_attribs=True)
        l=[]
        if type(d["annotation"]["object"]) == type(l):
            discard=1
        else:            
            x1=((float(d['annotation']['object']['bndbox']['xmin'])))/(float(d['annotation']['size']['width']))
            x2=((float(d['annotation']['object']['bndbox']['xmax'])))/(float(d['annotation']['size']['width']))
            y1=((float(d['annotation']['object']['bndbox']['ymin'])))/(float(d['annotation']['size']['height']))
            y2=((float(d['annotation']['object']['bndbox']['ymax'])))/(float(d['annotation']['size']['height']))
            cls=d['annotation']['object']['name']
            if(cls == 'person'):
              y_train.append([x2-x1, y2-y1])
y_train = np.array(y_train)
from sklearn.cluster import KMeans
km = KMeans(n_clusters=5)
km.fit(y_train)
km.cluster_centers_
anchors = [[0.45519332, 0.86366928],
       [0.16421121, 0.29978505],
       [0.83407913, 0.90081061],
       [0.27700465, 0.58052042],
       [0.60202617, 0.62422309]]
k=-1
pre_xtrain = []
y_train = []
cls = []
xtrain=[]
final_cls = []
dx = []
dy = []
dw= []
dh = []
final_delta = []
av = 0
x_train = []
img_paths = []
label_coords = []
y_delta = []
anc = []
for i in mylist[:10000]:
    av = av+1
    if(av%100==0):
        print(av)
    xml_file = xml_filepath +i
    #print(xml_file)
    arg1=i.split('.')[0]
    discard=0
    with open(xml_file, "rb") as f:    # notice the "rb" mode
        d = xmltodict.parse(f, xml_attribs=True)
        l=[]
        if type(d["annotation"]["object"]) == type(l):
            discard=1
        else:
            coords={arg1:[]}
            pre_xtrain.append(arg1)
            m=pre_xtrain[(k+1)]
            k = k+1
        if(discard==0):
            x1=((float(d['annotation']['object']['bndbox']['xmin'])))/(float(d['annotation']['size']['width']))
            x2=((float(d['annotation']['object']['bndbox']['xmax'])))/(float(d['annotation']['size']['width']))
            y1=((float(d['annotation']['object']['bndbox']['ymin'])))/(float(d['annotation']['size']['height']))
            y2=((float(d['annotation']['object']['bndbox']['ymax'])))/(float(d['annotation']['size']['height']))
            cls=d['annotation']['object']['name']
            if(cls == 'person'):
              coords[arg1].append(x1)
              coords[arg1].append(y1)
              coords[arg1].append(x2)
              coords[arg1].append(y2)
              coords[arg1].append(cls)
              
              filename = '...'  # Path to images folder
              img = filename
              img_size=224
              img = cv2.imread(filename)
              img2 = cv2.resize(img,(img_size,img_size))
              img2 = img2/255
              
              current_y = [int(x1*224), int(y1*224), int(x2*224), int(y2*224)]
              label_center = [(current_y[0]+current_y[2])/2,(current_y[1]+current_y[3])/2]              
              label = current_y
              
              current_y2 = [float(d['annotation']['object']['bndbox']['xmin']),
                            float(d['annotation']['object']['bndbox']['ymin']),
                            float(d['annotation']['object']['bndbox']['xmax'])-float(d['annotation']['object']['bndbox']['xmin']),
                            float(d['annotation']['object']['bndbox']['ymax'])-float(d['annotation']['object']['bndbox']['ymin'])]
              
              vgg_predict = vgg16_model.predict(img2.reshape(1,img_size,img_size,3))
              x_train.append(vgg_predict)

              target_class = np.zeros((num_grids,num_grids,5))
              target_delta = np.zeros((num_grids,num_grids,20))

              a,b = positive_grid_cell(label)
              
              for a2 in range(num_grids):
                for b2 in range(num_grids):
                  for m in range(len(anchors)):
                    dx, dy, dw, dh = closest_anchor_corrections(a2, b2, anchors[m], label, 224, 224)
                    target_class[a2,b2,m] = 0
                    target_delta[a2,b2,((4*m)):((4*m)+4)] = [dx, dy, dw, dh]
                    anc.append(anchors[m])
                    if((anchors[m] == find_closest_anchor(label,224, 224)) & (a2 == a) & (b2 == b)):
                      target_class[a2,b2,m] = 1
                      
              y_train.append(target_class.flatten())
              y_delta.append(target_delta)
              label_coords.append(current_y)
              img_paths.append(filename)     
      
import keras
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.utils import to_categorical,np_utils
from keras.layers import Dense, Input, GlobalMaxPooling1D,Dropout, Activation, Flatten
from keras.layers import Conv1D, MaxPooling1D, Embedding, Convolution2D, MaxPooling2D,Conv2D
from keras.models import Model
import random
from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers import Dense, Embedding
from keras.layers import LSTM , Bidirectional,Dropout
from keras import backend as K
from keras.layers.advanced_activations import LeakyReLU
from keras import regularizers
from keras.optimizers import Adam
optimizer = Adam(lr=0.001)
from keras.layers import BatchNormalization
from keras import regularizers
model = Sequential()
model.add(BatchNormalization(input_shape=(7,7,512)))
model.add(Conv2D(1024, (3,3), activation='relu',padding='valid'))
model.add(BatchNormalization())
model.add(Conv2D(5, (1,1), activation='relu',padding='same'))
model.add(Flatten())
model.add(Dense(125, activation='sigmoid'))
model.summary()
x_train = np.array(x_train)
y_train = np.array(y_train)
print(x_train.shape, y_train.shape)
np.max(y_train)
x_train = x_train.reshape(x_train.shape[0],7,7,512)
model.compile(loss='binary_crossentropy', optimizer=optimizer)
model.fit(x_train/np.max(x_train), y_train, epochs=5, batch_size = 32, validation_split = 0.1, verbose = 1)
y_train[0]
np.argmax(model.predict(x_train[-2].reshape(1,7,7,512)/np.max(x_train)))
np.argmax(y_train[-2])
delta_x = []
delta_y = []
for i in range(len(x_train)):
  delta_x.append(x_train[i])
  delta = y_delta[i].flatten()
  coord = np.argmax(model.predict(x_train[i].reshape(1,7,7,512)/np.max(x_train)))
  delta_y.append(delta[(coord*4):((coord*4)+4)])

x_train.shape
from keras.optimizers import Adam
optimizer = Adam(lr=0.001)
from keras.layers import BatchNormalization
from keras import regularizers
model2 = Sequential()
model2.add(BatchNormalization(input_shape=(7,7,512)))
model2.add(Conv2D(1024, (3,3), activation='relu',padding='valid'))
model2.add(BatchNormalization())
model2.add(Conv2D(5, (1,1), activation='relu',padding='same'))
model2.add(Flatten())
model2.add(Dense(4, activation='linear'))
model2.summary()
delta_x = np.array(delta_x)
delta_x.shape
np.max(delta_x)
delta_y = np.array(delta_y)
delta_y.shape
max_y = np.max(delta_y, axis=0)
delta_y2 = delta_y/max_y
model2.compile(loss = 'mean_absolute_error', optimizer = optimizer)
model2.fit(delta_x/np.max(x_train), delta_y2, epochs = 10, batch_size = 32, verbose = 1, validation_split = 0.1)
import cv2
img = cv2.imread('/content/Hemanvi.jpg')
img = cv2.resize(img,(224,224))
img = img/255
img2 = vgg16_model.predict(img.reshape(1,224,224,3))
img2.shape
arg = np.argmax(model.predict(img2/np.max(x_train)))
pred = model2.predict(img2/np.max(delta_x))[0]
count = 0
for a in range(5):
  for b in range(5):
    for c in range(5):
      if(count == arg):
        print(a,b,c)
        a2 = a
        b2 = b
        c2 = c      
      count+=1
pred1 = pred*max_y
xmin = pred1[0]*224+a2*224/num_grids - (anchors[c2][0]*pred1[2] * 224)/2
ymin = pred1[1]*224+b2*224/num_grids - (anchors[c2][1]*pred1[3] * 224)/2

w = anchors[c2][0]*pred1[2] * 224
h = anchors[c2][1]*pred1[3] * 224
import matplotlib.patches as mpatches
cand = [xmin, ymin, w, h]
cand = np.clip(cand, 1, 223)

fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
ax.imshow(img)

rect = mpatches.Rectangle(
(cand[0], cand[1]), cand[2], cand[3], fill=False, edgecolor='red', linewidth=1)
ax.add_patch(rect)
plt.grid('off')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17655404806242794, 'fraction_numerical': 0.05409240046111555, 'mean_word_length': 3.7030859049207674, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 18}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '594647', 'license': 'MIT', 'n_tokens_mistral': 4492, 'n_tokens_neox': 4206, 'n_words': 685, 'path': 'Chapter06/Anchor_box_based_person_detection_(1).ipynb', 'repo_name': 'tongni1975/Neural-Networks-with-Keras-Cookbook', 'size': '11277'}