{"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":"markdown","source":"# Introduction\n\nThe models you've built so far have relied on pre-trained models.  But they aren't the ideal solution for many use cases.  In this lesson, you will learn how to build totally new models.\n\n# Lesson\n","metadata":{}},{"cell_type":"code","source":"#importing libraries\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\ntrain_data = pd.read_csv('/kaggle/input/fashionmnist/fashion-mnist_train.csv')\ntest_data = pd.read_csv('/kaggle/input/fashionmnist/fashion-mnist_test.csv')\n\ndef sample_images_data(data):\n    # An empty list to collect some samples\n    sample_images = []\n    sample_labels = []\n\n    # Iterate over the keys of the labels dictionary defined in the above cell\n    for k in labels.keys():\n        # Get four samples for each category\n        samples = data[data[\"label\"] == k].head(4)\n        # Append the samples to the samples list\n        for j, s in enumerate(samples.values):\n            # First column contain labels, hence index should start from 1\n            img = np.array(samples.iloc[j, 1:]).reshape(28,28)\n            sample_images.append(img)\n            sample_labels.append(samples.iloc[j, 0])\n\n    print(\"Total number of sample images to plot: \", len(sample_images))\n    return sample_images, sample_labels\n\nlabels = {0 : \"T-shirt/top\", 1: \"Trouser\", 2: \"Pullover\", 3: \"Dress\", 4: \"Coat\",\n          5: \"Sandal\", 6: \"Shirt\", 7: \"Sneaker\", 8: \"Bag\", 9: \"Ankle Boot\"}\n\ntrain_sample_images, train_sample_labels = sample_images_data(train_data)","metadata":{"execution":{"iopub.status.busy":"2023-07-05T13:22:21.156202Z","iopub.execute_input":"2023-07-05T13:22:21.156508Z","iopub.status.idle":"2023-07-05T13:22:27.782413Z","shell.execute_reply.started":"2023-07-05T13:22:21.156467Z","shell.execute_reply":"2023-07-05T13:22:27.781568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Sample Code","metadata":{}},{"cell_type":"code","source":"def plot_sample_images(data_sample_images,data_sample_labels,cmap=\"Greys\"):\n    # Plot the sample images now\n    f, ax = plt.subplots(5,8, figsize=(16,10))\n\n    for i, img in enumerate(data_sample_images):\n        ax[i//8, i%8].imshow(img, cmap=cmap)\n        ax[i//8, i%8].axis('off')\n        ax[i//8, i%8].set_title(labels[data_sample_labels[i]])\n    plt.show()    \n    \nplot_sample_images(train_sample_images,train_sample_labels, \"Greys\")","metadata":{"execution":{"iopub.status.busy":"2023-07-05T13:22:27.783690Z","iopub.execute_input":"2023-07-05T13:22:27.784052Z","iopub.status.idle":"2023-07-05T13:22:30.033912Z","shell.execute_reply.started":"2023-07-05T13:22:27.783920Z","shell.execute_reply":"2023-07-05T13:22:30.033076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.python import keras\nfrom tensorflow.python.keras.models import Sequential\nfrom tensorflow.python.keras.layers import Dense, Flatten, Conv2D, Dropout, MaxPooling2D, AveragePooling2D, BatchNormalization\nfrom tensorflow.python.keras.callbacks import EarlyStopping\n\n\nimg_rows, img_cols = 28, 28\nnum_classes = 10\n\ndef data_prep(raw):\n    out_y = keras.utils.to_categorical(raw.label, num_classes)\n\n    num_images = raw.shape[0]\n    x_as_array = raw.values[:,1:]\n    x_shaped_array = x_as_array.reshape(num_images, img_rows, img_cols, 1)\n    out_x = x_shaped_array / 255\n    return out_x, out_y\n\ntrain_file = \"/kaggle/input/digit-recognizer/train.csv\"\nraw_data = pd.read_csv(train_file)\n\nx, y = data_prep(raw_data)\n\nmodel = Sequential()\n# Couches à ajouter ici pour la partie feature extraction\nmodel.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(img_rows, img_cols, 1)))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n# model.add(AveragePooling2D(pool_size=(2, 2)))\n\nmodel.add(Dropout(0.5))\nmodel.add(BatchNormalization())\n\nmodel.add(Flatten())\n# Couches à ajouter ici pour la partie classification\nmodel.add(Dense(128, activation='relu')) # Fully connected layer\nmodel.add(Dense(num_classes, activation='softmax'))\n\nmodel.compile(loss=keras.losses.categorical_crossentropy,\n              optimizer='adam',\n              metrics=['accuracy'])\n\ncallback = EarlyStopping(monitor='val_loss', patience=3)\n\nhistory = model.fit(x, y,\n          batch_size=128,\n          epochs=20, # increase epochs for better understanding of early stopping\n          validation_split=0.2,\n          callbacks=[callback])\n","metadata":{"execution":{"iopub.status.busy":"2023-07-05T13:22:30.035509Z","iopub.execute_input":"2023-07-05T13:22:30.035789Z","iopub.status.idle":"2023-07-05T13:24:55.072053Z","shell.execute_reply.started":"2023-07-05T13:22:30.035733Z","shell.execute_reply":"2023-07-05T13:24:55.071244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting accuracy\nplt.plot(history.history['acc'])\nplt.plot(history.history['val_acc'])\nplt.title('Model accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-05T13:26:41.873276Z","iopub.execute_input":"2023-07-05T13:26:41.873584Z","iopub.status.idle":"2023-07-05T13:26:42.158205Z","shell.execute_reply.started":"2023-07-05T13:26:41.873542Z","shell.execute_reply":"2023-07-05T13:26:42.157019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting loss\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('Model loss')\nplt.ylabel('Loss')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-05T13:24:55.353104Z","iopub.execute_input":"2023-07-05T13:24:55.353459Z","iopub.status.idle":"2023-07-05T13:24:55.656103Z","shell.execute_reply.started":"2023-07-05T13:24:55.353393Z","shell.execute_reply":"2023-07-05T13:24:55.654842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Your Turn\nYou are ready to **[build your own model](https://www.kaggle.com/kernels/fork/3349938)**.\n","metadata":{}}]}