{"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":"markdown","source":"## Import libraries\n","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.utils import image_dataset_from_directory\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom tensorflow.keras import models\nimport os\nimport shutil\nimport sys","metadata":{"execution":{"iopub.status.busy":"2023-07-01T00:59:33.889803Z","iopub.execute_input":"2023-07-01T00:59:33.890181Z","iopub.status.idle":"2023-07-01T00:59:39.083842Z","shell.execute_reply.started":"2023-07-01T00:59:33.890088Z","shell.execute_reply":"2023-07-01T00:59:39.083076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Dataset","metadata":{}},{"cell_type":"code","source":"dataset_dir = '../input/dog-breed-identification/train'\nlabels = pd.read_csv('../input/dog-breed-identification/labels.csv')","metadata":{"execution":{"iopub.status.busy":"2023-07-01T00:59:39.958628Z","iopub.execute_input":"2023-07-01T00:59:39.959443Z","iopub.status.idle":"2023-07-01T00:59:39.992538Z","shell.execute_reply.started":"2023-07-01T00:59:39.959390Z","shell.execute_reply":"2023-07-01T00:59:39.991752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Número de Classes","metadata":{}},{"cell_type":"code","source":"n_class = len(labels.breed.unique())\nn_class","metadata":{"execution":{"iopub.status.busy":"2023-07-01T00:59:44.609967Z","iopub.execute_input":"2023-07-01T00:59:44.610614Z","iopub.status.idle":"2023-07-01T00:59:44.628839Z","shell.execute_reply.started":"2023-07-01T00:59:44.610572Z","shell.execute_reply":"2023-07-01T00:59:44.627523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Pré processamento","metadata":{}},{"cell_type":"markdown","source":"Organizando as imagens em na pasta subset, cada imagem será adicionada na pasta de sua respectiva label.","metadata":{}},{"cell_type":"code","source":"import os\n\ndef make_dir(x):\n    if os.path.exists(x)==False:\n        os.makedirs(x)\n        \nbase_dir = './subset'\nmake_dir(base_dir)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T00:59:47.214345Z","iopub.execute_input":"2023-07-01T00:59:47.214620Z","iopub.status.idle":"2023-07-01T00:59:47.220650Z","shell.execute_reply.started":"2023-07-01T00:59:47.214589Z","shell.execute_reply":"2023-07-01T00:59:47.219941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = os.path.join(base_dir, 'train')\nmake_dir(train_dir)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T00:59:49.477486Z","iopub.execute_input":"2023-07-01T00:59:49.478200Z","iopub.status.idle":"2023-07-01T00:59:49.482286Z","shell.execute_reply.started":"2023-07-01T00:59:49.478161Z","shell.execute_reply":"2023-07-01T00:59:49.481310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"breeds = labels.breed.unique()\nfor breed in breeds:\n    # Make folder for each breed\n    _ = os.path.join(train_dir, breed)\n    make_dir(_)\n    \n    # Copy images to the corresponding folders\n    images = labels[labels.breed == breed]['id']\n    for image in images:\n        source = os.path.join(dataset_dir, f'{image}.jpg')\n        destination = os.path.join(train_dir, breed,f'{image}.jpg')                             \n        shutil.copyfile(source, destination)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T01:00:34.278043Z","iopub.execute_input":"2023-07-01T01:00:34.278359Z","iopub.status.idle":"2023-07-01T01:01:36.997807Z","shell.execute_reply.started":"2023-07-01T01:00:34.278326Z","shell.execute_reply":"2023-07-01T01:01:36.996979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"breeds","metadata":{"execution":{"iopub.status.busy":"2023-07-01T01:01:37.330481Z","iopub.execute_input":"2023-07-01T01:01:37.330760Z","iopub.status.idle":"2023-07-01T01:01:37.337688Z","shell.execute_reply.started":"2023-07-01T01:01:37.330727Z","shell.execute_reply":"2023-07-01T01:01:37.336996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 128","metadata":{"execution":{"iopub.status.busy":"2023-07-01T01:01:41.784006Z","iopub.execute_input":"2023-07-01T01:01:41.784741Z","iopub.status.idle":"2023-07-01T01:01:41.790759Z","shell.execute_reply.started":"2023-07-01T01:01:41.784700Z","shell.execute_reply":"2023-07-01T01:01:41.787627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(rescale=1./255, # rescale pixel values to [0,1] to reduce memory usage\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    validation_split=0.2) # set validation split\n\n\ntrain_generator = datagen.flow_from_directory(\n    directory=train_dir,\n    target_size=(224, 224),\n    batch_size=batch_size,\n    class_mode='sparse',\n    subset='training')\n\nvalidation_generator = datagen.flow_from_directory(\n    directory=train_dir,\n    target_size=(224, 224),\n    batch_size=batch_size,\n    class_mode='sparse',\n    subset='validation')","metadata":{"execution":{"iopub.status.busy":"2023-07-01T01:02:16.127966Z","iopub.execute_input":"2023-07-01T01:02:16.128567Z","iopub.status.idle":"2023-07-01T01:02:16.965464Z","shell.execute_reply.started":"2023-07-01T01:02:16.128526Z","shell.execute_reply":"2023-07-01T01:02:16.964675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Criando Modelo com o InceptionResNetV2","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import InceptionResNetV2\n\ninception_bottleneck = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(224, 224, 3))","metadata":{"execution":{"iopub.status.busy":"2023-07-01T01:02:43.997337Z","iopub.execute_input":"2023-07-01T01:02:43.997821Z","iopub.status.idle":"2023-07-01T01:02:55.491872Z","shell.execute_reply.started":"2023-07-01T01:02:43.997781Z","shell.execute_reply":"2023-07-01T01:02:55.491032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.vgg19 import VGG19\n\nvgg_model = VGG19(weights='imagenet', include_top=False, input_shape=(224, 224, 3))","metadata":{"execution":{"iopub.status.busy":"2023-07-01T01:02:24.712007Z","iopub.execute_input":"2023-07-01T01:02:24.712750Z","iopub.status.idle":"2023-07-01T01:02:30.521286Z","shell.execute_reply.started":"2023-07-01T01:02:24.712711Z","shell.execute_reply":"2023-07-01T01:02:30.520458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import InceptionV3\n\nInceptionV3_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n","metadata":{"execution":{"iopub.status.busy":"2023-07-01T01:02:55.493435Z","iopub.execute_input":"2023-07-01T01:02:55.493725Z","iopub.status.idle":"2023-07-01T01:03:00.352695Z","shell.execute_reply.started":"2023-07-01T01:02:55.493675Z","shell.execute_reply":"2023-07-01T01:03:00.351837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"InceptionV3_model.trainable = False\n\nmodel = models.Sequential()\nmodel.add(InceptionV3_model)\nmodel.add(GlobalAveragePooling2D())\nmodel.add(layers.Dropout(0.2))\nmodel.add(layers.Dense(n_class, activation='softmax'))\n\n\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n#model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-07-01T01:03:00.355269Z","iopub.execute_input":"2023-07-01T01:03:00.355820Z","iopub.status.idle":"2023-07-01T01:03:01.384993Z","shell.execute_reply.started":"2023-07-01T01:03:00.355778Z","shell.execute_reply":"2023-07-01T01:03:01.384276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Treinando e Validando o Modelo","metadata":{}},{"cell_type":"code","source":"#early_stop = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=10)\n\n\nhistory = model.fit(train_generator, \n          #batch_size=batch_size, \n          steps_per_epoch = train_generator.samples // batch_size,\n          validation_data = validation_generator, \n          validation_steps = validation_generator.samples // batch_size,\n          epochs=5,\n          verbose=1)\n          #callbacks=[early_stop])","metadata":{"execution":{"iopub.status.busy":"2023-07-01T01:07:42.410482Z","iopub.execute_input":"2023-07-01T01:07:42.410783Z","iopub.status.idle":"2023-07-01T01:18:12.092818Z","shell.execute_reply.started":"2023-07-01T01:07:42.410751Z","shell.execute_reply":"2023-07-01T01:18:12.090122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Testando o Modelo","metadata":{}},{"cell_type":"code","source":"!mkdir test_data\n!cp -r \"/kaggle/input/dog-breed-identification/test\" test_data","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen_test = ImageDataGenerator(rescale=1./255) # rescale pixel values to [0,1] to reduce memory usage\n\ntest_generator = datagen_test.flow_from_directory(\n    directory=\"test_data\",\n    target_size=(224, 224),\n    batch_size=batch_size,\n    class_mode=None,\n    shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-23T01:06:56.031520Z","iopub.execute_input":"2023-06-23T01:06:56.032213Z","iopub.status.idle":"2023-06-23T01:06:56.470092Z","shell.execute_reply.started":"2023-06-23T01:06:56.032164Z","shell.execute_reply":"2023-06-23T01:06:56.469285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_names = os.listdir('../input/dog-breed-identification/test/')\nimages_ids = [id.split('.')[0] for id in images_names]","metadata":{"execution":{"iopub.status.busy":"2023-06-23T01:06:56.472496Z","iopub.execute_input":"2023-06-23T01:06:56.472790Z","iopub.status.idle":"2023-06-23T01:06:56.487307Z","shell.execute_reply.started":"2023-06-23T01:06:56.472749Z","shell.execute_reply":"2023-06-23T01:06:56.486619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"probabilities = model.predict(test_generator)","metadata":{"execution":{"iopub.status.busy":"2023-06-23T02:11:04.059523Z","iopub.execute_input":"2023-06-23T02:11:04.060168Z","iopub.status.idle":"2023-06-23T02:11:49.482838Z","shell.execute_reply.started":"2023-06-23T02:11:04.060118Z","shell.execute_reply":"2023-06-23T02:11:49.481997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rows = []\nfor index, pred in enumerate(probabilities):\n    pred_format = [format(z, '.4f') for z in pred]\n    pred_format.insert(0, images_ids[index])\n    rows.append(pred_format)","metadata":{"execution":{"iopub.status.busy":"2023-06-23T02:12:02.687692Z","iopub.execute_input":"2023-06-23T02:12:02.688730Z","iopub.status.idle":"2023-06-23T02:12:04.040949Z","shell.execute_reply.started":"2023-06-23T02:12:02.688672Z","shell.execute_reply":"2023-06-23T02:12:04.040132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fields = breeds.tolist()\nfields.insert(0, \"id\")","metadata":{"execution":{"iopub.status.busy":"2023-06-23T01:20:48.048458Z","iopub.execute_input":"2023-06-23T01:20:48.048763Z","iopub.status.idle":"2023-06-23T01:20:48.054110Z","shell.execute_reply.started":"2023-06-23T01:20:48.048729Z","shell.execute_reply":"2023-06-23T01:20:48.053009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import csv\nwith open('submission.csv', 'w') as f:\n      \n    # using csv.writer method from CSV package\n    write = csv.writer(f)\n      \n    write.writerow(fields)\n    write.writerows(rows)","metadata":{"execution":{"iopub.status.busy":"2023-06-23T02:12:49.852666Z","iopub.execute_input":"2023-06-23T02:12:49.852989Z","iopub.status.idle":"2023-06-23T02:12:50.178184Z","shell.execute_reply.started":"2023-06-23T02:12:49.852953Z","shell.execute_reply":"2023-06-23T02:12:50.177216Z"},"trusted":true},"execution_count":null,"outputs":[]}]}