{"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 PIL import Image\nfrom matplotlib import pyplot as plt\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2022-11-26T00:51:13.953628Z","iopub.execute_input":"2022-11-26T00:51:13.954167Z","iopub.status.idle":"2022-11-26T00:51:18.490171Z","shell.execute_reply.started":"2022-11-26T00:51:13.954059Z","shell.execute_reply":"2022-11-26T00:51:18.489198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bs=1024\ns=250\nc = 224\nisize=(c,c)","metadata":{"execution":{"iopub.status.busy":"2022-11-26T00:51:18.491970Z","iopub.execute_input":"2022-11-26T00:51:18.492495Z","iopub.status.idle":"2022-11-26T00:51:18.497497Z","shell.execute_reply.started":"2022-11-26T00:51:18.492466Z","shell.execute_reply":"2022-11-26T00:51:18.496489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testp='../input/5-flowers-image-classification/test'","metadata":{"execution":{"iopub.status.busy":"2022-11-26T00:51:18.498895Z","iopub.execute_input":"2022-11-26T00:51:18.499447Z","iopub.status.idle":"2022-11-26T00:51:18.508273Z","shell.execute_reply.started":"2022-11-26T00:51:18.499408Z","shell.execute_reply":"2022-11-26T00:51:18.507394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport tensorflow_datasets as tfds","metadata":{"execution":{"iopub.status.busy":"2022-11-26T00:51:18.510796Z","iopub.execute_input":"2022-11-26T00:51:18.511131Z","iopub.status.idle":"2022-11-26T00:51:19.614043Z","shell.execute_reply.started":"2022-11-26T00:51:18.511099Z","shell.execute_reply":"2022-11-26T00:51:19.613106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tfds.disable_progress_bar()\n\nSEEDS = 42\n\nnp.random.seed(SEEDS)\ntf.random.set_seed(SEEDS)","metadata":{"execution":{"iopub.status.busy":"2022-11-26T00:51:19.615418Z","iopub.execute_input":"2022-11-26T00:51:19.616313Z","iopub.status.idle":"2022-11-26T00:51:19.628341Z","shell.execute_reply.started":"2022-11-26T00:51:19.616283Z","shell.execute_reply":"2022-11-26T00:51:19.625259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds, validation_ds = tfds.load(\n    \"tf_flowers\",\n    split=[\"train[:90%]\", \"train[90%:]\"],\n    as_supervised=True,\n)","metadata":{"execution":{"iopub.status.busy":"2022-11-26T00:51:19.629914Z","iopub.execute_input":"2022-11-26T00:51:19.630592Z","iopub.status.idle":"2022-11-26T00:51:25.522081Z","shell.execute_reply.started":"2022-11-26T00:51:19.630547Z","shell.execute_reply":"2022-11-26T00:51:25.520179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\nfor i, (image, label) in enumerate(train_ds.take(16)):\n    ax = plt.subplot(4, 4, i + 1)\n    plt.imshow(image)\n    plt.title(int(label))\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2022-11-26T00:51:25.523644Z","iopub.execute_input":"2022-11-26T00:51:25.523992Z","iopub.status.idle":"2022-11-26T00:51:26.554988Z","shell.execute_reply.started":"2022-11-26T00:51:25.523955Z","shell.execute_reply":"2022-11-26T00:51:26.553169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub=pd.read_csv('../input/5-flowers-image-classification/Sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-11-26T00:51:26.556092Z","iopub.execute_input":"2022-11-26T00:51:26.556413Z","iopub.status.idle":"2022-11-26T00:51:26.572329Z","shell.execute_reply.started":"2022-11-26T00:51:26.556384Z","shell.execute_reply":"2022-11-26T00:51:26.571454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras_preprocessing.image import ImageDataGenerator as I\ntgen=I(rescale=1/255)","metadata":{"execution":{"iopub.status.busy":"2022-11-26T00:51:26.573599Z","iopub.execute_input":"2022-11-26T00:51:26.574038Z","iopub.status.idle":"2022-11-26T00:51:26.580291Z","shell.execute_reply.started":"2022-11-26T00:51:26.574000Z","shell.execute_reply":"2022-11-26T00:51:26.579563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"RESIZE_TO = s\nCROP_TO = c\nBATCH_SIZE = bs\nSTEPS_PER_EPOCH = 10\nAUTO = tf.data.AUTOTUNE  # optimise the pipeline performance\nNUM_CLASSES = 5  # number of classes\nSCHEDULE_LENGTH = (\n    500  # we will train on lower resolution images and will still attain good results\n)\nSCHEDULE_BOUNDARIES = [\n    200,\n    300,\n    400,\n]  # more the dataset size the schedule length increase","metadata":{"execution":{"iopub.status.busy":"2022-11-26T00:51:26.585459Z","iopub.execute_input":"2022-11-26T00:51:26.585755Z","iopub.status.idle":"2022-11-26T00:51:26.593195Z","shell.execute_reply.started":"2022-11-26T00:51:26.585731Z","shell.execute_reply":"2022-11-26T00:51:26.592204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"sub","metadata":{"execution":{"iopub.status.busy":"2022-11-26T00:51:26.594328Z","iopub.execute_input":"2022-11-26T00:51:26.594638Z","iopub.status.idle":"2022-11-26T00:51:26.620726Z","shell.execute_reply.started":"2022-11-26T00:51:26.594604Z","shell.execute_reply":"2022-11-26T00:51:26.619927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testdf=sub.copy()","metadata":{"execution":{"iopub.status.busy":"2022-11-26T00:51:26.622364Z","iopub.execute_input":"2022-11-26T00:51:26.622630Z","iopub.status.idle":"2022-11-26T00:51:26.630528Z","shell.execute_reply.started":"2022-11-26T00:51:26.622606Z","shell.execute_reply":"2022-11-26T00:51:26.629553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testdf.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-11-26T00:51:26.631873Z","iopub.execute_input":"2022-11-26T00:51:26.632292Z","iopub.status.idle":"2022-11-26T00:51:26.640781Z","shell.execute_reply.started":"2022-11-26T00:51:26.632255Z","shell.execute_reply":"2022-11-26T00:51:26.639573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testdf","metadata":{"execution":{"iopub.status.busy":"2022-11-26T00:51:26.642537Z","iopub.execute_input":"2022-11-26T00:51:26.642896Z","iopub.status.idle":"2022-11-26T00:51:26.654427Z","shell.execute_reply.started":"2022-11-26T00:51:26.642861Z","shell.execute_reply":"2022-11-26T00:51:26.653340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testdf['id']=testdf['id'].astype('str')","metadata":{"execution":{"iopub.status.busy":"2022-11-26T00:51:26.656047Z","iopub.execute_input":"2022-11-26T00:51:26.656388Z","iopub.status.idle":"2022-11-26T00:51:26.664141Z","shell.execute_reply.started":"2022-11-26T00:51:26.656354Z","shell.execute_reply":"2022-11-26T00:51:26.663093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testdf['file']='../input/5-flowers-image-classification/test/'+testdf['id']+'.jpg'","metadata":{"execution":{"iopub.status.busy":"2022-11-26T00:51:26.665903Z","iopub.execute_input":"2022-11-26T00:51:26.666365Z","iopub.status.idle":"2022-11-26T00:51:26.674097Z","shell.execute_reply.started":"2022-11-26T00:51:26.666206Z","shell.execute_reply":"2022-11-26T00:51:26.673151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testd1=tgen.flow_from_dataframe(testdf, x_col='file', y_col=None, target_size=isize, class_mode=None, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-26T00:51:26.675406Z","iopub.execute_input":"2022-11-26T00:51:26.676089Z","iopub.status.idle":"2022-11-26T00:51:28.712434Z","shell.execute_reply.started":"2022-11-26T00:51:26.676053Z","shell.execute_reply":"2022-11-26T00:51:28.711384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow_hub as hub\nmodel_url = \"https://tfhub.dev/google/bit/m-r50x1/1\"\nmodule = hub.KerasLayer(model_url)","metadata":{"execution":{"iopub.status.busy":"2022-11-26T00:51:28.713986Z","iopub.execute_input":"2022-11-26T00:51:28.714593Z","iopub.status.idle":"2022-11-26T00:51:45.994426Z","shell.execute_reply.started":"2022-11-26T00:51:28.714555Z","shell.execute_reply":"2022-11-26T00:51:45.993432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MyBiTModel(tf.keras.Model):\n    \n    def __init__(self, num_classes, module):    \n        super().__init__()\n        self.num_classes = num_classes    \n        self.head = tf.keras.layers.Dense(num_classes, kernel_initializer='zero')    \n        self.bit_model = module\n    def call(self, images):    #No need to cut head off since we are using feature extractor model    \n        bit_embedding = self.bit_model(images)    \n        return self.head(bit_embedding)\n\nmodel = MyBiTModel(num_classes=5, module=module)","metadata":{"execution":{"iopub.status.busy":"2022-11-26T00:51:45.995851Z","iopub.execute_input":"2022-11-26T00:51:45.996228Z","iopub.status.idle":"2022-11-26T00:51:46.010829Z","shell.execute_reply.started":"2022-11-26T00:51:45.996192Z","shell.execute_reply":"2022-11-26T00:51:46.009538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SCHEDULE_LENGTH = SCHEDULE_LENGTH * 512 / BATCH_SIZE\n\n\n@tf.function\ndef preprocess_train(image, label):\n    image = tf.image.random_flip_left_right(image)\n    image = tf.image.resize(image, (RESIZE_TO, RESIZE_TO))\n    image = tf.image.random_crop(image, (CROP_TO, CROP_TO, 3))\n    image = image / 255.0\n    return (image, label)\n\n\n@tf.function\ndef preprocess_test(image, label):\n    image = tf.image.resize(image, (RESIZE_TO, RESIZE_TO))\n    image = image / 255.0\n    return (image, label)\n\n\nDATASET_NUM_TRAIN_EXAMPLES = train_ds.cardinality().numpy()\n\nrepeat_count = int(\n    SCHEDULE_LENGTH * BATCH_SIZE / DATASET_NUM_TRAIN_EXAMPLES * STEPS_PER_EPOCH\n)\nrepeat_count += 50 + 1  # To ensure at least there are 50 epochs of training","metadata":{"execution":{"iopub.status.busy":"2022-11-26T00:51:46.012170Z","iopub.execute_input":"2022-11-26T00:51:46.013066Z","iopub.status.idle":"2022-11-26T00:51:46.022344Z","shell.execute_reply.started":"2022-11-26T00:51:46.013027Z","shell.execute_reply":"2022-11-26T00:51:46.021375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training pipeline\npipeline_train = (\n    train_ds.shuffle(10000)\n    .repeat(repeat_count)  # Repeat dataset_size / num_steps\n    .map(preprocess_train, num_parallel_calls=AUTO)\n    .batch(BATCH_SIZE)\n    .prefetch(AUTO)\n)\n\n# Validation pipeline\npipeline_validation = (\n    validation_ds.map(preprocess_test, num_parallel_calls=AUTO)\n    .batch(BATCH_SIZE)\n    .prefetch(AUTO)\n)","metadata":{"execution":{"iopub.status.busy":"2022-11-26T00:51:46.023645Z","iopub.execute_input":"2022-11-26T00:51:46.024078Z","iopub.status.idle":"2022-11-26T00:51:46.185283Z","shell.execute_reply.started":"2022-11-26T00:51:46.024040Z","shell.execute_reply":"2022-11-26T00:51:46.184414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learning_rate = 0.003 * BATCH_SIZE / 512\n\n# Decay learning rate by a factor of 10 at SCHEDULE_BOUNDARIES.\nlr_schedule = keras.optimizers.schedules.PiecewiseConstantDecay(\n    boundaries=SCHEDULE_BOUNDARIES,\n    values=[\n        learning_rate,\n        learning_rate * 0.1,\n        learning_rate * 0.01,\n        learning_rate * 0.001,\n    ],\n)\n# optimizer = keras.optimizers.SGD(learning_rate=lr_schedule, momentum=0.9)\n\nloss_fn = keras.losses.SparseCategoricalCrossentropy(from_logits=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-26T00:51:46.186477Z","iopub.execute_input":"2022-11-26T00:51:46.186810Z","iopub.status.idle":"2022-11-26T00:51:46.193672Z","shell.execute_reply.started":"2022-11-26T00:51:46.186777Z","shell.execute_reply":"2022-11-26T00:51:46.192728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=tf.keras.optimizers.Adam(), loss=loss_fn, metrics=[\"accuracy\"])\ntrain_callbacks = [\n    keras.callbacks.EarlyStopping(\n        monitor=\"val_accuracy\", patience=10, restore_best_weights=True\n    )\n]\nhistory = model.fit(\n    pipeline_train,\n    batch_size=BATCH_SIZE,\n    epochs=15,\n    steps_per_epoch=STEPS_PER_EPOCH,\n    validation_data=pipeline_validation,\n    callbacks=train_callbacks,\n)","metadata":{"execution":{"iopub.status.busy":"2022-11-26T00:51:46.195139Z","iopub.execute_input":"2022-11-26T00:51:46.195765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=tf.keras.optimizers.Adam(1e-4), loss=loss_fn, metrics=[\"accuracy\"])\ntrain_callbacks = [\n    keras.callbacks.EarlyStopping(\n        monitor=\"val_accuracy\", patience=10, restore_best_weights=True\n    )\n]\nhistory = model.fit(\n    pipeline_train,\n    batch_size=BATCH_SIZE,\n    epochs=10,\n    steps_per_epoch=STEPS_PER_EPOCH,\n    validation_data=pipeline_validation,\n    callbacks=train_callbacks,\n)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Result=model.predict(testd1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"RR = np.zeros_like(Result)\nfor i in range(400):\n    RR[i][1] = Result[i][0]\n    RR[i][0] = Result[i][1]\n    RR[i][4] = Result[i][2]\n    RR[i][3] = Result[i][3]\n    RR[i][2] = Result[i][4]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(Result)):\n    sub.iloc[i,1] = RR[i].argmax()\nsub.to_csv(\"Submission.csv\", index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# np.savetxt(\"Ensemble0.csv\", RR, delimiter=',')","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}