我有一个大型图像数据集,我想在其上运行predict_generator。由于内存问题,我不能立刻运行所有这些。想法是
通过循环显示图像范围并对其进行预测,将小组图像迭代地提供给生成器。
将预测保存到文件
稍后在循环中打开文件,以读取代码中提到的计算概率的所有预测。
validation_generator = ImageDataGenerator(rescale=1./255).flow_from_directory(path, target_size=(img_width, img_height),
batch_size=6,shuffle=False)
print("generator built")
print (counter)
#file = open('Failed.py', 'w')
#for x in file:
# for i in range(counter):
# features = model.predict_generator(validation_generator,steps=2)
print("features found")
model = Sequential()
model.add(Flatten(input_shape=(3, 3, 1536)))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(6, activation='softmax'))
model.load_weights(top_model_weights_path)
print("top model loaded")
prediction_proba = model.predict_proba(features)
prediction_classes = model.predict_classes(features)
print(prediction_proba)
print(prediction_classes)
print("original file names")
print(validation_generator.filenames)
问题是如何将不同的预测保存在一个文件中。我试过为文件创建一个for循环,但不知道它应该如何工作?如果有人可以为所定义的目标提供暗示,那就太好了。
答案 0 :(得分:2)
预测和保存
i = 0
maximumPredictions = ??
for x,y in generator: #if the generator doesn't have y, use only "for x in..."
predictions = model.predict(x)
numpy.save('predictions/prediction' + str(i) +".npy", predictions)
i+=1
if i == maximumPredictions:
break;
加载和处理
files = [name for name in os.listdir('predictions')]
for file in files:
prediction = numpy.load('predictions/"+file)
#do what you want with the loaded predictions.