美文网首页ML
keras 训练网络初步

keras 训练网络初步

作者: SIAAAAAA | 来源:发表于2017-03-29 16:10 被阅读8912次

整理写程序时对keras疑问。

keras代码流程:

1.读取dataset

tips:文件夹里有许多文件,可能有隐藏文件什么的,要设置ignore the hidden

 dirlist = []
        files = os.listdir(Dir+'/Detection')
        print('crop pic for detection...')
        # ignore the hidden documents
        for f in files:
            if (os.path.isdir(Dir + '/')):
                if (f[0] == '.'):
                    pass
                else:
                    dirlist.append(f)

作为需要切patch的dataset,读了以后就顺手切图好了。 注意四周切到外围用mirror的细节,现在写的代码这一块太low了,以后要改,提高切图效率。切完了以后改code要加保存一下下一步。

做好了data和label后,记得shuffle。 (自己load的数据集,不是用它给的load函数的话。keras官方里说shuffle有坑,注意。)



shuffle和validation_split的顺序
模型的fit函数有两个参数,shuffle用于将数据打乱,validation_split用于在没有提供验证集的时候,按一定比例从训练集中取出一部分作为验证集
这里有个陷阱是,程序是先执行validation_split,再执行shuffle的,所以会出现这种情况:
假如你的训练集是有序的,比方说正样本在前负样本在后,又设置了validation_split,那么你的验证集中很可能将全部是负样本
同样的,这个东西不会有任何错误报出来,因为Keras不可能知道你的数据有没有经过shuffle,保险起见如果你的数据是没shuffle过的,最好手动shuffle一下

np.random.seed(1024)
random.shuffle(index)
data = data[index]
label = label[index]
splitpoint = int(round(num * 0.8))
(X_train, X_val) = (data[0:splitpoint], data[splitpoint:])
(Y_train, Y_val) = (label[0:splitpoint], label[splitpoint:])
X_train=X_train/255
X_val=X_val/255
  1. 写个model
    我写在 model.py里了,感觉这样train程序简洁一些。
    注意用tf 和th 的维度顺序不同。 tensorflow是 num x H x W x Channel
  2. 编译 需要设置 loss 等
    如果自己写的label形如[0,0,1,2,3,4,、、、、]
from keras.utils.np_utils import to_categorical
categorical_labels = to_categorical(int_labels, nb_classes=None)
  1. 将模型存入json
model_json = model.to_json()
with open("model.json", 'w') as json_file:
    json_file.write(model_json)
  1. model.summary 运行时查看model
  2. 用data_augmentation进行实时数据扩充:
    搜索时找到了应用sklearn网格调参的文章 http://geek.csdn.net/news/detail/95494
    设置callback可用来做一些动作: 存储最佳weight,调整学习率,tensorboard可视化。
best_weights_filepath = './best_we
earlyStopping=kcallbacks.EarlyStopping(monitor='val_loss', 
patience=20, verbose=1, mode='auto')
saveBestModel = kcallbacks.ModelCheckpoint(best_weights_filepath, 
monitor='val_loss', verbose=1, save_best_only=True, mode='auto')
reduce_lr = kcallbacks.ReduceLROnPlateau(monitor='val_loss', 
factor=1/math.e,verbose=1, patience=10, min_lr=0.0001)
tensorboard = kcallbacks.TensorBoard(log_dir='/home/amanda/anaconda2/
envs/tensorflow/lib/python2.7/site-packages/keras/examples/log/softmax/epoch')

callback放到这用:

    # Fit the model on the batches generated by datagen.flow().
    model.fit_generator(datagen.flow(X_train, Ytrain_cate,
                                     batch_size=batch_size),
                        steps_per_epoch=X_train.shape[0] // batch_size,
                        epochs=epochs,
                        validation_data=(X_val, Yval_cate),
                        verbose=2,
                        callbacks=[earlyStopping,saveBestModel,reduce_lr,tensorboard])

相关文章

网友评论

    本文标题:keras 训练网络初步

    本文链接:https://www.haomeiwen.com/subject/gbtyottx.html