{"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)\nimport matplotlib.pyplot as plt\nimport warnings\nwarnings.filterwarnings('ignore')\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","_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-02-04T22:49:31.380751Z","iopub.execute_input":"2022-02-04T22:49:31.381076Z","iopub.status.idle":"2022-02-04T22:49:55.083370Z","shell.execute_reply.started":"2022-02-04T22:49:31.381044Z","shell.execute_reply":"2022-02-04T22:49:55.082291Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport tensorflow as tf\nfrom tensorflow.keras import utils\nfrom tensorflow.keras.optimizers import SGD\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.constraints import max_norm\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D ","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-02-04T22:59:29.932396Z","iopub.execute_input":"2022-02-04T22:59:29.932932Z","iopub.status.idle":"2022-02-04T22:59:29.944242Z","shell.execute_reply.started":"2022-02-04T22:59:29.932893Z","shell.execute_reply":"2022-02-04T22:59:29.943252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# our dataset is present in keras.datasets \n\nfrom tensorflow.keras.datasets import cifar10\nfrom tensorflow.keras.datasets import cifar100\nimport matplotlib.pyplot as plt\n(train_X,train_Y),(test_X,test_Y)=cifar10.load_data()","metadata":{"execution":{"iopub.status.busy":"2022-02-04T22:52:09.326524Z","iopub.execute_input":"2022-02-04T22:52:09.327460Z","iopub.status.idle":"2022-02-04T22:52:21.663251Z","shell.execute_reply.started":"2022-02-04T22:52:09.327398Z","shell.execute_reply":"2022-02-04T22:52:21.662200Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/happy-whale-and-dolphin/train.csv')\n\nsub = pd.read_csv('../input/happy-whale-and-dolphin/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-04T22:53:09.980115Z","iopub.execute_input":"2022-02-04T22:53:09.980987Z","iopub.status.idle":"2022-02-04T22:53:10.141934Z","shell.execute_reply.started":"2022-02-04T22:53:09.980894Z","shell.execute_reply":"2022-02-04T22:53:10.140973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plotting the data to display the images\n\nnumber_of_images=6\nplt.figure(figsize=(15,9))\nfor index in range(number_of_images):\n    plt.subplot(330+1+index)\n    plt.imshow(train_X[index])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-04T22:53:19.862532Z","iopub.execute_input":"2022-02-04T22:53:19.863438Z","iopub.status.idle":"2022-02-04T22:53:20.564869Z","shell.execute_reply.started":"2022-02-04T22:53:19.863381Z","shell.execute_reply":"2022-02-04T22:53:20.563897Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#What the fuck!  Where are my Dolphins and Whales??? If I see any of these fins while I'm swimming, very likeky I'd have a stroke.","metadata":{}},{"cell_type":"code","source":"train_x = train_X.astype('float32')\ntrain_y = train_Y.astype('float32')\n\ntrain_x = train_X/255.0\ntrain_y = train_Y/255.0","metadata":{"execution":{"iopub.status.busy":"2022-02-04T22:58:30.324578Z","iopub.execute_input":"2022-02-04T22:58:30.326766Z","iopub.status.idle":"2022-02-04T22:58:31.396662Z","shell.execute_reply.started":"2022-02-04T22:58:30.326637Z","shell.execute_reply":"2022-02-04T22:58:31.395674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#One hot encoding for target class\n\nIn fact, I've already encoded the target and all the rest. ","metadata":{}},{"cell_type":"code","source":"train_Y = utils.to_categorical(train_Y)\ntest_Y = utils.to_categorical(test_Y)\n\nnum_classes = test_Y.shape[1]","metadata":{"execution":{"iopub.status.busy":"2022-02-04T22:59:42.701477Z","iopub.execute_input":"2022-02-04T22:59:42.701867Z","iopub.status.idle":"2022-02-04T22:59:42.711455Z","shell.execute_reply.started":"2022-02-04T22:59:42.701827Z","shell.execute_reply":"2022-02-04T22:59:42.709420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Create the sequential model and add the layers","metadata":{}},{"cell_type":"code","source":"model=Sequential()\nmodel.add(Conv2D(32,(3,3), input_shape=(32,32,3), padding='same', activation='relu', kernel_constraint=max_norm(3)))\nmodel.add(Dropout(0.2))\nmodel.add(Conv2D(32,(3,3), activation='relu', padding='same', kernel_constraint=max_norm(3)))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Flatten())\nmodel.add(Dense(512, activation='relu', kernel_constraint=max_norm(3)))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(num_classes, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-02-04T23:00:08.147051Z","iopub.execute_input":"2022-02-04T23:00:08.147429Z","iopub.status.idle":"2022-02-04T23:00:08.685976Z","shell.execute_reply.started":"2022-02-04T23:00:08.147379Z","shell.execute_reply":"2022-02-04T23:00:08.684868Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#The optimizer and compiling the model.","metadata":{}},{"cell_type":"code","source":"sgd = SGD(learning_rate=0.01, momentum=0.9, nesterov=False, decay=(0.01/25))\nmodel.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-02-04T23:00:49.558865Z","iopub.execute_input":"2022-02-04T23:00:49.559232Z","iopub.status.idle":"2022-02-04T23:00:49.577019Z","shell.execute_reply.started":"2022-02-04T23:00:49.559195Z","shell.execute_reply":"2022-02-04T23:00:49.576352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-04T23:01:06.503597Z","iopub.execute_input":"2022-02-04T23:01:06.504188Z","iopub.status.idle":"2022-02-04T23:01:06.515432Z","shell.execute_reply.started":"2022-02-04T23:01:06.504130Z","shell.execute_reply":"2022-02-04T23:01:06.514121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Model Summary","metadata":{}},{"cell_type":"code","source":"# uncomment below comment to visualize the mode architecture\n\ntf.keras.utils.plot_model(model, show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2022-02-04T23:01:26.800835Z","iopub.execute_input":"2022-02-04T23:01:26.801144Z","iopub.status.idle":"2022-02-04T23:01:27.768864Z","shell.execute_reply.started":"2022-02-04T23:01:26.801107Z","shell.execute_reply":"2022-02-04T23:01:27.767709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Train the model whithout any Whale or Dolphin. That's insane!!  Stop it. Stop it!","metadata":{}},{"cell_type":"code","source":"model.fit(train_x,train_Y,validation_data=(test_X,test_Y),epochs=30,batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2022-02-04T23:01:48.504004Z","iopub.execute_input":"2022-02-04T23:01:48.504567Z","iopub.status.idle":"2022-02-04T23:50:13.383682Z","shell.execute_reply.started":"2022-02-04T23:01:48.504524Z","shell.execute_reply":"2022-02-04T23:50:13.382580Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Calculate accuracy on test data","metadata":{}},{"cell_type":"code","source":"_,acc=model.evaluate(test_X,test_Y)\nprint(acc*100)","metadata":{"execution":{"iopub.status.busy":"2022-02-04T23:50:27.495095Z","iopub.execute_input":"2022-02-04T23:50:27.496050Z","iopub.status.idle":"2022-02-04T23:50:37.977806Z","shell.execute_reply.started":"2022-02-04T23:50:27.495929Z","shell.execute_reply":"2022-02-04T23:50:37.976673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Save the model and use it later.  Why do we use later if there is no Dolphins or Whales!","metadata":{}},{"cell_type":"code","source":"model.save('cifar-10.h5')","metadata":{"execution":{"iopub.status.busy":"2022-02-04T23:50:55.724511Z","iopub.execute_input":"2022-02-04T23:50:55.724836Z","iopub.status.idle":"2022-02-04T23:50:55.863538Z","shell.execute_reply.started":"2022-02-04T23:50:55.724804Z","shell.execute_reply":"2022-02-04T23:50:55.862577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('cifar-10.h5')\nresult = {\n    0:'Dolphin',\n    1:'Whale',    \n    }\n\n\n# the input image is required to be in the shape of dataset , i.e (32,32,3)\nim = Image.open('../input/happy-whale-and-dolphin/test_images/0033b2d4d58730.jpg')\n\n# if our image is on in format of (32,32,3) we need to change it usign \n# im=im.resize((32,32))\n\nplt.imshow(im)\nplt.show()\nim = np.expand_dims(im,axis=0)\nim = np.array(im)\n\n# now that our image is ready we can predict it using our model\nprediction = model.predict(im) \nclasses = np.argmax(prediction,axis=1)[0]\nprint(\"Class Predicted: \",classes,\"\\nLabel Predicted: \",result[classes])","metadata":{"execution":{"iopub.status.busy":"2022-02-04T23:54:23.175907Z","iopub.execute_input":"2022-02-04T23:54:23.177299Z","iopub.status.idle":"2022-02-04T23:54:24.965670Z","shell.execute_reply.started":"2022-02-04T23:54:23.177235Z","shell.execute_reply":"2022-02-04T23:54:24.964113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Finally a fin, even without errors you won't like to see any of this while you're swimming in th see  ","metadata":{}},{"cell_type":"markdown","source":"#It seems, I ran a model with No Dolphins or Whales. Probably, that will happen if we don't stop destroying the nature and extincting animals. Nothing that I plot is random. Maybe, in the future (when I'm not here anymore) it will make a lot of sense.","metadata":{}},{"cell_type":"markdown","source":"#Acknowledgement:\n\nMeet Nagadia https://www.kaggle.com/meetnagadia/image-classification-on-cifar10-using-dl/data\n","metadata":{}}]}