keras入门参考网址:
keras分类应用里的人脸预测kaggle:
人脸表情分类与识别:opencv人脸检测+Keras情绪分类(四)
数据量大无法载入时,节约内存model.fit_generator:
1 def generate_arrays_from_file(path): 2 while 1: 3 f = open(path) 4 for line in f: 5 # create Numpy arrays of input data 6 # and labels, from each line in the file 7 x, y = process_line(line) 8 yield (x, y) 9 f.close() 10 11 model.fit_generator(generate_arrays_from_file('/my_file.txt'), 12 samples_per_epoch=10000, nb_epoch=10) 13 --------------------- 14 作者:ShellCollector 15 来源:CSDN 16 原文:https://blog.csdn.net/jacke121/article/details/78877352
1 def generate_batch_data_random(x, y, batch_size): 2 """逐步提取batch数据到显存,降低对显存的占用""" 3 ylen = len(y) 4 loopcount = ylen // batch_size 5 while (True): 6 i = randint(0,loopcount) 7 yield x[i * batch_size:(i + 1) * batch_size], y[i * batch_size:(i + 1) * batch_size]
keras 两种训练模型方式fit和fit_generator(节省内存) 提供了对比,fit 和 fit_generator 两种代码
国外一个大佬Adrian Rosebrock写的博客:实现自定义Keras fit_generator函数(中文) How to use Keras fit and fit_generator (a hands-on tutorial) (英文)
利用fit_generator最小化显存占用比率/数据Batch化
1 #从节省内存的角度,通过生成器的方式来训练 2 def data_generator(data, targets, batch_size): 3 idx = np.arange(len(data)) 4 np.random.shuffle(idx) 5 batches = [idx[range(batch_size*i, min(len(data), batch_size*(i+1)))] for i in range(len(data)/batch_size+1)] 6 while True: 7 for i in batches: 8 xx, yy = np.array(map(gen_matrix, data[i])), np.array(map(gen_target, targets[i])) 9 yield (xx, yy) 10 11 12 batch_size = 1024 13 history = model.fit_generator(data_generator(d['words'], d['label'], batch_size), samples_per_epoch=len(d), nb_epoch=200) 14 model.save_weights('words_seq2seq_final_1.model')
keras数据自动生成器,继承keras.utils.Sequence,结合fit_generator实现节约内存训练
提前shuffle数据:
shuffle数据并且设置callback:存储最佳weight
1 np.random.seed(1024) 2 random.shuffle(index) 3 data = data[index] 4 label = label[index] 5 splitpoint = int(round(num * 0.8)) 6 (X_train, X_val) = (data[0:splitpoint], data[splitpoint:]) 7 (Y_train, Y_val) = (label[0:splitpoint], label[splitpoint:]) 8 X_train=X_train/255 9 X_val=X_val/255
提供了三种方法,非常详细