Building AlexNet Model

Kategori: Deep Learning , 29 Nisan 2020 , JanFranco


AlexNet 2012 yılında Alex Krizhevsky, Ilya Sutskever ve Geoffrey Hinton tarafından geliştirilmiştir. Resimden de görüldüğü gibi çok yüksek filtre ve bileşen sayılarına sahiptir. 5 Convolution katmanı, 3 Dense Layer mevcut. Birlikte AlexNeti oluşuturalım ve CIFAR10 veriseti üzerinden train edelim. CIFAR10 veriseti de keras kütüphanesinde hazır olarak bulunmaktadır. Verisetini import edelim, parçalara ayıralım:


import cv2
import numpy as np
from keras import utils
from keras.datasets import cifar10

(x_train, y_train), (x_test, y_test) = cifar10.load_data()
Veriseti üzerinde preprocessing işlemlerine gerek yoktur zaten yapılmış halde gelmektedir. Verisetinin boyutlarını inceleyelim ve one-hot-encoding işlemini gerçekleştirelim:


print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

num_classes = 10
y_train = utils.to_categorical(y_train, num_classes)
y_test = utils.to_categorical(y_test, num_classes)
Rastgele resimler inceleyelim:


for i in range(10):
  random_num = np.random.randint(0, len(x_train))
  random_img = x_train[random_num]
  cv2.imshow("Random Img", random_img)
  cv2.waitKey(0)
  
cv2.destroyAllWindows()
Modeli oluşturalım:


from keras.regularizers import l2
from keras.models import Sequential
from keras.optimizers import Adadelta
from keras.layers.normalization import BatchNormalization
from keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Activation, Flatten, ZeroPadding2D

model = Sequential()

