Search code examples
tensorflowkeras

I am trying to run the code for object detection this error is coming


I am trying to detect RRR actor Jr.NTR, while training I am getting this error : I tried checking hist to check which part of the data is not working but to me every things looks fine please some one diagnose my code

Epoch 1/40
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-51-df179639e952> in <cell line: 1>()
----> 1 hist = model.fit(train,epochs=40,validation_data=val, callbacks=[callbacks])

1 frames
/usr/local/lib/python3.10/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     50   try:
     51     ctx.ensure_initialized()
---> 52     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
     53                                         inputs, attrs, num_outputs)
     54   except core._NotOkStatusException as e:

InvalidArgumentError: Graph execution error:

Detected at node 'strided_slice_1' defined at (most recent call last):
    File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
      return _run_code(code, main_globals, None,
    File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
      exec(code, run_globals)
    File "/usr/local/lib/python3.10/dist-packages/ipykernel_launcher.py", line 16, in <module>
      app.launch_new_instance()
    File "/usr/local/lib/python3.10/dist-packages/traitlets/config/application.py", line 992, in launch_instance
      app.start()
    File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelapp.py", line 619, in start
      self.io_loop.start()
    File "/usr/local/lib/python3.10/dist-packages/tornado/platform/asyncio.py", line 195, in start
      self.asyncio_loop.run_forever()
    File "/usr/lib/python3.10/asyncio/base_events.py", line 603, in run_forever
      self._run_once()
    File "/usr/lib/python3.10/asyncio/base_events.py", line 1909, in _run_once
      handle._run()
    File "/usr/lib/python3.10/asyncio/events.py", line 80, in _run
      self._context.run(self._callback, *self._args)
    File "/usr/local/lib/python3.10/dist-packages/tornado/ioloop.py", line 685, in <lambda>
      lambda f: self._run_callback(functools.partial(callback, future))
    File "/usr/local/lib/python3.10/dist-packages/tornado/ioloop.py", line 738, in _run_callback
      ret = callback()
    File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 825, in inner
      self.ctx_run(self.run)
    File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 786, in run
      yielded = self.gen.send(value)
    File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 361, in process_one
      yield gen.maybe_future(dispatch(*args))
    File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 234, in wrapper
      yielded = ctx_run(next, result)
    File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 261, in dispatch_shell
      yield gen.maybe_future(handler(stream, idents, msg))
    File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 234, in wrapper
      yielded = ctx_run(next, result)
    File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 539, in execute_request
      self.do_execute(
    File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 234, in wrapper
      yielded = ctx_run(next, result)
    File "/usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py", line 302, in do_execute
      res = shell.run_cell(code, store_history=store_history, silent=silent)
    File "/usr/local/lib/python3.10/dist-packages/ipykernel/zmqshell.py", line 539, in run_cell
      return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
    File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 2975, in run_cell
      result = self._run_cell(
    File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3030, in _run_cell
      return runner(coro)
    File "/usr/local/lib/python3.10/dist-packages/IPython/core/async_helpers.py", line 78, in _pseudo_sync_runner
      coro.send(None)
    File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3257, in run_cell_async
      has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
    File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3473, in run_ast_nodes
      if (await self.run_code(code, result,  async_=asy)):
    File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3553, in run_code
      exec(code_obj, self.user_global_ns, self.user_ns)
    File "<ipython-input-51-df179639e952>", line 1, in <cell line: 1>
      hist = model.fit(train,epochs=40,validation_data=val, callbacks=[callbacks])
    File "/usr/local/lib/python3.10/dist-packages/keras/utils/traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1685, in fit
      tmp_logs = self.train_function(iterator)
    File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1284, in train_function
      return step_function(self, iterator)
    File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1268, in step_function
      outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1249, in run_step
      outputs = model.train_step(data)
    File "<ipython-input-46-14a3be721cf4>", line 19, in train_step
      batch_lloss = self.lloss(y[1], coords)
    File "<ipython-input-44-38a1384a3354>", line 2, in regression_loss
      true_coord = y_true[1][:,:2]
Node: 'strided_slice_1'
Detected at node 'strided_slice_1' defined at (most recent call last):
    File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
      return _run_code(code, main_globals, None,
    File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
      exec(code, run_globals)
    File "/usr/local/lib/python3.10/dist-packages/ipykernel_launcher.py", line 16, in <module>
      app.launch_new_instance()
    File "/usr/local/lib/python3.10/dist-packages/traitlets/config/application.py", line 992, in launch_instance
      app.start()
    File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelapp.py", line 619, in start
      self.io_loop.start()
    File "/usr/local/lib/python3.10/dist-packages/tornado/platform/asyncio.py", line 195, in start
      self.asyncio_loop.run_forever()
    File "/usr/lib/python3.10/asyncio/base_events.py", line 603, in run_forever
      self._run_once()
    File "/usr/lib/python3.10/asyncio/base_events.py", line 1909, in _run_once
      handle._run()
    File "/usr/lib/python3.10/asyncio/events.py", line 80, in _run
      self._context.run(self._callback, *self._args)
    File "/usr/local/lib/python3.10/dist-packages/tornado/ioloop.py", line 685, in <lambda>
      lambda f: self._run_callback(functools.partial(callback, future))
    File "/usr/local/lib/python3.10/dist-packages/tornado/ioloop.py", line 738, in _run_callback
      ret = callback()
    File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 825, in inner
      self.ctx_run(self.run)
    File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 786, in run
      yielded = self.gen.send(value)
    File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 361, in process_one
      yield gen.maybe_future(dispatch(*args))
    File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 234, in wrapper
      yielded = ctx_run(next, result)
    File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 261, in dispatch_shell
      yield gen.maybe_future(handler(stream, idents, msg))
    File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 234, in wrapper
      yielded = ctx_run(next, result)
    File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 539, in execute_request
      self.do_execute(
    File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 234, in wrapper
      yielded = ctx_run(next, result)
    File "/usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py", line 302, in do_execute
      res = shell.run_cell(code, store_history=store_history, silent=silent)
    File "/usr/local/lib/python3.10/dist-packages/ipykernel/zmqshell.py", line 539, in run_cell
      return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
    File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 2975, in run_cell
      result = self._run_cell(
    File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3030, in _run_cell
      return runner(coro)
    File "/usr/local/lib/python3.10/dist-packages/IPython/core/async_helpers.py", line 78, in _pseudo_sync_runner
      coro.send(None)
    File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3257, in run_cell_async
      has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
    File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3473, in run_ast_nodes
      if (await self.run_code(code, result,  async_=asy)):
    File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3553, in run_code
      exec(code_obj, self.user_global_ns, self.user_ns)
    File "<ipython-input-51-df179639e952>", line 1, in <cell line: 1>
      hist = model.fit(train,epochs=40,validation_data=val, callbacks=[callbacks])
    File "/usr/local/lib/python3.10/dist-packages/keras/utils/traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1685, in fit
      tmp_logs = self.train_function(iterator)
    File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1284, in train_function
      return step_function(self, iterator)
    File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1268, in step_function
      outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1249, in run_step
      outputs = model.train_step(data)
    File "<ipython-input-46-14a3be721cf4>", line 19, in train_step
      batch_lloss = self.lloss(y[1], coords)
    File "<ipython-input-44-38a1384a3354>", line 2, in regression_loss
      true_coord = y_true[1][:,:2]
Node: 'strided_slice_1'
2 root error(s) found.
  (0) INVALID_ARGUMENT:  Index out of range using input dim 1; input has only 1 dims
     [[{{node strided_slice_1}}]]
     [[binary_crossentropy/cond/pivot_f/_3/_17]]
  (1) INVALID_ARGUMENT:  Index out of range using input dim 1; input has only 1 dims
     [[{{node strided_slice_1}}]]
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_13093]

here is my code:

!pip install albumentations
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import albumentations as aug
import os
import cv2
import json

importing roboflow for dataset

!pip install roboflow
from roboflow import Roboflow
from tensorflow import keras
from keras.layers import Conv2D, Dense, GlobalMaxPooling2D, Input, MaxPooling2D
from keras.models import Model
from keras.applications import VGG16
rf = Roboflow(api_key="8OAIvpA0eeCXebsfWJ4U")
project = rf.workspace("malla-raraju-umx2i").project("celebrity-z8hhd")
dataset = project.version(2).download("coco")
augmentor = aug.Compose([aug.RandomCrop(width = 120, height = 120),
                         aug.HorizontalFlip(p=0.5),
                         aug.RandomBrightnessContrast(p=0.2),
                         aug.RandomGamma(p=0.2),
                         aug.VerticalFlip(p=0.5),
                         aug.RGBShift(p=0.2)],
                        bbox_params=aug.BboxParams(format='coco',
                                                    label_fields=['class_labels']))
with open('/content/Celebrity-2/train/_annotations.coco.json') as f:
  annot = json.load(f)
coords = annot['annotations'][1]['bbox']
img = cv2.imread(os.path.join('/content/Celebrity-2/train',annot['images'][1]['file_name']))
class_label = annot['categories'][1]['name']

sample_aug = augmentor(image=img, bboxes=[coords], class_labels=[class_label])

def lets_see(img, min_c, max_c, b_color):
  det = cv2.rectangle(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), min_c, max_c, b_color, 1)
  plt.imshow(det, origin ='upper')
lets_see(sample_aug['image'],tuple(np.array(sample_aug['bboxes'][0][0:2]).astype(int)),
         tuple(np.add(sample_aug['bboxes'][0][0:2],sample_aug['bboxes'][0][2:]).astype(int)),
         (255, 0, 0))

sample_aug['class_labels'] annot['categories']

os.remove('/content/Celebrity-2/README.dataset.txt')
os.remove('/content/Celebrity-2/README.roboflow.txt')
aug_dir = '/content/aug_data'
os.mkdir(aug_dir)

DataAugmentation pipeline

It might run but need to make few changes check aug_data folder for sure

data_dir = '/content/Celebrity-2'
sub_dir = ['test', 'train', 'valid', None]
try:
  for sub in sub_dir:
    if sub!=None:
      anot_f=os.path.join(data_dir,sub,'_annotations.coco.json')
      with open(anot_f) as f:
        f = json.load(f)
        for indx,image in enumerate(f['images']):
          img_path = image['file_name']
          coords = f['annotations'][indx]['bbox']
          label = f['annotations'][indx]['category_id']
          for _ in range(60):
            img = cv2.imread(os.path.join(data_dir,sub,img_path))
            aug_img = augmentor(image=img, bboxes=[coords], class_labels=['Jr.NTR'])

            annotation = {}
            annotation['image'] = img_path
            if len(aug_img['bboxes'])>0:
              annotation['bbox'] = aug_img['bboxes'][0]
            else:
              annotation['bbox'] = [0, 0, 0, 0]
            annotation['class'] = 1 if len(aug_img['class_labels'])>0 else 0

            if not os.path.exists(os.path.join(aug_dir,sub)):
              os.mkdir(os.path.join(os.path.join(aug_dir,sub)))

            if not os.path.exists(os.path.join(aug_dir,sub,'label')):
              os.mkdir(os.path.join(aug_dir,sub,'label'))
              with open(os.path.join(aug_dir,sub,'label',f'{img_path.split(".")[0]}.{_}.json'), 'w') as o:
                json.dump(annotation, o)
            else:
              with open(os.path.join(aug_dir,sub,'label',f'{img_path.split(".")[0]}.{_}.json'), 'w') as o:
                json.dump(annotation, o)

            if not os.path.exists(os.path.join(aug_dir,sub,'image')):
              os.mkdir(os.path.join(aug_dir,sub,'image'))
              cv2.imwrite(os.path.join(aug_dir,sub,'image',f'{img_path.split(".")[0]}.{_}.jpg'),
                          aug_img['image'])
            else:
              cv2.imwrite(os.path.join(aug_dir,sub,'image',f'{img_path.split(".")[0]}.{_}.jpg'),
                          aug_img['image'])
except Exception as e:
  print(e)

map labels into the data pipeline

def load_labels(label_path):
  with open(label_path.numpy(), 'r', encoding = 'utf-8') as f:
    label = json.load(f)
  return [label['class'], label['bbox']]

def load_images(image_path):
  byte_img = tf.io.read_file(image_path)
  img = tf.io.decode_jpeg(byte_img)
  return img

def label_pl(label):
  label_line = tf.data.Dataset.list_files(label, shuffle=False)
  label_line = label_line.map(lambda x: tf.py_function(load_labels, [x], [tf.uint8, tf.float32]))
  return label_line

def image_pl(img):
  img_line = tf.data.Dataset.list_files(img, shuffle=False)
  img_line = img_line.map(load_images)
  img_line = img_line.map(lambda x: tf.image.resize(x, (120, 120)))
  img_line = img_line.map(lambda x: x/255)
  return img_line

def data_pl(data):
  data_pl =   tf.data.Dataset.zip(data)
  data_pl =   data_pl.shuffle(int(len(data[0])*1.35))
  data_pl =   data_pl.batch(16)
  data_pl =   data_pl.prefetch(4) # eliminates bottle-necks in data pipeline
  return data_pl
data = [None]*3
img = [None]*3
lbl = [None]*3

i=0
for _ in  os.listdir(aug_dir):
  pl_img = image_pl(os.path.join(aug_dir,_,'image','*.jpg'))
  pl_labels = label_pl(os.path.join(aug_dir,_,'label','*.json'))
  img[i] = pl_img
  lbl[i] = pl_labels
  data[i] = data_pl((pl_img,pl_labels))
  i+=1
batch = data[0].as_numpy_iterator()
batch.next()[1] # every thing's worki'n fine
len(data[0]), len(data[1]), len(data[2])
len(img[0]), len(lbl[0]), len(img[1]), len(lbl[1]), len(img[2]), len(lbl[2])
batch_label = lbl[1].as_numpy_iterator()
batch_label.next()

batch = data[1].as_numpy_iterator()
batch.next()[1] # every thing's worki'n fine

vgg16 = VGG16(include_top=False) vgg16.summary() keras.utils.plot_model(vgg16, show_shapes=True, show_layer_activations=True)

def build_model():
  input = Input(shape=(120,120,3))

  vgg16 = VGG16(include_top=False)(input)

  features = GlobalMaxPooling2D(name = 'feature_map')(vgg16)

  classify_head = Dense(2048, activation='relu', name = 'classification_dense')(features)
  classify_head = Dense(1, activation = 'sigmoid', name = 'classifying_head')(classify_head)

  regress_head = Dense(2048, activation='relu', name = 'regression_dense')(features)
  regress_head = Dense(4, activation = 'relu', name = 'regression_head')(regress_head)

  return Model(inputs = input, outputs = [classify_head,regress_head])

ntr_detector = build_model() ntr_detector.summary() keras.utils.plot_model(ntr_detector, show_shapes=True, show_layer_activations=True)

bpe = len(data[1]) # batches per epoch
lr_decay = (1/.75-1)/bpe
def regression_loss(y_true, y_pred):
  true_coord = y_true[1][:,:2]
  pred_coord = y_pred[1][:2]

  true_w = y_true[1][:,2]
  true_h = y_true[1][:,3]

  pred_w = y_pred[1][2]
  pred_h = y_pred[1][3]

  delta_coord = tf.reduce_sum(tf.square(true_coord-pred_coord))
  delta_size = tf.reduce_sum(tf.square(tf.sqrt(true_w)-tf.sqrt(pred_w))+
                             tf.square(tf.sqrt(true_h)-tf.sqrt(pred_h)))
  return delta_coord+delta_size

opt = keras.optimizers.Adam(learning_rate=0.0001, weight_decay = lr_decay)
closs = keras.losses.BinaryCrossentropy()
lloss = regression_loss
class detect_ntr(Model):
  def __init__(self, ntr_detector, **kwargs):
    super().__init__(**kwargs)
    self.model = ntr_detector

  def compile(self, opt, closs, lloss, **kwargs):
    super().compile(**kwargs)
    self.opt = opt
    self.closs = closs
    self.lloss = lloss

  def train_step(self, batch, **kwargs):
    x,y = batch

    with tf.GradientTape() as tape:
      classes, coords = self.model(x, training=True)

      batch_closs = self.closs(y[0], classes)
      batch_lloss = self.lloss(y[1], coords)

      total_loss = batch_lloss + 0.5 * batch_closs

      gradient = tape.gradient(total_loss, self.model.trainable_variables)
    opt.apply_gradients(zip(gradient, self.model.trainable_variables))
    return {'Loss ' : total_loss, 'cLoss ' : batch_closs, 'lLoss ' : batch_lloss}
  def test_step(self, batch, **kwargs):
    x,y = batch

    classes, coords = self.model(x, training=True)

    batch_closs = self.closs(y[0], classes)
    batch_lloss = self.lloss(tf.cast(y[1], tf.float32), coords)

    total_loss = batch_lloss + 0.5 * batch_closs
    return {'val_Loss ' : total_loss, 'val_cLoss ' : batch_closs, 'val_lLoss ' : batch_lloss}

  def call(self, x, **kwargs):
    return self.model(x, **kwargs)

model = detect_ntr(ntr_detector) model.compile(opt, closs, lloss, metrics=['accuracy']) test, train, val = data

log_dir = '/content/logs'
callbacks = keras.callbacks.TensorBoard(log_dir=log_dir)

hist = model.fit(train,epochs=40,validation_data=val, callbacks=[callbacks])


Solution

  • I forgot I passed a sliced tensor and trying to slice it again, the error was resolved. Here is the solution

    def regression_loss(y_true, y_pred):
      true_coord = y_true[:,:2]
      pred_coord = y_pred[:,:2]
    
      true_w = y_true[:,2]
      true_h = y_true[:,3]
    
      pred_w = y_pred[:,2]
      pred_h = y_pred[:,3]
    
      delta_coord = tf.reduce_sum(tf.square(true_coord-pred_coord))
      delta_size = tf.reduce_sum(tf.square(tf.sqrt(true_w)-tf.sqrt(pred_w))+
                                 tf.square(tf.sqrt(true_h)-tf.sqrt(pred_h)))
      return delta_coord+delta_size```