0

Sample Code Feeding npy files to resnet

I am having trouble fitting the resnet50 model. The dataset is a very large .npy array for satellite images.

This is my code snippet:

SPLIT DATA (TRAIN-TEST)
# Definition of the train and test patch IDs, take 80 % for train
test_ID = [0, 7, 15]
test_eopatches = [sampled_eopatches[i] for i in test_ID]
train_ID = [i for i in range(len(patchIDs)) if i not in test_ID]
train_eopatches = [sampled_eopatches[i] for i in train_ID]

# Set the features and the labels for train and test sets
features_train = np.concatenate([eopatch.data["FEATURES_SAMPLED"] for eopatch in train_eopatches], axis=1)
labels_train = np.concatenate([eopatch.mask_timeless["LULC_ERODED"] for eopatch in train_eopatches], axis=0)

features_test = np.concatenate([eopatch.data["FEATURES_SAMPLED"] for eopatch in test_eopatches], axis=1)
labels_test = np.concatenate([eopatch.mask_timeless["LULC_ERODED"] for eopatch in test_eopatches], axis=0)

# Get shape
t, w1, h, f = features_train.shape
t, w2, h, f = features_test.shape

# Reshape to n x m
features_train = np.moveaxis(features_train, 0, 2).reshape(w1 * h, t * f)
labels_train = labels_train.reshape(w1 * h)
features_test = np.moveaxis(features_test, 0, 2).reshape(w2 * h, t * f)
labels_test = labels_test.reshape(w2 * h)


features_train.shape
labels_train.shape
features_test.shape
labels_test.shape

**Set up and train the model**

import numpy as np
import tensorflow as tf

train_dataset = tf.data.Dataset.from_tensor_slices((features_train, labels_train))
test_dataset = tf.data.Dataset.from_tensor_slices((features_test, labels_test))

BATCH_SIZE = 64
SHUFFLE_BUFFER_SIZE = 100

train_dataset = train_dataset.shuffle(SHUFFLE_BUFFER_SIZE).batch(BATCH_SIZE)
test_dataset = test_dataset.batch(BATCH_SIZE)


model = tf.keras.applications.resnet50.ResNet50(
    include_top= True,
    weights='imagenet',
    input_tensor=None,
    input_shape= None,
    pooling=None,
    classes=1000,
    #**kwargs
)

model.compile (
    loss = 'sparse_categorical_crossentropy',
    optimizer = 'adam',
    metrics = ['accuracy']
)

model.fit(features_train,labels_train)

` THIS IS THE ERROR


ValueError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_11452\911953915.py in ----> 1 model.fit(features_train,labels_train)

~\anaconda3\lib\site-packages\keras\utils\traceback_utils.py in error_handler(*args, **kwargs) 68 # To get the full stack trace, call: 69 # tf.debugging.disable_traceback_filtering() ---> 70 raise e.with_traceback(filtered_tb) from None 71 finally: 72 del filtered_tb

~\anaconda3\lib\site-packages\keras\engine\training.py in tf__train_function(iterator) 13 try: 14 do_return = True ---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope) 16 except: 17 do_return = False

ValueError: in user code:

File "C:\Users\DIT_Chairperson\anaconda3\lib\site-packages\keras\engine\training.py", line 1249, in train_function  *
    return step_function(self, iterator)
File "C:\Users\DIT_Chairperson\anaconda3\lib\site-packages\keras\engine\training.py", line 1233, in step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\DIT_Chairperson\anaconda3\lib\site-packages\keras\engine\training.py", line 1222, in run_step  **
    outputs = model.train_step(data)
File "C:\Users\DIT_Chairperson\anaconda3\lib\site-packages\keras\engine\training.py", line 1023, in train_step
    y_pred = self(x, training=True)
File "C:\Users\DIT_Chairperson\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
File "C:\Users\DIT_Chairperson\anaconda3\lib\site-packages\keras\engine\input_spec.py", line 295, in assert_input_compatibility
    raise ValueError(

ValueError: Input 0 of layer "resnet50" is incompatible with the layer: expected shape=(None, 224, 224, 3), found shape=(None, 735)

I keep on receiving this error. Can you help me resolve this?

Lance
  • 1
  • 1

0 Answers0