<jupyter_start><jupyter_text>Download data<jupyter_code># curl: download data and save to file
! curl -O https://www.robots.ox.ac.uk/~vgg/data/fgvc-aircraft/archives/fgvc-aircraft-2013b.tar.gz
! tar xzf fgvc-aircraft-2013b.tar.gz
!mv fgvc-aircraft-2013b dataset<jupyter_output>% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 2625M  100 2625M    0     0  28.6M      0  0:01:31  0:01:31 --:--:-- 29.1M<jupyter_text>Import Libraries<jupyter_code>import pathlib


import keras
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pickle
import seaborn as sns
import tensorflow as tf
import yaml

from PIL import Image
from keras.models import Sequential, load_model
from keras.layers import Conv2D, MaxPool2D, Dense, Flatten, Dropout
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split


# verify version keras
keras.__version__<jupyter_output><empty_output><jupyter_text>Constante<jupyter_code>#DATA_DIR = pathlib.Path('dataset/data')# pep8 : proposition d'amelioration de python
#DATA_DIR

#IMAGE_WIDTH = 128
#IMAGE_HEIGHT = IMAGE_WIDTH
#IMAGE_DEPTH = 3
from google.colab import drive
drive.mount('/content/drive')
yaml_file = open("/content/drive/MyDrive/model/learn.yaml", 'r')
yaml_content = yaml.load(yaml_file)

print("Key: Value")
for key, value in yaml_content.items():
   print(f"{key}: {value}")


DATA_DIR = yaml_content['DATA_DIR_TRAIN_FAMILY']
IMAGE_WIDTH = yaml_content['IMAGE_WIDTH']
IMAGE_HEIGHT = yaml_content['IMAGE_WIDTH']
IMAGE_DEPTH = yaml_content['IMAGE_DEPTH']
TARGET_NAME = yaml_content['TARGET_NAME']
TARGET_NAME2 = yaml_content['TARGET_NAME2']
TARGET_NAME3 = yaml_content['TARGET_NAME3']
DATA_DIR_TRAIN_MANUFACTURER = yaml_content['DATA_DIR_TRAIN_MANUFACTURER']
DATA_DIR_TRAIN_FAMILY = yaml_content['DATA_DIR_TRAIN_FAMILY']
DATA_DIR_TRAIN_VARIANT = yaml_content['DATA_DIR_TRAIN_VARIANT']
DATA_DIR_TEST_MANUFACTURER = yaml_content['DATA_DIR_TEST_MANUFACTURER']
DATA_DIR_TEST_FAMILY = yaml_content['DATA_DIR_TEST_FAMILY']
DATA_DIR_TEST_VARIANT = yaml_content['DATA_DIR_TEST_VARIANT']<jupyter_output><empty_output><jupyter_text>NB: Choose your target (manufacturer or family)In order to use the same code to make the detection for the category "manufacturer" and "family", we overwrite the data with the code below.<jupyter_code># allow to do for the manfacturer without restarting the constants part

TARGET_NAME2 = TARGET_NAME
DATA_DIR_TRAIN_FAMILY = DATA_DIR_TRAIN_MANUFACTURER
DATA_DIR_TEST_FAMILY = DATA_DIR_TEST_MANUFACTURER<jupyter_output><empty_output><jupyter_text>Function buil_image_database()<jupyter_code># allow to build our image database
def build_image_database(path, target):
    """Build a pandas dataframe with target class and access path to images.
   
    Parameters
    - - - - - -
    path (Path): path patern to read csv file containing images information.
    target (str): name of the target column.
   
    Returns
    - - - - -
    A pandas dataframe, including target class and path to image.
    """
   
   
    _df = pd.read_csv(path, sep='\t',
            names=['all'],
            dtype={'all': str} # ids are not int but string
            )

    # la fonction split() découpe sur une chaîne de charatères
    _df['image_id'] = _df['all'].apply(lambda x: x.split(' ')[0])

    # la fonction '<car>'.join(liste) concatène les éléments de liste en utilisant le séparateur <car>
    _df[target] = _df['all'].apply(lambda x: ' '.join(x.split(' ')[1:]))

    # la colonne path contient le chemin d'accès à l'image
    _df['path'] = _df['image_id'].apply(lambda x:  pathlib.Path('dataset/data/images') / (x + '.jpg'))

    return _df.drop(columns=['all'])<jupyter_output><empty_output><jupyter_text>Verify of the function build_image_database()<jupyter_code># apply build_image_database()
