{"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":"2023-02-01T21:25:48.622891Z","iopub.execute_input":"2023-02-01T21:25:48.623358Z","iopub.status.idle":"2023-02-01T21:25:48.659780Z","shell.execute_reply.started":"2023-02-01T21:25:48.623253Z","shell.execute_reply":"2023-02-01T21:25:48.658660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom tensorflow.keras.layers import Conv2D,Input,Dense,MaxPool2D,BatchNormalization,GlobalAvgPool2D\n","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:25:48.661899Z","iopub.execute_input":"2023-02-01T21:25:48.662621Z","iopub.status.idle":"2023-02-01T21:25:55.792295Z","shell.execute_reply.started":"2023-02-01T21:25:48.662583Z","shell.execute_reply":"2023-02-01T21:25:55.791314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Sequential model approach\n\nmodel = tensorflow.keras.Sequential(\n    [\n        Input(shape=(28,28,1)),\n        Conv2D(32,(3,3),activation='relu'),\n        Conv2D(64,(3,3),activation='relu'),\n        MaxPool2D(),\n        BatchNormalization(),\n        \n        Conv2D(128,(3,3),activation='relu'),\n        MaxPool2D(),\n        BatchNormalization(),\n        \n        GlobalAvgPool2D(),\n        Dense(64,activation='relu'),\n        Dense(10,activation='softmax')\n    ]\n)\n\n\n\ndef display_some_examples(examples,labels):\n    \n    plt.figure(figsize=(10,10))\n    \n    for i in range(25):\n        idx =np.random.randint(0,examples.shape[0]-1)\n        img= examples[idx]\n        label=labels[idx]\n        \n        plt.subplot(5,5,i+1)\n        plt.title(str(label))\n        plt.tight_layout()\n        plt.imshow(img,cmap='gray')\n\nif __name__=='__main__':\n    (x_train,y_train) ,(x_test,y_test) = tensorflow.keras.datasets.mnist.load_data()\n    \n    print(x_train.shape)\n    print(y_train.shape)\n    print(x_test.shape)\n    print(y_test.shape)\n    \n    if False:\n        \n        display_some_examples(x_train,y_train)\n        \n    #Normalize Data. The values goes from 0 to 255. \n    #And we change to float to avoid getting 0 values with INT    \n    x_train = x_train.astype('float32') / 255\n    x_test = x_test.astype('float32') /255\n    \n    \n    #Expand last dimension\n    x_train = np.expand_dims(x_train,axis=-1)\n    x_test = np.expand_dims(x_test,axis=-1)\n    \n    #to use categorical_crossentropy i have to use Ohe\n    y_train = tensorflow.keras.utils.to_categorical(y_train,10)\n    y_test = tensorflow.keras.utils.to_categorical(y_test,10)\n    \n    model.compile(optimizer='adam',loss='categorical_crossentropy',metrics='accuracy')\n    \n    \n    #model training\n    model.fit(x_train,y_train,batch_size=64,epochs=3,validation_split=0.2)\n    \n    model.evaluate(x_test,y_test,batch_size=64)\n    ","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:27:22.169333Z","iopub.execute_input":"2023-02-01T21:27:22.169804Z","iopub.status.idle":"2023-02-01T21:27:37.423378Z","shell.execute_reply.started":"2023-02-01T21:27:22.169765Z","shell.execute_reply":"2023-02-01T21:27:37.422381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Function model approach, function that returns the model\n\ndef func_model():\n    \n    my_input = Input(shape=(28,28,1))\n    x = Conv2D(32,(3,3),activation='relu')(my_input)\n    x = Conv2D(64,(3,3),activation='relu')(x)\n    x = MaxPool2D()(x)\n    x = BatchNormalization()(x)\n        \n    x = Conv2D(128,(3,3),activation='relu')(x)\n    x = MaxPool2D()(x)\n    x = BatchNormalization()(x)\n        \n    x = GlobalAvgPool2D()(x)\n    x = Dense(64,activation='relu')(x)\n    x = Dense(10,activation='softmax')(x)\n    \n    model = tensorflow.keras.Model(inputs= my_input,outputs=x)\n    \n    return model\n\ndef display_some_examples(examples,labels):\n    \n    plt.figure(figsize=(10,10))\n    \n    for i in range(25):\n        idx =np.random.randint(0,examples.shape[0]-1)\n        img= examples[idx]\n        label=labels[idx]\n        \n        plt.subplot(5,5,i+1)\n        plt.title(str(label))\n        plt.tight_layout()\n        plt.imshow(img,cmap='gray')\n\nif __name__=='__main__':\n    (x_train,y_train) ,(x_test,y_test) = tensorflow.keras.datasets.mnist.load_data()\n    \n    print(x_train.shape)\n    print(y_train.shape)\n    print(x_test.shape)\n    print(y_test.shape)\n    \n    if False:\n        \n        display_some_examples(x_train,y_train)\n        \n    #Normalize Data. The values goes from 0 to 255. \n    #And we change to float to avoid getting 0 values with INT    \n    x_train = x_train.astype('float32') / 255\n    x_test = x_test.astype('float32') /255\n    \n    \n    #Expand last dimension\n    x_train = np.expand_dims(x_train,axis=-1)\n    x_test = np.expand_dims(x_test,axis=-1)\n    \n    #to use categorical_crossentropy i have to use Ohe\n    y_train = tensorflow.keras.utils.to_categorical(y_train,10)\n    y_test = tensorflow.keras.utils.to_categorical(y_test,10)\n    \n    \n    model =func_model()\n    model.compile(optimizer='adam',loss='categorical_crossentropy',metrics='accuracy')\n    \n    \n    #model training\n    model.fit(x_train,y_train,batch_size=64,epochs=3,validation_split=0.2)\n    \n    model.evaluate(x_test,y_test,batch_size=64)\n    ","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:26:24.557119Z","iopub.execute_input":"2023-02-01T21:26:24.561335Z","iopub.status.idle":"2023-02-01T21:26:39.872354Z","shell.execute_reply.started":"2023-02-01T21:26:24.561292Z","shell.execute_reply":"2023-02-01T21:26:39.871400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Tensorflow inherit from it class\n\nclass MyCostumModel(tensorflow.keras.Model):\n    \n    def __init__(self):\n        super().__init__()\n        self.conv1= Conv2D(32,(3,3),activation='relu')\n        self.conv2= Conv2D(64,(3,3),activation='relu')\n        self.maxpool1= MaxPool2D()\n        self.batchnorm1= BatchNormalization()\n        \n        self.conv3= Conv2D(128,(3,3),activation='relu')\n        self.maxpool2= MaxPool2D()\n        self.batchnorm2= BatchNormalization()\n        \n        self.globalavgpool1=GlobalAvgPool2D()\n        self.dense1 = Dense(64,activation='relu')\n        self.dense2 = Dense(10,activation='softmax')\n        \n        \n    def call(self, my_input):\n        x= x = self.conv1(my_input)\n        x = self.conv2(x)\n        x = self.maxpool1(x)\n        x = self.batchnorm1(x)\n        x = self.conv3(x)\n        x = self.maxpool2(x)\n        x = self.batchnorm2(x)\n        x = self.globalavgpool1(x)\n        x = self.dense1(x)\n        x = self.dense2(x) \n\n        return x  \n    \n    \n\nif __name__=='__main__':\n    (x_train,y_train) ,(x_test,y_test) = tensorflow.keras.datasets.mnist.load_data()\n    \n    print(x_train.shape)\n    print(y_train.shape)\n    print(x_test.shape)\n    print(y_test.shape)\n    \n    if False:\n        \n        display_some_examples(x_train,y_train)\n        \n    #Normalize Data. The values goes from 0 to 255. \n    #And we change to float to avoid getting 0 values with INT    \n    x_train = x_train.astype('float32') / 255\n    x_test = x_test.astype('float32') /255\n    \n    \n    #Expand last dimension\n    x_train = np.expand_dims(x_train,axis=-1)\n    x_test = np.expand_dims(x_test,axis=-1)\n    \n    #to use categorical_crossentropy i have to use Ohe\n    y_train = tensorflow.keras.utils.to_categorical(y_train,10)\n    y_test = tensorflow.keras.utils.to_categorical(y_test,10)\n    \n    \n    model =MyCostumModel()\n    model.compile(optimizer='adam',loss='categorical_crossentropy',metrics='accuracy')\n    \n    \n    #model training\n    model.fit(x_train,y_train,batch_size=64,epochs=3,validation_split=0.2)\n    \n    model.evaluate(x_test,y_test,batch_size=64)\n    \n        \n        \n        \n    ","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:26:39.873783Z","iopub.execute_input":"2023-02-01T21:26:39.874392Z","iopub.status.idle":"2023-02-01T21:27:02.186060Z","shell.execute_reply.started":"2023-02-01T21:26:39.874353Z","shell.execute_reply":"2023-02-01T21:27:02.184961Z"},"trusted":true},"execution_count":null,"outputs":[]}]}