{"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 matplotlib.pyplot as plt\nfrom matplotlib import image\n%matplotlib inline\n\nfrom tensorflow.keras.preprocessing.image import img_to_array,load_img,ImageDataGenerator\n\n# Carregar os dados\ntrain_dir = '/kaggle/input/dog-breed-identification/train/'\ntest_dir = '/kaggle/input/dog-breed-identification/test/'\nlabels_csv = '/kaggle/input/dog-breed-identification/labels.csv'","metadata":{"execution":{"iopub.status.busy":"2023-06-27T00:46:18.756625Z","iopub.execute_input":"2023-06-27T00:46:18.757072Z","iopub.status.idle":"2023-06-27T00:46:18.766063Z","shell.execute_reply.started":"2023-06-27T00:46:18.757044Z","shell.execute_reply":"2023-06-27T00:46:18.765156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = pd.read_csv(labels_csv)\n\ndf=df1.assign(img_path=lambda x: train_dir + x['id'] +'.jpg')\nprint(df.shape)\n\nX=np.array([img_to_array(load_img(img, target_size=(150, 150))) for img in df['img_path'].values.tolist()])\nX.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-27T00:46:18.769021Z","iopub.execute_input":"2023-06-27T00:46:18.770665Z","iopub.status.idle":"2023-06-27T00:46:44.117174Z","shell.execute_reply.started":"2023-06-27T00:46:18.770606Z","shell.execute_reply":"2023-06-27T00:46:44.116428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y = pd.get_dummies(df['breed'])\nY.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-27T00:46:44.118241Z","iopub.execute_input":"2023-06-27T00:46:44.118671Z","iopub.status.idle":"2023-06-27T00:46:44.134391Z","shell.execute_reply.started":"2023-06-27T00:46:44.118646Z","shell.execute_reply":"2023-06-27T00:46:44.132746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_test,Y_train,Y_test = train_test_split(X,Y,test_size=0.2)\n\nprint(X_train.shape,Y_train.shape)\nprint(X_test.shape,Y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-06-27T00:46:44.136216Z","iopub.execute_input":"2023-06-27T00:46:44.136951Z","iopub.status.idle":"2023-06-27T00:46:44.548055Z","shell.execute_reply.started":"2023-06-27T00:46:44.136914Z","shell.execute_reply":"2023-06-27T00:46:44.54666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense,Activation,Conv2D,Flatten,MaxPool2D,Dropout","metadata":{"execution":{"iopub.status.busy":"2023-06-27T00:46:44.551172Z","iopub.execute_input":"2023-06-27T00:46:44.551549Z","iopub.status.idle":"2023-06-27T00:46:44.55638Z","shell.execute_reply.started":"2023-06-27T00:46:44.55152Z","shell.execute_reply":"2023-06-27T00:46:44.555237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Definir a arquitetura do modelo\nmodel = Sequential()\nmodel.add(Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Conv2D(128, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Conv2D(128, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Flatten())\nmodel.add(Dropout(0.5))\nmodel.add(Dense(512, activation='relu'))\nmodel.add(Dense(num_classes, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-06-27T00:46:44.558634Z","iopub.execute_input":"2023-06-27T00:46:44.559164Z","iopub.status.idle":"2023-06-27T00:46:44.676008Z","shell.execute_reply.started":"2023-06-27T00:46:44.559126Z","shell.execute_reply":"2023-06-27T00:46:44.674906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n             loss='categorical_crossentropy',\n             metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-06-27T00:46:44.67712Z","iopub.execute_input":"2023-06-27T00:46:44.677436Z","iopub.status.idle":"2023-06-27T00:46:44.690948Z","shell.execute_reply.started":"2023-06-27T00:46:44.677409Z","shell.execute_reply":"2023-06-27T00:46:44.689906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train,Y_train,\n         validation_data=(X_test,Y_test),\n         batch_size=32,\n         epochs=100,\n         verbose=2)","metadata":{"execution":{"iopub.status.busy":"2023-06-27T00:46:44.692353Z","iopub.execute_input":"2023-06-27T00:46:44.693116Z","iopub.status.idle":"2023-06-27T01:01:09.110316Z","shell.execute_reply.started":"2023-06-27T00:46:44.693085Z","shell.execute_reply":"2023-06-27T01:01:09.108508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"teste:::","metadata":{}},{"cell_type":"code","source":"from glob import glob\ntest_files = glob('../input/dog-breed-identification/test/*.jpg')\ntype_files = np.asarray(test_files)\n\ndata = pd.read_csv(\"/kaggle/input/dog-breed-identification/sample_submission.csv\")\nt = data['id']\ntest = pd.DataFrame(data=t,columns=['id'])\n\ntest['img_path'] = test_files\ns = int(224)\n\ntest_img = np.array([img_to_array(load_img(img,target_size=(150,150))) for img in test['img_path'].values.tolist()])\ntest_img.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-27T01:01:09.112411Z","iopub.execute_input":"2023-06-27T01:01:09.119122Z","iopub.status.idle":"2023-06-27T01:02:37.926796Z","shell.execute_reply.started":"2023-06-27T01:01:09.119072Z","shell.execute_reply":"2023-06-27T01:02:37.925787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#predict\npreds = model.predict(test_img)","metadata":{"execution":{"iopub.status.busy":"2023-06-27T01:03:19.991439Z","iopub.execute_input":"2023-06-27T01:03:19.992589Z","iopub.status.idle":"2023-06-27T01:04:17.453449Z","shell.execute_reply.started":"2023-06-27T01:03:19.992546Z","shell.execute_reply":"2023-06-27T01:04:17.45205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Criar o arquivo de submissão\nid = [test['img_path'][i][-15:-5] for i in range(len(test))]\nid = np.asarray(id)\nlabels = list(Y.keys())\npredictions = pd.DataFrame(data=preds,\n                 columns=labels)\npredictions['id'] = id\npredictions\n\n","metadata":{"execution":{"iopub.status.busy":"2023-06-27T01:04:31.229496Z","iopub.execute_input":"2023-06-27T01:04:31.230806Z","iopub.status.idle":"2023-06-27T01:04:37.717071Z","shell.execute_reply.started":"2023-06-27T01:04:31.230758Z","shell.execute_reply":"2023-06-27T01:04:37.716092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols = predictions.columns.tolist()\ncols = cols[-1:] + cols[:-1]\npredictions = predictions[cols]\npredictions.head(5)\nid","metadata":{"execution":{"iopub.status.busy":"2023-06-27T01:05:15.45361Z","iopub.execute_input":"2023-06-27T01:05:15.454049Z","iopub.status.idle":"2023-06-27T01:05:15.467846Z","shell.execute_reply.started":"2023-06-27T01:05:15.454018Z","shell.execute_reply":"2023-06-27T01:05:15.466252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.to_csv('../working/submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-27T01:05:17.991843Z","iopub.execute_input":"2023-06-27T01:05:17.992195Z","iopub.status.idle":"2023-06-27T01:05:18.871102Z","shell.execute_reply.started":"2023-06-27T01:05:17.99217Z","shell.execute_reply":"2023-06-27T01:05:18.87021Z"},"trusted":true},"execution_count":null,"outputs":[]}]}