{"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":"!pip install py7zr","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-28T01:04:50.030677Z","iopub.execute_input":"2022-09-28T01:04:50.031479Z","iopub.status.idle":"2022-09-28T01:05:04.125898Z","shell.execute_reply.started":"2022-09-28T01:04:50.031439Z","shell.execute_reply":"2022-09-28T01:05:04.124697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Extracting images\nimport os\nimport py7zr\nimport tensorflow as tf\nimport numpy as np\nimport pandas as pd\nimport cv2\nimport glob\n\ntemp_path = '/kaggle/temp/'\nif not os.path.exists(temp_path):\n    os.mkdir(temp_path)\ntrain_file_path = '/kaggle/input/cifar-10/train.7z'\narchive = py7zr.SevenZipFile(train_file_path, mode='r')\narchive.extractall(path=temp_path)\narchive.close()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T01:05:04.128533Z","iopub.execute_input":"2022-09-28T01:05:04.128953Z","iopub.status.idle":"2022-09-28T01:05:53.158385Z","shell.execute_reply.started":"2022-09-28T01:05:04.128911Z","shell.execute_reply":"2022-09-28T01:05:53.157401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = glob.glob('/kaggle/temp/train/*.png')\nlen(images)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T01:05:53.159874Z","iopub.execute_input":"2022-09-28T01:05:53.160503Z","iopub.status.idle":"2022-09-28T01:05:53.351680Z","shell.execute_reply.started":"2022-09-28T01:05:53.160464Z","shell.execute_reply":"2022-09-28T01:05:53.350660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Reading csv file for getting labels\nimg_labels = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv')\nimg_labels","metadata":{"execution":{"iopub.status.busy":"2022-09-28T01:05:53.353221Z","iopub.execute_input":"2022-09-28T01:05:53.353595Z","iopub.status.idle":"2022-09-28T01:05:53.784894Z","shell.execute_reply.started":"2022-09-28T01:05:53.353559Z","shell.execute_reply":"2022-09-28T01:05:53.783751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = img_labels['label'].unique()\nval_path = '/kaggle/temp/val/'\nif not os.path.exists(val_path):\n    os.mkdir(val_path)\nfor l in label:\n    if not os.path.exists('/kaggle/temp/train/'+l):\n        os.mkdir('/kaggle/temp/train/'+l)\n    \n    if not os.path.exists('/kaggle/temp/val/'+l):\n        os.mkdir('/kaggle/temp/val/'+l)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T01:05:53.787861Z","iopub.execute_input":"2022-09-28T01:05:53.788320Z","iopub.status.idle":"2022-09-28T01:05:53.804318Z","shell.execute_reply.started":"2022-09-28T01:05:53.788279Z","shell.execute_reply":"2022-09-28T01:05:53.803242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random as rd\ntrain_path = '/kaggle/temp/train/'\nval_path = '/kaggle/temp/val/'\nfor img in images:\n    prob = rd.random()\n    label = img_labels.iloc[int(img[19:-4])-1]['label']\n    if prob > 0.8:\n        os.rename(img,os.path.join(val_path,label,img[19:]))\n    else:\n        os.rename(img,os.path.join(train_path,label,img[19:]))","metadata":{"execution":{"iopub.status.busy":"2022-09-28T01:05:53.805530Z","iopub.execute_input":"2022-09-28T01:05:53.806278Z","iopub.status.idle":"2022-09-28T01:06:01.810022Z","shell.execute_reply.started":"2022-09-28T01:05:53.806240Z","shell.execute_reply":"2022-09-28T01:06:01.808900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = glob.glob('/kaggle/temp/train/*')\ntrain_images","metadata":{"execution":{"iopub.status.busy":"2022-09-28T01:06:01.811649Z","iopub.execute_input":"2022-09-28T01:06:01.812060Z","iopub.status.idle":"2022-09-28T01:06:01.824615Z","shell.execute_reply.started":"2022-09-28T01:06:01.812021Z","shell.execute_reply":"2022-09-28T01:06:01.823611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras.applications.xception import Xception, preprocess_input\n\ntrain_datagen = ImageDataGenerator(featurewise_center=False,\n                             samplewise_center=False,\n                             featurewise_std_normalization=False,\n                             samplewise_std_normalization=False,\n                             zca_whitening=False,\n                             rotation_range=10,\n                             zoom_range=0.1,\n                             width_shift_range=0.1,\n                             height_shift_range=0.1,\n                             horizontal_flip=False,\n                             vertical_flip=False,\n                             preprocessing_function=preprocess_input,\n                             rescale=None)\nvalid_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)\ntrain_generator = train_datagen.flow_from_directory(directory='/kaggle/temp/train/', shuffle=True, target_size=(71,71),batch_size=32)\nvalid_generator = valid_datagen.flow_from_directory(directory='/kaggle/temp/val/', shuffle=True, target_size=(71,71),batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T01:06:01.831347Z","iopub.execute_input":"2022-09-28T01:06:01.832092Z","iopub.status.idle":"2022-09-28T01:06:03.932686Z","shell.execute_reply.started":"2022-09-28T01:06:01.832049Z","shell.execute_reply":"2022-09-28T01:06:03.931615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from __future__ import print_function\nimport tensorflow.keras\nfrom tensorflow.keras.layers import Dense, Conv2D\nfrom tensorflow.keras.layers import BatchNormalization, Activation\nfrom tensorflow.keras.layers import AveragePooling2D, Input, Flatten\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import ModelCheckpoint\nfrom tensorflow.keras.callbacks import LearningRateScheduler\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.regularizers import l2\nfrom tensorflow.keras import backend as K\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.datasets import cifar10\nimport numpy as np\nimport os","metadata":{"execution":{"iopub.status.busy":"2022-09-28T01:06:03.934544Z","iopub.execute_input":"2022-09-28T01:06:03.935291Z","iopub.status.idle":"2022-09-28T01:06:03.954173Z","shell.execute_reply.started":"2022-09-28T01:06:03.935249Z","shell.execute_reply":"2022-09-28T01:06:03.953237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training parameters\nBATCH_SIZE = 32  # orig paper trained all networks with batch_size=128\nEPOCHS = 200 # 200\nUSE_AUGMENTATION = True\nNUM_CLASSES = 10\nCOLORS = 3\n\n# Subtracting pixel mean improves accuracy\nSUBTRACT_PIXEL_MEAN = True\n\n# Model version\n# Orig paper: version = 1 (ResNet v1), \n# Improved ResNet: version = 2 \n# (ResNet v2)\nVERSION = 1\n\n# Computed depth from supplied model parameter n\nif VERSION == 1:\n    DEPTH = COLORS * 6 + 2\nelif version == 2:\n    DEPTH = COLORS * 9 + 2","metadata":{"execution":{"iopub.status.busy":"2022-09-28T01:06:03.955969Z","iopub.execute_input":"2022-09-28T01:06:03.956415Z","iopub.status.idle":"2022-09-28T01:06:03.971717Z","shell.execute_reply.started":"2022-09-28T01:06:03.956379Z","shell.execute_reply":"2022-09-28T01:06:03.970721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def lr_schedule(epoch):\n    \"\"\"Learning Rate Schedule\n\n    Learning rate is scheduled to be reduced after 80, 120, 160, 180 epochs.\n    Called automatically every epoch as part of callbacks during training.\n\n    # Arguments\n        epoch (int): The number of epochs\n\n    # Returns\n        lr (float32): learning rate\n    \"\"\"\n    lr = 1e-3\n    if epoch > 180:\n        lr *= 0.5e-3\n    elif epoch > 160:\n        lr *= 1e-3\n    elif epoch > 120:\n        lr *= 1e-2\n    elif epoch > 80:\n        lr *= 1e-1\n    print('Learning rate: ', lr)\n    return lr","metadata":{"execution":{"iopub.status.busy":"2022-09-28T01:06:03.973661Z","iopub.execute_input":"2022-09-28T01:06:03.974436Z","iopub.status.idle":"2022-09-28T01:06:03.985134Z","shell.execute_reply.started":"2022-09-28T01:06:03.974398Z","shell.execute_reply":"2022-09-28T01:06:03.984067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resnet_layer(inputs,\n                 num_filters=16,\n                 kernel_size=3,\n                 strides=1,\n                 activation='relu',\n                 batch_normalization=True,\n                 conv_first=True):\n    \"\"\"2D Convolution-Batch Normalization-Activation stack builder\n\n    # Arguments\n        inputs (tensor): input tensor from input image or previous layer\n        num_filters (int): Conv2D number of filters\n        kernel_size (int): Conv2D square kernel dimensions\n        strides (int): Conv2D square stride dimensions\n        activation (string): activation name\n        batch_normalization (bool): whether to include batch normalization\n        conv_first (bool): conv-bn-activation (True) or\n            bn-activation-conv (False)\n\n    # Returns\n        x (tensor): tensor as input to the next layer\n    \"\"\"\n    conv = Conv2D(num_filters,\n                  kernel_size=kernel_size,\n                  strides=strides,\n                  padding='same',\n                  kernel_initializer='he_normal',\n                  kernel_regularizer=l2(1e-4))\n\n    x = inputs\n    if conv_first:\n        x = conv(x)\n        if batch_normalization:\n            x = BatchNormalization()(x)\n        if activation is not None:\n            x = Activation(activation)(x)\n    else:\n        if batch_normalization:\n            x = BatchNormalization()(x)\n        if activation is not None:\n            x = Activation(activation)(x)\n        x = conv(x)\n    return x","metadata":{"execution":{"iopub.status.busy":"2022-09-28T01:06:03.986846Z","iopub.execute_input":"2022-09-28T01:06:03.987488Z","iopub.status.idle":"2022-09-28T01:06:03.998131Z","shell.execute_reply.started":"2022-09-28T01:06:03.987452Z","shell.execute_reply":"2022-09-28T01:06:03.997183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resnet_v1(input_shape, depth, num_classes=10):\n    \"\"\"ResNet Version 1 Model builder [a]\n\n    Stacks of 2 x (3 x 3) Conv2D-BN-ReLU\n    Last ReLU is after the shortcut connection.\n    At the beginning of each stage, the feature \n    map size is halved (downsampled)\n    by a convolutional layer with strides=2, while the number of \n    filters is\n    doubled. Within each stage, the layers have the same number \n    filters and the same number of filters.\n    Features maps sizes:\n    stage 0: 32x32, 16\n    stage 1: 16x16, 32\n    stage 2:  8x8,  64\n    The Number of parameters is approx the same as Table 6 of [a]:\n    ResNet20 0.27M\n    ResNet32 0.46M\n    ResNet44 0.66M\n    ResNet56 0.85M\n    ResNet110 1.7M\n\n    # Arguments\n        input_shape (tensor): shape of input image tensor\n        depth (int): number of core convolutional layers\n        num_classes (int): number of classes (CIFAR10 has 10)\n\n    # Returns\n        model (Model): Keras model instance\n    \"\"\"\n    if (depth - 2) % 6 != 0:\n        raise ValueError('depth should be 6n+2 (eg 20, 32, 44 in [a])')\n    # Start model definition.\n    num_filters = 16\n    num_res_blocks = int((depth - 2) / 6)\n\n    inputs = Input(shape=input_shape)\n    x = resnet_layer(inputs=inputs)\n    # Instantiate the stack of residual units\n    for stack in range(3):\n        for res_block in range(num_res_blocks):\n            strides = 1\n            # first layer but not first stack\n            if stack > 0 and res_block == 0:  \n                strides = 2  # downsample\n            y = resnet_layer(inputs=x,\n                             num_filters=num_filters,\n                             strides=strides)\n            y = resnet_layer(inputs=y,\n                             num_filters=num_filters,\n                             activation=None)\n            # first layer but not first stack\n            if stack > 0 and res_block == 0:  \n                # linear projection residual shortcut connection to match\n                # changed dims\n                x = resnet_layer(inputs=x,\n                                 num_filters=num_filters,\n                                 kernel_size=1,\n                                 strides=strides,\n                                 activation=None,\n                                 batch_normalization=False)\n            x = tensorflow.keras.layers.add([x, y])\n            x = Activation('relu')(x)\n        num_filters *= 2\n\n    # Add classifier on top.\n    # v1 does not use BN after last shortcut connection-ReLU\n    x = AveragePooling2D(pool_size=8)(x)\n    y = Flatten()(x)\n    outputs = Dense(num_classes,\n                    activation='softmax',\n                    kernel_initializer='he_normal')(y)\n\n    # Instantiate model.\n    model = Model(inputs=inputs, outputs=outputs)\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-09-28T01:06:04.001348Z","iopub.execute_input":"2022-09-28T01:06:04.001812Z","iopub.status.idle":"2022-09-28T01:06:04.015979Z","shell.execute_reply.started":"2022-09-28T01:06:04.001786Z","shell.execute_reply":"2022-09-28T01:06:04.014538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resnet_v2(input_shape, depth, num_classes=10):\n    \"\"\"ResNet Version 2 Model builder [b]\n\n    Stacks of (1 x 1)-(3 x 3)-(1 x 1) BN-ReLU-Conv2D or also known as\n    bottleneck layer\n    First shortcut connection per layer is 1 x 1 Conv2D.\n    Second and onwards shortcut connection is identity.\n    At the beginning of each stage, the feature map size is \n    halved (downsampled) by a convolutional layer with \n    strides=2, while the number of filter maps is\n    doubled. Within each stage, the layers have the same\n    number filters and the same filter map sizes.\n    Features maps sizes:\n    conv1  : 32x32,  16\n    stage 0: 32x32,  64\n    stage 1: 16x16, 128\n    stage 2:  8x8,  256\n\n    # Arguments\n        input_shape (tensor): shape of input image tensor\n        depth (int): number of core convolutional layers\n        num_classes (int): number of classes (CIFAR10 has 10)\n\n    # Returns\n        model (Model): Keras model instance\n    \"\"\"\n    if (depth - 2) % 9 != 0:\n        raise ValueError('depth should be 9n+2 (eg 56 or 110 in [b])')\n    # Start model definition.\n    num_filters_in = 16\n    num_res_blocks = int((depth - 2) / 9)\n\n    inputs = Input(shape=input_shape)\n    # v2 performs Conv2D with BN-ReLU on input before splitting into 2 paths\n    x = resnet_layer(inputs=inputs,\n                     num_filters=num_filters_in,\n                     conv_first=True)\n\n    # Instantiate the stack of residual units\n    for stage in range(3):\n        for res_block in range(num_res_blocks):\n            activation = 'relu'\n            batch_normalization = True\n            strides = 1\n            if stage == 0:\n                num_filters_out = num_filters_in * 4\n                if res_block == 0:  # first layer and first stage\n                    activation = None\n                    batch_normalization = False\n            else:\n                num_filters_out = num_filters_in * 2\n                if res_block == 0:  # first layer but not first stage\n                    strides = 2    # downsample\n\n            # bottleneck residual unit\n            y = resnet_layer(inputs=x,\n                             num_filters=num_filters_in,\n                             kernel_size=1,\n                             strides=strides,\n                             activation=activation,\n                             batch_normalization=batch_normalization,\n                             conv_first=False)\n            y = resnet_layer(inputs=y,\n                             num_filters=num_filters_in,\n                             conv_first=False)\n            y = resnet_layer(inputs=y,\n                             num_filters=num_filters_out,\n                             kernel_size=1,\n                             conv_first=False)\n            if res_block == 0:\n                # linear projection residual shortcut connection to match\n                # changed dims\n                x = resnet_layer(inputs=x,\n                                 num_filters=num_filters_out,\n                                 kernel_size=1,\n                                 strides=strides,\n                                 activation=None,\n                                 batch_normalization=False)\n            x = tensorflow.keras.layers.add([x, y])\n\n        num_filters_in = num_filters_out\n\n    # Add classifier on top.\n    # v2 has BN-ReLU before Pooling\n    x = BatchNormalization()(x)\n    x = Activation('relu')(x)\n    x = AveragePooling2D(pool_size=8)(x)\n    y = Flatten()(x)\n    outputs = Dense(num_classes,\n                    activation='softmax',\n                    kernel_initializer='he_normal')(y)\n\n    # Instantiate model.\n    model = Model(inputs=inputs, outputs=outputs)\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-09-28T01:06:04.020835Z","iopub.execute_input":"2022-09-28T01:06:04.021401Z","iopub.status.idle":"2022-09-28T01:06:04.034229Z","shell.execute_reply.started":"2022-09-28T01:06:04.021366Z","shell.execute_reply":"2022-09-28T01:06:04.033122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_shape = (71,71,3)\n\n\n# Create the neural network\nif VERSION == 2:\n    model = resnet_v2(input_shape=input_shape, depth=DEPTH)\nelse:\n    model = resnet_v1(input_shape=input_shape, depth=DEPTH)\n\nmodel.compile(loss='categorical_crossentropy',\n              optimizer=Adam(lr=lr_schedule(0)),\n              metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T01:06:04.035577Z","iopub.execute_input":"2022-09-28T01:06:04.035956Z","iopub.status.idle":"2022-09-28T01:06:07.211556Z","shell.execute_reply.started":"2022-09-28T01:06:04.035920Z","shell.execute_reply":"2022-09-28T01:06:07.210524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\n\nstart_time = time.time()\n\n# Prepare callbacks for model saving and for learning rate adjustment.\nlr_scheduler = LearningRateScheduler(lr_schedule)\n\nlr_reducer = ReduceLROnPlateau(factor=np.sqrt(0.1),\n                               cooldown=0,\n                               patience=5,\n                               min_lr=0.5e-6)\n\ncallbacks = [lr_reducer, lr_scheduler]\n\nmodel.fit(train_generator,\n              batch_size=BATCH_SIZE,\n              epochs=EPOCHS,\n              validation_data=valid_generator,\n              shuffle=True,\n              callbacks=callbacks)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-28T01:06:07.213063Z","iopub.execute_input":"2022-09-28T01:06:07.214948Z","iopub.status.idle":"2022-09-28T06:03:54.165907Z","shell.execute_reply.started":"2022-09-28T01:06:07.214908Z","shell.execute_reply":"2022-09-28T06:03:54.164998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('/kaggle/temp/cifar-10/')\nmodel.save('/kaggle/temp/cifar-10/CIFAR_TL.h5')","metadata":{"execution":{"iopub.status.busy":"2022-09-28T06:03:54.167257Z","iopub.execute_input":"2022-09-28T06:03:54.168129Z","iopub.status.idle":"2022-09-28T06:03:54.389357Z","shell.execute_reply.started":"2022-09-28T06:03:54.168091Z","shell.execute_reply":"2022-09-28T06:03:54.388393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = model.evaluate(valid_generator, verbose=1)\nprint('Test loss:', scores[0])\nprint('Test accuracy:', scores[1])","metadata":{"execution":{"iopub.status.busy":"2022-09-28T06:03:54.390822Z","iopub.execute_input":"2022-09-28T06:03:54.391542Z","iopub.status.idle":"2022-09-28T06:03:59.556430Z","shell.execute_reply.started":"2022-09-28T06:03:54.391501Z","shell.execute_reply":"2022-09-28T06:03:59.555516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_path = '/kaggle/temp/test'\nif not os.path.exists(temp_path):\n    os.mkdir(temp_path)\ntrain_file_path = '/kaggle/input/cifar-10/test.7z'\narchive = py7zr.SevenZipFile(train_file_path, mode='r')\narchive.extractall(path=temp_path)\narchive.close()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T06:03:59.559628Z","iopub.execute_input":"2022-09-28T06:03:59.559928Z","iopub.status.idle":"2022-09-28T06:17:31.314713Z","shell.execute_reply.started":"2022-09-28T06:03:59.559901Z","shell.execute_reply":"2022-09-28T06:17:31.313523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Preprocessing the test data\ntest_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)\n\ntest_gen = test_datagen.flow_from_directory(directory='/kaggle/temp/test',target_size=(71,71),batch_size=32,class_mode=None,shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T06:17:31.316198Z","iopub.execute_input":"2022-09-28T06:17:31.317268Z","iopub.status.idle":"2022-09-28T06:17:37.271531Z","shell.execute_reply.started":"2022-09-28T06:17:31.317224Z","shell.execute_reply":"2022-09-28T06:17:37.270402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tic = time.time()\ntest_gen.reset()\npredictions_vecs = model.predict(test_gen)\n\npredictions_final = np.argmax(predictions_vecs, axis=1)\ntoc = time.time()\ntoc - tic","metadata":{"execution":{"iopub.status.busy":"2022-09-28T06:17:37.273088Z","iopub.execute_input":"2022-09-28T06:17:37.273914Z","iopub.status.idle":"2022-09-28T06:20:02.838048Z","shell.execute_reply.started":"2022-09-28T06:17:37.273872Z","shell.execute_reply":"2022-09-28T06:20:02.836675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_final","metadata":{"execution":{"iopub.status.busy":"2022-09-28T06:20:02.839631Z","iopub.execute_input":"2022-09-28T06:20:02.840014Z","iopub.status.idle":"2022-09-28T06:20:02.846688Z","shell.execute_reply.started":"2022-09-28T06:20:02.839979Z","shell.execute_reply":"2022-09-28T06:20:02.845702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(type(train_generator.class_indices))\nprint(train_generator.class_indices)\n\nclasses = {value:key for (key,value) in train_generator.class_indices.items()}\nprint(classes)\n\npredicted_classes=np.empty(shape=300000,dtype=np.dtype('U20'))\n\nind=0\nfor i in predictions_final.tolist():\n    predicted_classes[ind]=classes[i]\n    ind=ind+1\n    \nfilenames_wo_ext = []\nfor fname in test_gen.filenames:\n    filenames_wo_ext.append(int(fname.split(sep=\"/\")[1].split(sep=\".\")[0])-1)\n\npredicted_classes_final = np.empty(shape=300000,dtype=np.dtype('U20'))\npredicted_classes_final[filenames_wo_ext]=predicted_classes","metadata":{"execution":{"iopub.status.busy":"2022-09-28T06:20:02.848095Z","iopub.execute_input":"2022-09-28T06:20:02.848694Z","iopub.status.idle":"2022-09-28T06:20:03.212992Z","shell.execute_reply.started":"2022-09-28T06:20:02.848636Z","shell.execute_reply":"2022-09-28T06:20:03.211978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('../input/cifar-10/sampleSubmission.csv',header='infer')\nsub.info()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T06:20:03.214587Z","iopub.execute_input":"2022-09-28T06:20:03.214992Z","iopub.status.idle":"2022-09-28T06:20:03.346508Z","shell.execute_reply.started":"2022-09-28T06:20:03.214952Z","shell.execute_reply":"2022-09-28T06:20:03.345469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['label'] = predicted_classes_final\nsub.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T06:20:03.347785Z","iopub.execute_input":"2022-09-28T06:20:03.348408Z","iopub.status.idle":"2022-09-28T06:20:03.660771Z","shell.execute_reply.started":"2022-09-28T06:20:03.348368Z","shell.execute_reply":"2022-09-28T06:20:03.659807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}