{"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-06-27T21:18:12.105460Z","iopub.execute_input":"2022-06-27T21:18:12.106254Z","iopub.status.idle":"2022-06-27T21:18:12.145511Z","shell.execute_reply.started":"2022-06-27T21:18:12.106160Z","shell.execute_reply":"2022-06-27T21:18:12.144570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom tensorflow.keras.optimizers import SGD, Adam\nfrom keras.utils.np_utils import to_categorical\nfrom keras import backend as K\nfrom keras.constraints import maxnorm\nfrom keras.preprocessing.image import ImageDataGenerator\nimport matplotlib.pyplot as plt\nfrom PIL import Image\n","metadata":{"execution":{"iopub.status.busy":"2022-06-27T21:18:18.254845Z","iopub.execute_input":"2022-06-27T21:18:18.255229Z","iopub.status.idle":"2022-06-27T21:18:24.919235Z","shell.execute_reply.started":"2022-06-27T21:18:18.255197Z","shell.execute_reply":"2022-06-27T21:18:24.918379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.datasets import cifar10\n(x_train, y_train), (x_test, y_test) = cifar10.load_data()\nprint(y_train.shape)\nprint(type(y_train))","metadata":{"execution":{"iopub.status.busy":"2022-06-27T21:36:52.116042Z","iopub.execute_input":"2022-06-27T21:36:52.116415Z","iopub.status.idle":"2022-06-27T21:36:52.900309Z","shell.execute_reply.started":"2022-06-27T21:36:52.116367Z","shell.execute_reply":"2022-06-27T21:36:52.899468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.astype('float32')\nx_test = x_test.astype('float32')\nx_train = x_train/255.0\nx_test = x_test/255.0\ny_train = to_categorical(y_train)\ny_test  = to_categorical(y_test)\nnum_classes = y_test.shape[1]\nprint(num_classes)\nprint('x_train_shape: ', x_train.shape)\nprint('test_shape: ', y_train.shape)\n","metadata":{"execution":{"iopub.status.busy":"2022-06-27T21:37:16.881700Z","iopub.execute_input":"2022-06-27T21:37:16.882072Z","iopub.status.idle":"2022-06-27T21:37:17.300430Z","shell.execute_reply.started":"2022-06-27T21:37:16.882040Z","shell.execute_reply":"2022-06-27T21:37:17.299383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train[:1])","metadata":{"execution":{"iopub.status.busy":"2022-06-27T21:37:30.415987Z","iopub.execute_input":"2022-06-27T21:37:30.416835Z","iopub.status.idle":"2022-06-27T21:37:30.424301Z","shell.execute_reply.started":"2022-06-27T21:37:30.416792Z","shell.execute_reply":"2022-06-27T21:37:30.423482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(9):\n    plt.subplot(3,3,i+1)\n    plt.imshow(x_train[i]) \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-27T21:37:45.459871Z","iopub.execute_input":"2022-06-27T21:37:45.460249Z","iopub.status.idle":"2022-06-27T21:37:46.119988Z","shell.execute_reply.started":"2022-06-27T21:37:45.460218Z","shell.execute_reply":"2022-06-27T21:37:46.119255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"x_train_shape: \", x_train.shape)\nprint(\"y_train_shape: \", y_train.shape)\nprint(\"x_test_shape: \", x_test.shape)\nprint(\"y_test_shape: \", y_test.shape)\n","metadata":{"execution":{"iopub.status.busy":"2022-06-27T21:37:57.966505Z","iopub.execute_input":"2022-06-27T21:37:57.966864Z","iopub.status.idle":"2022-06-27T21:37:57.972382Z","shell.execute_reply.started":"2022-06-27T21:37:57.966835Z","shell.execute_reply":"2022-06-27T21:37:57.971622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = Adam(learning_rate=0.001)\nepochs = 50\nbatch_size = 100","metadata":{"execution":{"iopub.status.busy":"2022-06-27T21:38:01.060306Z","iopub.execute_input":"2022-06-27T21:38:01.061132Z","iopub.status.idle":"2022-06-27T21:38:01.066471Z","shell.execute_reply.started":"2022-06-27T21:38:01.061091Z","shell.execute_reply":"2022-06-27T21:38:01.065571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(filters = 32,kernel_size = (5,5), padding='same',\n                 activation = 'relu', input_shape = (32,32,3)))\nmodel.add(Conv2D(filters = 32,kernel_size = (3,3), padding='same',\n                 activation = 'relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Conv2D(filters = 64,kernel_size = (3,3), padding='same',\n                 activation = 'relu'))\nmodel.add(Conv2D(filters = 32,kernel_size = (3,3), padding='same',\n                 activation = 'relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Flatten())\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1000, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1000, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-06-27T21:38:03.390468Z","iopub.execute_input":"2022-06-27T21:38:03.391158Z","iopub.status.idle":"2022-06-27T21:38:03.470034Z","shell.execute_reply.started":"2022-06-27T21:38:03.391123Z","shell.execute_reply":"2022-06-27T21:38:03.469186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = optimizer, loss = 'categorical_crossentropy', metrics=['categorical_accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-06-27T21:39:55.393817Z","iopub.execute_input":"2022-06-27T21:39:55.394199Z","iopub.status.idle":"2022-06-27T21:39:55.405006Z","shell.execute_reply.started":"2022-06-27T21:39:55.394166Z","shell.execute_reply":"2022-06-27T21:39:55.404223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n            rotation_range = 0.5,\n            width_shift_range = 0.5,\n            height_shift_range = 0.5,\n            zoom_range = 0.5)\ndatagen.fit(x_train)","metadata":{"execution":{"iopub.status.busy":"2022-06-27T21:38:09.276736Z","iopub.execute_input":"2022-06-27T21:38:09.277088Z","iopub.status.idle":"2022-06-27T21:38:09.518471Z","shell.execute_reply.started":"2022-06-27T21:38:09.277057Z","shell.execute_reply":"2022-06-27T21:38:09.517592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-06-27T21:38:11.925832Z","iopub.execute_input":"2022-06-27T21:38:11.926193Z","iopub.status.idle":"2022-06-27T21:38:11.932244Z","shell.execute_reply.started":"2022-06-27T21:38:11.926161Z","shell.execute_reply":"2022-06-27T21:38:11.931387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x_train, y_train, epochs = epochs,\n                              validation_data=(x_test, y_test), steps_per_epoch = batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-06-27T21:43:03.931048Z","iopub.execute_input":"2022-06-27T21:43:03.931451Z","iopub.status.idle":"2022-06-27T21:46:27.383309Z","shell.execute_reply.started":"2022-06-27T21:43:03.931383Z","shell.execute_reply":"2022-06-27T21:46:27.382283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.evaluate(x_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-06-27T22:33:35.235903Z","iopub.execute_input":"2022-06-27T22:33:35.236284Z","iopub.status.idle":"2022-06-27T22:33:36.596605Z","shell.execute_reply.started":"2022-06-27T22:33:35.236252Z","shell.execute_reply":"2022-06-27T22:33:36.595687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_test[0].shape)","metadata":{"execution":{"iopub.status.busy":"2022-06-27T21:46:27.409135Z","iopub.execute_input":"2022-06-27T21:46:27.409648Z","iopub.status.idle":"2022-06-27T21:46:27.417978Z","shell.execute_reply.started":"2022-06-27T21:46:27.409596Z","shell.execute_reply":"2022-06-27T21:46:27.416977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights('model_28_06_2022_20_26.h5')","metadata":{"execution":{"iopub.status.busy":"2022-06-27T21:47:27.673164Z","iopub.execute_input":"2022-06-27T21:47:27.673537Z","iopub.status.idle":"2022-06-27T21:47:27.717987Z","shell.execute_reply.started":"2022-06-27T21:47:27.673505Z","shell.execute_reply":"2022-06-27T21:47:27.717024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\n\n\nimg = cv2.imread('../input/kamyonet-resim/kamyonet.jpg')\nimg2 = cv2.imread('../input/birdimage/cat.jpg')\nimg, img2 = cv2.cvtColor(img,cv2.COLOR_BGR2RGB),cv2.cvtColor(img2,cv2.COLOR_BGR2RGB)\nimg, img2 = cv2.resize(img,(32,32), interpolation = cv2.INTER_AREA), cv2.resize(img2,(32,32), interpolation = cv2.INTER_AREA)\nimg,img2 = np.array(img),np.array(img2)\nprint(img.shape)\nprint(img2.shape)\nplt.subplot(221)\nplt.imshow(img)\nplt.subplot(222)\nplt.imshow(img2)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-06-27T22:32:04.469492Z","iopub.execute_input":"2022-06-27T22:32:04.469876Z","iopub.status.idle":"2022-06-27T22:32:04.724774Z","shell.execute_reply.started":"2022-06-27T22:32:04.469847Z","shell.execute_reply":"2022-06-27T22:32:04.723829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = ['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck']","metadata":{"execution":{"iopub.status.busy":"2022-06-27T22:20:43.331744Z","iopub.execute_input":"2022-06-27T22:20:43.332113Z","iopub.status.idle":"2022-06-27T22:20:43.336802Z","shell.execute_reply.started":"2022-06-27T22:20:43.332081Z","shell.execute_reply":"2022-06-27T22:20:43.335741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_array = np.expand_dims(img2, axis=0)\nprediction  = model.predict(img_array)\nprint(prediction[0])\nprediction = np.around(prediction)\nx = prediction[0].astype(int)\nprint(prediction[0])\nprint(class_names[np.where(x == 1)[0][0]])","metadata":{"execution":{"iopub.status.busy":"2022-06-27T22:32:35.802495Z","iopub.execute_input":"2022-06-27T22:32:35.802862Z","iopub.status.idle":"2022-06-27T22:32:35.852869Z","shell.execute_reply.started":"2022-06-27T22:32:35.802833Z","shell.execute_reply":"2022-06-27T22:32:35.851437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-27T22:16:43.805378Z","iopub.execute_input":"2022-06-27T22:16:43.806186Z","iopub.status.idle":"2022-06-27T22:16:43.969275Z","shell.execute_reply.started":"2022-06-27T22:16:43.806150Z","shell.execute_reply":"2022-06-27T22:16:43.968271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}