<jupyter_start><jupyter_text>import Modules<jupyter_code>import os
import cv2
import numpy as np
import random

import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras.layers import Input, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D
from tensorflow.keras.layers import AveragePooling2D, MaxPooling2D, Dropout, GlobalMaxPooling2D, GlobalAveragePooling2D
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import EarlyStopping

from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report,roc_curve, auc

import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Read images<jupyter_code>lick_folder = 'lick'
void_folder = 'void'

lick_image_list = os.listdir(lick_folder)
void_image_list = os.listdir(void_folder)
lick_nsample = len(lick_image_list)
void_nsample = len(void_image_list)
# attention: positive and negative sample number balance!

n_total_sample = lick_nsample + void_nsample
X = np.empty(shape = (n_total_sample, 300, 300, 3)) # resize image into 300x300
count = 0
for image_filename in lick_image_list:
	image_fullname = os.path.join(lick_folder,image_filename)
	img = cv2.cvtColor(cv2.imread(image_fullname), cv2.COLOR_BGR2RGB).astype(np.float32)/255.0
	X[count,:,:,:]=img
	count += 1

for image_filename in void_image_list:
	image_fullname = os.path.join(void_folder,image_filename)
	img = cv2.cvtColor(cv2.imread(image_fullname), cv2.COLOR_BGR2RGB).astype(np.float32)/255.0
	X[count,:,:,:]=img
	count += 1

y = np.concatenate((np.ones(shape = (lick_nsample, 1)),np.zeros(shape = (void_nsample, 1))))
idx = random.randint(0,lick_nsample)
image_fullname = os.path.join(lick_folder,lick_image_list[idx])
img_lick_example = cv2.cvtColor(cv2.imread(image_fullname), cv2.COLOR_BGR2RGB)
plt.imshow(img_lick_example)
plt.show()

idx = random.randint(0,void_nsample)
image_fullname = os.path.join(void_folder,void_image_list[idx])
img_void_example = cv2.cvtColor(cv2.imread(image_fullname), cv2.COLOR_BGR2RGB)
plt.imshow(img_void_example)<jupyter_output><empty_output><jupyter_text>Train Test set splitting<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(X, y)
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)<jupyter_output><empty_output><jupyter_text>Conv Model<jupyter_code>def LickModel(input_shape):
    """
    Implementation of the LickModel.
    
    Arguments:
    input_shape -- shape of the images of the dataset
        (height, width, channels) as a tuple.  
        Note that this does not include the 'batch' as a dimension.
        If you have a batch like 'X_train', 
        then you can provide the input_shape using
        X_train.shape[1:]    

    Returns:
    model -- a Model() instance
    """
    
    # input place holder
    X_input = Input(input_shape)     
    # zero-padding
    X = ZeroPadding2D((3, 3))(X_input)
    
    # CONV -- BN -- RELU
    X = Conv2D(32, (9,9), strides = (1, 1), name = 'conv0')(X)
    X = BatchNormalization(axis = 3, name = 'bn0')(X)
    X = Activation('relu')(X)    
    # MAXPOOL
    X = MaxPooling2D((2,2), name='max_pool')(X)
    
    # CONV -- BN -- RELU
    X = Conv2D(64, (7,7), strides = (1, 1), name = 'conv1')(X)
    X = BatchNormalization(axis = 3, name = 'bn1')(X)
    X = Activation('relu')(X)    
    # MAXPOOL
    X = MaxPooling2D((2,2), name='max_pool1')(X)
    
        
    # CONV -- BN -- RELU
    X = Conv2D(64, (5,5), strides = (1, 1), name = 'conv2')(X)
    X = BatchNormalization(axis = 3, name = 'bn2')(X)
    X = Activation('relu')(X)    
    # MAXPOOL
    X = MaxPooling2D((2,2), name='max_pool2')(X)
    
    # CONV -- BN -- RELU
    X = Conv2D(128, (3,3), strides = (1, 1), name = 'conv3',kernel_regularizer =tf.keras.regularizers.l1( l=0.01))(X)
    X = BatchNormalization(axis = 3, name = 'bn3')(X)
    X = Activation('relu')(X)    
    # MAXPOOL
    X = MaxPooling2D((2,2), name='max_pool3')(X)
    
    '''# CONV -- BN -- RELU
    X = Conv2D(128, (3,3), strides = (1, 1), name = 'conv4')(X)
    X = BatchNormalization(axis = 3, name = 'bn4')(X)
    X = Activation('relu')(X)    
    # MAXPOOL
    X = MaxPooling2D((2,2), name='max_pool4')(X)
    '''
    # FLATEN and FULLYCONNECTED
    X = Flatten()(X)
    X = Dropout(0.2)(X)
    X = Dense(1, activation='sigmoid', name = 'fc')(X)
    
    # CREATE MODEL
    model = Model(inputs=X_input, outputs=X, name='LickModel')    
    return model<jupyter_output><empty_output><jupyter_text>Fit and evaluate the model<jupyter_code>lickModel = LickModel(X_train.shape[1:])
