{"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":"from sklearn.metrics import confusion_matrix, accuracy_score\nfrom sklearn.metrics import precision_recall_fscore_support\nfrom sklearn.preprocessing import StandardScaler\nfrom scipy.cluster.vq import kmeans, vq, whiten\nfrom sklearn.svm import LinearSVC\nfrom keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D as Convolution2D, MaxPooling2D, Dropout, Flatten\nfrom keras.layers.core import Activation, Dense\nfrom keras.datasets import mnist\nfrom keras.preprocessing.image import ImageDataGenerator\nimport tensorflow as tf\nimport pandas as pd\nimport numpy as np\nimport pylab as pl\nimport cv2\nimport os\nimport matplotlib.pyplot as plt\nimport keras.utils.np_utils as np_utils\nfrom keras.layers.advanced_activations import LeakyReLU","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-13T18:25:13.295268Z","iopub.execute_input":"2021-06-13T18:25:13.295854Z","iopub.status.idle":"2021-06-13T18:25:20.091802Z","shell.execute_reply.started":"2021-06-13T18:25:13.295689Z","shell.execute_reply":"2021-06-13T18:25:20.0906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MULTICLASS_DATASET_PATH = '../input/vcomp2/multiclass.csv'\nIMGS_TRAIN = '../input/imet-2019-fgvc6/train/'\nIMGS_TEST = '../input/imet-2019-fgvc6/test/'","metadata":{"execution":{"iopub.status.busy":"2021-06-13T18:25:20.09349Z","iopub.execute_input":"2021-06-13T18:25:20.093917Z","iopub.status.idle":"2021-06-13T18:25:20.102799Z","shell.execute_reply.started":"2021-06-13T18:25:20.09387Z","shell.execute_reply":"2021-06-13T18:25:20.101507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Dataset Parse**","metadata":{}},{"cell_type":"code","source":"# Split the dataset into training a training sample and a testing sample\n\ndef parseDataset(file, test_split, val_split):\n  dataset = pd.read_csv(file)\n  shuffle_dataset = dataset.sample(frac=0.9, random_state=42)\n  splits = [int(test_split*len(dataset)), int((test_split+val_split)*len(dataset))]\n  trainSample, validationSample, testSample = np.split(shuffle_dataset, splits)\n  return trainSample, validationSample, testSample","metadata":{"execution":{"iopub.status.busy":"2021-06-13T18:25:20.105546Z","iopub.execute_input":"2021-06-13T18:25:20.106137Z","iopub.status.idle":"2021-06-13T18:25:20.116817Z","shell.execute_reply.started":"2021-06-13T18:25:20.106052Z","shell.execute_reply":"2021-06-13T18:25:20.115365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Remove image id and get array of classes only\n\ndef getClasses(sample):\n  classes = []\n  for attribute in sample['attribute_ids']:\n    classes.append(attribute)\n  return classes","metadata":{"execution":{"iopub.status.busy":"2021-06-13T18:25:20.119316Z","iopub.execute_input":"2021-06-13T18:25:20.120121Z","iopub.status.idle":"2021-06-13T18:25:20.129747Z","shell.execute_reply.started":"2021-06-13T18:25:20.120053Z","shell.execute_reply":"2021-06-13T18:25:20.128388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Remove image attribute and get array of ids only\n\ndef getIds(sample):\n  ids = []\n  for attribute in sample['id']:\n    ids.append(attribute)\n    \n  return ids","metadata":{"execution":{"iopub.status.busy":"2021-06-13T18:25:20.132149Z","iopub.execute_input":"2021-06-13T18:25:20.132671Z","iopub.status.idle":"2021-06-13T18:25:20.142368Z","shell.execute_reply.started":"2021-06-13T18:25:20.132622Z","shell.execute_reply":"2021-06-13T18:25:20.141044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get valid pictures\n\ndef validPictures(ids, oldAttr):\n    idPaths = []\n    attr = []\n    for index, imageId in enumerate(ids):\n        imagePath = os.path.join(IMGS_TRAIN, imageId + '.png')\n        if not os.path.exists(imagePath):\n            imagePath = os.path.join(IMGS_TEST, imageId + '.png')\n        if not os.path.exists(imagePath):\n            print('ERROR: image ' + imageId + ' not found')\n            continue\n        idPaths.append(cv2.resize(cv2.imread(imagePath), (200,200), interpolation=cv2.INTER_CUBIC))\n        attr.append(oldAttr[index]);\n    return idPaths, attr","metadata":{"execution":{"iopub.status.busy":"2021-06-13T18:25:20.146419Z","iopub.execute_input":"2021-06-13T18:25:20.146794Z","iopub.status.idle":"2021-06-13T18:25:20.158544Z","shell.execute_reply.started":"2021-06-13T18:25:20.146734Z","shell.execute_reply":"2021-06-13T18:25:20.157328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#DataGenerator\n\ndef dataGenerator(idPaths, attr):\n    datagen = ImageDataGenerator(\n          rescale=1./255,\n          rotation_range=20,\n          width_shift_range=0.2,\n          height_shift_range=0.2,\n          horizontal_flip=True,\n          fill_mode='nearest')\n    idPaths = np.array(idPaths)\n    attr = np.array(attr)\n    generator = datagen.flow(idPaths, attr)\n    return generator","metadata":{"execution":{"iopub.status.busy":"2021-06-13T18:25:20.160482Z","iopub.execute_input":"2021-06-13T18:25:20.161079Z","iopub.status.idle":"2021-06-13T18:25:20.173696Z","shell.execute_reply.started":"2021-06-13T18:25:20.161011Z","shell.execute_reply":"2021-06-13T18:25:20.172513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Multiclass Classification**","metadata":{}},{"cell_type":"markdown","source":"**CNN**","metadata":{}},{"cell_type":"code","source":"# Parse data\ntrainSample, validationSample, testSample = parseDataset(MULTICLASS_DATASET_PATH, 0.7, 0.15)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T18:25:20.178446Z","iopub.execute_input":"2021-06-13T18:25:20.178838Z","iopub.status.idle":"2021-06-13T18:25:20.229107Z","shell.execute_reply.started":"2021-06-13T18:25:20.178803Z","shell.execute_reply":"2021-06-13T18:25:20.227985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Generate training data\ntrainIds = getIds(trainSample)\ntrainAttributes = getClasses(trainSample)\n\n# Get only valid pictures\n\ntrainIdPaths, trainAttr = validPictures (trainIds, trainAttributes)\n\ndel trainSample\ndel trainIds\ndel trainAttributes","metadata":{"execution":{"iopub.status.busy":"2021-06-13T18:25:20.2317Z","iopub.execute_input":"2021-06-13T18:25:20.231995Z","iopub.status.idle":"2021-06-13T18:29:19.718516Z","shell.execute_reply.started":"2021-06-13T18:25:20.231965Z","shell.execute_reply":"2021-06-13T18:29:19.717147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Generate validation data\nvalidationIds = getIds(validationSample)\nvalidationAttributes = getClasses(validationSample)\n\n## Get only valid pictures\nvalidationIdPaths, validationAttr = validPictures (validationIds, validationAttributes)\n\ndel validationSample\ndel validationIds\ndel validationAttributes","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-06-13T18:29:19.720405Z","iopub.execute_input":"2021-06-13T18:29:19.720805Z","iopub.status.idle":"2021-06-13T18:30:08.988456Z","shell.execute_reply.started":"2021-06-13T18:29:19.720761Z","shell.execute_reply":"2021-06-13T18:30:08.987185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Generate test data\ntestIds = getIds(testSample)\ntestAttributes = getClasses(testSample)\n\n## Get only valid pictures\ntestIdPaths, testAttr = validPictures (testIds, testAttributes)\n\ndel testIds\ndel testAttributes","metadata":{"execution":{"iopub.status.busy":"2021-06-13T18:30:08.990034Z","iopub.execute_input":"2021-06-13T18:30:08.990454Z","iopub.status.idle":"2021-06-13T18:30:25.388622Z","shell.execute_reply.started":"2021-06-13T18:30:08.990412Z","shell.execute_reply":"2021-06-13T18:30:25.387391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = dataGenerator(trainIdPaths, trainAttr)\n\ndel trainIdPaths\ndel trainAttr","metadata":{"execution":{"iopub.status.busy":"2021-06-13T18:30:25.390387Z","iopub.execute_input":"2021-06-13T18:30:25.390851Z","iopub.status.idle":"2021-06-13T18:30:30.468379Z","shell.execute_reply.started":"2021-06-13T18:30:25.390804Z","shell.execute_reply":"2021-06-13T18:30:30.467089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_generator = dataGenerator(validationIdPaths, validationAttr)\n\ndel validationIdPaths\ndel validationAttr","metadata":{"execution":{"iopub.status.busy":"2021-06-13T18:30:30.470194Z","iopub.execute_input":"2021-06-13T18:30:30.470611Z","iopub.status.idle":"2021-06-13T18:30:31.123659Z","shell.execute_reply.started":"2021-06-13T18:30:30.470554Z","shell.execute_reply":"2021-06-13T18:30:31.122407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = dataGenerator(testIdPaths, testAttr)\n\ndel testIdPaths\ndel testAttr","metadata":{"execution":{"iopub.status.busy":"2021-06-13T18:30:31.125361Z","iopub.execute_input":"2021-06-13T18:30:31.125888Z","iopub.status.idle":"2021-06-13T18:30:31.284418Z","shell.execute_reply.started":"2021-06-13T18:30:31.125813Z","shell.execute_reply":"2021-06-13T18:30:31.28318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Define Model\n\nmodel = Sequential()\nmodel.add(Convolution2D(32, kernel_size = (3,3), activation='linear', padding='valid'))\nmodel.add(LeakyReLU(alpha=0.1))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n#model.add(Dropout(0.25))\nmodel.add(Convolution2D(64, kernel_size = (3,3)))\nmodel.add(LeakyReLU(alpha=0.1))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n#model.add(Dropout(0.25))\nmodel.add(Convolution2D(128, kernel_size = (3,3)))\nmodel.add(LeakyReLU(alpha=0.1))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n#model.add(Dropout(0.25))\nmodel.add(Flatten())\nmodel.add(Dense(128, activation='linear')) #max output value\nmodel.add(LeakyReLU(alpha=0.1))\n#model.add(Dropout(0.5))\nmodel.add(Activation('softmax'))\nmodel.compile(loss='sparse_categorical_crossentropy',\n optimizer='adadelta',\n metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-06-13T18:30:31.286155Z","iopub.execute_input":"2021-06-13T18:30:31.286697Z","iopub.status.idle":"2021-06-13T18:30:33.775724Z","shell.execute_reply.started":"2021-06-13T18:30:31.286628Z","shell.execute_reply":"2021-06-13T18:30:33.774632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Train\n \ntrainedModel = model.fit(\n    train_generator,\n    steps_per_epoch=None,\n    epochs=25,\n    validation_data=validation_generator,\n    shuffle=True,\n    verbose=2)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-06-13T18:30:33.777435Z","iopub.execute_input":"2021-06-13T18:30:33.777902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Test\n\nscore = model.evaluate(test_generator)\nprint(\"Test accuracy: %0.05f\" % score[1])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Learning curve\nprint(\"\\n\")\nprint(trainedModel.history.keys())\n\n# summarize history for accuracy\nplt.plot(trainedModel.history['accuracy'])\nplt.plot(trainedModel.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'validation'], loc='upper left')\nplt.show()\n\n# summarize history for loss\nplt.plot(trainedModel.history['loss'])\nplt.plot(trainedModel.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'validation'], loc='upper left')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#métricas\n\ntest_labels = np.argmax(np.array(getClasses(testSample)), axis=1)\npredictions = np.argmax(model.predict(getIds(testSample)), axis=1)\nprint(precision_recall_fscore_support(test_labels, predictions, average='micro'))","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}