build_image_database(DATA_DIR_TRAIN_FAMILY, TARGET_NAME2).head(2)
# apply build_image_database()
build_image_database(DATA_DIR_TRAIN_VARIANT, TARGET_NAME3).head(2)
# apply build_image_database() in order to create the dataframe manufacturer_df
manufacturer_df = build_image_database(DATA_DIR_TRAIN_MANUFACTURER,TARGET_NAME)
# see the dataframe manufacturer_df
manufacturer_df.head(2)
# show the image in manufacturer_df
plt.imshow(plt.imread(manufacturer_df.head(1)['path'].values[0]))<jupyter_output><empty_output><jupyter_text>Function Show_image()<jupyter_code># the function :
# - take a df(argument)
#-take a row(row : argument)
#-take a column (target : argument)
# -it displays (the value of target ) and the associated image, for the line in argument


def show_image(df, row, target):
  """ Show an image from an image database , with the associated class

  Parameters
  -----------
  df(pd.DataFrame): images definition dataframe
  row(int):row index in df of image to be displayed
  target(str):name of target column

  Returns
  --------
  None
  """
  assert target in df.columns, "missing target column in dataframe"
  assert 'path' in df.columns , "Missing image path in dataframe"
  print(df.iloc[row, ][target])
  plt.imshow(plt.imread(df.iloc[row, ]['path']))
  return<jupyter_output><empty_output><jupyter_text>Function load_resize_image()<jupyter_code>def load_resize_image(path,height,width):
  """Load an image and resize it to the target size
    Parameters
    --------
    path(Path): access path to image file
    height(int): resize image to this height
    width(int):resize image to this width

    Returns
    ----------
    nb.array containing resize



  """
  
  return np.array(Image.open(path).resize((width, height)))<jupyter_output><empty_output><jupyter_text>Function build_classifiaction_model()<jupyter_code>def build_classification_model(df:pd.DataFrame, target:str, images:str):
  """Build a TF model using information from target and images columns in dataframe

  Parameters
  ---------------
  df(pd.Dataframe):dataframe with target and images columns
  target(str):column name for target variable
  images(str):column name for images


  Returns
  -----------
  TF model built & compiled
  """
  nb_classes=df[target].nunique()# compute number of classees for output layer
  size = df[images].iloc[0].shape# com
  #Building the model
  model = Sequential()
  model.add(Conv2D(filters=32, kernel_size=(5,5), activation='relu', input_shape=size))
  model.add(Conv2D(filters=32, kernel_size=(5,5), activation='relu'))
  model.add(MaxPool2D(pool_size=(2, 2)))
  model.add(Dropout(rate=0.25))
  model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))
  model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))
  model.add(MaxPool2D(pool_size=(2, 2)))
  model.add(Dropout(rate=0.25))
  model.add(Flatten())
  model.add(Dense(256, activation='relu'))
  model.add(Dropout(rate=0.5))
  model.add(Dense(nb_classes, activation='softmax'))# couche de sortie à nb_classes

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

  return model<jupyter_output><empty_output><jupyter_text>Function build_x_and_y()Build train & test set<jupyter_code>def build_x_and_y(df:pd.DataFrame,target:str,images:str,stratify=None):
  """Build x tensor and y tensor for model fitting 

  Parameters
  --------------
  df(pd.DataFrame):dataframe contanining images and target
  target(str):name of target column
  images(str): name of images column
  
  Returns
  ---------
  x(np.array):tensor of x values
  y(np.array):name of y values
  """
  x=np.array(df[images].to_list())
  y= tf.keras.utils.to_categorical(df[target].astype('category').cat.codes)
  y_svm = np.array(df[target].astype("category").cat.codes)
 

  return x, y, y_svm