model.add(Conv2D(96, (11, 11), input_shape=x_train.shape[1:], padding='same', kernel_regularizer=l2(0)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(256, (5, 5), padding='same'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(512, (3, 3), padding='same'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(1024, (3, 3), padding='same'))
model.add(BatchNormalization())
model.add(Activation('relu'))

model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(1024, (3, 3), padding='same'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense(3072))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dropout(0.5))

model.add(Dense(4096))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dropout(0.5))

model.add(Dense(num_classes))
model.add(BatchNormalization())
model.add(Activation('softmax'))

print(model.summary())

model.compile(loss = 'categorical_crossentropy', optimizer = Adadelta(), metrics = ['accuracy'])

>>

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_8 (Conv2D)            (None, 32, 32, 96)        34944     
_________________________________________________________________
batch_normalization_11 (Batc (None, 32, 32, 96)        384       
_________________________________________________________________
activation_11 (Activation)   (None, 32, 32, 96)        0         
_________________________________________________________________
max_pooling2d_7 (MaxPooling2 (None, 16, 16, 96)        0         
_________________________________________________________________
conv2d_9 (Conv2D)            (None, 16, 16, 256)       614656    
_________________________________________________________________
batch_normalization_12 (Batc (None, 16, 16, 256)       1024      
_________________________________________________________________
activation_12 (Activation)   (None, 16, 16, 256)       0         
_________________________________________________________________
max_pooling2d_8 (MaxPooling2 (None, 8, 8, 256)         0         
_________________________________________________________________
zero_padding2d_4 (ZeroPaddin (None, 10, 10, 256)       0         
_________________________________________________________________
conv2d_10 (Conv2D)           (None, 10, 10, 512)       1180160   
_________________________________________________________________
batch_normalization_13 (Batc (None, 10, 10, 512)       2048      
_________________________________________________________________
activation_13 (Activation)   (None, 10, 10, 512)       0         
_________________________________________________________________
max_pooling2d_9 (MaxPooling2 (None, 5, 5, 512)         0         
_________________________________________________________________
zero_padding2d_5 (ZeroPaddin (None, 7, 7, 512)         0         
_________________________________________________________________
conv2d_11 (Conv2D)           (None, 7, 7, 1024)        4719616   
_________________________________________________________________
batch_normalization_14 (Batc (None, 7, 7, 1024)        4096      
_________________________________________________________________
activation_14 (Activation)   (None, 7, 7, 1024)        0         
_________________________________________________________________
zero_padding2d_6 (ZeroPaddin (None, 9, 9, 1024)        0         
_________________________________________________________________
conv2d_12 (Conv2D)           (None, 9, 9, 1024)        9438208   
_________________________________________________________________
batch_normalization_15 (Batc (None, 9, 9, 1024)        4096      
_________________________________________________________________
activation_15 (Activation)   (None, 9, 9, 1024)        0         
_________________________________________________________________
max_pooling2d_10 (MaxPooling (None, 4, 4, 1024)        0         
_________________________________________________________________
flatten_2 (Flatten)          (None, 16384)             0         
_________________________________________________________________
dense_4 (Dense)              (None, 3072)              50334720  
_________________________________________________________________
batch_normalization_16 (Batc (None, 3072)              12288     
_________________________________________________________________
activation_16 (Activation)   (None, 3072)              0         
_________________________________________________________________
dropout_3 (Dropout)          (None, 3072)              0         
_________________________________________________________________
dense_5 (Dense)              (None, 4096)              12587008  
_________________________________________________________________
batch_normalization_17 (Batc (None, 4096)              16384     
_________________________________________________________________
activation_17 (Activation)   (None, 4096)              0         
_________________________________________________________________
dropout_4 (Dropout)          (None, 4096)              0         
_________________________________________________________________
dense_6 (Dense)              (None, 10)                40970     
_________________________________________________________________
batch_normalization_18 (Batc (None, 10)                40        
_________________________________________________________________
activation_18 (Activation)   (None, 10)                0         
=================================================================
Total params: 78,990,642
Trainable params: 78,970,462
Non-trainable params: 20,180
_________________________________________________________________
None
Modelde çok fazla bileşen mevcut olduğundan train işlemi çok uzun sürecektir. Bu nedenle epoch sayısını düşük tutarak train edelim:


batch_size = 32
epochs = 5

history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_test, y_test), shuffle=True)

scores = model.evaluate(x_test, y_test, verbose=1)
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])

>>

Train on 50000 samples, validate on 10000 samples
Epoch 1/5
50000/50000 [==============================] - 210s 4ms/step - loss: 1.3699 - acc: 0.5213 - val_loss: 1.4103 - val_acc: 0.5128
Epoch 2/5
50000/50000 [==============================] - 209s 4ms/step - loss: 1.0137 - acc: 0.6538 - val_loss: 1.9001 - val_acc: 0.4035
Epoch 3/5
50000/50000 [==============================] - 209s 4ms/step - loss: 0.8251 - acc: 0.7197 - val_loss: 1.0614 - val_acc: 0.6375
Epoch 4/5
50000/50000 [==============================] - 209s 4ms/step - loss: 0.6809 - acc: 0.7708 - val_loss: 1.1185 - val_acc: 0.6297
Epoch 5/5
50000/50000 [==============================] - 209s 4ms/step - loss: 0.5521 - acc: 0.8173 - val_loss: 1.0383 - val_acc: 0.6614
10000/10000 [==============================] - 6s 638us/step
Test loss: 1.0383071174621583
Test accuracy: 0.6614
Tüm kodlar birlikte:


import cv2
import numpy as np
from keras import utils
from keras.datasets import cifar10
from keras.regularizers import l2
from keras.models import Sequential
from keras.optimizers import Adadelta
from keras.layers.normalization import BatchNormalization
from keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Activation, Flatten, ZeroPadding2D

(x_train, y_train), (x_test, y_test) = cifar10.load_data()

print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

num_classes = 10
y_train = utils.to_categorical(y_train, num_classes)
y_test = utils.to_categorical(y_test, num_classes)

for i in range(10):
  random_num = np.random.randint(0, len(x_train))
  random_img = x_train[random_num]
  cv2.imshow("Random Img", random_img)
  cv2.waitKey(0)
  
cv2.destroyAllWindows()

model = Sequential()

model.add(Conv2D(96, (11, 11), input_shape=x_train.shape[1:], padding='same', kernel_regularizer=l2(0)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(256, (5, 5), padding='same'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(512, (3, 3), padding='same'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(1024, (3, 3), padding='same'))
model.add(BatchNormalization())
model.add(Activation('relu'))

model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(1024, (3, 3), padding='same'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense(3072))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dropout(0.5))

model.add(Dense(4096))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dropout(0.5))

model.add(Dense(num_classes))
model.add(BatchNormalization())
model.add(Activation('softmax'))

print(model.summary())

model.compile(loss = 'categorical_crossentropy', optimizer = Adadelta(), metrics = ['accuracy'])

batch_size = 32
epochs = 5

history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_test, y_test), shuffle=True)

scores = model.evaluate(x_test, y_test, verbose=1)
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])


Yorumlar

Henüz bir yorum bulunmuyor.
Yorum bırakın