我正在设置一个简单的u-net实现,将Gipl文件转换为PIL-Images。不幸的是,在添加DataGenerator以更有效地分配GPU性能之后,网络在初始化第一个时期后停止工作,而没有任何进一步的输出。
当我在模型上禁用use_multiprocessing时,网络会为第一个时期生成图像,但很快就会用尽内存。再次启用该选项后,将不生成任何图像,也不会生成任何输出。至少应该开始准备图像。
unet模型:
model = Model(inputs=[inputs], outputs=[outputs])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=[mean_iou])
model.summary()
params_train = {'dim': (512,512,1),
'batch_size': 16,
'n_classes': 2,
'n_channels': 1,
'shuffle': True}
X, Xv, Y, Yv = getSubsets(X_train_path, Y_train_path)
training_generator = DataGenerator(X, Y, **params_train)
validation_generator = validation_generator = DataGenerator(Xv, Yv, **params_train)
earlystopper = EarlyStopping(patience=5, verbose=1)
checkpointer = ModelCheckpoint('model-2019-1.h5', verbose=1, save_best_only=True)# path of model
print('last output, no output of datagenerator')
model_checkpoint = ModelCheckpoint('unet_fmr.hdf5', monitor='loss',verbose=1, save_best_only=True)
results = model.fit_generator(generator=training_generator,
validation_data=validation_generator,
verbose=1,
use_multiprocessing = True,
epochs=30,
callbacks=[model_checkpoint])
编辑和数据生成器:
class DataGenerator(keras.utils.Sequence):
def __init__(self, list_IDs, labels, batch_size, dim, n_channels=1,
n_classes=2, shuffle=True):
#...initialize variables
self.on_epoch_end()
def __getitem__(self, index):
'Generate one batch of data'
indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
X, Y = self.__data_generation(indexes)
return X, Y
def __len__(self):
return int(np.floor(len(self.list_IDs) / self.batch_size))
def on_epoch_end(self):
self.indexes = np.arange(len(self.list_IDs))
if self.shuffle == True:
np.random.shuffle(self.indexes)
def getImage(self, path):
x_train_array = getGiplAsArray(path)
x_train_i = getResizedGiplImageByArray(x_train_array)
return x_train_i
def make3D(self, img):
img = np.reshape(img, (img.shape[0], img.shape[1], 1))
return img
def __data_generation(self, indexes):
print('__data_generation()')
list_IDs_temp = [self.list_IDs[k] for k in indexes]
list_labels_temp = [self.labels[k] for k in indexes]
X = np.empty((self.batch_size, *self.dim))
Y = np.empty((self.batch_size, *self.dim))
for i, ID in enumerate(list_IDs_temp):
temp = self.getImage(ID)
X[i,] = self.make3D(temp)
for i, ID in enumerate(list_labels_temp):
temp = self.getImage(ID)
Y[i,] = self.make3D(temp)
return X, Y
给定的输出,以等待时间结束:
Total params: 1,940,817
Trainable params: 1,940,817
Non-trainable params: 0
initialization of DataGen
on_epoch_end()
Epoch 1/30