{"cells":[{"metadata":{},"cell_type":"markdown","source":"## Using all 8 cores of a v3 TPU using pytorch/XLA"},{"metadata":{},"cell_type":"markdown","source":"## setup for pytorch/xla on TPU\nthanks to this [kernel](https://www.kaggle.com/byrachonok/pytorch-xla-for-tpuv3)"},{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"import os\nimport collections\nfrom datetime import datetime, timedelta\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Imports"},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nimport re\nimport cv2\nimport time\nimport tensorflow\nimport collections\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm\nfrom glob import glob\nfrom PIL import Image\nimport requests, threading\nimport matplotlib.pyplot as plt\nfrom datetime import datetime, timedelta\n\n\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.listdir('../input')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"CLASSES = ['pink primrose',    'hard-leaved pocket orchid', 'canterbury bells', 'sweet pea',     'wild geranium',     'tiger lily',           'moon orchid',              'bird of paradise', 'monkshood',        'globe thistle',         # 00 - 09\n           'snapdragon',       \"colt's foot\",               'king protea',      'spear thistle', 'yellow iris',       'globe-flower',         'purple coneflower',        'peruvian lily',    'balloon flower',   'giant white arum lily', # 10 - 19\n           'fire lily',        'pincushion flower',         'fritillary',       'red ginger',    'grape hyacinth',    'corn poppy',           'prince of wales feathers', 'stemless gentian', 'artichoke',        'sweet william',         # 20 - 29\n           'carnation',        'garden phlox',              'love in the mist', 'cosmos',        'alpine sea holly',  'ruby-lipped cattleya', 'cape flower',              'great masterwort', 'siam tulip',       'lenten rose',           # 30 - 39\n           'barberton daisy',  'daffodil',                  'sword lily',       'poinsettia',    'bolero deep blue',  'wallflower',           'marigold',                 'buttercup',        'daisy',            'common dandelion',      # 40 - 49\n           'petunia',          'wild pansy',                'primula',          'sunflower',     'lilac hibiscus',    'bishop of llandaff',   'gaura',                    'geranium',         'orange dahlia',    'pink-yellow dahlia',    # 50 - 59\n           'cautleya spicata', 'japanese anemone',          'black-eyed susan', 'silverbush',    'californian poppy', 'osteospermum',         'spring crocus',            'iris',             'windflower',       'tree poppy',            # 60 - 69\n           'gazania',          'azalea',                    'water lily',       'rose',          'thorn apple',       'morning glory',        'passion flower',           'lotus',            'toad lily',        'anthurium',             # 70 - 79\n           'frangipani',       'clematis',                  'hibiscus',         'columbine',     'desert-rose',       'tree mallow',          'magnolia',                 'cyclamen ',        'watercress',       'canna lily',            # 80 - 89\n           'hippeastrum ',     'bee balm',                  'pink quill',       'foxglove',      'bougainvillea',     'camellia',             'mallow',                   'mexican petunia',  'bromelia',         'blanket flower',        # 90 - 99\n           'trumpet creeper',  'blackberry lily',           'common tulip',     'wild rose']                                                                                                                                               # 100 - 102","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## create classes dict"},{"metadata":{"trusted":true},"cell_type":"code","source":"import pickle\nclass_dict={}\ninverted_class_dict={}\nfor i,c in enumerate(CLASSES):\n    class_dict[c]=i\n    inverted_class_dict[str(i)]=c\n    \n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with open('class_dict.pkl',mode='wb') as fout:\n    pickle.dump(class_dict,fout)\nwith open('inverted_class_dict.pkl',mode='wb') as fout:\n    pickle.dump(inverted_class_dict,fout)    ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## create train dataframe"},{"metadata":{"trusted":true},"cell_type":"code","source":"img_ids=[]\nlabels=[]\nfor i,c in enumerate(CLASSES):\n    #\n    class_glob=glob(f'../input/104-flowers-garden-of-eden/jpeg-224x224/train/{c}/*')\n    print(len(class_glob))\n    for img_id in class_glob:\n#         print(img_id)\n        img_ids.append(img_id)\n        labels.append(i)\n#         break\n#     break\n\nprint(len(img_ids),len(labels))    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"d = {'img_id': img_ids, 'label': labels}\ntrain_df=pd.DataFrame(d)\ntrain_df.head()\ntrain_df.to_csv('train.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# valid data"},{"metadata":{"trusted":true},"cell_type":"code","source":"img_ids=[]\nlabels=[]\nfor i,c in enumerate(CLASSES):\n    #\n    class_glob=glob(f'../input/104-flowers-garden-of-eden/jpeg-224x224/val/{c}/*')\n    print(len(class_glob))\n    for img_id in class_glob:\n#         print(img_id)\n        img_ids.append(img_id)\n        labels.append(i)\n#         break\n#     break\n\nprint(len(img_ids),len(labels))    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"d = {'img_id': img_ids, 'label': labels}\ntrain_df=pd.DataFrame(d)\ntrain_df.head()\ntrain_df.to_csv('valid.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_jpeg = glob('../input/104-flowers-garden-of-eden/jpeg-224x224/test/*')\nlen(test_jpeg)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Dataset"}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":4}