<jupyter_start><jupyter_text><jupyter_code>ls
from google.colab import files
f=files.upload()
!wget --load-cookies ./kaggle.com_cookies.txt https://www.kaggle.com/ashishpatel26/fer2018/download -O data.zip
!unzip data.zip 
import tensorflow as tf
import tensorflow.keras as k
import tensorflow.keras.layers as kl
import numpy as np 
import sklearn as sk
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
data=pd.read_csv("fer20131.csv")
data
import cv2

d={
      0:[0,0,0,0,0,0,1],
      1:[0,0,0,0,0,1,0],
      2:[0,0,0,0,1,0,0],
      3:[0,0,0,1,0,0,0],
      4:[0,0,1,0,0,0,0],
      5:[0,1,0,0,0,0,0],
      6:[1,0,0,0,0,0,0],
  }
def imageform(c):
    img=list(map(int,c.split(" ")))
    img=np.reshape(img,(48,48,1))
    # a=np.ones((len(img),1))
    # a=a*img
    # n=np.reshape(a,(48,48))
    # v=cv2.resize(n,(128,128))
    # v=np.reshape(v,(128,128,1))
    return img/255
def data1(a):
    d={
      0:[0,0,0,0,0,0,1],
      1:[0,0,0,0,0,1,0],
      2:[0,0,0,0,1,0,0],
      3:[0,0,0,1,0,0,0],
      4:[0,0,1,0,0,0,0],
      5:[0,1,0,0,0,0,0],
      6:[1,0,0,0,0,0,0],
      }
    x_train=[]
    x_test=[]
    y_train=[]
    y_spac=[]
    y_test=[]
    for i in a:
        y_spac.append(i[0])
        y_train.append(d[i[0]])
        x_train.append(imageform(i[1]))
        
    return x_train,y_train,y_spac


train_data=data[data["Usage"]=="Training"]
val_data=data[data["Usage"]=="PrivateTest"]
train=[]
for i in range(7):
  train.append(train_data[train_data["emotion"]==i].to_numpy())
l=[]
for i in range(7):
  l.append(np.shape(train[i])[0])
plt.bar(range(7),l)
plt.show()
train_1=train_data[train_data["emotion"]==1].to_numpy()
tdata=train_data.to_numpy()
for i in range(10):
  tdata=np.append(tdata,train_1)
tdata=np.reshape(tdata,(-1,3))
tdata1=pd.DataFrame(tdata)
train=[]
for i in range(7):
  train.append(tdata1[tdata1[0]==i].to_numpy())
l=[]
for i in range(7):
  l.append(np.shape(train[i])[0])

def imageform(c):
  img=list(map(int,c.split(" ")))
  img=np.reshape(img,(48,48,1))
  return img

train_data=data[data["Usage"]=="Training"]
val_data=data[data["Usage"]=="PrivateTest"]
test_data=data[data["Usage"]=="PublicTest"]
train_data=train_data.to_numpy()
val_data=val_data.to_numpy()
test_data=test_data.to_numpy()


x=data1(train_data)
y=data1(val_data)
z=data1(test_data)
row=48
col=48
classes=7
x_train=np.asarray(x[0])
y_train=np.asarray(x[1])
y_strain=np.asarray(x[2])
val_x=np.asarray(y[0])
val_y=np.asarray(y[1])
val_ys=np.asarray(y[2])
x_test=np.asarray(z[0])
y_test=np.asarray(z[1])
y_stest=np.asarray(z[2])
print(np.shape(x_train))
print(np.shape(y_train))
print(np.shape(val_x))
print(np.shape(val_y))
print(np.shape(x_test))
print(np.shape(y_test))
from keras.layers import Input, Conv2D, MaxPooling2D
from keras.layers import Dense, Flatten
from keras.models import Model
from keras.models import Sequential, load_model
from keras.layers import (Activation, Dropout, Flatten, Dense, GlobalMaxPooling2D,
                          BatchNormalization, Input, Conv2D, GlobalAveragePooling2D)
from keras.applications.resnet50 import ResNet50
from keras.callbacks import ModelCheckpoint
from keras import metrics
from keras.optimizers import Adam 
from keras import backend as K

