<jupyter_start><jupyter_text><jupyter_code>import tensorflow as tf
import numpy as np
from tensorflow.keras.models import *
from tensorflow.keras.layers import *

import cv2
import tensorflow.keras.backend as K
import matplotlib.pyplot as plt
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.applications.inception_v3 import InceptionV3


def getrect(img):
  ss=cv2.ximgproc.segmentation.createSelectiveSearchSegmentation()
  ss.setBaseImage(img)
  ss.switchToSelectiveSearchFast()
  rects=ss.process()
  ans=[]
  for i in range(min(2000,len(rects))):
    x,y,w,h=rects[i]
    if w>=0.35*np.shape(img)[1] and h>=0.3*np.shape(img)[0]:
      ans.append([x,y,w,h])
  return ans
inp=Input(shape=(224,224,3))
res=ResNet50(include_top=False,weights='imagenet',input_tensor=Input(shape=(224,224,3)))
x=res.output
x = GlobalAveragePooling2D()(x)
#x=Flatten()(x)
#print(x)
x=Dense(4,activation='softmax')(x)
model=Model(res.input,x)
!git clone https://github.com/EscVM/OIDv4_ToolKit.git
!pip3 install -r /content/OIDv4_ToolKit/requirements.txt
!python /content/OIDv4_ToolKit/main.py downloader --classes Tiger --type_csv train --limit 100
!python /content/OIDv4_ToolKit/main.py downloader --classes Deer --type_csv train --limit 100
!python /content/OIDv4_ToolKit/main.py downloader --classes Lion --type_csv train --limit 100
!python /content/OIDv4_ToolKit/main.py downloader --classes Zebra --type_csv train --limit 100
import glob
x=[]
y=[]
path='OID/Dataset/train/'
for i in ['Lion','Tiger','Deer','Zebra']:
  for file in glob.glob(path+i+'/*.jpg'):
    im=cv2.imread(file)
    im=cv2.resize(im,(224,224))
    im=cv2.cvtColor(im,cv2.COLOR_BGR2RGB)
    im=np.array(im)
    if i == 'Lion':
      label=np.array([1,0,0,0])
    if i == 'Tiger':
      label=np.array([0,1,0,0])
    if i == 'Deer':
      label=np.array([0,0,1,0])
    if i == 'Zebra':
      label=np.array([0,0,0,1])
    x.append(im)
    y.append(label)
x=np.array(x)
y=np.array(y)
res.trainable=False
model.compile(loss='categorical_crossentropy')
print(np.shape(x))
print(np.shape(y))
history=model.fit(x=x,y=y,batch_size=20,epochs=100)
plt.plot(np.arange(1,101),history.history['loss'],label='loss')
#plt.plot(np.arange(1,101),history.history['accuracy'],label='accuracy')
plt.legend()
plt.show()
def pred(img):
  rects=getrect(img)
  ans=[]
  for rect in rects:
    a,b,c,d=rect 
    tempimage=img[b:b+d,a:a+c]
    tempimage=cv2.resize(tempimage,(224,224))
    tempimage=np.reshape(tempimage,(1,224,224,3))
    result=model.predict(tempimage)[0]
    ind=np.argmax(result)
    if result[ind]>0.8:
      ans.append([ind,result[ind],a,b,c,d])
  return ans


testim=cv2.imread('OID/Dataset/train/Tiger/08d1b88885af1c8e.jpg')
testim=cv2.cvtColor(testim,cv2.COLOR_BGR2RGB)
boxes=pred(testim)
from tensorflow.image import  *
tempboxes=[i[2:] for i in boxes]
scores=[i[1] for i in boxes]
boxind=non_max_suppression(tempboxes,scores,1).numpy()
boxind=np.reshape(boxind,(1))
boxind=np.array(boxind,dtype=np.int)
dic={0:'Lion',1:'Tiger',2:'Deer',3:'Zebra'}
for i in boxind:
  ind=int(i)
  a,b,c,d=boxes[ind][2:]
  out=cv2.rectangle(testim,(a,b),(a+c,b+d),(0,255,0),(2))
  cv2.putText(out,dic[lab],(a,b-10),cv2.FONT_HERSHEY_SIMPLEX,0.9,(36,255,12),2)

plt.imshow(out)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18303435694740042, 'fraction_numerical': 0.04803891760413499, 'mean_word_length': 5.67342799188641, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '552841', 'license': 'MIT', 'n_tokens_mistral': 1475, 'n_tokens_neox': 1386, 'n_words': 198, 'path': 'other algorithms/R_CNN.ipynb', 'repo_name': 'akshay-121/SURP-Animal-species-detection-from-videos', 'size': '3289'}