我真的需要这里的帮助,不知道我在做什么错。我有一个CNN可以对图像进行分类,效果很好。现在,我想在最后一个密集层之前集成一个年龄变量,该模型仍在运行,但不再愿意保存。确实,我有这个错误:“ TypeError :(‘不是JSON可序列化的:’,Dimension(1001))”。从网上找到的示例中,我看不到我的错误:
https://github.com/keras-team/keras/issues/2818
https://github.com/keras-team/keras/issues/9342
正常工作时的代码:
base_model = applications.resnet50.ResNet50(include_top=True,
weights='imagenet',
pooling='avg',
input_shape=(params['n_rows'], params['n_cols'],
params['n_channels']))
#Adding custom Layers
x = Dense(1024, activation='relu',input_shape=base_model.output_shape[1:],
kernel_regularizer=regularizers.l2(0.001))(base_model.output)
x = Dropout(0.60)(x)
out = Dense(params['n_classes'], activation='sigmoid')(x)
#creating the final model
model = Model(inputs=base_model.input, outputs=out)
,当我添加元数据(与每个图像相关的简单数字)时:
base_model = applications.resnet50.ResNet50(include_top=True, #inception_v3.InceptionV3
weights='imagenet', #imagenet
pooling='avg',
input_shape=(params['n_rows'], params['n_cols'],
params['n_channels']))
#adding age (it should be connected to all local features hence not just adding it at the end)
metadata_input = Input(shape=(1,))
#Adding custom Layers
merge_info = concatenate([metadata_input, base_model.output], axis=1)
x = Dense(1024, activation='relu', input_shape=merge_info.shape[1:],
kernel_regularizer=regularizers.l2(0.001))(merge_info)
x = Dropout(0.60)(x)
main_output = Dense(params['n_classes'], activation='sigmoid')(x)
#creating the final model
model = Model(inputs=[base_model.input, metadata_input], outputs=main_output)
此外,我的错误可能来自数据生成器:
class DataGenerator(keras.utils.Sequence):
'Generates data for Keras'
def __init__(self, list_IDs, labels, image_path, mask_path, batch_size, n_rows, n_cols, n_channels, n_classes,
augmentation=None, preprocessing=None, shuffle=True, normalize=False, age=None):
self.n1 = n_rows
self.n2 = n_cols
self.batch_size = batch_size
self.image_path = image_path
self.mask_path = mask_path
self.labels = labels
self.age = age
self.list_IDs = list_IDs
self.n_channels = n_channels
self.n_classes = n_classes
self.shuffle = shuffle
self.normalize = normalize
self.augmentation = augmentation
self.preprocessing = preprocessing
self.on_epoch_end()
def __len__(self):
'number of step per epoch'
return int(np.floor(len(self.list_IDs) / self.batch_size))
def __getitem__(self, index):
'Generate one batch of data'
# Generate indexes of the batch
indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
# Find list of IDs
list_IDs_temp = [self.list_IDs[k] for k in indexes]
# Generate data
return(self.__data_generation(list_IDs_temp))
def on_epoch_end(self):
'Updates indexes after each epoch'
self.indexes = np.arange(len(self.list_IDs))
if self.shuffle == True:
np.random.shuffle(self.indexes)
def __data_generation(self, list_IDs_temp):
'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels) where n_sampled=batch_size
# Initialization
X = np.empty((self.batch_size, self.n1, self.n2, self.n_channels))
a = np.empty((self.batch_size), dtype=int)
y = np.empty((self.batch_size), dtype=int)
# Generate data
for i, ID in enumerate(list_IDs_temp):
#handle image
image = image_augmentation_with_maskrcnn(ID=ID, n1=self.n1,n2=self.n2,
image_path=self.image_path, mask_path=self.mask_path,
augmentation=self.augmentation,
normalize=self.normalize,
preprocessing=self.preprocessing)
X[i,] = image
#handle class
y[i] = self.labels[ID]
#handle age
if self.age is not None:
#handle age
a[i] = self.age[ID]
#handle age
if self.age is not None:
return [X,a], keras.utils.to_categorical(y, num_classes=self.n_classes)
else:
return X, keras.utils.to_categorical(y, num_classes=self.n_classes)
任何见解都会有很大帮助。谢谢!