deepface ResourceExhaustedError: failed to allocate memory [Op:AddV2] - python

I am new to deeplearning.
I am trying to use the deepface library in my local machine. I used pip install deepface to install the library, tried on python 3.7.13, 3.8.13 and 3.9.13 which were all created using conda virtual environment.
However when running the code snippet below, I am getting the same error when running on my local machine. Do I need a GPU to run the library? If yes, how do I set it up? Because from the online guides/ articles, none of them mentioned the need of installing / setup a GPU.
I have a GeForce MX450 on my local pc.
code
import cv2
from deepface import DeepFace
import numpy as np
def analyse_face():
imagepath = "happy_face_woman.png"
image = cv2.imread(imagepath)
face_analysis = DeepFace.analyze(image)
print(face_analysis)
print(analyse_face())
Error:
ResourceExhaustedError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_14196\3829791526.py in <module>
12 print(face_analysis)
13
---> 14 analyse_face()
~\AppData\Local\Temp\ipykernel_14196\3829791526.py in analyse_face()
9 imagepath = "happy_face_woman.png"
10 image = cv2.imread(imagepath)
---> 11 face_analysis = DeepFace.analyze(image)
12 print(face_analysis)
13
c:\Users\user_name\anaconda3\envs\deepFacepy37\lib\site-packages\deepface\DeepFace.py in analyze(img_path, actions, models, enforce_detection, detector_backend, prog_bar)
352
353 if 'age' in actions and 'age' not in built_models:
--> 354 models['age'] = build_model('Age')
355
356 if 'gender' in actions and 'gender' not in built_models:
c:\Users\user_name\anaconda3\envs\deepFacepy37\lib\site-packages\deepface\DeepFace.py in build_model(model_name)
61 model = models.get(model_name)
62 if model:
---> 63 model = model()
...
-> 1922 seed=self.make_legacy_seed())
1923
1924 def truncated_normal(self, shape, mean=0., stddev=1., dtype=None):
ResourceExhaustedError: failed to allocate memory [Op:AddV2]
Different Error output
ResourceExhaustedError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_14196\3829791526.py in <module>
12 print(face_analysis)
13
---> 14 analyse_face()
~\AppData\Local\Temp\ipykernel_14196\3829791526.py in analyse_face()
9 imagepath = "happy_face_woman.png"
10 image = cv2.imread(imagepath)
---> 11 face_analysis = DeepFace.analyze(image)
12 print(face_analysis)
13
c:\Users\user_name\anaconda3\envs\deepFacepy37\lib\site-packages\deepface\DeepFace.py in analyze(img_path, actions, models, enforce_detection, detector_backend, prog_bar)
352
353 if 'age' in actions and 'age' not in built_models:
--> 354 models['age'] = build_model('Age')
355
356 if 'gender' in actions and 'gender' not in built_models:
c:\Users\user_name\anaconda3\envs\deepFacepy37\lib\site-packages\deepface\DeepFace.py in build_model(model_name)
61 model = models.get(model_name)
62 if model:
---> 63 model = model()
...
-> 1922 seed=self.make_legacy_seed())
1923
1924 def truncated_normal(self, shape, mean=0., stddev=1., dtype=None):
ResourceExhaustedError: OOM when allocating tensor with shape[7,7,512,4096] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc [Op:RandomUniform]
Additional Info
I've ran the command to check my GPU usage and the details is as follows:
!nvidia-smi

Why do not you disable GPU?
import os
os.environ["CUDA_VISIBLE_DEVICES"]=""

Related

error with unknown path on jupyter notebook

I am running a code on jupyter notebook using virtual machine (Ubuntu). Normally when there is an error it points to the file where the error is giving its path but I got an error with a path I do not have
ValueError Traceback (most recent call last)
/tmp/ipykernel_84/10827517.py in <module>
----> 1 vae_cl.train(max_epochs=500, plan_kwargs=dict(weight_decay=0.0))
/tmp/ipykernel_84/1274271952.py in train(self, max_epochs, n_samples_per_label, check_val_every_n_epoch, train_size, validation_size, batch_size, use_gpu, plan_kwargs, **trainer_kwargs)
187 use_gpu=use_gpu,
188 check_val_every_n_epoch=check_val_every_n_epoch,
--> 189 **trainer_kwargs,
190 )
191 return runner()
/tmp/ipykernel_84/2912366039.py in __init__(self, model, training_plan, data_splitter, max_epochs, use_gpu, **trainer_kwargs)
59 self.data_splitter = data_splitter
60 self.model = model
---> 61 gpus, device = parse_use_gpu_arg(use_gpu)
62 self.gpus = gpus
63 self.device = device
ValueError: too many values to unpack (expected 2)
What I noticed is that the function it is pointing to (---> 61) is actually in the file I am running but I do not understand why the path is like that '/tmp/ipykernel_84/1274271952.py'
Any idea?

