我的数据集包含超过10000张图像(dim = 1024x1024),并且我正在使用tf.keras DataGenerator批量加载数据。但是,当我使用model.fit_generator拟合模型时,出现错误:InvalidArgumentError:不兼容的形状:[10,224,224,3]与[10,224,224]。 我的输入尺寸是224x224x3。
这是代码段:
import math
import random
import cv2
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.utils import Sequence
from tensorflow.keras.applications.mobilenet import preprocess_input
class DataGenerator(Sequence):
def __init__(self, dataset, batch_size=10, shuffle=True, predict=False):
self.dataset = dataset
self.batch_size=batch_size
self.shuffle=shuffle
self.predict=predict
self.on_epoch_end()
def __len__(self):
return math.ceil(len(self.dataset)/self.batch_size)
def __getitem__(self, index):
indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
image_batch = [self.dataset[i][1]['dicom'] for i in indexes]
bbox_batch = [self.dataset[i][1]['boxes'] for i in indexes]
X = self.__generate_X(image_batch)
if self.predict:
return X
else:
masks = self.__generate_masks(image_batch, bbox_batch)
return X, masks
def __generate_X(self, image_batch):
X = np.zeros((len(image_batch), IMAGE_WIDTH, IMAGE_HEIGHT,3), dtype=np.float32)
for k, image_path in enumerate(image_batch):
img = dicom.read_file(image_path).pixel_array
img = cv2.resize(img, dsize=(IMAGE_HEIGHT, IMAGE_WIDTH), interpolation=cv2.INTER_CUBIC)
img = np.stack((img,)*3, axis=-1)
X[k] = preprocess_input(np.array(img, dtype=np.float32))
return X
def __generate_masks(self, image_batch, bbox_batch):
X = np.zeros((len(image_batch), IMAGE_WIDTH, IMAGE_HEIGHT,3), dtype=np.float32)
for k, image_path in enumerate(image_batch):
img = dicom.read_file(image_path).pixel_array
img = cv2.resize(img, dsize=(IMAGE_HEIGHT, IMAGE_WIDTH), interpolation=cv2.INTER_CUBIC)
img = np.stack((img,)*3, axis=-1)
X[k] = preprocess_input(np.array(img, dtype=np.float32))
return X
masks = np.zeros((len(bbox_batch), IMAGE_WIDTH, IMAGE_HEIGHT))
width_factor = IMAGE_WIDTH/imageWidth
height_factor = IMAGE_HEIGHT/imageHeight
for k, bbox_items in enumerate(bbox_batch):
if len(bbox_items) > 0:
for idx, val in enumerate(bbox_items):
x1 = round(val[0]* width_factor)
x2 = round((val[0]+val[2])* width_factor)
y1 = round(val[1]*height_factor)
y2 = round((val[1]+val[3])*height_factor)
masks[k][y1:y2, x1:x2]=1
return masks
def on_epoch_end(self):
self.indexes = np.arange(len(self.dataset))
if self.shuffle == True:
np.random.shuffle(self.indexes)
model = create_model()
optimizer = Adam(lr = 0.001)
model.compile(loss=loss, optimizer=optimizer, metrics=[dice_coefficient])
train_gen = DataGenerator(X_train, batch_size=10, predict=False, shuffle=True)
model.fit_generator(train_gen, validation_data = None, epochs=1, verbose=1)
输入:X_train和X_val是numpy数组 Tensorflow版本:1.15.0 Keras版本:2.2.4 这是我使用fit_generator时遇到的错误
InvalidArgumentError Traceback (most recent call last)
<ipython-input-17-b4c5d8755e72> in <module>
----> 1 model.fit_generator(generator=train_gen, validation_data=None, epochs=1, shuffle=True, verbose=1)
2
~\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1294 shuffle=shuffle,
1295 initial_epoch=initial_epoch,
-> 1296 steps_name='steps_per_epoch')
1297
1298 def evaluate_generator(self,
~\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training_generator.py in model_iteration(model, data, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch, mode, batch_size, steps_name, **kwargs)
263
264 is_deferred = not model._is_compiled
--> 265 batch_outs = batch_function(*batch_data)
266 if not isinstance(batch_outs, list):
267 batch_outs = [batch_outs]
~\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training.py in train_on_batch(self, x, y, sample_weight, class_weight, reset_metrics)
1015 self._update_sample_weight_modes(sample_weights=sample_weights)
1016 self._make_train_function()
-> 1017 outputs = self.train_function(ins) # pylint: disable=not-callable
1018
1019 if reset_metrics:
~\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\backend.py in __call__(self, inputs)
3474
3475 fetched = self._callable_fn(*array_vals,
-> 3476 run_metadata=self.run_metadata)
3477 self._call_fetch_callbacks(fetched[-len(self._fetches):])
3478 output_structure = nest.pack_sequence_as(
~\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\client\session.py in __call__(self, *args, **kwargs)
1470 ret = tf_session.TF_SessionRunCallable(self._session._session,
1471 self._handle, args,
-> 1472 run_metadata_ptr)
1473 if run_metadata:
1474 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
InvalidArgumentError: Incompatible shapes: [10,224,224,3] vs. [10,224,224]
[[{{node training/Adam/gradients/gradients/loss/reshape_loss/mul_2_grad/BroadcastGradientArgs}}]]
我非常感谢您提供解决此问题的指导。
答案 0 :(得分:0)
您进行了归一化吗?
例如:
from keras import backend as k
if k.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, image_row, image_column)
x_test = x_test.reshape(x_test.shape[0], 1, image_row, image_column)
input_shape = (1, image_row, image_column)
else:
x_train = x_tr.reshape(x_tr.shape[0], image_row, image_column, 1)
x_test = x_te.reshape(x_te.shape[0], image_row, image_column, 1)
input_shape = (image_row, image_column, 1)
在训练任何数据集之前,您需要应用归一化。