资源简介

Keras实现经典的卷积神经网络用于cifar10图像分类:NIN,VGG,ResNet,DenseNet,SeNet

资源截图

代码片段和文件信息

import keras
import numpy as np
from keras import optimizers
from keras.datasets import cifar10
from keras.models import Sequential
from keras.layers import Conv2D Dense Flatten MaxPooling2D
from keras.callbacks import LearningRateScheduler TensorBoard
from keras.preprocessing.image import ImageDataGenerator

batch_size    = 128
epochs        = 200
iterations    = 391
num_classes   = 10
log_filepath  = ‘./lenet_dp_da‘
mean          = [125.307 122.95 113.865]
std           = [62.9932 62.0887 66.7048]

def build_model():
    model = Sequential()
    model.add(Conv2D(6 (5 5) padding=‘valid‘ activation = ‘relu‘ kernel_initializer=‘he_normal‘ input_shape=(32323)))
    model.add(MaxPooling2D((2 2) strides=(2 2)))
    model.add(Conv2D(16 (5 5) padding=‘valid‘ activation = ‘relu‘ kernel_initializer=‘he_normal‘))
    model.add(MaxPooling2D((2 2) strides=(2 2)))
    model.add(Flatten())
    model.add(Dense(120 activation = ‘relu‘ kernel_initializer=‘he_normal‘))
    model.add(Dense(84 activation = ‘relu‘ kernel_initializer=‘he_normal‘))
    model.add(Dense(10 activation = ‘softmax‘ kernel_initializer=‘he_normal‘))
    sgd = optimizers.SGD(lr=.1 momentum=0.9 nesterov=True)
    model.compile(loss=‘categorical_crossentropy‘ optimizer=sgd metrics=[‘accuracy‘])
    return model

def scheduler(epoch):
    if epoch <= 60:
        return 0.05
    if epoch <= 120:
        return 0.01
    if epoch <= 160:    
        return 0.002
    return 0.0004

if __name__ == ‘__main__‘:

    # load data
    (x_train y_train) (x_test y_test) = cifar10.load_data()
    y_train = keras.utils.to_categorical(y_train num_classes)
    y_test = keras.utils.to_categorical(y_test num_classes)
    x_train = x_train.astype(‘float32‘)
    x_test = x_test.astype(‘float32‘)
    
    # data preprocessing  [raw - mean / std]
    for i in range(3):
        x_train[:::i] = (x_train[:::i] - mean[i]) / std[i]
        x_test[:::i] = (x_test[:::i] - mean[i]) / std[i]
        
    # build network
    model = build_model()
    print(model.summary())
    # set callback
    tb_cb = TensorBoard(log_dir=log_filepath histogram_freq=0)
    change_lr = LearningRateScheduler(scheduler)
    cbks = [change_lrtb_cb]

    # using real-time data augmentation
    print(‘Using real-time data augmentation.‘)
    datagen = ImageDataGenerator(horizontal_flip=True
            width_shift_range=0.125height_shift_range=0.125fill_mode=‘constant‘cval=0.)

    datagen.fit(x_train)

    # start traing 
    model.fit_generator(datagen.flow(x_train y_trainbatch_size=batch_size)
                        steps_per_epoch=iterations
                        epochs=epochs
                        callbacks=cbks
                        validation_data=(x_test y_test))
    # save model
    model.save(‘lenet.h5‘)




 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-12-20 02:00  cifar-10-cnn-master\
     目录           0  2017-12-20 02:00  cifar-10-cnn-master\1_Lecun_Network\
     文件        2839  2017-12-20 02:00  cifar-10-cnn-master\1_Lecun_Network\LeNet_dp_da_keras.py
     文件        3081  2017-12-20 02:00  cifar-10-cnn-master\1_Lecun_Network\LeNet_dp_da_wd_keras.py
     文件        2380  2017-12-20 02:00  cifar-10-cnn-master\1_Lecun_Network\LeNet_dp_keras.py
     文件        2119  2017-12-20 02:00  cifar-10-cnn-master\1_Lecun_Network\LeNet_keras.py
     目录           0  2017-12-20 02:00  cifar-10-cnn-master\2_Network_in_Network\
     文件        4654  2017-12-20 02:00  cifar-10-cnn-master\2_Network_in_Network\Network_in_Network_bn_keras.py
     文件        4285  2017-12-20 02:00  cifar-10-cnn-master\2_Network_in_Network\Network_in_Network_keras.py
     目录           0  2017-12-20 02:00  cifar-10-cnn-master\2_Network_in_Network\nin\
     文件      422438  2017-12-20 02:00  cifar-10-cnn-master\2_Network_in_Network\nin\events.out.tfevents.1501857002.dlsummer-BM1AF-BP1AF-BM6AF
     目录           0  2017-12-20 02:00  cifar-10-cnn-master\2_Network_in_Network\nin_bn\
     文件     1776892  2017-12-20 02:00  cifar-10-cnn-master\2_Network_in_Network\nin_bn\events.out.tfevents.1501979688.bg-CGI
     目录           0  2017-12-20 02:00  cifar-10-cnn-master\3_Vgg19_Network\
     文件        7645  2017-12-20 02:00  cifar-10-cnn-master\3_Vgg19_Network\Vgg19_keras.py
     文件        1582  2017-12-20 02:00  cifar-10-cnn-master\3_Vgg19_Network\Vgg_prediction.py
     目录           0  2017-12-20 02:00  cifar-10-cnn-master\3_Vgg19_Network\test_pic\
     文件       82410  2017-12-20 02:00  cifar-10-cnn-master\3_Vgg19_Network\test_pic\cat.jpg
     文件        8892  2017-12-20 02:00  cifar-10-cnn-master\3_Vgg19_Network\test_pic\puzzle.jpeg
     文件       12270  2017-12-20 02:00  cifar-10-cnn-master\3_Vgg19_Network\test_pic\tiger.jpeg
     目录           0  2017-12-20 02:00  cifar-10-cnn-master\4_Residual_Network\
     文件        5255  2017-12-20 02:00  cifar-10-cnn-master\4_Residual_Network\ResNet_keras.py
     目录           0  2017-12-20 02:00  cifar-10-cnn-master\5_Wide_Residual_Network\
     文件        5003  2017-12-20 02:00  cifar-10-cnn-master\5_Wide_Residual_Network\Wide_ResNet_keras.py
     目录           0  2017-12-20 02:00  cifar-10-cnn-master\6_ResNeXt\
     文件        5421  2017-12-20 02:00  cifar-10-cnn-master\6_ResNeXt\ResNeXt_keras.py
     目录           0  2017-12-20 02:00  cifar-10-cnn-master\7_DenseNet\
     文件        5248  2017-12-20 02:00  cifar-10-cnn-master\7_DenseNet\DenseNet_keras.py
     目录           0  2017-12-20 02:00  cifar-10-cnn-master\8_SENet\
     文件        6442  2017-12-20 02:00  cifar-10-cnn-master\8_SENet\SENet_Keras.py
     目录           0  2017-12-20 02:00  cifar-10-cnn-master\9_Multi-GPU\
............此处省略21个文件信息

评论

共有 条评论