Tensorflow VGG16 Attribute Error: module tensorflow has no attribute get_default_graph? [duplicate]

I am doing some task related to image captioning and I have loaded the weights of inception model like this
model = InceptionV3(weights='imagenet')
But I am getting error like this:
AttributeError: module 'tensorflow' has no attribute 'get_default_graph'
What should I do? Please help.
Here is the full output of above code.
1 . --------------------------------------------------------------------------- AttributeError Traceback (most recent
call last) in ()
1 # Load the inception v3 model
----> 2 model = InceptionV3(include_top=True,weights='imagenet')
3 # InceptionV3(weights='imagenet')
~/anaconda3/lib/python3.6/site-packages/keras/applications/__init__.py
in wrapper(*args, **kwargs)
26 kwargs['models'] = models
27 kwargs['utils'] = utils
---> 28 return base_fun(*args, **kwargs)
29
30 return wrapper
~/anaconda3/lib/python3.6/site-packages/keras/applications/inception_v3.py
in InceptionV3(*args, **kwargs)
9 #keras_modules_injection
10 def InceptionV3(*args, **kwargs):
---> 11 return inception_v3.InceptionV3(*args, **kwargs)
12
13
~/anaconda3/lib/python3.6/site-packages/keras_applications/inception_v3.py
in InceptionV3(include_top, weights, input_tensor, input_shape,
pooling, classes, **kwargs)
155
156 if input_tensor is None:
--> 157 img_input = layers.Input(shape=input_shape)
158 else:
159 if not backend.is_keras_tensor(input_tensor):
~/anaconda3/lib/python3.6/site-packages/keras/engine/input_layer.py
in Input(shape, batch_shape, name, dtype, sparse, tensor)
176 name=name, dtype=dtype,
177 sparse=sparse,
--> 178 input_tensor=tensor)
179 # Return tensor including _keras_shape and _keras_history.
180 # Note that in this case train_output and test_output are the same pointer.
~/anaconda3/lib/python3.6/site-packages/keras/legacy/interfaces.py
in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name + '` call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
~/anaconda3/lib/python3.6/site-packages/keras/engine/input_layer.py
in __init__(self, input_shape, batch_size, batch_input_shape, dtype,
input_tensor, sparse, name)
37 if not name:
38 prefix = 'input'
---> 39 name = prefix + '_' + str(K.get_uid(prefix))
40 super(InputLayer, self).__init__(dtype=dtype, name=name)
41
~/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py
in get_uid(prefix)
72 """
73 global _GRAPH_UID_DICTS
---> 74 graph = tf.get_default_graph()
75 if graph not in _GRAPH_UID_DICTS:
76 _GRAPH_UID_DICTS[graph] = defaultdict(int)
AttributeError: module 'tensorflow' has no attribute
'get_default_graph'
Change
Import keras.<something>.<something>
to
Import tensorflow.keras.<something>.<something>
where "something" refers to the module you want to import. It worked for me.
Another cause due to which this is happening is that in tensorflow_backend.py
located in : lib/python3.6/site-packages/keras/backend/
uses tf.compat.v1.get_default_graph for obtaining graph
instead of tf.get_default_graph.
By replacing this in the directory this problem can be solved successfully.
Keras integrated into TensorFlow 2.0
The article below cleared this up for me. Key points are:
Keras is included in the TensorFlow 2.0 package
So no need to install the stand-alone Keras package in your environment
And now the fore-mentioned solutions of using "from tensorflow.keras…" make sense.
After recognizing that and making the change, my code samples work with some minor changes here and there.
https://www.pyimagesearch.com/2019/10/21/keras-vs-tf-keras-whats-the-difference-in-tensorflow-2-0/
I fixed this problem by replacing tensorflow.keras.* to tensorflow.python.keras.*
Working example:
from tensorflow.python.keras.models import Sequential
Two Steps solved this issue for me in Google Colabs
First Step
Change
from keras.something import something
To
from tensorflow.keras.something import something
Second Step
Use
tf.compat.v1.get_default_graph
instead of tf.get_default_graph
Reason
https://www.pyimagesearch.com/2019/10/21/keras-vs-tf-keras-whats-the-difference-in-tensorflow-2-0/
In my case, replacing
from keras.models import models
with:
from tensorflow.keras.models import models
in my script, fixed this problem.

Can't convert Core ML model to Onnx (then to Tensorflow Lite)

I'm trying to convert a trained Core ML model to TensorFlow Lite. I find I need convert it to Onnx first.
The problems is that I get errors. I've tried with different versions of python, onnxmltools, winmltools and it doesn't seems to work. I also tried docker image of onnx ecosystem with same result. Can any one help me with it? Thanks in advance.
Script I used:
import coremltools
import onnxmltools
input_coreml_model = '../model.mlmodel'
output_onnx_model = '../model.onnx'
coreml_model = coremltools.utils.load_spec(input_coreml_model)
onnx_model = onnxmltools.convert_coreml(coreml_model)
onnxmltools.utils.save_model(onnx_model, output_onnx_model)
IndexError Traceback (most recent call last)
<ipython-input-11-94a6dc527869> in <module>
3
4 # Convert the CoreML model into ONNX
----> 5 onnx_model = onnxmltools.convert_coreml(coreml_model)
6
7 # Save as protobuf
/usr/local/lib/python3.6/dist-packages/onnxmltools/convert/main.py in convert_coreml(model, name, initial_types, doc_string, target_opset, targeted_onnx, custom_conversion_functions, custom_shape_calculators)
16 from .coreml.convert import convert
17 return convert(model, name, initial_types, doc_string, target_opset, targeted_onnx,
---> 18 custom_conversion_functions, custom_shape_calculators)
19
20
/usr/local/lib/python3.6/dist-packages/onnxmltools/convert/coreml/convert.py in convert(model, name, initial_types, doc_string, target_opset, targeted_onnx, custom_conversion_functions, custom_shape_calculators)
58 target_opset = target_opset if target_opset else get_opset_number_from_onnx()
59 # Parse CoreML model as our internal data structure (i.e., Topology)
---> 60 topology = parse_coreml(spec, initial_types, target_opset, custom_conversion_functions, custom_shape_calculators)
61
62 # Parse CoreML description, author, and license. Those information will be attached to the final ONNX model.
/usr/local/lib/python3.6/dist-packages/onnxmltools/convert/coreml/_parse.py in parse_coreml(model, initial_types, target_opset, custom_conversion_functions, custom_shape_calculators)
465 # Instead of using CoremlModelContainer, we directly pass the model in because _parse_model is CoreML-specific.
466 _parse_model(topology, scope, model)
--> 467 topology.compile()
468
469 for variable in topology.find_root_and_sink_variables():
/usr/local/lib/python3.6/dist-packages/onnxconverter_common/topology.py in compile(self)
630 self._resolve_duplicates()
631 self._fix_shapes()
--> 632 self._infer_all_types()
633 self._check_structure()
634
/usr/local/lib/python3.6/dist-packages/onnxconverter_common/topology.py in _infer_all_types(self)
506 pass # in Keras converter, the shape calculator can be optional.
507 else:
--> 508 operator.infer_types()
509
510 def _resolve_duplicates(self):
/usr/local/lib/python3.6/dist-packages/onnxconverter_common/topology.py in infer_types(self)
108 def infer_types(self):
109 # Invoke a core inference function
--> 110 registration.get_shape_calculator(self.type)(self)
111
112
/usr/local/lib/python3.6/dist-packages/onnxmltools/convert/coreml/shape_calculators/neural_network/Concat.py in calculate_concat_output_shapes(operator)
22 if variable.type.shape[0] != 'None' and variable.type.shape[0] != output_shape[0]:
23 raise RuntimeError('Only dimensions along C-axis can be different')
---> 24 if variable.type.shape[2] != 'None' and variable.type.shape[2] != output_shape[2]:
25 raise RuntimeError('Only dimensions along C-axis can be different')
26 if variable.type.shape[3] != 'None' and variable.type.shape[3] != output_shape[3]:
IndexError: list index out of range

AttributeError: module 'tensorflow' has no attribute 'io'

My problem is when i try to run this code
if log_to_tensorboard: from torch.utils.tensorboard import SummaryWriter
if log_to_tensorboard: writer = SummaryWriter()
I get this error:
(import SummaryWriter works without any problems, but then I try run "writer = SummaryWriter()" and it doesnt work)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-23-d77d9d09e62b> in <module>
----> 1 writer = SummaryWriter()
/anaconda3/lib/python3.6/site-packages/torch/utils/tensorboard/writer.py in __init__(self, log_dir, comment, purge_step, max_queue, flush_secs, filename_suffix)
223 # and recreated later as needed.
224 self.file_writer = self.all_writers = None
--> 225 self._get_file_writer()
226
227 # Create default bins for histograms, see generate_testdata.py in tensorflow/tensorboard
/anaconda3/lib/python3.6/site-packages/torch/utils/tensorboard/writer.py in _get_file_writer(self)
254 if self.all_writers is None or self.file_writer is None:
255 self.file_writer = FileWriter(self.log_dir, self.max_queue,
--> 256 self.flush_secs, self.filename_suffix)
257 self.all_writers = {self.file_writer.get_logdir(): self.file_writer}
258 if self.purge_step is not None:
/anaconda3/lib/python3.6/site-packages/torch/utils/tensorboard/writer.py in __init__(self, log_dir, max_queue, flush_secs, filename_suffix)
64 log_dir = str(log_dir)
65 self.event_writer = EventFileWriter(
---> 66 log_dir, max_queue, flush_secs, filename_suffix)
67
68 def get_logdir(self):
/anaconda3/lib/python3.6/site-packages/tensorboard/summary/writer/event_file_writer.py in __init__(self, logdir, max_queue_size, flush_secs, filename_suffix)
71 """
72 self._logdir = logdir
---> 73 if not tf.io.gfile.exists(logdir):
74 tf.io.gfile.makedirs(logdir)
75 self._file_name = os.path.join(logdir, "events.out.tfevents.%010d.%s.%s.%s" %
/anaconda3/lib/python3.6/site-packages/tensorboard/lazy.py in __getattr__(self, attr_name)
63 class LazyModule(types.ModuleType):
64 def __getattr__(self, attr_name):
---> 65 return getattr(load_once(self), attr_name)
66
67 def __dir__(self):
AttributeError: module 'tensorflow' has no attribute 'io'
How to fix it?
I uninstall and install tensorflow, upgraded tensorboard and torch - that didnt help me
I met the same problem. And my solution is as follow:
Check to see if tensorflow-tensorboard is exists.
pip uninstall tensorflow-tensorboard
install tensorboard==1.14
because an error occurred: TensorBoard logging requires TensorBoard with Python summary writer installed. This should be available in 1.14 or above.
ref: https://github.com/pytorch/pytorch/issues/20140
pip install tensorboard==1.14.0
I was able to solve this problem by using the solution recommended here which suggests to:
pip install tensorflow-io
You can upgrade the version of tensorboard

NameError: name 'exit' is not defined/tfnet/darkflow/custom dataset object detection

I'm trying to train my custom dataset by creating yolov3.cfg file and yolov3.weights file with labelled annotations and images using darkflow. However when I'm trying to run tfnet = TFNet(history), it throws an error of "exit not defined".
I have installed darkflow by the following steps:
In Anaconda Prompt:
git clone https://github.com/thtrieu/darkflow.git
cd darkflow
python3 setup.py build_ext –inplace
pip install
then:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import cv2
from darkflow.net.build import TFNet
history = {"model": "C:/Users/Business Intelli/Desktop/Object-Detection/Dataset/yolov3.cfg",
"load": "C:/Users/Business Intelli/Desktop/Object-Detection/Dataset/yolov3.weights",
"batch": 8,
"epoch": 50,
"gpu": 1.0,
"train": True,
"annotation": "C:/Users/Business Intelli/Desktop/Object-Detection/Dataser/Stumps",
"dataset": "C:/Users/Business Intelli/Desktop/Object-Detection/Dataser/Stumps"}
tfnet = TFNet(history)
Parsing C:/Users/Business Intelli/Desktop/Object-Detection/Dataset/yolov3.cfg
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-10-6f6b945047c5> in <module>
----> 1 tfnet = TFNet(history)
~\Anaconda3\lib\site-packages\darkflow\net\build.py in __init__(self, FLAGS, darknet)
56
57 if darknet is None:
---> 58 darknet = Darknet(FLAGS)
59 self.ntrain = len(darknet.layers)
60
~\Anaconda3\lib\site-packages\darkflow\dark\darknet.py in __init__(self, FLAGS)
15
16 print('Parsing {}'.format(self.src_cfg))
---> 17 src_parsed = self.parse_cfg(self.src_cfg, FLAGS)
18 self.src_meta, self.src_layers = src_parsed
19
~\Anaconda3\lib\site-packages\darkflow\dark\darknet.py in parse_cfg(self, model, FLAGS)
66 cfg_layers = cfg_yielder(*args)
67 meta = dict(); layers = list()
---> 68 for i, info in enumerate(cfg_layers):
69 if i == 0: meta = info; continue
70 else: new = create_darkop(*info)
~\Anaconda3\lib\site-packages\darkflow\utils\process.py in cfg_yielder(model, binary)
314 #-----------------------------------------------------
315 else:
--> 316 exit('Layer {} not
implemented'.format(d['type']))
317
318 d['_size'] = list([h, w, c, l, flat])
NameError: name 'exit' is not defined
So I faced the same problem and the issue is with the process.py file in
darkflow --> utils folder.
Apparently the exit() method is not in-built so you have to add this line in process.py
from sys import exit
Note : If your code is reaching this point it means the models can't read the layers. The weights file that I downloaded for yolov3 gave me same trouble and I couldn't find a link that has a proper weight file for yolov3 that works in darkflow. So I had to stick to yolo.cfg and yolo.weights .

Categories

Resources