'AttributeError: 'NoneType' object has no attribute 'image_data_format' keras

I am trying for testing purpose to classify real time objects using a pretrained vgg-19 model, also I have enabled multithreading in the code. While I run the below code, I retrieve error. I am unable to figure it out I tried different solutions but I am unable to fix it.

  from keras.applications.vgg19 import decode_predictions
  from keras_applications.vgg19 import VGG19, preprocess_input
  label = ''
  frame = None
  class MyThread(threading.Thread):
  def __init__(self):
     threading.Thread.__init__(self)
  def run(self):
     global label
     print("[INFO] loading network...")
     self.model = VGG19(weights="imagenet")
     while (~(frame is None)):
        (inID, label) = self.predict(frame)
  def predict(self, frame):
    image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB).astype(np.float32)
    image = image.transpose((2, 0, 1))
    image = image.reshape((1,) + image.shape)
    image = preprocess_input(image)
    preds = self.model.predict(image)
    return decode_predictions(preds)[0]
 cap = cv2.VideoCapture(0)
 if (cap.isOpened()):
   print("Camera OK")
 else:
   cap.open()
keras_thread = MyThread()
keras_thread.start()
while (True):
 ret, original = cap.read()
 frame = cv2.resize(original, (224, 224))
 cv2.putText(original, "Label: {}".format(label), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
 cv2.imshow("Classification", original)
 if (cv2.waitKey(1) & 0xFF == ord('q')):
     break;
cap.release()
frame = None
cv2.destroyAllWindows()
sys.exit()

Below is my error, I retrieve while running the code

     File "C:\miniconda3\envs\tensorflow\lib\threading.py", line 916, in _bootstrap_inner
     self.run()
   File "C:/Users/video_testing.py", line 17, in run
      self.model = VGG16(weights=None)
   File "C:\miniconda3\envs\tensorflow\lib\site-packages\keras_applications\vgg16.py", line 97, in VGG16
   data_format=backend.image_data_format(),
   AttributeError: 'NoneType' object has no attribute 'image_data_format'
   Process finished with exit code -1

Thank you, your help is highly appreciated.



Solution 1:[1]

I have similar error AttributeError: 'NoneType' object has no attribute 'image_data_format'. I corrected it by called specific preprocess_input method inside ResNet50 class.

From

from keras.applications.resnet import ResNet50
from keras.preprocessing.image import img_to_array, load_img
from keras_applications.imagenet_utils import preprocess_input

To

from keras.applications.resnet import ResNet50, preprocess_input
from keras.preprocessing.image import img_to_array, load_img

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Khawar Islam