{"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":"import numpy as np\nimport pandas as pd\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\n%matplotlib inline\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten\nfrom keras.layers import Conv2D, MaxPooling2D, BatchNormalization\nfrom tensorflow.keras.optimizers import RMSprop\nimport keras\nimport keras.utils\nfrom keras import utils as np_utils\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:53:14.449816Z","iopub.execute_input":"2022-04-08T06:53:14.450058Z","iopub.status.idle":"2022-04-08T06:53:20.167377Z","shell.execute_reply.started":"2022-04-08T06:53:14.449987Z","shell.execute_reply":"2022-04-08T06:53:20.166611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_img(folder):\n    imgg=[]\n    for filename in os.listdir(folder):\n        img = plt.imread(os.path.join(folder , filename))\n        #img = cv2.resize(img , (200 , 200))\n        imgg.append(img)\n    return imgg","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:53:20.169904Z","iopub.execute_input":"2022-04-08T06:53:20.170366Z","iopub.status.idle":"2022-04-08T06:53:20.176377Z","shell.execute_reply.started":"2022-04-08T06:53:20.170327Z","shell.execute_reply":"2022-04-08T06:53:20.174829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = '../input/jpeg-happywhale-128x128/train_images-128-128/train_images-128-128'\ntest = '../input/jpeg-happywhale-128x128/test_images-128-128/test_images-128-128'\ntrain_df = '../input/happy-whale-and-dolphin/train.csv'","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:53:20.180636Z","iopub.execute_input":"2022-04-08T06:53:20.180836Z","iopub.status.idle":"2022-04-08T06:53:20.192647Z","shell.execute_reply.started":"2022-04-08T06:53:20.180812Z","shell.execute_reply":"2022-04-08T06:53:20.191839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = read_img(train)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:53:20.194784Z","iopub.execute_input":"2022-04-08T06:53:20.195099Z","iopub.status.idle":"2022-04-08T06:56:34.480214Z","shell.execute_reply.started":"2022-04-08T06:53:20.195062Z","shell.execute_reply":"2022-04-08T06:56:34.479450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b = read_img(test)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:56:34.482462Z","iopub.execute_input":"2022-04-08T06:56:34.482946Z","iopub.status.idle":"2022-04-08T06:58:47.351422Z","shell.execute_reply.started":"2022-04-08T06:56:34.482895Z","shell.execute_reply":"2022-04-08T06:58:47.350651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(train_df)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:58:47.354138Z","iopub.execute_input":"2022-04-08T06:58:47.354372Z","iopub.status.idle":"2022-04-08T06:58:47.439746Z","shell.execute_reply.started":"2022-04-08T06:58:47.354335Z","shell.execute_reply":"2022-04-08T06:58:47.439027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.shape(df)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:58:47.442435Z","iopub.execute_input":"2022-04-08T06:58:47.442684Z","iopub.status.idle":"2022-04-08T06:58:47.450069Z","shell.execute_reply.started":"2022-04-08T06:58:47.442652Z","shell.execute_reply":"2022-04-08T06:58:47.449351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.shape(a)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:58:47.451453Z","iopub.execute_input":"2022-04-08T06:58:47.451874Z","iopub.status.idle":"2022-04-08T06:58:48.213155Z","shell.execute_reply.started":"2022-04-08T06:58:47.451831Z","shell.execute_reply":"2022-04-08T06:58:48.212290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:58:48.214484Z","iopub.execute_input":"2022-04-08T06:58:48.215336Z","iopub.status.idle":"2022-04-08T06:58:48.231050Z","shell.execute_reply.started":"2022-04-08T06:58:48.215295Z","shell.execute_reply":"2022-04-08T06:58:48.230378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = a\nX_test = b","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:58:48.234067Z","iopub.execute_input":"2022-04-08T06:58:48.234276Z","iopub.status.idle":"2022-04-08T06:58:48.237314Z","shell.execute_reply.started":"2022-04-08T06:58:48.234246Z","shell.execute_reply":"2022-04-08T06:58:48.236697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"values, counts = np.unique(df['species'], return_counts=True)\nprint(values)\nprint(counts)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:58:48.238482Z","iopub.execute_input":"2022-04-08T06:58:48.239149Z","iopub.status.idle":"2022-04-08T06:58:48.293992Z","shell.execute_reply.started":"2022-04-08T06:58:48.239112Z","shell.execute_reply":"2022-04-08T06:58:48.293202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nplt.figure(figsize = (40,12))\nsns.barplot(values , counts)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:58:48.295243Z","iopub.execute_input":"2022-04-08T06:58:48.295488Z","iopub.status.idle":"2022-04-08T06:58:49.495419Z","shell.execute_reply.started":"2022-04-08T06:58:48.295454Z","shell.execute_reply":"2022-04-08T06:58:49.494608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()\ndf['species'] = le.fit_transform(df['species'])","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:58:49.496686Z","iopub.execute_input":"2022-04-08T06:58:49.501658Z","iopub.status.idle":"2022-04-08T06:58:49.661544Z","shell.execute_reply.started":"2022-04-08T06:58:49.501615Z","shell.execute_reply":"2022-04-08T06:58:49.660703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = df['species']","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:58:49.663417Z","iopub.execute_input":"2022-04-08T06:58:49.663705Z","iopub.status.idle":"2022-04-08T06:58:49.668142Z","shell.execute_reply.started":"2022-04-08T06:58:49.663668Z","shell.execute_reply":"2022-04-08T06:58:49.667426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.shape(X_train))\nprint(np.shape(y_train))\nprint(np.shape(X_test))","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:58:49.669305Z","iopub.execute_input":"2022-04-08T06:58:49.669802Z","iopub.status.idle":"2022-04-08T06:58:50.860591Z","shell.execute_reply.started":"2022-04-08T06:58:49.669764Z","shell.execute_reply":"2022-04-08T06:58:50.858812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_train[888])\n","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:58:50.862019Z","iopub.execute_input":"2022-04-08T06:58:50.862300Z","iopub.status.idle":"2022-04-08T06:58:51.080019Z","shell.execute_reply.started":"2022-04-08T06:58:50.862257Z","shell.execute_reply":"2022-04-08T06:58:51.079332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_train[8888])\n","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:58:51.081286Z","iopub.execute_input":"2022-04-08T06:58:51.081628Z","iopub.status.idle":"2022-04-08T06:58:51.294103Z","shell.execute_reply.started":"2022-04-08T06:58:51.081590Z","shell.execute_reply":"2022-04-08T06:58:51.293338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\ny_train = to_categorical(y_train)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:58:51.296201Z","iopub.execute_input":"2022-04-08T06:58:51.296663Z","iopub.status.idle":"2022-04-08T06:58:51.304313Z","shell.execute_reply.started":"2022-04-08T06:58:51.296631Z","shell.execute_reply":"2022-04-08T06:58:51.303565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:58:51.305862Z","iopub.execute_input":"2022-04-08T06:58:51.306054Z","iopub.status.idle":"2022-04-08T06:58:51.315473Z","shell.execute_reply.started":"2022-04-08T06:58:51.306023Z","shell.execute_reply":"2022-04-08T06:58:51.314823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.array(X_train)\nX_test = np.array(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:58:51.316822Z","iopub.execute_input":"2022-04-08T06:58:51.317230Z","iopub.status.idle":"2022-04-08T06:58:52.482190Z","shell.execute_reply.started":"2022-04-08T06:58:51.317193Z","shell.execute_reply":"2022-04-08T06:58:52.481438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cp = keras.callbacks.ModelCheckpoint(filepath = './weights' , monitor='accuracy' , save_best_only=True)\nes = keras.callbacks.EarlyStopping(monitor='loss',patience=3)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:58:52.483663Z","iopub.execute_input":"2022-04-08T06:58:52.483890Z","iopub.status.idle":"2022-04-08T06:58:52.489005Z","shell.execute_reply.started":"2022-04-08T06:58:52.483856Z","shell.execute_reply":"2022-04-08T06:58:52.488337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = keras.Input(shape=(X_train.shape[1],X_train.shape[2],X_train.shape[3]))\nx = Conv2D(64, (3, 3), activation='relu', padding='same')(inputs)\nx = Conv2D(32, (3, 3), activation='relu', padding='same',)(x)\nx = MaxPooling2D(pool_size=(2, 2))(x)\nx = Dropout(0.2)(x)\nx = Conv2D(64, (3, 3), activation='relu', padding='same')(x)\nx = Conv2D(32, (3, 3), activation='relu', padding='same',)(x)\nx = MaxPooling2D(pool_size=(2, 2))(x)\nx = Conv2D(64, (3, 3), activation='relu', padding='same')(x)\nx = Conv2D(32, (3, 3), activation='relu', padding='same',)(x)\nx = MaxPooling2D(pool_size=(2, 2))(x)\nx = Flatten()(x)\nx = Dense(128,activation='relu')(x)\nx = Dense(64,activation='relu')(x)\nx = Dense(32,activation='relu')(x)\noutput = Dense(30, activation='softmax')(x)\nmodel = keras.Model(inputs, output)\n\nmodel.compile(optimizer='adam',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:58:52.490280Z","iopub.execute_input":"2022-04-08T06:58:52.490740Z","iopub.status.idle":"2022-04-08T06:58:54.933269Z","shell.execute_reply.started":"2022-04-08T06:58:52.490702Z","shell.execute_reply":"2022-04-08T06:58:54.932554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:58:54.934586Z","iopub.execute_input":"2022-04-08T06:58:54.934994Z","iopub.status.idle":"2022-04-08T06:58:54.948942Z","shell.execute_reply.started":"2022-04-08T06:58:54.934955Z","shell.execute_reply":"2022-04-08T06:58:54.948236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"values, counts = np.unique(df['species'], return_counts=True)\nprint(values)\nprint(counts)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:58:54.950059Z","iopub.execute_input":"2022-04-08T06:58:54.950366Z","iopub.status.idle":"2022-04-08T06:58:54.959070Z","shell.execute_reply.started":"2022-04-08T06:58:54.950328Z","shell.execute_reply":"2022-04-08T06:58:54.958161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.shape(y_train)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:58:54.960289Z","iopub.execute_input":"2022-04-08T06:58:54.961146Z","iopub.status.idle":"2022-04-08T06:58:54.966843Z","shell.execute_reply.started":"2022-04-08T06:58:54.961111Z","shell.execute_reply":"2022-04-08T06:58:54.965995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#class_weight = {0:1 , 1:1 , 2:1 , 4:7 , 5:9 , 6:100 , 7:60 , 8:60 , 9:2 , 10:2 , 11:8 , 12:200 , 13:100 , 14:10 , 15:1 , 16:8 , 17:7 , 18:50 , 19:7 , 20:7 , 21:80 , 22:8 , 23:100 , 24:100 , 25:20 , 26:20 , 27:10 , 28:20 , 29:20}\nhistory = model.fit(X_train, y_train , epochs = 5 , callbacks = [cp , es] )","metadata":{"execution":{"iopub.status.busy":"2022-04-08T06:58:54.967980Z","iopub.execute_input":"2022-04-08T06:58:54.968732Z","iopub.status.idle":"2022-04-08T07:02:08.363802Z","shell.execute_reply.started":"2022-04-08T06:58:54.968695Z","shell.execute_reply":"2022-04-08T07:02:08.363088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}