{"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 # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport keras\nfrom sklearn.base import BaseEstimator, ClassifierMixin, TransformerMixin\nfrom sklearn.pipeline import Pipeline\nfrom keras import layers, applications, callbacks, optimizers\nfrom keras.utils import to_categorical\nfrom sklearn.metrics import accuracy_score\nimport numpy as np\n\nlabel = pd.read_csv(\"/kaggle/input/dog-breed-identification/labels.csv\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-22T13:58:24.968065Z","iopub.execute_input":"2023-06-22T13:58:24.968551Z","iopub.status.idle":"2023-06-22T13:58:33.314575Z","shell.execute_reply.started":"2023-06-22T13:58:24.968517Z","shell.execute_reply":"2023-06-22T13:58:33.313588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"breeds = np.unique(label[\"breed\"]).astype(str)","metadata":{"execution":{"iopub.status.busy":"2023-06-22T13:58:33.318314Z","iopub.execute_input":"2023-06-22T13:58:33.319324Z","iopub.status.idle":"2023-06-22T13:58:33.335527Z","shell.execute_reply.started":"2023-06-22T13:58:33.319290Z","shell.execute_reply":"2023-06-22T13:58:33.334497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train =  np.zeros([label.shape[0], 331, 331, 3],dtype=np.uint8)\ny_train = np.empty([label.shape[0],1], dtype=breeds.dtype)\n\n\nfor i ,filename in enumerate(label['id'].values):\n    path = os.path.join(\"/kaggle/input/dog-breed-identification/train\", filename) + \".jpg\"\n    image = keras.utils.load_img(path,target_size=x_train.shape[1:3])\n    input_arr = keras.utils.img_to_array(image)\n    x_train[i] = input_arr\n    del input_arr\n    y_train[i] = label[\"breed\"][i] ","metadata":{"execution":{"iopub.status.busy":"2023-06-22T13:58:33.337044Z","iopub.execute_input":"2023-06-22T13:58:33.337448Z","iopub.status.idle":"2023-06-22T13:59:40.075605Z","shell.execute_reply.started":"2023-06-22T13:58:33.337417Z","shell.execute_reply":"2023-06-22T13:59:40.074610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_features(X):\n    inputs =keras.Input(shape=X.shape[1:])\n    conv_base = applications.inception_resnet_v2.InceptionResNetV2(\n    weights=\"imagenet\",\n    include_top=False)\n    x = applications.inception_resnet_v2.preprocess_input(inputs)\n    x = conv_base(x)\n    x = layers.GlobalAveragePooling2D()(x)\n    model = keras.Model(inputs=inputs, outputs=x)\n    \n    return model.predict(X, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-06-22T13:59:40.078354Z","iopub.execute_input":"2023-06-22T13:59:40.079016Z","iopub.status.idle":"2023-06-22T13:59:40.086547Z","shell.execute_reply.started":"2023-06-22T13:59:40.078983Z","shell.execute_reply":"2023-06-22T13:59:40.085576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class RedeNeural(BaseEstimator, ClassifierMixin):\n    def __init__(self, epochs=5, batch_size=128):\n        self.epochs = epochs\n        self.batch_size = batch_size\n    def fit(self, X, y):\n        self.labels, ids = np.unique(y, return_inverse=True)\n        yhot = to_categorical(ids)      \n        n_classes = self.labels.shape[0]\n        features_preprocessn = get_features(X)\n        \n        self.model = keras.Sequential([ layers.Dense(256,input_shape=(features_preprocessn.shape[1],)),\n                                        layers.Dropout(0.5),\n                                        layers.Dense(n_classes,activation= 'softmax')])         \n        \n        self.model.compile(optimizer=\"adam\",\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])        \n    \n        self.model.fit(features_preprocessn, \n                       yhot,\n                       epochs=self.epochs, \n                       batch_size=self.batch_size,\n                       validation_split=0.2)\n        return self\n\n    def predict(self, X, y=None,to_submission=False):\n        probabilities = self.model.predict(X)\n        print(\"to_submission: \", to_submission)\n        if to_submission is False:\n            ypred = self.labels[np.argmax(probabilities, axis=1)]\n            return ypred\n        else:\n            return probabilities\n\nclass DividePor255(BaseEstimator, TransformerMixin):\n    def fit(self, X, y):\n        return self\n    def transform(self, X, y=None):\n        return np.array(X, dtype=\"float32\") / 255\n\nclass MudaShape(BaseEstimator, TransformerMixin):\n    def fit(self, X, y):\n        return self\n    def transform(self, X, y=None):\n        return X.reshape((-1,224,224,3))\n\nmodelo = Pipeline([\n    #(\"scaler\", DividePor255()),    \n    (\"ann\", RedeNeural(epochs=20))\n])","metadata":{"execution":{"iopub.status.busy":"2023-06-22T13:59:40.089720Z","iopub.execute_input":"2023-06-22T13:59:40.091112Z","iopub.status.idle":"2023-06-22T13:59:40.112777Z","shell.execute_reply.started":"2023-06-22T13:59:40.091080Z","shell.execute_reply":"2023-06-22T13:59:40.111887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelo.fit(x_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-06-22T13:59:40.117399Z","iopub.execute_input":"2023-06-22T13:59:40.118056Z","iopub.status.idle":"2023-06-22T14:01:38.277634Z","shell.execute_reply.started":"2023-06-22T13:59:40.118021Z","shell.execute_reply":"2023-06-22T14:01:38.276537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del x_train\ndel y_train\ndel label","metadata":{"execution":{"iopub.status.busy":"2023-06-22T14:01:38.279209Z","iopub.execute_input":"2023-06-22T14:01:38.279569Z","iopub.status.idle":"2023-06-22T14:01:38.476084Z","shell.execute_reply.started":"2023-06-22T14:01:38.279537Z","shell.execute_reply":"2023-06-22T14:01:38.474987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_filenames = [\"/kaggle/input/dog-breed-identification/test/\" + fname for fname in os.listdir(\"/kaggle/input/dog-breed-identification/test\")]\nx_test =  np.zeros([len(test_filenames), 331, 331, 3],dtype=np.uint8)\n\nfor i ,filename in enumerate(test_filenames):\n    image = keras.utils.load_img(filename,target_size=x_test.shape[1:3])\n    input_arr = keras.utils.img_to_array(image)\n    x_test[i] = input_arr\n    del input_arr\n ","metadata":{"execution":{"iopub.status.busy":"2023-06-22T14:01:38.478023Z","iopub.execute_input":"2023-06-22T14:01:38.478449Z","iopub.status.idle":"2023-06-22T14:02:52.699133Z","shell.execute_reply.started":"2023-06-22T14:01:38.478409Z","shell.execute_reply":"2023-06-22T14:02:52.698067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_features =  get_features(x_test)","metadata":{"execution":{"iopub.status.busy":"2023-06-22T14:02:52.700832Z","iopub.execute_input":"2023-06-22T14:02:52.701193Z","iopub.status.idle":"2023-06-22T14:04:14.285335Z","shell.execute_reply.started":"2023-06-22T14:02:52.701159Z","shell.execute_reply":"2023-06-22T14:04:14.284251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del x_test\ndel test_filenames","metadata":{"execution":{"iopub.status.busy":"2023-06-22T14:04:14.290798Z","iopub.execute_input":"2023-06-22T14:04:14.291113Z","iopub.status.idle":"2023-06-22T14:04:14.502894Z","shell.execute_reply.started":"2023-06-22T14:04:14.291071Z","shell.execute_reply":"2023-06-22T14:04:14.501691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-06-22T14:04:14.504421Z","iopub.execute_input":"2023-06-22T14:04:14.504899Z","iopub.status.idle":"2023-06-22T14:04:15.543404Z","shell.execute_reply.started":"2023-06-22T14:04:14.504865Z","shell.execute_reply":"2023-06-22T14:04:15.542370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_df = pd.DataFrame(columns=np.concatenate((np.array([\"id\"]),breeds)))\npreds_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-22T14:04:15.546817Z","iopub.execute_input":"2023-06-22T14:04:15.547116Z","iopub.status.idle":"2023-06-22T14:04:15.595464Z","shell.execute_reply.started":"2023-06-22T14:04:15.547081Z","shell.execute_reply":"2023-06-22T14:04:15.594074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_df[\"id\"] = [fname.split(\".\")[0] for fname in os.listdir(\"/kaggle/input/dog-breed-identification/test\")]\npreds_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-22T14:04:15.596626Z","iopub.execute_input":"2023-06-22T14:04:15.597520Z","iopub.status.idle":"2023-06-22T14:04:15.789434Z","shell.execute_reply.started":"2023-06-22T14:04:15.597488Z","shell.execute_reply":"2023-06-22T14:04:15.788453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = modelo.predict(test_features,to_submission = True)","metadata":{"execution":{"iopub.status.busy":"2023-06-22T14:04:15.790776Z","iopub.execute_input":"2023-06-22T14:04:15.792006Z","iopub.status.idle":"2023-06-22T14:04:17.270845Z","shell.execute_reply.started":"2023-06-22T14:04:15.791966Z","shell.execute_reply":"2023-06-22T14:04:17.269779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred","metadata":{"execution":{"iopub.status.busy":"2023-06-22T14:04:17.273206Z","iopub.execute_input":"2023-06-22T14:04:17.273815Z","iopub.status.idle":"2023-06-22T14:04:17.287523Z","shell.execute_reply.started":"2023-06-22T14:04:17.273769Z","shell.execute_reply":"2023-06-22T14:04:17.286163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_df.loc[:,list(breeds)]= y_pred\n\npreds_df.to_csv('submission.csv',index=None)\npreds_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-22T14:04:26.509683Z","iopub.execute_input":"2023-06-22T14:04:26.510077Z","iopub.status.idle":"2023-06-22T14:04:30.295120Z","shell.execute_reply.started":"2023-06-22T14:04:26.510050Z","shell.execute_reply":"2023-06-22T14:04:30.293930Z"},"trusted":true},"execution_count":null,"outputs":[]}]}