<jupyter_start><jupyter_text><jupyter_code>!pip install tensorflow-gpu
import numpy as np
from tqdm import tqdm
import cv2
import os
import zipfile
import shutil
from keras.models import Model
from keras.layers import Dense
from keras.layers import Flatten
from keras.applications.vgg16 import VGG16
from keras.applications.vgg19 import VGG19
import keras
import tensorflow as tf
tf.config.experimental.list_physical_devices('GPU')
zip_ref = zipfile.ZipFile('/content/PlantDoc cropped.zip', 'r')
zip_ref.extractall("/content")
zip_ref.close()
os.remove('/content/PlantDoc cropped/annotations/test/classes.txt')
shutil.move('/content/PlantDoc cropped/annotations/train/classes.txt','/content/classes.txt')
len(os.listdir('/content/PlantDoc cropped/annotations/train'))
train_img = '/content/PlantDoc cropped/train'
test_img = '/content/PlantDoc cropped/test'
train_anno = '/content/PlantDoc cropped/annotations/train'
test_anno = '/content/PlantDoc cropped/annotations/test'
def resize(dire,inp_shape):
  for i in tqdm(os.listdir(dire)):
    img = cv2.imread(os.path.join(dire,i))
    img = cv2.resize(img,inp_shape,interpolation = cv2.INTER_CUBIC)
    cv2.imwrite(os.path.join(dire,i),img)
def hor_flip_box(dire):
  for i in tqdm(os.listdir(dire)):
    k=[]
    file = open(os.path.join(dire,i),'r')
    file1 = open(os.path.join(dire,'lr__'+i),'w')    ##.split('.')[0]+'_lr'+'.txt'
    s = file.readlines()
    #print(i)
    #print('s:',s)
    k = s[0].split(' ') 
    #print('k:',k)
    k[1] = str(round(1-float(k[1]),6))
    file1.write(' '.join(k)) 
    file.close()
    file1.close()
def hor_flip(dire):
  for i in tqdm(os.listdir(dire)):
    img = cv2.imread(os.path.join(dire,i))
    imgc = cv2.flip(img,1) 
    cv2.imwrite(os.path.join(dire,'lr__'+i),imgc)
def ver_flip_box(dire):
  for i in tqdm(os.listdir(dire)):
    k=[]
    file = open(os.path.join(dire,i),'r')
    file1 = open(os.path.join(dire,'ud__'+i),'w')    ##.split('.')[0]+'_ud'+'.txt'
    s = file.readlines()
    #print(i)
    #print('s:',s)
    k = s[0].split(' ') 
    #print('k:',k)
    k[2] = str(round(1-float(k[2]),6))
    file1.write(' '.join(k)) 
    file.close()
    file1.close()
def ver_flip(dire):
  for i in tqdm(os.listdir(dire)):
    img = cv2.imread(os.path.join(dire,i))
    imgc = cv2.flip(img,0) 
    cv2.imwrite(os.path.join(dire,'ud__'+i),imgc)
ver_flip(train_img)
ver_flip_box(train_anno)
ver_flip(test_img)
ver_flip_box(test_anno)
hor_flip(train_img)
hor_flip_box(train_anno)
hor_flip(test_img)
hor_flip_box(test_anno)
len(os.listdir('/content/PlantDoc cropped/annotations/test'))
resize(train_img,(224,224))
resize(test_img,(224,224))
anntr = sorted(os.listdir(train_anno))
imgtr = sorted(os.listdir(train_img))
annte = sorted(os.listdir(test_anno))
imgte = sorted(os.listdir(test_img))
X_tr = []
X_te = []
y_tr_cl = []
y_tr_bx = []
y_te_cl = []
y_te_bx = []
for i in tqdm(imgtr):
   X_tr.append(cv2.imread(os.path.join(train_img,i)))
for i in tqdm(imgte):
   X_te.append(cv2.imread(os.path.join(test_img,i)))
for i in tqdm(anntr):
  k=[]
  file = open(os.path.join(train_anno,i),'r')   
  s = file.read().splitlines()
  k = list(map(lambda x:float(x),s[0].split(' ')))
  y_tr_cl.append(k[0:1])
  y_tr_bx.append(k[1:])
  file.close()
