<jupyter_start><jupyter_text>Keras implementation of Brain CNN<jupyter_code>import tensorflow as tf
import numpy as np
from keras.utils.np_utils import to_categorical 
from keras.models import Sequential
from keras.layers import Dense, Activation, Flatten
from keras.layers.convolutional import Convolution2D, Conv2D
from keras.layers.pooling import MaxPooling2D
from keras.layers.normalization import BatchNormalization
from keras.optimizers import SGD
from keras.optimizers import Adam
from keras.regularizers import l2
from keras.callbacks import ModelCheckpoint
IMG_DIM = 64
SNAPS = 8 ##Number of snaps over 4 seconds
STRIP_DEPTH = 8
STRIP_WIDTH = 42 * SNAPS
STRIP_HEIGHT = 51 * STRIP_DEPTH
SLICE = 1
CHANNELS = 1
CLASSES = 2
conv1_filter = 4
conv2_filter = 8
conv3_filter = 8
conv4_filter = 8
conv5_filter = 32
conv6_filter = 32
experiment = 's8snap'
path = './datasets/' + experiment + '/'

_04847_img = np.load(path + '4847_' + experiment + '-image.npy')
_04799_img = np.load(path + '4799_' + experiment + '-image.npy')
_04820_img = np.load(path + '4820_' + experiment + '-image.npy')
_05675_img = np.load(path + '5675_' + experiment + '-image.npy')
_05680_img = np.load(path + '5680_' + experiment + '-image.npy')
_05710_img = np.load(path + '5710_' + experiment + '-image.npy')

_04847_lbl = np.load(path + '4847_' + experiment + '-label-onehot.npy')
_04799_lbl = np.load(path + '4799_' + experiment + '-label-onehot.npy')
_04820_lbl = np.load(path + '4820_' + experiment + '-label-onehot.npy')
_05675_lbl = np.load(path + '5675_' + experiment + '-label-onehot.npy')
_05680_lbl = np.load(path + '5680_' + experiment + '-label-onehot.npy')
_05710_lbl = np.load(path + '5710_' + experiment + '-label-onehot.npy')<jupyter_output><empty_output><jupyter_text>Leave on example out validation<jupyter_code>train_img = np.vstack((_04847_img[5:,], _04799_img[5:,], _04820_img[5:,], _05675_img[5:,], _05680_img[5:,]))
train_lbl = np.vstack((_04847_lbl[5:,], _04799_lbl[5:,], _04820_lbl[5:,], _05675_lbl[5:,], _05680_lbl[5:,]))
val_img = np.vstack((_04847_img[:5,], _04799_img[:5,], _04820_img[:5,], _05675_img[:5,], _05680_img[:5,]))
val_lbl = np.vstack((_04847_lbl[:5,], _04799_lbl[:5,], _04820_lbl[:5,], _05675_lbl[:5,], _05680_lbl[:5,]))

# n = 10
# train_img = np.vstack((_04847_img[n:,], _04799_img[n:,], _04820_img[n:,], _05675_img[n:,], _05680_img[n:,], _05710_img[n:,]))
# train_lbl = np.vstack((_04847_lbl[n:,], _04799_lbl[n:,], _04820_lbl[n:,], _05675_lbl[n:,], _05680_lbl[n:,], _05710_lbl[n:,]))
# val_img = np.vstack((_04847_img[:n,], _04799_img[:n,], _04820_img[:n,], _05675_img[:n,], _05680_img[:n,], _05710_img[:n,]))
# val_lbl = np.vstack((_04847_lbl[:n,], _04799_lbl[:n,], _04820_lbl[:n,], _05675_lbl[:n,], _05680_lbl[:n,], _05710_img[:n,]))
# %matplotlib inline
# import matplotlib.pyplot as plt

# plt.imshow(xtrain[1,:,:,0])
# print xtrain[1].shape
np.random.seed(0)
# shuffle = np.random.permutation(database.shape[0])

# test = database[shuffle[0:100],:]
# val = database[shuffle[100:200],:]
# train = database[shuffle[200:],:]

xtrain = np.reshape(train_img, (train_img.shape[0], train_img.shape[1], train_img.shape[2], 1))
ytrain = train_lbl

xval = np.reshape(val_img, (val_img.shape[0], val_img.shape[1], val_img.shape[2], 1))
yval = val_lbl

