Training data format for EfficientNet
@WenTheProgrammer 
I tried to replicate the issue on Colab using TF v2.12 but facing a different error. Could you please provide all dependencies   and detailed steps to replicate  the issue reported here ?
Please find the gist [here](https://colab.research.google.com/gist/tiruk007/ce6db2a01e2f147c24d1383e9f3a7f5a/untitled190.ipynb) for reference.

Thank you !  Hi @WenTheProgrammer,
  
To fix the error, you can convert x_train to a NumPy array using np.stack and y_train to a NumPy array using np.array before fitting it to the model.
```python
import numpy as np

x_train = np.stack(x_train, axis=0)
y_train = np.array(y_train)

history = model.fit(x_train, y_train, batch_size=8, epochs=100)

```
This will ensure that the input data is in the correct format for the model. Let me know if this works for you.

Thank you! Thank you @tiruk007 and @nitya-khuntia for your suggestions!

To your question @tiruk007, the following code simulates my program. By the way my tensorflow version is 2.10.1.
```
import numpy as np
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.applications import EfficientNetB7
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
from tensorflow.keras.models import Sequential

x_train = np.random.rand(4317, 600, 600)
y_train = np.random.randint(low=0, high=2, size=(4317,), dtype=np.int32)
x_val = np.random.rand(1233, 600, 600)
y_val = np.random.randint(low=0, high=2, size=(1233,), dtype=np.int32)
x_test = np.random.rand(618, 600, 600)
y_test = np.random.randint(low=0, high=2, size=(618,), dtype=np.int32)

base_model = EfficientNetB7(include_top=False, weights=None, input_shape=(600,600,1), classes=2)
base_model.trainable = True
model = Sequential()
model.add(base_model)
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
history = model.fit(x_train, y_train, batch_size=2, epochs=100)
```
The `np.stack` method worked for me, @nitya-khuntia. However, a new memory-related error occurred.

When I used EfficientNetB7 model, I received the following error message:
```
MemoryError: Unable to allocate 1.66 GiB for an array with shape (618, 600, 600) and data type float64
The system cannot find the path specified.
2023-04-23 21:01:00.199194: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2023-04-23 21:01:00.199432: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
```
The error is related to my test dataset, while my computer seemed to have just enough memory to prepare my training and validation datasets. When I switched to EfficientNetB1 model, which has an input size of 240, the error disappeared.

Interestingly, when I removed my `x_test` and `y_test` to save memory and ran the code again, I received a different error:
```
{{function_node __wrapped__AddV2_device_/job:localhost/replica:0/task:0/device:CPU:0}} OOM when allocating tensor with shape[1,1,384,2304] and type float on /job:localhost/replica:0/task:0/device:CPU:0 by allocator cpu [Op:AddV2]

The system cannot find the path specified.
2023-04-23 21:13:19.354984: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2023-04-23 21:13:19.355127: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2023-04-23 21:14:42.095665: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2023-04-23 21:14:42.097306: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cublas64_11.dll'; dlerror: cublas64_11.dll not found
2023-04-23 21:14:42.098308: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cublasLt64_11.dll'; dlerror: cublasLt64_11.dll not found
2023-04-23 21:14:42.098580: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cufft64_10.dll'; dlerror: cufft64_10.dll not found
2023-04-23 21:14:42.867423: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cusparse64_11.dll'; dlerror: cusparse64_11.dll not found
2023-04-23 21:14:42.869108: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudnn64_8.dll'; dlerror: cudnn64_8.dll not found
2023-04-23 21:14:42.869164: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1934] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
2023-04-23 21:14:42.883158: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-04-23 21:14:46.496880: W tensorflow/core/framework/op_kernel.cc:1780] OP_REQUIRES failed at cwise_ops_common.h:138 : RESOURCE_EXHAUSTED: OOM when allocating tensor with shape[1,1,384,2304] and type float on /job:localhost/replica:0/task:0/device:CPU:0 by allocator cpu
```

Now I am wondering if I want to train my model with a large training dataset, is there anything I can do to prevent the error? I already specified my batch size as 2. @WenTheProgrammer 

The errors you are encountering are related to memory issues when running your TensorFlow code with the EfficientNetB7 model. 

* One possible solution could be to reduce the batch size during training. Currently, you are using a batch size of 2 (batch_size=2) in your model.fit call. You can try reducing the batch size to a smaller value, such as 1, or experiment with different batch sizes to find a value that works within the available memory of your system.
>it's important to note that using very small batch sizes, such as 1, may also have an impact on training performance and convergence. 
* Additionally, you can consider reducing the dimensions of images before feeding them into the EfficientNetB7 model, as the larger input size of 600x600 may require more memory. For example, you can reduce the input image size to 300x300.



* The error messages "Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found" and similar messages indicate that some GPU libraries are missing or not properly installed. You can refer to the TensorFlow installation guide for GPU support (https://www.tensorflow.org/install/gpu).
 
By addressing these issues and verifying your TensorFlow installation, you should be able to resolve the errors and successfully run your code with the EfficientNetB7 model. Hi @nitya-khuntia,

Regarding tensorflow installation, I am pretty sure that I installed everything correctly, since I previously had issues detecting GPU and I reinstalled everything following the exact same instructions that you suggested.

If my understanding is correct, EfficientNetB7 would resize images into 600x600 using bilinear interpolation, if the input image size is not 600x600. However, my images are not regular photos taken by a camera, they are synthesized artificially and each pixel value has a specific meaning, so I would not like to introduce interpolated values that previously do not exist. Is there a way to change EfficientNetB7’s interpolation method to nearest-neighbor? Also, I do not wish to have my images normalized for the same reason of not introducing new values to images. Is there a way to prevent normalization within EfficientNetB7?

Thank you! Hi @WenTheProgrammer ,

OOM error occurs due to the large input data size which can't be accommodated by the system's memory. I tried the code with smaller size of input data and its executed fine as per attached [gist](https://colab.research.google.com/gist/SuryanarayanaY/d5a3659b320bb20e3a8d0e94443639bc/60340.ipynb). I have reduced the size of input to (200, 600, 600) from (4317, 600, 600) and it executed fine with OOM error which proves that OOM error happened in your case is purely due to Higher input size.

`EfficientNetB7` is a pre-built model and the only parameters to play are the arguments that mentioned in the [API](https://www.tensorflow.org/api_docs/python/tf/keras/applications/efficientnet/EfficientNetB7). We can't customize the implementation of the `EfficientNet` AFAIK and for that you need to create your own custom model.

 Hi @SuryanarayanaY,

I have a few follow up questions then:

- If my input image size is 600x600, then EfficientNetB7 will not resize the input images anymore?
- In addition to resizing, does EfficientNetB7 function perform any other pre-processing that would alter input image pixel values, such as normalization, before training on the images?
- Is there a way to generate training data "on the go" during training, such as instead of preparing all training data at once, only preparing the needed batch for the current training iteration, so that computer memory won't be a problem? 

@WenTheProgrammer , PFA replies below.

> * If my input image size is 600x600, then EfficientNetB7 will not resize the input images anymore?



input_shape | Optional shape tuple, only to be specified if include_top is False. It should have exactly 3 inputs channels.
-- | --




> * In addition to resizing, does EfficientNetB7 function perform any other pre-processing that would alter input image pixel values, such as normalization, before training on the images?

Each Keras Application expects a specific kind of input preprocessing. For EfficientNet, input preprocessing is included as part of the model (as a Rescaling layer), and thus [tf.keras.applications.efficientnet.preprocess_input](https://www.tensorflow.org/api_docs/python/tf/keras/applications/efficientnet/preprocess_input) is actually a pass-through function. EfficientNet models expect their inputs to be float tensors of pixels with values in the [0-255] range.





> * Is there a way to generate training data "on the go" during training, such as instead of preparing all training data at once, only preparing the needed batch for the current training iteration, so that computer memory won't be a problem?

To avoid loading all the data into memory you can use tf.data.Dataset for constructing data pipeline. Please refer to attached [source](https://www.tensorflow.org/api_docs/python/tf/data/Dataset) for more details.

 Thanks! Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/60340">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/60340">No</a>

I would like to train a EfficientNetB7 model from scratch to classify 2D arrays into two 2 classes, but it seems like I did not prepare my data in the correct format. Currently my `x_train` is a list of float64 arrays with a 600x600 size, my `y_train` is a list of integers that are either 0 or 1. Of course `x_train` and `y_train` have the same length. This is what I have so far:
```
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.applications import EfficientNetB7
from tensorflow.keras.models import Sequential

base_model = EfficientNetB7(include_top=False, weights=None, input_shape=(600,600,1), classes=2)
base_model.trainable = True
model = Sequential()
model.add(base_model)
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
history = model.fit(x_train, y_train, batch_size=8, epochs=100)
```
The last line currently gives me an error: 
```
ValueError: Failed to find data adapter that can handle input: (<class 'list'> containing values of types {"<class 'numpy.ndarray'>"}), (<class 'list'> containing values of types {"<class 'int'>"})
```
What would be the right format for training data? Any help is appreciated! 