機器學習三 卷積神經網絡作業


本來這門課程http://speech.ee.ntu.edu.tw/~tlkagk/courses_ML16.html 作業是用卷積神經網絡做半監督學習,這個還沒完全解決,於是先從基礎的開始,用keras 實現cifar10。

以下是代碼

  1 # -*- coding: utf-8 -*-
  2 __author__ = 'Administrator'
  3 
  4 
  5 from keras.datasets import cifar10
  6 from keras.utils import np_utils
  7 from keras.models import Sequential
  8 from keras.layers import Convolution2D, MaxPooling2D
  9 from keras.layers import Dense, Dropout, Activation, Flatten
 10 from keras.optimizers import SGD
 11 from keras.preprocessing.image import ImageDataGenerator
 12 import matplotlib.pyplot as plt
 13 
 14 # 下載數據
 15 (X_train, y_train), (X_test, y_test) = cifar10.load_data()
 16 print('X_train shape:', X_train.shape)
 17 print(X_train.shape[2], 'train samples')
 18 
 19 #對訓練和測試數據處理,轉為float
 20 X_train = X_train.astype('float32')
 21 X_test = X_test.astype('float32')
 22 #對數據進行歸一化到0-1 因為圖像數據最大是255
 23 X_train /= 255
 24 X_test /= 255
 25 
 26 #一共10類
 27 nb_classes = 10
 28 
 29 # 將標簽進行轉換為one-shot
 30 Y_train = np_utils.to_categorical(y_train, nb_classes)
 31 Y_test = np_utils.to_categorical(y_test, nb_classes)
 32 
 33 #搭建網絡
 34 model = Sequential()
 35 # 2d卷積核,包括32個3*3的卷積核  因為X_train的shape是【樣本數,通道數,圖寬度,圖高度】這樣排列的,而input_shape不需要(也不能)指定樣本數。
 36 model.add(Convolution2D(32, 3, 3, border_mode='same',
 37                         input_shape=X_train.shape[1:]))#指定輸入數據的形狀
 38 model.add(Activation('relu'))#激活函數
 39 model.add(Convolution2D(32, 3, 3))
 40 model.add(Activation('relu'))
 41 model.add(MaxPooling2D(pool_size=(2, 2)))                #maxpool
 42 model.add(Dropout(0.25))                                 #dropout
 43 model.add(Flatten())                                     #壓扁平准備全連接
 44 #全連接
 45 model.add(Dense(512))                                    #添加512節點的全連接
 46 model.add(Activation('relu'))                           #激活
 47 model.add(Dropout(0.5))
 48 model.add(Dense(nb_classes))                             #添加輸出10個節點
 49 model.add(Activation('softmax'))                         #采用softmax激活
 50 
 51 #設定求解器
 52 sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
 53 model.compile(loss='categorical_crossentropy',
 54               optimizer=sgd,
 55               metrics=['accuracy'])
 56 #進行訓練
 57 batch_size = 32
 58 nb_epoch = 200
 59 data_augmentation = False #是否數據擴充,主要針對樣本過小方案
 60 
 61 if not data_augmentation:
 62     print('Not using data augmentation.')
 63     result=model.fit(X_train, Y_train,
 64               batch_size=batch_size,
 65               nb_epoch=nb_epoch,
 66               validation_data=(X_test, Y_test),
 67               shuffle=True)
 68 else:
 69     print('Using real-time data augmentation.')
 70 
 71     # this will do preprocessing and realtime data augmentation
 72     datagen = ImageDataGenerator(
 73         featurewise_center=False,  # set input mean to 0 over the dataset
 74         samplewise_center=False,  # set each sample mean to 0
 75         featurewise_std_normalization=False,  # divide inputs by std of the dataset
 76         samplewise_std_normalization=False,  # divide each input by its std
 77         zca_whitening=False,  # apply ZCA whitening
 78         rotation_range=0,  # randomly rotate images in the range (degrees, 0 to 180)
 79         width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)
 80         height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)
 81         horizontal_flip=True,  # randomly flip images
 82         vertical_flip=False)  # randomly flip images
 83 
 84     # compute quantities required for featurewise normalization
 85     # (std, mean, and principal components if ZCA whitening is applied)
 86     datagen.fit(X_train)
 87 
 88     # fit the model on the batches generated by datagen.flow()
 89     result=model.fit_generator(datagen.flow(X_train, Y_train,
 90                         batch_size=batch_size),
 91                         samples_per_epoch=X_train.shape[0],
 92                         nb_epoch=nb_epoch,
 93                         validation_data=(X_test, Y_test))
 94 
 95 #model.save_weights(weights,accuracy=False)
 96 
 97 # 繪制出結果
 98 plt.figure
 99 plt.plot(result.epoch,result.history['acc'],label="acc")
100 plt.plot(result.epoch,result.history['val_acc'],label="val_acc")
101 plt.scatter(result.epoch,result.history['acc'],marker='*')
102 plt.scatter(result.epoch,result.history['val_acc'])
103 plt.legend(loc='under right')
104 plt.show()
105 plt.figure
106 plt.plot(result.epoch,result.history['loss'],label="loss")
107 plt.plot(result.epoch,result.history['val_loss'],label="val_loss")
108 plt.scatter(result.epoch,result.history['loss'],marker='*')
109 plt.scatter(result.epoch,result.history['val_loss'],marker='*')
110 plt.legend(loc='upper right')
111 plt.show()

以下是正確率和損失曲線

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM