A custom Image DataGen for stacked denoising autoencoder (DAE) in Keras on GPU (exceptions)

172 Views Asked by At

Have you ever created a custom ImageDataGenerator for Keras?
For one of our projects we have already created two sets of "clean images" and multiple noisy versions of each one as the "noisy images", in two folders. I want to design and train an stacked denoising autoencoder to learn denoising these images (document scans).
I have searched around and found multiple sample codes for DAE on MNIST, where they directly load all images into memory. But in my case I can not load all images at once, so I think I should use Keras ImageDataGenerator or something like that. I even have found sample AutoEncoders that use ImageDataGenerators to load the data where it returns the same exact image both as the input and output, and even add some augmentation to both input and output images in their flow_from_directory() here.
But as I mentioned earlier, I want to design a "Denoising AutoEncoder" and want to train it on an "existing" set of clean/noisy images. I found here a nice description on defining custom DataGenerators in Keras, and have tried to adopt it to my case, but I am getting exceptions!

Please find my Code:
I have created a Jupyter Notebook shared on Google Colab here and appreciate it if you guys can take a look at it and let me know what the source of problem is.

Current Exception:

Epoch 1/5
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
h:\Projects\DenoisingAutoencoder\DAE_DataGen.ipynb Cell 7' in <cell line: 2>()
      1 # Train model on dataset
----> 2 model.fit(training_generator,
      3             validation_data=validation_generator,
      4             steps_per_epoch=500, 
      5             epochs=5,
      6             # use_multiprocessing=True,
      7             # workers=6,
      8             # callbacks=[tensorboard_callback]
      9             )

File c:\Users\.conda\envs\me_env_gpu\lib\site-packages\keras\utils\traceback_utils.py:67, in filter_traceback.<locals>.error_handler(*args, **kwargs)
     65 except Exception as e:  # pylint: disable=broad-except
     66   filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67   raise e.with_traceback(filtered_tb) from None
     68 finally:
     69   del filtered_tb

File c:\Users\.conda\envs\me_env_gpu\lib\site-packages\tensorflow\python\eager\execute.py:54, in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     52 try:
     53   ctx.ensure_initialized()
---> 54   tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
     55                                       inputs, attrs, num_outputs)
     56 except core._NotOkStatusException as e:
     57   if name is not None:

InvalidArgumentError: Graph execution error:

Detected at node 'model_1/conv2d_transpose_2/conv2d_transpose' defined at (most recent call last):
    File "c:\Users\.conda\envs\me_env_gpu\lib\runpy.py", line 196, in _run_module_as_main
      return _run_code(code, main_globals, None,
    File "c:\Users\.conda\envs\me_env_gpu\lib\runpy.py", line 86, in _run_code
      exec(code, run_globals)
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\ipykernel_launcher.py", line 17, in <module>
      app.launch_new_instance()
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\traitlets\config\application.py", line 965, in launch_instance
      app.start()
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\ipykernel\kernelapp.py", line 712, in start
      self.io_loop.start()
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\tornado\platform\asyncio.py", line 199, in start
      self.asyncio_loop.run_forever()
    File "c:\Users\.conda\envs\me_env_gpu\lib\asyncio\base_events.py", line 600, in run_forever
      self._run_once()
    File "c:\Users\.conda\envs\me_env_gpu\lib\asyncio\base_events.py", line 1896, in _run_once
      handle._run()
    File "c:\Users\.conda\envs\me_env_gpu\lib\asyncio\events.py", line 80, in _run
      self._context.run(self._callback, *self._args)
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\ipykernel\kernelbase.py", line 504, in dispatch_queue
      await self.process_one()
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\ipykernel\kernelbase.py", line 493, in process_one
      await dispatch(*args)
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\ipykernel\kernelbase.py", line 400, in dispatch_shell
      await result
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\ipykernel\kernelbase.py", line 724, in execute_request
      reply_content = await reply_content
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\ipykernel\ipkernel.py", line 383, in do_execute
      res = shell.run_cell(
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\ipykernel\zmqshell.py", line 528, in run_cell
      return super().run_cell(*args, **kwargs)
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\IPython\core\interactiveshell.py", line 2880, in run_cell
      result = self._run_cell(
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\IPython\core\interactiveshell.py", line 2935, in _run_cell
      return runner(coro)
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\IPython\core\async_helpers.py", line 129, in _pseudo_sync_runner
      coro.send(None)
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\IPython\core\interactiveshell.py", line 3134, in run_cell_async
      has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\IPython\core\interactiveshell.py", line 3337, in run_ast_nodes
      if await self.run_code(code, result, async_=asy):
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\IPython\core\interactiveshell.py", line 3397, in run_code
      exec(code_obj, self.user_global_ns, self.user_ns)
    File "C:\Users\AppData\Local\Temp\ipykernel_40284\3459445018.py", line 2, in <cell line: 2>
      model.fit(training_generator,
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
      return fn(*args, **kwargs)
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\keras\engine\training.py", line 1384, in fit
      tmp_logs = self.train_function(iterator)
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\keras\engine\training.py", line 1021, in train_function
      return step_function(self, iterator)
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\keras\engine\training.py", line 1010, in step_function
      outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\keras\engine\training.py", line 1000, in run_step
      outputs = model.train_step(data)
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\keras\engine\training.py", line 859, in train_step
      y_pred = self(x, training=True)
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
      return fn(*args, **kwargs)
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\keras\engine\base_layer.py", line 1096, in __call__
      outputs = call_fn(inputs, *args, **kwargs)
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\keras\utils\traceback_utils.py", line 92, in error_handler
      return fn(*args, **kwargs)
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\keras\engine\functional.py", line 451, in call
      return self._run_internal_graph(
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\keras\engine\functional.py", line 589, in _run_internal_graph
      outputs = node.layer(*args, **kwargs)
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
      return fn(*args, **kwargs)
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\keras\engine\base_layer.py", line 1096, in __call__
      outputs = call_fn(inputs, *args, **kwargs)
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\keras\utils\traceback_utils.py", line 92, in error_handler
      return fn(*args, **kwargs)
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\keras\layers\convolutional.py", line 1336, in call
      outputs = backend.conv2d_transpose(
    File "c:\Users\.conda\envs\me_env_gpu\lib\site-packages\keras\backend.py", line 5720, in conv2d_transpose
      x = tf.compat.v1.nn.conv2d_transpose(x, kernel, output_shape, strides,
Node: 'model_1/conv2d_transpose_2/conv2d_transpose'
Conv2DSlowBackpropInput: Size of out_backprop doesn't match computed: actual = 98, computed = 293 spatial_dim: 2 input: 586 filter: 3 output: 98 stride: 2 dilation: 1
     [[{{node model_1/conv2d_transpose_2/conv2d_transpose}}]] [Op:__inference_train_function_1189]
1

There are 1 best solutions below

0
On

I just checked your code on Google Colab, and I see quite good comments. You probably need to restart your server to make things roll. I am not yet an expert in Keras's DataImageGenerator, but the first thing that comes to my mind is to replace your DAE with a normal feedforward net!