<jupyter_start><jupyter_text>Import resources<jupyter_code>import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
train_cnn = pd.read_csv("data/training.csv")
test_cnn = pd.read_csv("data/test.csv")<jupyter_output><empty_output><jupyter_text>Preprocess Data<jupyter_code>train_cnn.dropna(inplace=True)
train_cnn.shape
from joblib import Parallel, delayed

def format_img(x):
    return np.asarray([int(e) for e in x.split(' ')], dtype=np.uint8).reshape(96,96)

with Parallel(n_jobs=10, verbose=1, prefer='threads') as ex:
    x = ex(delayed(format_img)(e) for e in train_cnn.Image)
with Parallel(n_jobs=10, verbose=1, prefer='threads') as ex:
    test = ex(delayed(format_img)(e) for e in test_cnn.Image)
test = np.stack(test)[..., None]
x = np.stack(x)[..., None]
x.shape, test.shape
plt.imshow(x[10,:,:,0])
y = train_cnn.iloc[:, :-1].values
y.shape<jupyter_output><empty_output><jupyter_text>Visualize a random image from the Data<jupyter_code>def show(x, y=None):
    plt.imshow(x[..., 0], 'gray')
    if y is not None:
        points = np.vstack(np.split(y, 15)).T
        plt.plot(points[0], points[1], 'o', color='red')
        
    plt.axis('off')
sample_idx = np.random.choice(len(x))    
show(x[sample_idx], y[sample_idx])<jupyter_output><empty_output><jupyter_text>Train validation split<jupyter_code>from sklearn.model_selection import train_test_split

x_train, x_val, y_train, y_val = train_test_split(x, y, test_size=0.2, random_state=42)
x_train.shape, x_val.shape
x[:,9,9,0]<jupyter_output><empty_output><jupyter_text>Normalization<jupyter_code>x_train=(x_train/255)
x_val=(x_val/255)
test=(test/255)<jupyter_output><empty_output><jupyter_text>First we'll use ResNet 50 trained with images of ImageNet They will be preprocessed according to resnet preprocessing<jupyter_code>from keras.applications.resnet50 import ResNet50, preprocess_input<jupyter_output>Using TensorFlow backend.<jupyter_text>Model<jupyter_code>test.shape
test=np.array([test[:,:,:,0],test[:,:,:,0],test[:,:,:,0]])
test=np.swapaxes(test,0,1)
test=np.swapaxes(test,1,2)
test=np.swapaxes(test,2,3)
test.shape
x_train[:,:,:,0].shape
x_train=np.array([x_train[:,:,:,0],x_train[:,:,:,0],x_train[:,:,:,0]])
x_train.shape
x_val[:,:,:,0].shape
x_val=np.array([x_val[:,:,:,0],x_val[:,:,:,0],x_val[:,:,:,0]])
x_val.shape
x_train=np.swapaxes(x_train,0,1)
x_train=np.swapaxes(x_train,1,2)
x_train=np.swapaxes(x_train,2,3)
x_train.shape
x_val=np.swapaxes(x_val,0,1)
x_val=np.swapaxes(x_val,1,2)
x_val=np.swapaxes(x_val,2,3)
x_val.shape
base_model = ResNet50(include_top=False, input_shape=(96,96,3), pooling='avg')
base_model.trainable = False
base_model.summary()
y.shape
from keras.models import Sequential 
from keras.layers import Dense, Flatten,BatchNormalization, Dropout
from keras.optimizers import Adam
from keras import regularizers