for i in tqdm(annte):
  k=[]
  file = open(os.path.join(test_anno,i),'r')   
  s = file.read().splitlines()
  k = list(map(lambda x:float(x),s[0].split(' ')))
  y_te_cl.append(k[0:1])
  y_te_bx.append(k[1:])
  file.close()  
x_tr = np.array(X_tr)
x_te = np.array(X_te)
x_tr = x_tr/255.0
x_te = x_te/255.0
y_tr_cl = np.array(y_tr_cl)
y_tr_bx = np.array(y_tr_bx)
y_te_cl = np.array(y_te_cl)
y_te_bx = np.array(y_te_bx)
model = VGG19(include_top=False,classes=4,weights=None,input_shape=(224,224,3))
model.summary()
model = VGG16(classes=4,weights=None,input_shape=(224,224,3),include_top=False)
model.summary()
model.outputs[0]
flat1 = Flatten()(model.outputs[0])
dense1 = Dense(4096, activation='relu')(flat1)
dense2 = Dense(4096, activation='relu')(dense1)
class_out = Dense(2,activation='softmax',name="class_output")(dense2)
box_out = Dense(4,name="box_output")(dense2)
model = Model(inputs=model.inputs, outputs=[class_out,box_out])
tf.keras.utils.plot_model(model, "multi_input_and_output_model.png", show_shapes=True)
model.compile(
    optimizer= keras.optimizers.RMSprop(1e-3),
    loss={
        "box_output": keras.losses.MeanSquaredError(),
        "class_output": keras.losses.SparseCategoricalCrossentropy(),
    },
    metrics={
        "box_output": [
            keras.metrics.MeanAbsoluteError()
        ],
        "class_output": [keras.metrics.CategoricalAccuracy()],
    },
)
history = model.fit(x_tr, y_tr_bx, batch_size=4, epochs=15,validation_data=(x_te, y_te_bx))
history.history['box_output_loss']
history.history['val_box_output_loss']
import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 12))
ax1.plot(history.history['box_output_loss'], color='b', label="Training loss")
ax1.plot(history.history['val_box_output_loss'], color='r', label="validation loss")
ax1.set_xticks(np.arange(1, 4, 1))
ax1.set_yticks(np.arange(0, 0.01, 0.005))
 
#ax2.plot(history.history['accuracy'], color='b', label="Training accuracy")
#ax2.plot(history.history['val_accuracy'], color='r',label="Validation accuracy")
#ax2.set_xticks(np.arange(1, 15, 1))
 
#legend = plt.legend(loc='best', shadow=True)
plt.tight_layout()
plt.show()<jupyter_output><empty_output><jupyter_text>**testing**<jupyter_code>#os.mkdir('/content/testimg')
shutil.move('/content/pot1.jpeg','/content/testimg/pot1.jpeg')
os.remove('/content/testimg/pot2.jpg')
os.listdir('/content/testimg')
resize('/content/testimg',(224,224))
xtest=[]
for i in tqdm(os.listdir('/content/testimg')):
  img = cv2.imread(os.path.join('/content/testimg',i))
  xtest.append(img)
xtest = np.array(xtest)/255.0
 ypr = model.predict(xtest,batch_size=1)
ypr[1]
y_tr_bx
import matplotlib.pyplot as plt
img = cv2.imread('/content/pot1.jpeg')
plt.imshow(img)
print(img.shape)
l = ypr[1][0]
l
x1 = int(round((l[0]-l[2]/2)*img.shape[1]))
x2 = int(round((l[0]+l[2]/2)*img.shape[1]))
y1 = int(round((l[1]-l[3]/2)*img.shape[0]))
y2 = int(round((l[1]+l[3]/2)*img.shape[0]))
x2
cv2.rectangle(img,(x1,y1),(x2,y2),(256,256,256),1)
plt.imshow(img)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19244862195316234, 'fraction_numerical': 0.03170304285486698, 'mean_word_length': 5.472164948453608, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '281175', 'license': 'MIT', 'n_tokens_mistral': 2753, 'n_tokens_neox': 2654, 'n_words': 362, 'path': 'Models/leaf_localisation_model.ipynb', 'repo_name': 'vkk1710/SOS---Leaf-Localization', 'size': '6277'}