{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimg_size= 224","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-12T06:41:37.198145Z","iopub.execute_input":"2022-04-12T06:41:37.198591Z","iopub.status.idle":"2022-04-12T06:41:37.232217Z","shell.execute_reply.started":"2022-04-12T06:41:37.198464Z","shell.execute_reply":"2022-04-12T06:41:37.231269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nfrom datetime import datetime\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications.mobilenet_v2 import MobileNetV2\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, GlobalAveragePooling2D\nfrom tensorflow.keras.optimizers import SGD, Adam\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping, TensorBoard, ModelCheckpoint\nfrom tensorflow.keras.models import load_model","metadata":{"execution":{"iopub.status.busy":"2022-04-12T06:41:37.234755Z","iopub.execute_input":"2022-04-12T06:41:37.235176Z","iopub.status.idle":"2022-04-12T06:41:43.546517Z","shell.execute_reply.started":"2022-04-12T06:41:37.23513Z","shell.execute_reply":"2022-04-12T06:41:43.545095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import DenseNet121 #128\nfrom tensorflow.keras.applications.nasnet import NASNetMobile #224 size= 38.02 MB\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2022-04-12T06:41:43.54861Z","iopub.execute_input":"2022-04-12T06:41:43.548947Z","iopub.status.idle":"2022-04-12T06:41:43.902918Z","shell.execute_reply.started":"2022-04-12T06:41:43.548903Z","shell.execute_reply":"2022-04-12T06:41:43.90187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nimport tensorflow as tf\nfrom keras_preprocessing.image import ImageDataGenerator\nfrom mpl_toolkits.axes_grid1 import ImageGrid\nimport itertools \nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-04-12T06:41:43.905611Z","iopub.execute_input":"2022-04-12T06:41:43.905858Z","iopub.status.idle":"2022-04-12T06:41:43.99715Z","shell.execute_reply.started":"2022-04-12T06:41:43.905826Z","shell.execute_reply":"2022-04-12T06:41:43.995932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntrain_dir = '../input/dog-breed-identification/train'\ntest_dir ='../input/dog-breed-identification/test'","metadata":{"execution":{"iopub.status.busy":"2022-04-12T06:41:44.00294Z","iopub.execute_input":"2022-04-12T06:41:44.004041Z","iopub.status.idle":"2022-04-12T06:41:44.011221Z","shell.execute_reply.started":"2022-04-12T06:41:44.003993Z","shell.execute_reply":"2022-04-12T06:41:44.010082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ndef append_ext(fn):\n    return fn+\".jpg\"\ntraindf = pd.read_csv('../input/dog-breed-identification/labels.csv',dtype=str)\ntestdf = pd.read_csv('../input/dog-breed-identification/sample_submission.csv',dtype=str)\ntraindf[\"id\"] = traindf[\"id\"].apply(append_ext)\ntestdf[\"id\"] = testdf[\"id\"].apply(append_ext)\n","metadata":{"execution":{"iopub.status.busy":"2022-04-12T06:41:44.013104Z","iopub.execute_input":"2022-04-12T06:41:44.014347Z","iopub.status.idle":"2022-04-12T06:41:44.625534Z","shell.execute_reply.started":"2022-04-12T06:41:44.014297Z","shell.execute_reply":"2022-04-12T06:41:44.624466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dogs= traindf['breed'].unique()\nsDogs=sorted(dogs)\n# sDogs","metadata":{"execution":{"iopub.status.busy":"2022-04-12T06:41:44.627268Z","iopub.execute_input":"2022-04-12T06:41:44.627697Z","iopub.status.idle":"2022-04-12T06:41:44.63689Z","shell.execute_reply.started":"2022-04-12T06:41:44.627651Z","shell.execute_reply":"2022-04-12T06:41:44.63583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog_breeds_labels=['affenpinscher', 'afghan hound', 'african hunting dog', 'airedale', 'american staffordshire terrier', 'appenzeller',\n'australian terrier', 'basenji', 'basset', 'beagle', 'bedlington terrier', 'bernese mountain dog', 'black-and-tan coonhound', \n'blenheim spaniel', 'bloodhound', 'bluetick', 'border collie', 'border terrier', 'borzoi', 'boston bull', 'bouvier des flandres',\n'boxer', 'brabancon griffon', 'briard', 'brittany spaniel', 'bull mastiff', 'cairn', 'cardigan', 'chesapeake bay retriever',\n'chihuahua', 'chow', 'clumber', 'cocker spaniel', 'collie', 'curly-coated retriever', 'dandie dinmont', 'dhole', 'dingo', \n'dobermann', 'english foxhound', 'english setter', 'english springer', 'entlebucher', 'eskimo dog', 'flat-coated retriever', \n'french bulldog', 'german shepherd', 'german short-haired pointer', 'giant schnauzer', 'golden retriever', 'gordon setter',\n'great dane', 'great pyrenees', 'greater swiss mountain dog', 'groenendael', 'ibizan hound', 'irish setter', 'irish terrier',\n'irish water spaniel', 'irish wolfhound', 'italian greyhound', 'japanese spaniel', 'keeshond', 'kelpie', 'kerry blue terrier',\n'komondor', 'kuvasz', 'labrador retriever', 'lakeland terrier', 'leonberg', 'lhasa', 'malamute', 'malinois', 'maltese dog',\n'mexican hairless', 'miniature pinscher', 'miniature poodle', 'miniature schnauzer', 'newfoundland', 'norfolk terrier',\n'norwegian elkhound', 'norwich terrier', 'old english sheepdog', 'otterhound', 'papillon', 'pekinese', 'Pembroke Welsh Corgi',\n'pomeranian', 'pug', 'redbone', 'rhodesian ridgeback', 'rottweiler', 'saint bernard', 'saluki', 'samoyed', \n'schipperke', 'scotch terrier', 'scottish deerhound', 'sealyham terrier', 'shetland sheepdog', 'shih-tzu',\n'siberian husky', 'silky terrier', 'soft-coated wheaten terrier', 'staffordshire bullterrier', 'standard poodle',\n'standard schnauzer', 'sussex spaniel', 'tibetan mastiff', 'tibetan terrier', 'toy poodle', 'toy terrier', \n'vizsla', 'walker hound', 'weimaraner', 'welsh springer spaniel', 'west highland white terrier', 'whippet',\n'wire-haired fox terrier', 'yorkshire terrier']","metadata":{"execution":{"iopub.status.busy":"2022-04-12T06:41:44.6393Z","iopub.execute_input":"2022-04-12T06:41:44.640174Z","iopub.status.idle":"2022-04-12T06:41:44.654032Z","shell.execute_reply.started":"2022-04-12T06:41:44.640115Z","shell.execute_reply":"2022-04-12T06:41:44.65284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dogInterpreter = tf.lite.Interpreter(model_path = '../input/models-quantized/dogClassificationMobileNet224(78)_quantized.tflite')\n","metadata":{"execution":{"iopub.status.busy":"2022-04-12T06:41:44.658434Z","iopub.execute_input":"2022-04-12T06:41:44.659168Z","iopub.status.idle":"2022-04-12T06:41:44.875969Z","shell.execute_reply.started":"2022-04-12T06:41:44.659126Z","shell.execute_reply":"2022-04-12T06:41:44.874703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dogClassification(path):\n    try:\n        img = plt.imread(path)\n    except:\n        img = plt.imread(\"../input/dog-breed-identification/test/\" + path)\n#     plt.imshow(img)\n#     plt.show()\n    input_details = dogInterpreter.get_input_details()\n    output_details = dogInterpreter.get_output_details()\n    img_size= 224\n    ans= []\n    new_img = cv2.resize(img,(img_size, img_size)).astype(\"float32\")\n    new_img = np.reshape(new_img,[1, img_size, img_size, 3])/225.0\n\n    np.set_printoptions(formatter={'float_kind':'{:f}'.format})\n    dogInterpreter.allocate_tensors()\n    dogInterpreter.set_tensor(input_details[0]['index'], new_img)\n    dogInterpreter.invoke()\n    pred = dogInterpreter.get_tensor(output_details[0]['index'])\n    arr= pred[0]\n#     print(arr)\n#     print(len(arr))\n    return arr\n#     arr1=arr.argsort()[-3:][::-1]  #get the top three results\n#     ans=[]\n#     for i in arr1:\n#         ans.append(f'{dog_breeds_labels[i].title()}: {float(\"{0:.2f}\".format(arr[i]))*100}%') # show upto 2 decimal palces\n#     return {\"data\": ans}","metadata":{"execution":{"iopub.status.busy":"2022-04-12T06:41:44.880968Z","iopub.execute_input":"2022-04-12T06:41:44.881429Z","iopub.status.idle":"2022-04-12T06:41:44.893964Z","shell.execute_reply.started":"2022-04-12T06:41:44.881394Z","shell.execute_reply":"2022-04-12T06:41:44.89291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dogClassification(\"../input/dog-breed-identification/test/000621fb3cbb32d8935728e48679680e.jpg\")","metadata":{"execution":{"iopub.status.busy":"2022-04-12T06:41:44.897904Z","iopub.execute_input":"2022-04-12T06:41:44.898353Z","iopub.status.idle":"2022-04-12T06:41:44.907491Z","shell.execute_reply.started":"2022-04-12T06:41:44.898312Z","shell.execute_reply":"2022-04-12T06:41:44.906452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testdf.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T06:41:44.909714Z","iopub.execute_input":"2022-04-12T06:41:44.910473Z","iopub.status.idle":"2022-04-12T06:41:44.947227Z","shell.execute_reply.started":"2022-04-12T06:41:44.910423Z","shell.execute_reply":"2022-04-12T06:41:44.946215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(sDogs)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T06:41:44.949015Z","iopub.execute_input":"2022-04-12T06:41:44.949482Z","iopub.status.idle":"2022-04-12T06:41:44.957228Z","shell.execute_reply.started":"2022-04-12T06:41:44.949408Z","shell.execute_reply":"2022-04-12T06:41:44.955865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(columns = sDogs)\ni = 1\ncount = 1\nfor img_id in testdf[\"id\"]:\n    count+=1\n    pred = dogClassification(img_id)\n    df = df.append(pd.Series(pred, index = sDogs),  ignore_index=True)\n    if (count % 100) == 0:\n        df.to_csv('./submission.csv', index=False, float_format='%.2f')\n        print(\"saved\", i)\n        i+=1","metadata":{"execution":{"iopub.status.busy":"2022-04-12T06:41:44.959124Z","iopub.execute_input":"2022-04-12T06:41:44.959759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d = pd.read_csv(\"./submission.csv\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}