# Load train & test dataset

train_df= build_image_database(DATA_DIR_TRAIN_FAMILY,TARGET_NAME2)
test_df= build_image_database(DATA_DIR_TEST_FAMILY,TARGET_NAME2)

train_df['resized_image']=train_df.apply(lambda r:load_resize_image(r['path'],IMAGE_HEIGHT,IMAGE_WIDTH),axis=1)

test_df['resized_image']=test_df.apply(lambda r:load_resize_image(r['path'],IMAGE_HEIGHT,IMAGE_WIDTH),axis=1)
#train_df
# Build tensors for training & testing

X_train,y_train,y_train_svm = build_x_and_y(train_df,TARGET_NAME2,'resized_image')
X_test,y_test,y_test_svm = build_x_and_y(test_df,TARGET_NAME2,'resized_image')

# BUILD TF classification model
model = build_classification_model(train_df,TARGET_NAME2,'resized_image')

# To be done once to retrieve the TPU information
try:
  tpu = tf.distribute.cluster_resolver.TPUClusterResolver()  # TPU detection
  print('Running on TPU ', tpu.cluster_spec().as_dict()['worker'])
except ValueError:
  raise BaseException('ERROR: Not connected to a TPU runtime; please see the previous cell in this notebook for instructions!')

tf.config.experimental_connect_to_cluster(tpu)
tf.tpu.experimental.initialize_tpu_system(tpu)
tpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)
with tpu_strategy.scope():
  model= build_classification_model(train_df, TARGET_NAME2, 'resized_image')

model.summary()
%%time
epochs = 30
history = model.fit(X_train, y_train, batch_size=96, epochs=epochs, 
                    validation_data=(X_test, y_test),
                    #callbacks=[tensorboard_callback]
                    )
# verify the shape

X_test[10:11].shape
# the prediction of X_test[10:11]
np.argmax(model.predict(X_test[10:11]))
# the probabilty of prediction of X_test[10:11]
model.predict(X_test[10:11])
# see the categorisation of modality of manufacturer
test_df[TARGET_NAME2].astype('category').cat.codes
# see the modality of manaufacturer
test_df[TARGET_NAME2]
# show image of the row equal to 10 for the target manufacturer
show_image(test_df, 10, TARGET_NAME2)
# the prediction of X_test[10:20
#np.argmax(model.predict(X_test[10:20]), axis=1)
# see the modality from the categorisation
train_df[TARGET_NAME2].astype('category').cat.categories<jupyter_output><empty_output><jupyter_text>fucntion of the classify_image()<jupyter_code>def classify_images(images, model, classes_names=None):
  """Classify images through a TF model.
  Parameters
  ----------
  images (np.array): set of images to classify
  model (tf.keras.Model): TF/ Keras model
  classes_names: dictionnary with classes names
  Returns
  -------
  predicted classes
  """

  results = model.predict(images) # predict for images
  classes = np.argmax(results, axis=1) # np.argmax returns the index of the max value per row
  if classes_names is not None:
    classes = np.array(classes_names[classes])
  return classes
# verification of the shape of the function classify_image()
classify_images(X_test, model).shape
# see the confusion matrix in the train
fig, ax = plt.subplots(figsize=(12,10))
sns.heatmap(pd.crosstab(np.argmax(y_train, axis=1), 
                        classify_images(X_train, model), 
                        normalize='index'),
            cmap='vlag',
            ax=ax)
# see the confusion matrix of the test
#we can see our model classifies genelrally the planes in 4

fig, ax = plt.subplots(figsize=(12,10))
sns.heatmap(pd.crosstab(np.argmax(y_test, axis=1), 
                        classify_images(X_test, model), 
                        normalize='index'),
            cmap='vlag',
            ax=ax)