lickModel.summary()
# learning_rate=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-07
lickModel.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])<jupyter_output><empty_output><jupyter_text>Early stopping to prevent overfitting<jupyter_code># Or Early stopping to prevent overfitting
callback = EarlyStopping(monitor='loss', patience=3)
lickModel.fit(x = X_train, y = y_train, validation_data = (X_test, y_test), callbacks=[callback], epochs = 30, batch_size = 16, verbose = 2)<jupyter_output>Epoch 1/30
16/16 - 2s - loss: 21.6564 - accuracy: 0.5586 - val_loss: 23.1654 - val_accuracy: 0.5581
Epoch 2/30
16/16 - 1s - loss: 16.4529 - accuracy: 0.8477 - val_loss: 20.6538 - val_accuracy: 0.5581
Epoch 3/30
16/16 - 1s - loss: 11.9406 - accuracy: 0.9453 - val_loss: 12.3011 - val_accuracy: 0.5581
Epoch 4/30
16/16 - 1s - loss: 8.1176 - accuracy: 0.9180 - val_loss: 8.2649 - val_accuracy: 0.5581
Epoch 5/30
16/16 - 1s - loss: 5.2417 - accuracy: 0.9102 - val_loss: 4.2316 - val_accuracy: 0.8488
Epoch 6/30
16/16 - 1s - loss: 3.1500 - accuracy: 0.9570 - val_loss: 3.5051 - val_accuracy: 0.4419
Epoch 7/30
16/16 - 1s - loss: 2.1758 - accuracy: 0.9336 - val_loss: 1.7872 - val_accuracy: 0.9884
Epoch 8/30
16/16 - 1s - loss: 1.4799 - accuracy: 0.9258 - val_loss: 1.5786 - val_accuracy: 0.8023
Epoch 9/30
16/16 - 1s - loss: 1.5038 - accuracy: 0.8867 - val_loss: 1.4701 - val_accuracy: 0.9535
Epoch 10/30
16/16 - 1s - loss: 1.4059 - accuracy: 0.9258 - val_loss: 1.4847 - val_accuracy: 0.8837
Epoch 11/30
1[...]<jupyter_text>Prediction and evaluation<jupyter_code>preds = lickModel.evaluate(x = X_test, y = y_test)
print ("Loss = " + str(preds[0]))
print ("Test Accuracy = " + str(preds[1]))
y_pred = lickModel.predict(X_test)
y_pred_class = y_pred>0.5
classification_report(y_test, y_pred_class)
y_pred_class = y_pred>0.5
fpr, tpr, threshold = roc_curve(y_test, y_pred)
plt.plot(fpr, tpr)
print('AUC:',auc(fpr, tpr))<jupyter_output>AUC: 0.9956140350877193
{'fraction_non_alphanumeric': 0.13963691376701967, 'fraction_numerical': 0.07170953101361574, 'mean_word_length': 4.160811865729898, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '105789', 'license': 'MIT', 'n_tokens_mistral': 2922, 'n_tokens_neox': 2622, 'n_words': 546, 'path': 'lick_detect/lick_model_add_dropout_regularization.ipynb', 'repo_name': 'li-shen-amy/lick_detect', 'size': '6610'}