Here, i trying to build a Manhole object detection using cnn , In this model i got 95% accuracy and after training. im getting false positive like , if i test image with manhole(trained object) for detection it will draw the bounding box and i test a random image without trained object a random bounding box appears that is the problem , same in the live webcam testing but here if the object is not even detecting , getting some random bounding boxes in frame .Here i providing my code pls help
import pandas as pd
import cv2
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.layers import Conv2D, Input, BatchNormalization``, Flatten, MaxPool2D, Dense
from pathlib import Path
train_img = Path("DATASET/train/Manhole")
val_img = Path("DATASETvalidManhole")
train_csv = pd.read_csv('DATASET/train/Manhole/_annotations.csv')
val_csv = pd.read_csv('DATASET/valid/_annotations.csv')
#print(train_csv)
train_csv[['xmin', 'ymin', 'xmax', 'ymax']] = train_csv[['xmin', 'ymin', 'xmax', 'ymax']].fillna(0)
train_csv[['xmin','ymin','xmax','ymax']] = train_csv[['xmin','ymin','xmax','ymax']].astype(int)
train_csv.drop_duplicates(subset='filename',inplace=True, ignore_index=True)
val_csv.drop_duplicates(subset='filename', inplace=True, ignore_index=True)
def datagenerator(df ,batch_size ,path):
while True:
images = np.zeros((batch_size,640,640,3))
bounding_box_coords = np.zeros((batch_size, 4))
for i in range(batch_size):
rand_index = np.random.randint(0, train_csv.shape[0])
row = df.loc[rand_index, :]
images[i] = cv2.imread(str(path/row.filename)) / 255.
bounding_box_coords[i] = np.array([row.xmin, row.ymin, row.xmax, row.ymax])
yield {'filename': images}, {'coords': bounding_box_coords}
# example, label = next(datagenerator(batch_size=16))
# img = example['filename'][0]
# bbox_coords = label['coords'][0]
# x1, y1, x2, y2 = map(int, bbox_coords)
# print('bbox cords',x1,y1,x2,y2)
# cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 3)
# cv2.putText(img, '', (x1,y1-10),cv2.FONT_HERSHEY_DUPLEX, 0.8, (0, 0, 255), 2)
# # plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# plt.imshow(img)
# plt.show()
input_ = Input(shape=[640, 640, 3], name='filename')
x = input_
x = Conv2D(16, (3,3), activation='relu', padding='same')(x)
x = BatchNormalization()(x)
x = MaxPool2D(2, padding='same')(x)
x = Conv2D(32, (3,3), activation='relu', padding='same')(x)
x = BatchNormalization()(x)
x = MaxPool2D(2, padding='same')(x)
x = Conv2D(64, (3,3), activation='relu', padding='same')(x)
x = BatchNormalization()(x)
x = MaxPool2D(2, padding='same')(x)
x = Conv2D(128, (3,3), activation='relu', padding='same')(x)
x = BatchNormalization()(x)
x = MaxPool2D(2, padding='same')(x)
x = Conv2D(256, (3,3), activation='relu', padding='same')(x)
x = BatchNormalization()(x)
x = MaxPool2D(2, padding='same')(x)
x = Conv2D(312, (3,3), activation='relu', padding='same')(x)
x = BatchNormalization()(x)
x = MaxPool2D(2, padding='same')(x)
x = Conv2D(500, (3,3), activation='relu', padding='same')(x)
x = BatchNormalization()(x)
x = MaxPool2D(2, padding='same')(x)
x = Conv2D(580, (3,3), activation='relu', padding='same')(x)
x = BatchNormalization()(x)
x = MaxPool2D(2, padding='same')(x)
x = Conv2D(680, (3,3), activation='relu', padding='same')(x)
x = BatchNormalization()(x)
x = MaxPool2D(2, padding='same')(x)
x = Flatten()(x)
x = Dense(256, activation='relu')(x)
x = Dense(32, activation='relu')(x)
output_coords = Dense(4, name='coords')(x)
model = tf.keras.models.Model(input_,output_coords)
model.summary()
model.compile(loss={'coords': 'mse'},
optimizer=tf.keras.optimizers.Adam(5e-5),
metrics={'coords': 'accuracy'})
checkpoint_callback = ModelCheckpoint('model_Checkpoint.h5', monitor='val_loss', save_best_only=True, mode='min')
model.fit(datagenerator(df=train_csv,batch_size=6,path=train_img),
epochs=80, steps_per_epoch=150,
validation_data=datagenerator(df=val_csv,batch_size=6,path=val_img),
validation_steps=240,
callbacks=[checkpoint_callback])
model.save('model2.h5')
i need the code to detect the trained object properly in the live webcam without appears any bounding boxes and receive confidence value from the cnn so i can set threshold values for detect
FAYAS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.