# see the confusion matrix of the test
#we can see our model classifies genelrally the planes in Boeing

fig, ax = plt.subplots(figsize=(12,10))
sns.heatmap(pd.crosstab(test_df[TARGET_NAME2], 
                        classify_images(X_test, model, test_df[TARGET_NAME2].astype('category').cat.categories), 
                        normalize='index'),
            cmap='vlag',
            ax=ax)
# function to save_model
import datetime
def save_model(model, basename):
  """Save tf/Keras model

  Model file is named model + timestamp.

  Parameters
  ----------
  model (tf/Keras model): model to be saved
  basename: location to save model file
  """
  model.save('{}.h5'.format(basename))
  return
from google.colab import drive
drive.mount('/content/drive')
# apply the function save_model()
save_model(model, TARGET_NAME2)
# relodded the model
#reloaded_model = load_model('/content/dataset/data_2022-04-05_19-38-28.h5')
# prediction with the model reloaded
#np.argmax(reloaded_model.predict(X_test[10:20]), axis=1)<jupyter_output><empty_output><jupyter_text>IMAGE CLASSIFICATION WITH SVM<jupyter_code># Suppert vector machine:

svc = SVC(kernel="linear", gamma = "auto")
# Create flatten base

X_train_scaled_s = X_train / 255
X_test_scaled_s = X_test / 255
# Take the len of our image

lenofimage = len(X_train_scaled_s)
lenofimagetest = len(X_test_scaled_s)
# Reshape our datasets

X_train_scaled_svc_s = X_train_scaled_s.reshape(lenofimage, -1)
X_test_scaled_svc_s = X_test_scaled_s.reshape(lenofimagetest, -1) 
# Fit the model

svc.fit(X_train_scaled_svc_s, y_train_svm)
y_predict_train=svc.predict(X_train_scaled_svc_s)
# Make the model prediction

y_predict = svc.predict(X_test_scaled_svc_s)
# Print the accuracy of the model


print("The accuracy of the model is:", accuracy_score(y_test_svm,y_predict))
# creating a confusion matrix

cm_lin = confusion_matrix(y_test_svm, y_predict)

# Plot the confusion matrix with number

plt.figure(figsize=(12,10))
sns.heatmap(pd.crosstab(y_test_svm,  y_predict,
                        normalize='index'),
            cmap='vlag'
            )


# see the confusion matrix in the train model with number

fig, ax = plt.subplots(figsize=(12,10))
sns.heatmap(pd.crosstab(y_train_svm, 
                        y_predict_train, 
                        normalize='index'),
            cmap='vlag')
           
## See the confusion matrix of the train with namems


fig, ax = plt.subplots(figsize=(12,10))
sns.heatmap(pd.crosstab(train_df[TARGET_NAME2], 
                        train_df[TARGET_NAME2].astype('category').cat.categories[y_predict_train], 
                        normalize='index'),
            cmap='vlag')
test_df[TARGET_NAME2]
# See the confusion matrix of the test


fig, ax = plt.subplots(figsize=(12, 10))
sns.heatmap(pd.crosstab(test_df[TARGET_NAME2],
                        test_df[TARGET_NAME2].astype('category').cat.categories[y_predict],
                        normalize='index'),
            cmap='vlag',
            );
#file_dir = '/content/drive/MyDrive/image_classifier/base_dir.h5'
#base_model.save(file_dir)
# Save the model

filename = "model_svm.sav"
pickle.dump(svc, open(filename,"wb"))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11547692518686141, 'fraction_numerical': 0.015291778097785091, 'mean_word_length': 4.468316460442445, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 50, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '589028', 'license': 'MIT', 'n_tokens_mistral': 5150, 'n_tokens_neox': 4913, 'n_words': 1113, 'path': 'notebooks/train_classification_model_neural_network__and_svm.ipynb', 'repo_name': 'SaleemAmu/plane-classification', 'size': '14583'}