### Model ###                                                                                                                               
model = Sequential()
mde = 0                                                                                                                                                                      
k_init = 'he_normal'
ridge = 0.0001

model.add(Convolution2D(conv1_filter, kernel_size=(3, 3), strides=(1, 1),
                        padding='same', data_format="channels_last", activation=None, use_bias=True,
                        kernel_regularizer=l2(ridge),
                        kernel_initializer=k_init, bias_initializer='zeros', input_shape=(STRIP_HEIGHT, STRIP_WIDTH, CHANNELS)))                                                                                                   
model.add(BatchNormalization())
model.add(Activation('relu'))

model.add(Convolution2D(conv2_filter, kernel_size=(5, 5), strides=(1, 1),
                        padding='same', data_format="channels_last", activation=None, use_bias=True,
                        kernel_regularizer=l2(ridge),
                        kernel_initializer=k_init, bias_initializer='zeros'))                                                                                                   
model.add(BatchNormalization())
model.add(Activation('relu'))

model.add(MaxPooling2D(pool_size = (2, 2), strides=2, padding='valid'))

# model.add(Convolution2D(conv3_filter, kernel_size=(7, 7), strides=(1, 1),
#                         padding='same', data_format="channels_last", activation=None, use_bias=True,
#                         kernel_regularizer=l2(ridge),
#                         kernel_initializer=k_init, bias_initializer='zeros'))                                                                                                   
# model.add(BatchNormalization())
# model.add(Activation('relu'))

# model.add(Convolution2D(conv4_filter, kernel_size=(9, 9), strides=(1, 1),
#                         padding='same', data_format="channels_last", activation=None, use_bias=True,
#                         kernel_regularizer=l2(ridge),
#                         kernel_initializer=k_init, bias_initializer='zeros'))                                                                                                   
# model.add(BatchNormalization())
# model.add(Activation('relu'))

# model.add(MaxPooling2D(pool_size = (2, 2), strides=2, padding='valid'))

# model.add(Flatten())
# model.add(Dense(1024, kernel_initializer=k_init,kernel_regularizer=l2(ridge)))
# model.add(BatchNormalization())
# model.add(Activation('relu'))
# model.add(Dense(512, kernel_initializer=k_init,kernel_regularizer=l2(ridge)))
# model.add(BatchNormalization())
# model.add(Activation('relu'))
# model.add(Dense(256, kernel_initializer=k_init,kernel_regularizer=l2(ridge)))
# model.add(BatchNormalization())
# model.add(Activation('relu'))
# model.add(Dense(CLASSES, kernel_initializer=k_init,kernel_regularizer=l2(ridge)))
# model.add(BatchNormalization())
# model.add(Activation('softmax'))

Lr = 1e-4 
dcy = 1e-6                                                                                                                                                                                
m = 0.5
# sgd = SGD(lr=Lr, momentum=m, decay=dcy,  nesterov=True)
adam = Adam(lr=Lr, decay=dcy)

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

model.summary()
print('learning rate: %f, decay: %f' %(Lr, dcy))

from keras.backend import get_session
get_session().run(tf.global_variables_initializer())
a = model.fit(xtrain, ytrain, batch_size = 5, epochs= 100, verbose = 1)

loss_and_metrics = model.evaluate(xval, yval, batch_size=5)

print "Loss and accuracy: ", loss_and_metrics<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 408, 336, 4)       40        
_________________________________________________________________
batch_normalization_1 (Batch (None, 408, 336, 4)       16        
_________________________________________________________________
activation_1 (Activation)    (None, 408, 336, 4)       0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 408, 336, 8)       808       
_________________________________________________________________
batch_normalization_2 (Batch (None, 408, 336, 8)       32        
_________________________________________________________________
activation_2 (Activation)    (None, 408, 336, 8)       0         
_________________________________________________________________
max_poolin[...]
{'fraction_non_alphanumeric': 0.14308641975308642, 'fraction_numerical': 0.06641975308641976, 'mean_word_length': 2.965247185511503, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '528284', 'license': 'MIT', 'n_tokens_mistral': 2900, 'n_tokens_neox': 2589, 'n_words': 441, 'path': 'experiments/408_672_1channel_16snaps_subject/brain_cnn_keras.ipynb', 'repo_name': 'ArkAung/brain_cnn', 'size': '8100'}