|
import numpy as np |
|
import scipy as sp |
|
import datetime |
|
|
|
import cifar10 |
|
import cifar10_sub150823 as cifar10_sub |
|
import convnet150907 as convnet |
|
|
|
|
|
# Conv-Pool-Conv-Pool-ReLu-ReLu-Softmax |
|
def CPCPRRS( Xnch, Xrow, Xcol, K, dropout = False ): |
|
|
|
if dropout: |
|
do = [ 0.8, 0.8, 0.8, 0.5, 0.5, 1.0 ] |
|
else: |
|
do = [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] |
|
|
|
bmode = 'full' |
|
|
|
# 0th Layer |
|
Xdim = ( Xnch, Xrow, Xcol ) |
|
L0 = convnet.T4InputLayer( Xdim, dropout = do[0] ) |
|
|
|
# 1st Layer |
|
W1dim = ( 128, 5, 5 ) |
|
L1conv = convnet.ConvLayer( Xdim, W1dim, 'ReLu', withBias = True, border_mode = bmode ) |
|
ds1, st1 = ( 2, 2 ), None |
|
L1pool = convnet.PoolLayer( L1conv.Yshape, ds1, st = st1, dropout = do[1] ) |
|
H1 = L1pool.Dout |
|
|
|
# 2nd Layer |
|
W2dim = ( 128, 5, 5 ) |
|
L2conv = convnet.ConvLayer( L1pool.Yshape, W2dim, 'ReLu', withBias = True, border_mode = bmode ) |
|
ds2, st2 = ( 2, 2 ), None |
|
L2pool = convnet.PoolLayer( L2conv.Yshape, ds2, st = st2, dropout = do[2] ) |
|
H2 = L2pool.Dout |
|
|
|
# 3rd Layer |
|
H3 = 1000 |
|
L3 = convnet.FullLayer( H2, H3, 'ReLu', withBias = True, dropout = do[3], T4toMat = True ) |
|
|
|
# 4th Layer |
|
H4 = 1000 |
|
L4 = convnet.FullLayer( H3, H4, 'ReLu', withBias = True, dropout = do[4], T4toMat = False ) |
|
|
|
# 5th Layer |
|
L5 = convnet.FullLayer( H4, K, 'linear', withBias = True, dropout = do[5], T4toMat = False ) |
|
|
|
cnn = convnet.CNN( [ L0, L1conv, L1pool, L2conv, L2pool, L3, L4, L5 ] ) |
|
|
|
|
|
print '### Conv-Pool-Conv-Pool-ReLu-ReLu-Softmax' |
|
print '# T4InputLayer: ', L0.Xshape, ' dropout = ', L0.dropout |
|
layer = L1conv |
|
print '# ConvLayer: ', layer.Wshape, ' bmode = ', layer.border_mode, ' dropout = ', layer.dropout |
|
layer = L1pool |
|
print '# PoolLayer: ', layer.Yshape, ' ds = ', layer.ds, ' st = ', layer.st, ' dropout = ', layer.dropout |
|
layer = L2conv |
|
print '# ConvLayer: ', layer.Wshape, ' bmode = ', layer.border_mode, ' dropout = ', layer.dropout |
|
layer = L2pool |
|
print '# PoolLayer: ', layer.Yshape, ' ds = ', layer.ds, ' st = ', layer.st, ' dropout = ', layer.dropout |
|
layer = L3 |
|
print '# FullLayer: ', layer.Din, layer.Nunit, layer.afunc, layer.dropout |
|
layer = L4 |
|
print '# FullLayer: ', layer.Din, layer.Nunit, layer.afunc, layer.dropout |
|
layer = L5 |
|
print '# FullLayer: ', layer.Din, layer.Nunit, layer.afunc, layer.dropout |
|
|
|
|
|
return cnn |
|
|
|
|
|
# computing the recognition rate |
|
def recograte( cnn, X, label, batchsize, dstshape ): |
|
|
|
N = X.shape[0] |
|
nbatch = int( np.ceil( float( N ) / batchsize ) ) |
|
|
|
LL = 0.0 |
|
cnt = 0 |
|
for ib in range( nbatch - 1 ): |
|
ii = np.arange( ib*batchsize, (ib+1)*batchsize ) |
|
XX = cifar10_sub.clipcenter( X[ii], dstshape ) |
|
Z = cnn.output( XX ) |
|
LL += cnn.cost( Z, label[ii] ) * ii.shape[0] |
|
cnt += np.sum( label[ii] == np.argmax( Z, axis = 1 ) ) |
|
ib = nbatch - 1 |
|
ii = np.arange( ib*batchsize, N ) |
|
XX = cifar10_sub.clipcenter( X[ii], dstshape ) |
|
Z = cnn.output( XX ) |
|
LL += cnn.cost( Z, label[ii] ) * ii.shape[0] |
|
cnt += np.sum( label[ii] == np.argmax( Z, axis = 1 ) ) |
|
|
|
return LL / N, float( cnt ) / N |
|
|
|
|
|
def weightnorm( cnn ): |
|
|
|
W2list = [] |
|
for layer in cnn.Layers: |
|
if isinstance( layer, convnet.T4InputLayer ) or isinstance( layer, convnet.PoolLayer ): |
|
continue |
|
Wb = layer.getWeight() |
|
if layer.withBias: |
|
W = Wb[0] |
|
else: |
|
W = Wb |
|
W2list.append( np.mean( np.square( W ) ) ) |
|
|
|
return np.asarray( W2list ) |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
idstr = datetime.datetime.now().strftime('%Y%m%d-%H%M%S') |
|
print '### ID: ', idstr |
|
|
|
dirCIFAR10 = '../140823-pylearn2/data/cifar10/cifar-10-batches-py' |
|
cifar = cifar10.CIFAR10( dirCIFAR10 ) |
|
ZCAwhitening = True |
|
tfunc = cifar10_sub.translate2 |
|
dstshape = ( 24, 24 ) |
|
|
|
|
|
##### setting the training data & the validation data |
|
# |
|
Xraw, label, t = cifar.loadData( 'L' ) |
|
Xraw /= 255 |
|
xm = np.mean( Xraw, axis = 0 ) |
|
Xraw -= xm |
|
if ZCAwhitening: |
|
X, Uzca = cifar10_sub.ZCAtrans( Xraw, Uzca = None ) |
|
else: |
|
X = Xraw |
|
X = np.asarray( X, dtype = np.float32 ) |
|
label = np.asarray( label, dtype = np.int32 ) |
|
|
|
idxL, idxV = cifar.genIndexLV( label ) |
|
XL, labelL = X[idxL], label[idxL] |
|
XV, labelV = X[idxV], label[idxV] |
|
NL, Xnch = XL.shape[0], XL.shape[1] |
|
Xrow, Xcol = dstshape[0], dstshape[1] |
|
NV = XV.shape[0] |
|
K = cifar.nclass |
|
Xdim = ( Xrow, Xcol ) |
|
|
|
np.random.seed( 0 ) |
|
batchsize = 100 |
|
idxB = cifar10_sub.makebatchindex( NL, batchsize ) |
|
nbatch = idxB.shape[0] |
|
|
|
|
|
##### setting the test data |
|
# |
|
XTraw, labelT, tT = cifar.loadData( 'T' ) |
|
XTraw /= 255 |
|
XTraw -= xm |
|
if ZCAwhitening: |
|
XT = cifar10_sub.ZCAtrans( XTraw, Uzca = Uzca ) |
|
else: |
|
XT = XTraw |
|
XT = np.asarray( XT, dtype = np.float32 ) |
|
labelT = np.asarray( labelT, dtype = np.int32 ) |
|
NT = XT.shape[0] |
|
|
|
|
|
##### initializing |
|
# |
|
cnn = CPCPRRS( Xnch, Xrow, Xcol, K, dropout = False ) |
|
|
|
|
|
##### training |
|
# |
|
eta, mu, lam = 0.01, 0.95, 0.0 |
|
#eta, mu, lam = 0.01, 0.95, 0.0001 |
|
nepoch = 200 |
|
print '# eta = ', eta, ' mu = ', mu, ' lam = ', lam |
|
print '# ZCAwhitening = ', ZCAwhitening, ' tfunc = ', tfunc.__name__, ' dstshape = ', dstshape |
|
|
|
print '### training: NL = ', NL, ' NV = ', NV, ' K = ', K, ' batchsize = ', batchsize |
|
|
|
i = 0 |
|
mnLLL, rrL = recograte( cnn, XL, labelL, batchsize, dstshape ) |
|
mnLLV, rrV = recograte( cnn, XV, labelV, batchsize, dstshape ) |
|
print '%d | %.4f %.2f | %.4f %.2f' % ( i, mnLLL, rrL * 100, mnLLV, rrV * 100 ), |
|
w2 = weightnorm( cnn ) |
|
print ' | ', w2 |
|
|
|
for i in range( 1, nepoch ): |
|
|
|
# training (selecting each batch in random order) |
|
for ib in np.random.permutation( nbatch ): |
|
ii = idxB[ib, :] |
|
XX = tfunc( XL[ii], dstshape ) |
|
cnn.train( XX, labelL[ii], eta, mu, lam ) |
|
|
|
# printing error rates etc. |
|
if (i < 10) or ( i % 10 == 0 ): |
|
mnLLL, rrL = recograte( cnn, XL, labelL, batchsize, dstshape ) |
|
mnLLV, rrV = recograte( cnn, XV, labelV, batchsize, dstshape ) |
|
print '%d | %.4f %.2f | %.4f %.2f' % ( i, mnLLL, rrL * 100, mnLLV, rrV * 100 ), |
|
if i % 50 == 0: |
|
mnLLT, rrT = recograte( cnn, XT, labelT, batchsize, dstshape ) |
|
print '| %.4f %.2f' % ( mnLLT, rrT * 100 ), |
|
|
|
w2 = weightnorm( cnn ) |
|
print ' | ', w2 |
|
|
|
|
|
i = nepoch |
|
|
|
##### setting the test data |
|
# |
|
print '# NT = ', NT |
|
mnLLT, rrT = recograte( cnn, XT, labelT, batchsize, dstshape ) |
|
print '%d | %.4f %.2f | %.4f %.2f | %.4f %.2f' % ( i, mnLLL, rrL * 100, mnLLV, rrV * 100, mnLLT, rrT * 100 ) |
|
|
|
print '### ID: ', idstr |