{"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\n#for 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":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\ntrain_labels = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\nimage_train_path = '/kaggle/input/dog-breed-identification/train'","metadata":{"execution":{"iopub.status.busy":"2023-06-17T21:45:26.543420Z","iopub.execute_input":"2023-06-17T21:45:26.543797Z","iopub.status.idle":"2023-06-17T21:45:26.564009Z","shell.execute_reply.started":"2023-06-17T21:45:26.543769Z","shell.execute_reply":"2023-06-17T21:45:26.563096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels","metadata":{"execution":{"iopub.status.busy":"2023-06-17T21:45:28.675785Z","iopub.execute_input":"2023-06-17T21:45:28.676655Z","iopub.status.idle":"2023-06-17T21:45:28.692820Z","shell.execute_reply.started":"2023-06-17T21:45:28.676600Z","shell.execute_reply":"2023-06-17T21:45:28.691718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_size = 128\nimg_shape = (img_size, img_size, 3)\nbatch_size = 256","metadata":{"execution":{"iopub.status.busy":"2023-06-17T21:46:42.619034Z","iopub.execute_input":"2023-06-17T21:46:42.619392Z","iopub.status.idle":"2023-06-17T21:46:42.624329Z","shell.execute_reply.started":"2023-06-17T21:46:42.619364Z","shell.execute_reply":"2023-06-17T21:46:42.623119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport math\nimport numpy as np\nfrom tensorflow.keras.utils import load_img\nfrom tensorflow.keras.utils import img_to_array\nfrom tensorflow.keras.applications.vgg19 import preprocess_input\n\n\nclass DogBreedGenerator(tf.keras.utils.Sequence):\n\n    def __init__(self, x_set, y_set, batch_size: int, resolution=img_shape[:2]):\n        self.x, self.y = x_set, y_set\n        self.batch_size = batch_size\n        self.resolution = resolution\n\n    def __len__(self):\n        return math.ceil(len(self.x) / self.batch_size)\n\n    def __getitem__(self, index: int):\n        low = index * self.batch_size\n        high = min(low + self.batch_size, len(self.x))\n        batch_x = self.x[low:high]\n        batch_y = self.y[low:high]\n        final_batch_x = np.array([\n            preprocess_input(img_to_array(load_img(file_name, target_size=self.resolution)))\n            for file_name in batch_x\n        ])\n        return final_batch_x, np.array(batch_y)","metadata":{"execution":{"iopub.status.busy":"2023-06-17T21:47:12.654348Z","iopub.execute_input":"2023-06-17T21:47:12.654723Z","iopub.status.idle":"2023-06-17T21:47:12.665307Z","shell.execute_reply.started":"2023-06-17T21:47:12.654696Z","shell.execute_reply":"2023-06-17T21:47:12.664054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from os import path\n\nall_image_paths = [path.join(image_train_path, id + '.jpg') for id in train_labels.id]\nall_image_paths[:3]","metadata":{"execution":{"iopub.status.busy":"2023-06-17T21:47:31.367771Z","iopub.execute_input":"2023-06-17T21:47:31.368134Z","iopub.status.idle":"2023-06-17T21:47:31.401770Z","shell.execute_reply.started":"2023-06-17T21:47:31.368104Z","shell.execute_reply":"2023-06-17T21:47:31.400556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#One hot codding\nfrom sklearn.preprocessing import LabelEncoder\nfrom keras.utils import np_utils\n\nnum_classes = len(train_labels.breed.unique())\nlabel_encoder = LabelEncoder()\nbreed = label_encoder.fit_transform(train_labels.breed)\ntrain_y = np_utils.to_categorical(breed, num_classes=num_classes)","metadata":{"execution":{"iopub.status.busy":"2023-06-17T21:48:26.702155Z","iopub.execute_input":"2023-06-17T21:48:26.703085Z","iopub.status.idle":"2023-06-17T21:48:26.716037Z","shell.execute_reply.started":"2023-06-17T21:48:26.703044Z","shell.execute_reply":"2023-06-17T21:48:26.715084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_generator = DogBreedGenerator(all_image_paths, train_y, batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-06-17T21:48:43.399995Z","iopub.execute_input":"2023-06-17T21:48:43.400421Z","iopub.status.idle":"2023-06-17T21:48:43.405862Z","shell.execute_reply.started":"2023-06-17T21:48:43.400388Z","shell.execute_reply":"2023-06-17T21:48:43.404561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modelo","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications.vgg19 import VGG19\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Flatten, Dropout\n\nvgg_model = VGG19(weights='imagenet', include_top=False, input_shape=img_shape)\n\nx = vgg_model.output\nx = GlobalAveragePooling2D()(x)\nx = Dropout(0.2)(x)\nout = Dense(num_classes, activation='softmax')(x)\n\nmodel = Model(inputs=vgg_model.input, outputs=out)\nvgg_model.trainable = False\n\nmetrics = ['accuracy']\nloss_name = 'categorical_crossentropy'\nmodel.compile(optimizer='adam', loss=loss_name, metrics=metrics)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-06-17T21:49:03.342067Z","iopub.execute_input":"2023-06-17T21:49:03.342809Z","iopub.status.idle":"2023-06-17T21:49:03.851412Z","shell.execute_reply.started":"2023-06-17T21:49:03.342777Z","shell.execute_reply":"2023-06-17T21:49:03.850601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#treino\nhistory=model.fit(\n    data_generator, batch_size=batch_size, epochs=5,\n    verbose=1, shuffle=True\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-17T21:49:40.343252Z","iopub.execute_input":"2023-06-17T21:49:40.343613Z","iopub.status.idle":"2023-06-17T21:52:25.189493Z","shell.execute_reply.started":"2023-06-17T21:49:40.343586Z","shell.execute_reply":"2023-06-17T21:52:25.188495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Novo dataset\ndataset_path = '/kaggle/input/dog-breed-identification'\n!mkdir data\n!cp -r \"{dataset_path}/test\" data\n!mv data test\n!mv test/test test/data","metadata":{"execution":{"iopub.status.busy":"2023-06-17T21:54:41.637166Z","iopub.execute_input":"2023-06-17T21:54:41.637974Z","iopub.status.idle":"2023-06-17T21:56:11.052165Z","shell.execute_reply.started":"2023-06-17T21:54:41.637940Z","shell.execute_reply":"2023-06-17T21:56:11.050722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from os import walk\ntest_path = '/kaggle/input/dog-breed-identification/test/'\ntest_filenames = next(walk(test_path), (None, None, []))[2]","metadata":{"execution":{"iopub.status.busy":"2023-06-17T21:56:17.086264Z","iopub.execute_input":"2023-06-17T21:56:17.086662Z","iopub.status.idle":"2023-06-17T21:56:18.880242Z","shell.execute_reply.started":"2023-06-17T21:56:17.086631Z","shell.execute_reply":"2023-06-17T21:56:18.879310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ntest_data_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_data_gen.flow_from_directory(\n    directory='test',\n    target_size=(img_size, img_size),\n    batch_size=batch_size,\n    class_mode=None,\n    shuffle=False,\n    seed=42\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-17T21:56:21.627251Z","iopub.execute_input":"2023-06-17T21:56:21.627615Z","iopub.status.idle":"2023-06-17T21:56:21.968144Z","shell.execute_reply.started":"2023-06-17T21:56:21.627588Z","shell.execute_reply":"2023-06-17T21:56:21.967172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images_ids = [image_id.split('.')[0] for image_id in test_filenames]\nprobabilities = model.predict(test_generator)\npredictions = np.argmax(probabilities, axis=-1)\npredictions","metadata":{"execution":{"iopub.status.busy":"2023-06-17T21:56:26.949321Z","iopub.execute_input":"2023-06-17T21:56:26.950018Z","iopub.status.idle":"2023-06-17T21:56:53.064768Z","shell.execute_reply.started":"2023-06-17T21:56:26.949986Z","shell.execute_reply":"2023-06-17T21:56:53.063447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"[format(z, '.4f') for z in list(probabilities[0])]","metadata":{"execution":{"iopub.status.busy":"2023-06-17T21:57:00.848335Z","iopub.execute_input":"2023-06-17T21:57:00.848702Z","iopub.status.idle":"2023-06-17T21:57:00.857588Z","shell.execute_reply.started":"2023-06-17T21:57:00.848675Z","shell.execute_reply":"2023-06-17T21:57:00.856539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dicionário p/ instanciar dataframe\ndict_results = {'id': []}\nbreeds = train_labels.breed.unique()\n\nfor breed in breeds:\n    dict_results[breed] = []\n\ndict_results","metadata":{"execution":{"iopub.status.busy":"2023-06-17T21:57:07.071947Z","iopub.execute_input":"2023-06-17T21:57:07.072345Z","iopub.status.idle":"2023-06-17T21:57:07.086692Z","shell.execute_reply.started":"2023-06-17T21:57:07.072316Z","shell.execute_reply":"2023-06-17T21:57:07.085590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for (image_id, probabilities_instance) in zip(test_images_ids, probabilities):\n    dict_results['id'].append(image_id)\n    \n    for breed_index, breed in enumerate(breeds):\n        dict_results[breed].append(format(probabilities_instance[breed_index], '.4f'))","metadata":{"execution":{"iopub.status.busy":"2023-06-17T21:57:12.151799Z","iopub.execute_input":"2023-06-17T21:57:12.152171Z","iopub.status.idle":"2023-06-17T21:57:13.867577Z","shell.execute_reply.started":"2023-06-17T21:57:12.152142Z","shell.execute_reply":"2023-06-17T21:57:13.866379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_dataframe = pd.DataFrame(data=dict_results)\noutput_dataframe.to_csv('submission.csv', index=False, sep=',')","metadata":{"execution":{"iopub.status.busy":"2023-06-17T21:57:16.327779Z","iopub.execute_input":"2023-06-17T21:57:16.328228Z","iopub.status.idle":"2023-06-17T21:57:17.338717Z","shell.execute_reply.started":"2023-06-17T21:57:16.328166Z","shell.execute_reply":"2023-06-17T21:57:17.337573Z"},"trusted":true},"execution_count":null,"outputs":[]}]}