ResourceExhaustedError: OOM error when training detection Model

Hi, my name is choi eo jin from south korea.

I am using your code now :slight_smile:

train-code :

from imageai.Detection.Custom import DetectionModelTrainer

trainer = DetectionModelTrainer()
trainer.setTrainConfig(object_names_array=[“Kim”], batch_size=4, num_experiments=200, train_from_pretrained_model=“pretrained-yolov3.h5”)

But when i was running train-code,
I got this error message:

Traceback (most recent call last):
File “”, line 1, in
File “D:\pycharm\PyCharm Community Edition 2019.2.1\helpers\pydev_pydev_bundle\”, line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File “D:\pycharm\PyCharm Community Edition 2019.2.1\helpers\”, line 18, in execfile
exec(compile(contents+"\n", file, ‘exec’), glob, loc)
File “C:/Users/User/PycharmProjects/CAPSTONE/”, line 10, in
File “C:\Users\User\PycharmProjects\CAPSTONE\venv\lib\site-packages\imageai\Detection\Custom_ init”, line 301, in trainModel
File “C:\Users\User\PycharmProjects\CAPSTONE\venv\lib\site-packages\keras\legacy\”, line 91, in wrapper
return func(*args, **kwargs)
File “C:\Users\User\PycharmProjects\CAPSTONE\venv\lib\site-packages\keras\engine\”, line 1658, in fit_generator
File “C:\Users\User\PycharmProjects\CAPSTONE\venv\lib\site-packages\keras\engine\”, line 215, in fit_generator
File “C:\Users\User\PycharmProjects\CAPSTONE\venv\lib\site-packages\keras\engine\”, line 1449, in train_on_batch
outputs = self.train_function(ins)
File “C:\Users\User\PycharmProjects\CAPSTONE\venv\lib\site-packages\keras\backend\”, line 2979, in call
return self._call(inputs)
File “C:\Users\User\PycharmProjects\CAPSTONE\venv\lib\site-packages\keras\backend\”, line 2937, in _call
fetched = self._callable_fn(*array_vals)
File “C:\Users\User\PycharmProjects\CAPSTONE\venv\lib\site-packages\tensorflow\python\client\”, line 1439, in call
File “C:\Users\User\PycharmProjects\CAPSTONE\venv\lib\site-packages\tensorflow\python\framework\”, line 528, in exit
tensorflow.python.framework.errors_impl.ResourceExhaustedError: OOM when allocating tensor with shape[2,26,26,512] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
[[{{node replica_1/model_1/leaky_42/LeakyRelu}}]]
Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.
[[{{node replica_0/model_1/bnorm_62/FusedBatchNorm}}]]
Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.

In this situation, how can i fix it…?

Thank you.

@chldjwls123 What type of GPU does your machine have? You are experiencing this issue because the batch_size=4 stated in your code exceeds the capacity of your GPU memory as you can see in the error line tensorflow.python.framework.errors_impl.ResourceExhaustedError:

Change the batch_size to 2 and let me know if the problem persists.


Thanks for all your help :slight_smile:

Actually, i have another question about live-stream video using a web-cam.

If i want to use a custom version, should i just use a below code? or change something?

from imageai.Detection import VideoObjectDetection
import os
import cv2
execution_path = os.getcwd()

camera = cv2.VideoCapture(0)

detector = VideoObjectDetection()
detector.setModelPath(os.path.join(execution_path , “resnet50_coco_best_v2.0.1.h5”))

video_path = detector.detectObjectsFromVideo(
output_file_path=os.path.join(execution_path, “camera_detected_video”),
frames_per_second=20, log_progress=True, minimum_percentage_probability=40)

For detecting and analyzing objects in video using your custom detection model, you will use the
CustomVideoObjectDetection class from

from imageai.Detection.Custom import CustomVideoObjectDetection.

See the link below for full documentation and sample code.