import keras
from keras.models import Model<jupyter_output><empty_output><jupyter_text>1.1   VGG16 was publised in 2014 and is one of the simplest (among the other cnn architectures used in Imagenet competition). It's Key Characteristics are:This network contains total 16 layers in which weights and bias parameters are learnt.A total of 13 convolutional layers are stacked one after the other and 3 dense layers for classification.The number of filters in the convolution layers follow an increasing pattern (similar to decoder architecture of autoencoder).The informative features are obtained by max pooling layers applied at different steps in the architecture.The dense layers comprises of 4096, 4096, and 1000 nodes each.The cons of this architecture are that it is slow to train and produces the model with very large size. ![vgg16.png](data:image/png;base64,iVB<KEY>//s//wd/9/T<KEY>//<KEY>//<KEY>)<jupyter_code>from keras.layers import Input, Conv2D, MaxPooling2D
from keras.layers import Dense, Flatten
from keras.models import Model
from keras.layers import BatchNormalization
vgg=k.Sequential()
vgg.add(Conv2D(filters=64, kernel_size=(3,3), padding="same", activation="relu",input_shape=(48,48,1)))
vgg.add(BatchNormalization())
vgg.add(Conv2D(filters=64, kernel_size=(3,3), padding="same", activation="relu"))
vgg.add(MaxPooling2D((2, 2)))
vgg.add( Conv2D(filters=128, kernel_size=(3,3), padding="same", activation="relu"))
vgg.add(Dropout(0.5))
vgg.add( Conv2D(filters=128, kernel_size=(3,3), padding="same", activation="relu"))
vgg.add(MaxPooling2D((2, 2)))
vgg.add( Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"))
vgg.add( Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"))
vgg.add(Dropout(0.5))
vgg.add( Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"))
vgg.add(MaxPooling2D((2, 2)))
vgg.add( Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
vgg.add( Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
vgg.add(Dropout(0.5))
vgg.add( Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
vgg.add(MaxPooling2D((2, 2)))
vgg.add( Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
vgg.add( Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
vgg.add(Dropout(0.5))
vgg.add( Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
vgg.add(MaxPooling2D((2, 2)))
vgg.add(Flatten())
vgg.add(Dense(4096,activation="relu"))
vgg.add(Dropout(0.5))
vgg.add(Dense(4096,activation="relu"))
vgg.add(Dropout(0.5))
vgg.add(Dense(1024,activation="relu"))
vgg.add(Dense(7,activation="softmax"))
vgg.compile(loss='categorical_crossentropy', optimizer=k.optimizers.Adam(lr=0.0001, decay=1e-6), metrics=['accuracy'])
vgg.summary()
vgg.fit(x_train,y_train,epochs=20,batch_size=32,validation_data=(val_x,val_y))
vgg_output=vgg.evaluate(x_test,y_test)

# calculates precision for 1:100 dataset with 90 tp and 30 fp
from sklearn.metrics import precision_score,recall_score,f1_score
y_pred=vgg.predict(x_test)
def bin_to(x):
  s=[]
  for i in x:
    p=max(i)
    j=np.where(i==p)
    s.append(j)
  return s
yt=np.reshape(bin_to(y_test),(-1))
yp=np.reshape(bin_to(y_pred),(-1))
p=precision_score(yt,yp,labels=[0,1,2,3,4,5,6], average='macro')
r=recall_score(yt,yp,labels=[0,1,2,3,4,5,6],average='macro')
f=f1_score(yt,yp,labels=[0,1,2,3,4,5,6],average='macro')
p
r
f<jupyter_output><empty_output><jupyter_text>---**RESNET50**<jupyter_code>from keras.models import Sequential, load_model
from keras.layers import (Activation, Dropout, Flatten, Dense, GlobalMaxPooling2D,
                          BatchNormalization, Input, Conv2D, GlobalAveragePooling2D)
from keras.applications.resnet50 import ResNet50
from keras.callbacks import ModelCheckpoint
from keras import metrics
from keras.optimizers import Adam 
from keras import backend as K

import keras
from keras.models import Model
function = "softmax"
def resnet50_model(input_shape, n_out):
    input_tensor = Input(shape=input_shape)
    base_model = ResNet50(include_top=False,
                   weights=None,
                   input_tensor=input_tensor)
    # base_model.load_weights('../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5')
    # x = GlobalAveragePooling2D()(base_model.output)
    x = Flatten()(base_model.output)
    x = Dense(4096, activation='relu')(x)
    x = Dropout(0.5)(x)
    x = Dense(1024, activation='relu')(x)
    final_output = Dense(n_out, activation=function, name='final_output')(x)
    model = Model(input_tensor, final_output)
    
    return model
resnet50=resnet50_model((48,48,1),7)

resnet50.compile(optimizer=k.optimizers.Adam(lr=0.0001, decay=1e-6), 
                  loss='categorical_crossentropy', 
                  metrics=['accuracy'])
resnet50.summary()
resnet50.fit(x_train,y_train,epochs=50,batch_size=32,validation_data=(val_x,val_y),verbose=1)
resnet_out=resnet50.evaluate(x_test,y_test)
resnet_out
from sklearn.metrics import precision_score,recall_score,f1_score
y_pred=resnet50.predict(x_test)
def bin_to(x):
  s=[]
  for i in x:
    p=max(i)
    j=np.where(i==p)
    s.append(j)
  return s
yt=np.reshape(bin_to(y_test),(-1))
yp=np.reshape(bin_to(y_pred),(-1))
p=precision_score(yt,yp,labels=[0,1,2,3,4,5,6], average='macro')
r=recall_score(yt,yp,labels=[0,1,2,3,4,5,6],average='macro')
f=f1_score(yt,yp,labels=[0,1,2,3,4,5,6],average='macro')
p
r
f<jupyter_output><empty_output><jupyter_text>remove  kernel_regularizer=k.regularizers.l2(0.01)<jupyter_code>from keras import layers
from keras import models
from keras import optimizers

modelN = models.Sequential()
modelN.add(layers.Conv2D(32, (3, 3), padding='same', activation='relu',
                        input_shape=(48, 48, 1),kernel_regularizer=k.regularizers.l2(0.01)))

modelN.add(layers.Conv2D(32, (3, 3), padding='same', activation='relu',))
modelN.add(layers.Conv2D(32, (3, 3), padding='same', activation='relu'))
modelN.add(layers.MaxPooling2D(pool_size=(2, 2)))
 
modelN.add(layers.Conv2D(64, (3, 3), padding='same', activation='relu', ))
modelN.add(Dropout(0.5))
modelN.add(layers.Conv2D(64, (3, 3), padding='same', activation='relu'))
modelN.add(layers.Conv2D(64, (3, 3), padding='same', activation='relu'))
modelN.add(layers.MaxPooling2D(pool_size=(2, 2)))

modelN.add(layers.Conv2D(128, (3, 3), padding='same', activation='relu'))

modelN.add(layers.Conv2D(128, (3, 3), padding='same', activation='relu'))
modelN.add(Dropout(0.5))
modelN.add(layers.Conv2D(128, (3, 3), padding='same', activation='relu'))
modelN.add(layers.MaxPooling2D(pool_size=(2, 2)))

modelN.add(layers.Flatten())  # this converts our 3D feature maps to 1D feature vectors
modelN.add(layers.Dense(512, activation='relu'))
modelN.add(Dropout(0.5))
modelN.add(layers.Dense(128, activation='relu'))
modelN.add(layers.Dense(7, activation='softmax'))

# optimizer:
modelN.compile(optimizer=k.optimizers.Adam(lr=0.0001, decay=1e-6), 
                  loss=tf.keras.losses.sparse_categorical_crossentropy,
                  metrics=['accuracy'])
print ('Training....')
!mkdir saved_models
modelN.save("./saved_models/proposed_model")


n=modelN.summary()
modelF = modelN.fit(x_train, y_strain, epochs=50, batch_size=32,validation_data=(val_x,val_ys))
 modelN.evaluate(x_test,y_stest)
from sklearn.metrics import precision_score,recall_score,f1_score
y_pred=modelN.predict(x_test)
def bin_to(x):
  s=[]
  for i in x:
    p=max(i)
    j=np.where(i==p)
    s.append(j)
  return s
yt=np.reshape(bin_to(y_test),(-1))
yp=np.reshape(bin_to(y_pred),(-1))
yp=6-yp
p=precision_score(yt,yp,labels=[0,1,2,3,4,5,6], average='macro')
r=recall_score(yt,yp,labels=[0,1,2,3,4,5,6],average='macro')
f=f1_score(yt,yp,labels=[0,1,2,3,4,5,6],average='macro')
yt
yp
p
r
f<jupyter_output><empty_output><jupyter_text>**OWN!!!!!!!**<jupyter_code>from keras import layers
from keras import models
from keras import optimizers

modelN = models.Sequential()
modelN.add(layers.Conv2D(32, (3, 3), padding='same', activation='relu',
                        input_shape=(48, 48, 1)))
modelN.add(layers.Conv2D(32, (3, 3), padding='same', activation='relu', kernel_regularizer=k.regularizers.l2(0.01)))
modelN.add(layers.Conv2D(32, (3, 3), padding='same', activation='relu'))
modelN.add(layers.MaxPooling2D(pool_size=(2, 2)))
 
modelN.add(layers.Conv2D(64, (3, 3), padding='same', activation='relu', kernel_regularizer=k.regularizers.l2(0.01)))
modelN.add(layers.Conv2D(64, (3, 3), padding='same', activation='relu'))
modelN.add(layers.Conv2D(64, (3, 3), padding='same', activation='relu'))
modelN.add(layers.MaxPooling2D(pool_size=(2, 2)))

modelN.add(layers.Conv2D(128, (3, 3), padding='same', activation='relu', kernel_regularizer=k.regularizers.l2(0.01)))
modelN.add(layers.Conv2D(128, (3, 3), padding='same', activation='relu'))
modelN.add(layers.Conv2D(128, (3, 3), padding='same', activation='relu'))
modelN.add(layers.MaxPooling2D(pool_size=(2, 2)))

modelN.add(layers.Flatten())  # this converts our 3D feature maps to 1D feature vectors
modelN.add(layers.Dense(512, activation='relu'))
modelN.add(layers.Dense(128, activation='relu'))
modelN.add(layers.Dense(7, activation='softmax'))

# optimizer:
modelN.compile(optimizer=k.optimizers.Adam(lr=0.0001, decay=1e-6), 
                  loss='categorical_crossentropy', 
                  metrics=['accuracy'])
print ('Training....')
nb_epoch = 50
batch_size = 64
modelN.summary()

modelF = modelN.fit(x_train, y_train, epochs=nb_epoch, batch_size=batch_size,validation_data=(val_x,val_y))<jupyter_output>Training....
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_13 (Conv2D)           (None, 48, 48, 32)        320       
_________________________________________________________________
conv2d_14 (Conv2D)           (None, 48, 48, 32)        9248      
_________________________________________________________________
conv2d_15 (Conv2D)           (None, 48, 48, 32)        9248      
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 24, 24, 32)        0         
_________________________________________________________________
conv2d_16 (Conv2D)           (None, 24, 24, 64)        18496     
_________________________________________________________________
conv2d_17 (Conv2D)           (None, 24, 24, 64)        36928     
_________________________________________[...]<jupyter_text>**OWN2!!!!!!!!!!!!!!**<jupyter_code>from keras import layers  as l
model = Sequential()

model.add(Conv2D(8, (5, 5), input_shape=(48,48,1), padding='same', activation = 'relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(16, (5, 5), padding='same', activation = 'relu', kernel_regularizer=k.regularizers.l2(0.01)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3), activation = 'relu',))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense(128, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(32, activation = 'relu'))

model.add(Dense(7, activation = 'softmax'))
model.compile(optimizer=k.optimizers.Adam(lr=0.0001, decay=1e-6), 
                  loss='categorical_crossentropy', 
                  metrics=['accuracy'])
model.fit(255*x_train,y_train,batch_size=7, epochs=100, verbose=1, validation_data=(255*val_x,val_y))<jupyter_output>Epoch 1/100
4252/4252 [==============================] - 15s 4ms/step - loss: 2.3867 - accuracy: 0.2117 - val_loss: 2.1426 - val_accuracy: 0.2449
Epoch 2/100
4252/4252 [==============================] - 15s 4ms/step - loss: 2.0635 - accuracy: 0.2176 - val_loss: 1.9912 - val_accuracy: 0.2449
Epoch 3/100
4252/4252 [==============================] - 15s 4ms/step - loss: 1.9688 - accuracy: 0.2177 - val_loss: 1.9398 - val_accuracy: 0.2449
Epoch 4/100
4252/4252 [==============================] - 15s 4ms/step - loss: 1.9284 - accuracy: 0.2177 - val_loss: 1.9210 - val_accuracy: 0.2449
Epoch 5/100
4252/4252 [==============================] - 15s 3ms/step - loss: 1.9184 - accuracy: 0.2177 - val_loss: 1.9150 - val_accuracy: 0.2449
Epoch 6/100
4252/4252 [==============================] - 15s 3ms/step - loss: 1.8807 - accuracy: 0.2394 - val_loss: 1.8036 - val_accuracy: 0.2967
Epoch 7/100
4252/4252 [==============================] - 15s 4ms/step - loss: 1.8239 - accuracy: 0.2794 - val_loss: 1.7451 -[...]<jupyter_text>**OWN3!!!!!!!!!!!**<jupyter_code>def get_model(input_size, classes=7):
     #Initialising the CNN
    model = tf.keras.models.Sequential()   

    model.add(l.Conv2D(32, kernel_size=(3, 3), padding='same', activation='relu', input_shape =input_size))
    model.add(l.Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same'))
    model.add(l.BatchNormalization())
    model.add(l.MaxPooling2D(2, 2))
    model.add(l.Dropout(0.25))

    model.add(l.Conv2D(128, kernel_size=(3, 3), activation='relu', padding='same', kernel_regularizer=k.regularizers.l2(0.01)))
    model.add(l.Conv2D(256, kernel_size=(3, 3), activation='relu', ))
    model.add(l.BatchNormalization())
    model.add(l.MaxPooling2D(pool_size=(2, 2)))
    model.add(l.Dropout(0.25))

    model.add(l.Flatten())
    model.add(l.Dense(1024, activation='relu'))
    model.add(l.Dropout(0.5))
    model.add(l.Dense(256, activation='relu'))
    model.add(l.Dropout(0.5))
    model.add(l.Dense(128, activation='relu'))
    model.add(l.Dropout(0.5))
    model.add(l.Dense(64, activation='relu'))
    model.add(l.Dropout(0.5))
    model.add(l.Dense(32, activation='relu'))
    
    model.add(l.Dense(classes, activation='softmax'))
    

    #Compliling the model
    model.compile(optimizer=k.optimizers.Adam(lr=0.0001, decay=1e-6), 
                  loss='categorical_crossentropy', 
                  metrics=['accuracy'])
    return model
fernet = get_model((row,col,1), classes)
fernet.summary()

hist=fernet.fit(x_train,y_train,epochs=100,batch_size=128,validation_data=(val_x,val_y))
fer=fernet.predict(val_x)
fer

def acc(pred_val,val_y):
  pred=[]
  rel=[]

  for i in pred_val:
    max=np.where(i==np.max(i))
    pred.append(max)
  for i in val_y:
    rel.append(np.where(i==1))
  
  pred=np.asarray(pred)
  rel=np.asarray(rel)
  s=sum(abs(rel-pred))/len(pred)
  p=0
  for i,j in zip(pred,rel):
    if i==j:
      p=p+1
  return p/len(pred),pred,rel

  
a=acc(fer,val_y)<jupyter_output><empty_output><jupyter_text>**OWN4!!!!!!!!!**<jupyter_code>from keras import layers as l
cnn_model = k.Sequential()
cnn_model.add(l.Conv2D(500, kernel_size=(4, 4), activation='relu', padding='same', input_shape = (48, 48, 1)))
cnn_model.add(l.BatchNormalization())
cnn_model.add(l.MaxPooling2D(pool_size=(3, 3), strides=(4, 4)))
cnn_model.add(l.Dropout(0.2))
cnn_model.add(l.Flatten())
cnn_model.add(l.Dense(500, activation='relu'))
cnn_model.add(l.Dropout(0.5))
cnn_model.add(l.Dense(100, activation='relu'))
cnn_model.add(l.Dense(7, activation='softmax'))
# checkpoint = k.ModelCheckpoint('best_cnn_model.h5', verbose=1, monitor='val_loss',save_best_only=True, mode='auto')
cnn_model.compile(loss='categorical_crossentropy',optimizer=k.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999), metrics=['accuracy'])

his=cnn_model.fit(x_train[10000:20000], y_train[10000:20000], epochs=20, batch_size=128,validation_data=(val_x,val_y))

 np.shape(x[1])
m 

def evaluate(model,x_test_y_test):
  y_pred=model.predict(x_test)
  for i in y_pred:
    max=np.where(i==np.max(i))
    pred.append(max)
  for i in y_test:
    rel.append(np.where(i==1))
  
  pred=np.asarray(pred)
  rel=np.asarray(rel)
  loss,acc=model.evaluate(255*x_test,y_test,batch_size=8)
  presion=sk.metrics.precision_score(y_true, y_pred)
  recall=sk.metrics.recall_score(y_test, y_pred)
  f1=sk.metrics.f1_score(y_test,y_pred)
  con_max= sk.metrics.confusion_matrix(y_test, y_pred)
  return loss,acc,presion,recall,f1,con_max<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18447955390334572, 'fraction_numerical': 0.06417802560925237, 'mean_word_length': 5.649158942670786, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '405589', 'license': 'MIT', 'n_tokens_mistral': 8200, 'n_tokens_neox': 7531, 'n_words': 1201, 'path': 'nnmajor_project_.ipynb', 'repo_name': 'killerlock/Underwater-Broadband-Communication', 'size': '138526'}