{"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-12T02:06:21.910072Z","iopub.execute_input":"2023-06-12T02:06:21.910532Z","iopub.status.idle":"2023-06-12T02:06:31.021018Z","shell.execute_reply.started":"2023-06-12T02:06:21.910497Z","shell.execute_reply":"2023-06-12T02:06:31.019448Z"},"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=\"S10\")\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-12T02:06:31.023239Z","iopub.execute_input":"2023-06-12T02:06:31.023721Z","iopub.status.idle":"2023-06-12T02:07:59.982740Z","shell.execute_reply.started":"2023-06-12T02:06:31.023677Z","shell.execute_reply":"2023-06-12T02:07:59.981812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nx_train_splited, x_train_to_test, y_train_splited, y_train_to_test = train_test_split(x_train, y_train, test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T02:07:59.984150Z","iopub.execute_input":"2023-06-12T02:07:59.984749Z","iopub.status.idle":"2023-06-12T02:08:01.765902Z","shell.execute_reply.started":"2023-06-12T02:07:59.984718Z","shell.execute_reply":"2023-06-12T02:08:01.764579Z"},"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_v3.InceptionV3(\n    weights=\"imagenet\",\n    include_top=False)\n    x = applications.inception_v3.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-12T02:08:01.768486Z","iopub.execute_input":"2023-06-12T02:08:01.768833Z","iopub.status.idle":"2023-06-12T02:08:01.775541Z","shell.execute_reply.started":"2023-06-12T02:08:01.768797Z","shell.execute_reply":"2023-06-12T02:08:01.774357Z"},"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.Dropout(0.7,input_shape=(features_preprocessn.shape[1],)),\n                                       layers.Dense(n_classes,activation= 'softmax')])  \n        adam = optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, epsilon=None,  amsgrad=False)\n        \n        self.model.compile(optimizer=adam,\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n        \n        reduce = callbacks.ReduceLROnPlateau(monitor='val_acc', factor=.01, patience=3, min_lr=1e-5,verbose = 1)\n        early = callbacks.EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)\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                       callbacks=[reduce , early])\n        return self\n\n    def predict(self, X, y=None):\n        probabilities = self.model.predict(X)\n        ypred = self.labels[np.argmax(probabilities, axis=1)]\n        return ypred\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-12T02:08:01.777665Z","iopub.execute_input":"2023-06-12T02:08:01.778102Z","iopub.status.idle":"2023-06-12T02:08:01.794586Z","shell.execute_reply.started":"2023-06-12T02:08:01.778062Z","shell.execute_reply":"2023-06-12T02:08:01.793022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelo.fit(x_train_splited, y_train_splited)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T02:08:01.796432Z","iopub.execute_input":"2023-06-12T02:08:01.796988Z","iopub.status.idle":"2023-06-12T02:25:46.966933Z","shell.execute_reply.started":"2023-06-12T02:08:01.796955Z","shell.execute_reply":"2023-06-12T02:25:46.965376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features_to_test = get_features(x_train_to_test)\nfeatures_to_test = np.concatenate([features_to_test],axis=-1)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T02:26:06.505586Z","iopub.execute_input":"2023-06-12T02:26:06.506053Z","iopub.status.idle":"2023-06-12T02:30:30.092521Z","shell.execute_reply.started":"2023-06-12T02:26:06.506020Z","shell.execute_reply":"2023-06-12T02:30:30.091313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = modelo.predict(features_to_test)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T02:30:30.094939Z","iopub.execute_input":"2023-06-12T02:30:30.095438Z","iopub.status.idle":"2023-06-12T02:30:30.336112Z","shell.execute_reply.started":"2023-06-12T02:30:30.095396Z","shell.execute_reply":"2023-06-12T02:30:30.334855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_train_to_test, y_pred)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T02:34:25.868097Z","iopub.execute_input":"2023-06-12T02:34:25.868722Z","iopub.status.idle":"2023-06-12T02:34:25.880725Z","shell.execute_reply.started":"2023-06-12T02:34:25.868681Z","shell.execute_reply":"2023-06-12T02:34:25.879238Z"},"trusted":true},"execution_count":null,"outputs":[]}]}