top_model = Sequential([
    Dense(512, activation='relu', input_shape=(2048,),kernel_initializer='he_normal'),
    Dense(256, activation='relu',kernel_initializer='he_normal'),
    Dropout(0.7),
    Dense(128, activation='relu',kernel_regularizer=regularizers.l2(0.01),kernel_initializer='he_normal'),
    Dropout(0.7),
    Dense(96, activation='relu',kernel_regularizer=regularizers.l2(0.01),kernel_initializer='he_normal'),
    Dropout(0.7),
    Dense(48, activation='relu',kernel_regularizer=regularizers.l2(0.01),kernel_initializer='he_normal'),
    Dense(30)
])
top_model.compile(loss='mse', optimizer=Adam(0.001), metrics=['mae'])
top_model.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 512)               1049088   
_________________________________________________________________
dense_2 (Dense)              (None, 256)               131328    
_________________________________________________________________
dropout_1 (Dropout)          (None, 256)               0         
_________________________________________________________________
dense_3 (Dense)              (None, 128)               32896     
_________________________________________________________________
dropout_2 (Dropout)          (None, 128)               0         
_________________________________________________________________
dense_4 (Dense)              (None, 96)                12384     
_________________________________________________________________
dropout_3 [...]<jupyter_text>The final model consists of the convolutional resnet layer and the dense layer.<jupyter_code>final_model = Sequential([base_model, top_model])
final_model.compile(loss='mse', optimizer=Adam(0.001), metrics=['mae'])
final_model.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
resnet50 (Model)             (None, 2048)              23587712  
_________________________________________________________________
sequential_1 (Sequential)    (None, 30)                1231822   
=================================================================
Total params: 24,819,534
Trainable params: 1,231,822
Non-trainable params: 23,587,712
_________________________________________________________________<jupyter_text>Train the neural network Pre compute the weights of the convolutional layer<jupyter_code>precomputed_train = base_model.predict(x_train, batch_size=256, verbose=1)
precomputed_train.shape
precomputed_val = base_model.predict(x_val, batch_size=256, verbose=1)
precomputed_val.shape
log = top_model.fit(precomputed_train, y_train, epochs=600, batch_size=256, validation_data=[precomputed_val, y_val])<jupyter_output>Train on 1712 samples, validate on 428 samples
Epoch 1/600
1712/1712 [==============================] - 2s 1ms/step - loss: 2557.1981 - mean_absolute_error: 47.0322 - val_loss: 2465.7270 - val_mean_absolute_error: 46.1859
Epoch 2/600
1712/1712 [==============================] - 0s 256us/step - loss: 2128.4736 - mean_absolute_error: 41.6156 - val_loss: 1725.1975 - val_mean_absolute_error: 37.7085
Epoch 3/600
1712/1712 [==============================] - 0s 269us/step - loss: 1583.0811 - mean_absolute_error: 32.8225 - val_loss: 1139.1831 - val_mean_absolute_error: 29.1387
Epoch 4/600
1712/1712 [==============================] - 0s 265us/step - loss: 1176.0993 - mean_absolute_error: 28.2269 - val_loss: 1151.9135 - val_mean_absolute_error: 30.2079
Epoch 5/600
1712/1712 [==============================] - 0s 264us/step - loss: 907.4858 - mean_absolute_error: 24.4966 - val_loss: 679.5754 - val_mean_absolute_error: 22.4221
Epoch 6/600
1712/1712 [==============================] - 0s 274us/step -[...]<jupyter_text>Training loss<jupyter_code>def show_results(*logs):
    trn_loss, val_loss, trn_acc, val_acc = [], [], [], []
    
    for log in logs:
        trn_loss += log.history['loss']
        val_loss += log.history['val_loss']
    
    fig, ax = plt.subplots(figsize=(8,4))
    ax.plot(trn_loss, label='train')
    ax.plot(val_loss, label='validation')
    ax.set_xlabel('epoch'); ax.set_ylabel('loss')
    ax.legend()
    
show_results(log)<jupyter_output><empty_output><jupyter_text>Results Function to display results<jupyter_code>def show_pred(x, y_real, y_pred):
    fig, axes = plt.subplots(1, 2, figsize=(10,5))
    for ax in axes:
        ax.imshow(x[0, ..., 0], 'gray')
        ax.axis('off')
        
    points_real = np.vstack(np.split(y_real[0], 15)).T
    points_pred = np.vstack(np.split(y_pred[0], 15)).T
    axes[0].plot(points_pred[0], points_pred[1], 'o', color='red')
    axes[0].set_title('Predictions', size=16)
    axes[1].plot(points_real[0], points_real[1], 'o', color='green')
    axes[1].plot(points_pred[0], points_pred[1], 'o', color='red', alpha=0.5)
    axes[1].set_title('Real', size=16)
x_val[0,None].shape
sample_x = x_train[9, None]
sample_y = y_val[9, None]
pred = final_model.predict(sample_x)
show_pred(sample_x, sample_y, pred)
results=final_model.predict(test)
results
lookup = pd.read_csv("data/IdLookupTable.csv")
lookid_list = list(lookup['FeatureName'])
imageID = list(lookup['ImageId']-1)
pre_list = list(results)

rowid = lookup['RowId']
rowid=list(rowid)
len(rowid)

feature = []
for f in list(lookup['FeatureName']):
    feature.append(lookid_list.index(f))
    preded = []
for x,y in zip(imageID,feature):
    preded.append(results[x][y])
rowid = pd.Series(rowid,name = 'RowId')
loc = pd.Series(preded,name = 'Location')
submission = pd.concat([rowid,loc],axis = 1)
submission.to_csv('submission_resnet.csv',index = False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17667199269573156, 'fraction_numerical': 0.058091759872175304, 'mean_word_length': 5.510401188707281, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '443473', 'license': 'MIT', 'n_tokens_mistral': 3337, 'n_tokens_neox': 3002, 'n_words': 511, 'path': 'computer_vision-master/computer_vision_V1.ipynb', 'repo_name': 'aissam-out/Deep-Learning-101', 'size': '8762'}