{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-19T07:47:22.427417Z","iopub.execute_input":"2022-02-19T07:47:22.428318Z","iopub.status.idle":"2022-02-19T07:47:22.439384Z","shell.execute_reply.started":"2022-02-19T07:47:22.428274Z","shell.execute_reply":"2022-02-19T07:47:22.438405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Manipulateur des tableaux\nimport numpy as np\n\n# Manipulation des images| OpenCV\nimport cv2\n\n# Afficher les images et evaluer le model résultat\nimport matplotlib.pyplot as plt\n\n# Model VGG16  \nfrom keras.applications.vgg16 import VGG16\n\n# Preparation du model VGG16\nfrom keras.layers import Dense,Flatten,Input\nfrom keras.models import Sequential\n\n# One hot label encoding\nfrom keras.utils import np_utils\n\n# Base de donnee CIFAR10\nfrom keras.datasets import cifar10\n","metadata":{"execution":{"iopub.status.busy":"2022-02-19T07:47:22.441415Z","iopub.execute_input":"2022-02-19T07:47:22.441773Z","iopub.status.idle":"2022-02-19T07:47:22.448158Z","shell.execute_reply.started":"2022-02-19T07:47:22.441737Z","shell.execute_reply":"2022-02-19T07:47:22.447268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Chargement de donnee\n\n(x_train,y_train),(x_test,y_test) = cifar10.load_data()\nprint(\"Shape of x_train is \",x_train.shape)\nprint(\"Shape of y_train is \",y_train.shape)\nprint(\"Shape of x_test  is \",x_test.shape)\nprint(\"Shape of y_test  is\",y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T07:47:22.449937Z","iopub.execute_input":"2022-02-19T07:47:22.450159Z","iopub.status.idle":"2022-02-19T07:47:23.185974Z","shell.execute_reply.started":"2022-02-19T07:47:22.450133Z","shell.execute_reply":"2022-02-19T07:47:23.185241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# one hot encoding\ny_train =  np_utils.to_categorical(y_train,num_classes=10)\ny_test =  np_utils.to_categorical(y_test,num_classes=10)\n\nprint(\"New shape of y_train is \",y_train.shape)\nprint(\"New shape of y_test  is \",y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T07:47:23.187334Z","iopub.execute_input":"2022-02-19T07:47:23.187559Z","iopub.status.idle":"2022-02-19T07:47:23.194782Z","shell.execute_reply.started":"2022-02-19T07:47:23.187529Z","shell.execute_reply":"2022-02-19T07:47:23.193932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Include top = ajouter des couches entièrement connectés au couche\n# Weights = utilise  weights deja entainer(trained in imagenet)\nvgg = VGG16(include_top=False,weights=\"imagenet\",input_shape=(32,32,3))\n\nvgg.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-19T07:47:23.197372Z","iopub.execute_input":"2022-02-19T07:47:23.197651Z","iopub.status.idle":"2022-02-19T07:47:23.468119Z","shell.execute_reply.started":"2022-02-19T07:47:23.197616Z","shell.execute_reply":"2022-02-19T07:47:23.467413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Creation d'un model \nmodel = Sequential()\n\n# Ajouter les couches au modèle blank\nfor layer in vgg.layers:\n    model.add(layer)\n    \n#N'entraînez plus les couches, car elles sont déjà entraînées\nfor layer in model.layers:\n    layer.trainable = False\n    \n#Ajout de couches entièrement connectées\n\nmodel.add(Flatten())\nmodel.add(Dense(units=4098, activation='relu', name='fc1')) \nmodel.add(Dense(units=4098, activation='relu', name='fc2'))\nmodel.add(Dense(units=10, activation='softmax', name='predictions'))\n\n# verifier le model\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-19T07:47:23.469494Z","iopub.execute_input":"2022-02-19T07:47:23.469760Z","iopub.status.idle":"2022-02-19T07:47:23.578715Z","shell.execute_reply.started":"2022-02-19T07:47:23.469725Z","shell.execute_reply":"2022-02-19T07:47:23.578063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compiler le model\nmodel.compile(optimizer=\"adam\",loss=\"categorical_crossentropy\",metrics=[\"accuracy\"])\n\n# Lancer l'entraînement.\n#Nous utilisons %15 du data d'entrainement comme ensemble de validation.\nhist = model.fit(x_train,y_train,epochs=20,batch_size=68, validation_split =0.15)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-19T07:47:23.581266Z","iopub.execute_input":"2022-02-19T07:47:23.581446Z","iopub.status.idle":"2022-02-19T07:50:32.015179Z","shell.execute_reply.started":"2022-02-19T07:47:23.581421Z","shell.execute_reply":"2022-02-19T07:50:32.014464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nIMG_SIZE=32\npath1='../input/test-data/cat.jpeg'\n#path1='../input/tst123/mix.jpg'\nimg_data1 = cv2.imread(path1)\nfrom matplotlib import pyplot as plt\nplt.imshow(img_data1)\n\nimg_data1 = cv2.resize(img_data1, (IMG_SIZE, IMG_SIZE))\ndata1 = img_data1.reshape(-1, IMG_SIZE, IMG_SIZE, 3)\n\n\nmodel_out=model.predict(data1)\nprint(model_out)\n\n\nif np.argmax(model_out) == 1:\n    str_label = 'Automobile'\n    print(str_label)\nif np.argmax(model_out) == 2:\n    str_label = 'Bird'\n    print(str_label)\nif np.argmax(model_out) == 3:\n    str_label = 'Cat'\n    print(str_label)\nif np.argmax(model_out) == 4:\n    str_label = 'Deer'\n    print(str_label)\nif np.argmax(model_out) == 0:\n    str_label = 'Airplane'\n    print(str_label)\nif np.argmax(model_out) == 5:\n    str_label = 'Dog'\n    print(str_label)\nif np.argmax(model_out) == 6:\n    str_label = 'Frog'\n    print(str_label)\nif np.argmax(model_out) == 7:\n    str_label = 'Horse'\n    print(str_label)\nif np.argmax(model_out) == 8:\n    str_label = 'Ship'\n    print(str_label)\nif np.argmax(model_out) == 9:\n    str_label = 'Truck'\n    print(str_label)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T07:52:01.053998Z","iopub.execute_input":"2022-02-19T07:52:01.054256Z","iopub.status.idle":"2022-02-19T07:52:01.325639Z","shell.execute_reply.started":"2022-02-19T07:52:01.054227Z","shell.execute_reply":"2022-02-19T07:52:01.324954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_scores = model.evaluate(x_test, y_test, verbose=2)\nprint(\"Test loss:\", test_scores[0])\nprint(\"Test accuracy:\", test_scores[1])","metadata":{"execution":{"iopub.status.busy":"2022-02-19T07:52:06.975917Z","iopub.execute_input":"2022-02-19T07:52:06.976344Z","iopub.status.idle":"2022-02-19T07:52:09.635331Z","shell.execute_reply.started":"2022-02-19T07:52:06.976306Z","shell.execute_reply":"2022-02-19T07:52:09.634523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_y = model.predict(x_test)\npredict_y = np.round(predict_y)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T07:52:35.699761Z","iopub.execute_input":"2022-02-19T07:52:35.700307Z","iopub.status.idle":"2022-02-19T07:52:37.524863Z","shell.execute_reply.started":"2022-02-19T07:52:35.700270Z","shell.execute_reply":"2022-02-19T07:52:37.524096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = [\n    \"airplane\",\n    \"automobile\",\n    \"bird\",\n    \"cat\",\n    \"deer\",\n    \"dog\",\n    \"frog\",\n    \"horse\",\n    \"ship\",\n    \"truck\"\n]","metadata":{"execution":{"iopub.status.busy":"2022-02-19T07:52:37.961088Z","iopub.execute_input":"2022-02-19T07:52:37.961539Z","iopub.status.idle":"2022-02-19T07:52:37.966308Z","shell.execute_reply.started":"2022-02-19T07:52:37.961494Z","shell.execute_reply":"2022-02-19T07:52:37.965636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport csv\n\n\nwith open('./csv_res.csv', 'w', encoding='UTF8') as f:\n    # create the csv writer\n    writer = csv.writer(f)\n    writer.writerow([\"id\",\"label\"])\n    i = 1\n    for prediction in predict_y:\n        writer.writerow([i,labels[np.argmax(prediction)]])\n        i+=1","metadata":{"execution":{"iopub.status.busy":"2022-02-19T07:52:43.638381Z","iopub.execute_input":"2022-02-19T07:52:43.639264Z","iopub.status.idle":"2022-02-19T07:52:43.692519Z","shell.execute_reply.started":"2022-02-19T07:52:43.639218Z","shell.execute_reply":"2022-02-19T07:52:43.691824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os\n# import shutil\n# os.system('import py7zlib')\n# from keras.preprocessing.image import ImageDataGenerator\n# from py7zr import unpack_7zarchive\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# shutil.register_unpack_format('7zip', ['.7z'], unpack_7zarchive)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T07:50:37.104375Z","iopub.status.idle":"2022-02-19T07:50:37.105313Z","shell.execute_reply.started":"2022-02-19T07:50:37.105067Z","shell.execute_reply":"2022-02-19T07:50:37.105093Z"},"trusted":true},"execution_count":null,"outputs":[]}]}