Tensorflow Object Detection API Detects Old Classes as My Custom Object - tensorflow

I am triying to create a custom object detector with Tensorflow Object Detection API. I followed the API documentation,and many other tutorials. I am using pre-trained faster rcnn model and default config file values (i edited "PATH TO BE CONFİGURED" areas correctly)(my label map has only one item and its id is 1). After training with 6660 training examples, results are not perfect but its okay for my test images. The main problem is when i test the model with different images that doesn't contain my custom object, it detects other objects as my custom object. Especially, if the object is in coco dataset(my model pre-trained on coco dataset) like car, knife, laptop etc. model detecting their location and tagging them as my custom object. Is this a bug or am i missing something ?
My config file, label map,test code and files ı used for creating tf records are in here : https://github.com/mstferis/tensorflow_object_detection
total loss graphic in tensorboard
with this model i take these outputs. It detects handguns (thats what i want) but it also detects other objects as handgun.
false example 1
false example 2
false example 3
true example 1
true example 2
*Note (Update): Finally I added pictures of other classes to my training set and labeled them just as my custom object. Now model predicts less false positives. But i still couldn't find what was wrong before.

Related

Feed image data without class label

I am trying to implement image super resolution using SRGAN. In the process, I used DIV2K dataset (http://data.vision.ee.ethz.ch/cvl/DIV2K/DIV2K_train_HR.zip) as my source.
I have worked with image classification using CNN (I used keras.layers.convolutional.Conv2D). But in this case we don't have class label in my data source.
I have unzipped the file and kept in D:\Unzipped\DIV2K_train_HR. Then used following command to read the files.
img_dataset = tensorflow.keras.utils.image_dataset_from_directory("D:\\unzipped")
Then created the model as follows
model = Sequential()
model.add(Conv2D(filters=64,kernel_size=(3,3),activation="relu",input_shape=(256,256,3)))
model.add(AveragePooling2D(pool_size=(2,2)))
model.add(Conv2D(filters=64,kernel_size=(3,3),activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
model.compile(optimizer='sgd', loss='mse')
model.fit(img_dataset,batch_size=32, epochs=10)
But I am getting error : "Graph execution error". I am unable to find the root cause behind this error. Is this error appearing as the class label is missing (I think as per code DIV2K_train_HR is treated as one class label)? Or is this happening due to images don't have one specific size?
Note: This code does not match with SRGAN architecture. I am new to GAN and trying to move ahead step by step. I got stuck in the first step itself.
Yes, the error message is because you don't have labels in your dataset.
As a first step in GAN network you need to create a discriminator model: given some image it should recognize if it is a real or fake image. You can take images from your dataset and label them as 1 ("real images"). Then generate "fake images" by down-sampling and up-sampling images from your dataset and label them as 0. Train your discriminator model so that it can distinguish between original and processed images.
After that, you create generator model. The generator model takes a down-sampled version of the image as an input and creates an up-sampled version in original resolution. GAN model combines generator and discriminator models by passing output from generator to discriminator. The target label is 1, i.e. we want generator create up-sampled versions of images, which discriminator can't distinguish from the real ones. Now train GAN network (set 'trainable' to false for discriminator model weights).
After your generator manages to produce images, which discriminator can't distinguish from the real, you take them, label as 0 and train discriminator again. Then train generator again etc.
The process continues until discriminator can't distinguish fake images from the real ones anymore (i.e. accuracy doesn't exceed 0.5).
Please see a simple example on ("Generative Adversarial Networks"):
https://github.com/ageron/handson-ml3/blob/main/17_autoencoders_gans_and_diffusion_models.ipynb
This code is explained in ch. 17 in book "Hands-on Machine Learning with Scikit-Learn, Keras and TensorFlow (3rd edition)" by Aurélien Géron.

Use Tensorflow2 saved model for object detection

im quite new to object detection but i managed to train my first Tensorflow custom model yesterday. I think it worked fine besides some warnings, at least i got my exported_model folder with checkpoint, saved model and pipeline.config. I built it with exporter_main_v2.py from Tensorflow. I just loaded some images of deers and want to try to detect some on different pictures.
That's what i would like to test now, but i dont know how. I already did an object detection tutorial with pre trained models and it worked fine. I tried to just replace config_file_path, saved_model_path and image_path with the paths linking to my exported model but it didnt work:
error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\tensorflow\tf_io.cpp:42: error: (-2:Unspecified error) FAILED: ReadProtoFromBinaryFile(param_file, param). Failed to parse GraphDef file: D:\VSCode\Machine_Learning_Tests\Tensorflow\workspace\exported_models\first_model\saved_model\saved_model.pb in function 'cv::dnn::ReadTFNetParamsFromBinaryFileOrDie'
There are endless tutorials on how to train custom detection but i cant find a good explanation how to manually test my exported model.
Thanks in advance!
EDIT: I need to know how to build a script where i can import a model i saved with Tensorflow exporter_main_v2.py and an image i want to test this model on and get a result, either in text or with rectangels in picture. Seeing many tutorials but none works for me with a model i saved with Tensorflow exporter_main_v2.py
From the error it looks like you have a model saved as .pb. If you want to do inference you can write something like this:
# load the model
model = tf.keras.models.load_model(my_model_dir)
prediction = model.predict(x=x_test, ...)
You'll have to set x which is the only mandatory argument. It is your test dataset (the images you want to obtain predictions from). Also, predict is useful when you have a great amount of images to predict. It handles the prediction in a batched way, avoiding filling up the memory. If you have just a few you can use directly the __call__() method of your model, like this:
prediction = model(x_test, training=False)
More about prediction can be found at the Tensorflow documentation.

Tensorflow object detection api mis classifying objects

I followed a simple tutorial to train a custom object detector.
I got my loss up to 0.6, however my issue is that the detected will classify other objects as what I've trained it with. For example in my case it classifies a dog as macarooni and cheese.
What am I doing wrong ?
I faced exactly the same issue, where the model "remembered" the previous objects. There is a new configuration in the config file that is was not implemented when the video was made.
Inside the ssd_mobilenet_v1_pet.config file you have to specify the path to the checkpoint where the training will start, so it will have all the weights from the previous training, this config is fine_tune_checkpoint, below that there is from_detection_checkpoint so it will use the specified checkpoint, after that there is load_all_detection_checkpoint_vars which is set to true by default, but must be false if you want the model to "forget" the objects that it was trained on.
The problem is that load_all_detection_checkpoint_vars will load and fix all the weights, including the ones in the final layers not just the lower layer ones, so it will remember the classification and detection from past objects and misclassify with the new ones, since your *.pbtxt has different classifications. If you set it to false it will load the data and learn new weights for the final layers based only on your training set.

mAP decreases on re-training tensorflow object detection

I tried to train the tensorflow object detection API on the pascalVOC dataset (for all 20 classes) using the faster_rcnn_resnet101 (trained on coco dataset with 91 classes) model as base model (provided in the repo). I used the config provided in the repo.
The training generated several .ckpt files at several iteration points including the ckpt files at iteration 0 (on which the training has no effect).
Then I performed the evaluation on the .ckpt files using the pascalVOC validation data for all 20 classes.
Important note: the 20 pascalVOC categories are actually a subset of the 91 Coco categories.
My evaluation results show that the performance for the 20 classes goes down by 15% and more for all iterations in the training. As per the config I ran the training for 200K iterations and kept all hyper-parameters the same as in the original graph.
My question is: Can anyone explain what is wrong here? Why does the performance (mAP) go down even though the pascalVOC categories are a subset of the Coco categories.
Also how can I ensure that the performance for these 20 classes do not go down even after subsequent training on datasets belonging to this class.
Or else please let me know if I am missing something.

can not detect the correct class when using Tensorflow object detection API

I am using tensorflow object detection API to train and detect my dataset. This dataset has 5 classes(every class has 50 images), and it contains two very similar classes(red and black). After the train process, I detected the test images and found that, the model always detects a target of red class as a target of black class, the other classes is detected as correct class.
I trained the model with faster_rcnn_resnet101_breads.congfig, and using fine_tune_checkpoint.I set the learning_rate to 0.003(the original is 0.0003).
Can you tell me what is wrong with my model, and how much the learning_rate should I set?
The compare result of my config file and samples config file:compare result
train curves:train curves
> black class: https://i.stack.imgur.com/eWrlK.jpg
> red class: https://i.stack.imgur.com/TuRjg.jpg