我在fit_generator中遇到了一个奇怪的问题
model.fit_generator(generate_arrays_from_file(trainSizeListImgDic[s], s, batchSize), steps_per_epoch=math.floor(size / batchSize), epochs=20,
verbose=2, validation_data=generate_arrays_from_file(testSizeListImgDic[s], s, batchSize),
validation_steps=vs,callbacks=[EarlyStoppingByAccVal(monitor='val_acc', value=0.90, verbose=1),checkpointer])
和我的generate_arrays_from_file读取:
def generate_arrays_from_file( SizeListImg ,img_size,batch):
size = len(SizeListImg.images)
dim = re.split('[,()]', img_size)
dataX = np.zeros((batch, int(dim[1]), int(dim[2]), 1), dtype=np.float32)
dataY = np.zeros((batch, num_classes), dtype=np.float32)
loopcount = math.floor( size/batch )-1
if loopcount==0:
loopcount = 1
counter = 0
while (True):
i = random.randint(0,loopcount)
for ind in range( (i*batch) , (i + 1)*batch ):
try:
dataX[counter, :, :, 0] = SizeListImg.images[ind]
except :
print('dim='+ str(dim) )
print('error counter=' + str(counter) + " i="+str(i) + " ind=" + str(ind) + " batch="+str(batch) + "\n" )
print("SizeListImg.images="+str( len(SizeListImg.images) ) )
print( "img0 = "+SizeListImg.images_names[0])
print("img1 = " + SizeListImg.images_names[1])
print("img2 = " + SizeListImg.images_names[2])
print("img3 = " + SizeListImg.images_names[3])
for j, imgClass in enumerate(imgClasses):
dataY[counter, j] = (SizeListImg.labels[ind] == imgClass)
counter += 1
if counter>=batch:
yield (dataX,dataY)
counter = 0
dataX = np.zeros((batch, int(dim[1]), int(dim[2]), 1), dtype=np.float32) #not tf.zeros((25, 200, 200, 1)) please noted different: np.zeros(
dataY = np.zeros((batch, num_classes), dtype=np.float32)
在训练过程中,我发现训练图像139的大小减小为验证图像22的大小,这导致索引错误,但是图像确实来自训练图像集。但是,如果我将批次从20减少到10,则没有任何错误。 fit_generator对我有任何阴谋吗?