{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Working on the dataset using fastai v4\n\nI just wanted to apply the fastai lesson2, fundamentals on this dataset. It looks really beginner friendly. "},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"!pip install --use-feature=2020-resolver fastbook\nimport fastbook\nfastbook.setup_book()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from fastbook import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Config.config_path","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!dir ../input/humpback-whale-identification","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"input_images = \"../input/humpback-whale-identification\"\nim = Image.open(input_images + \"/train/0000e88ab.jpg\")\nim.to_thumb(256,256)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"TRAIN = input_images + \"/train\"\nTEST = input_images + \"/test\"\nLABELS = input_images + \"/train.csv\"\nSAMPLE_SUB = input_images + \"sample_submission.csv\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(LABELS).set_index('Image')\nunique_labels = np.unique(train_df.Id.values)\n\nlabels_dict = dict()\nlabels_list = []\nfor i in range(len(unique_labels)):\n    labels_dict[unique_labels[i]] = i\n    labels_list.append(unique_labels[i])\n\nprint(\"Number of classes: {}\".format(len(unique_labels)))\ntrain_names = train_df.index.values\ntrain_df.Id = train_df.Id.apply(lambda x: labels_dict[x])\ntrain_labels = np.asarray(train_df.Id.values)\ntest_names = [f for f in os.listdir(TEST)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels_count = train_df.Id.value_counts()\n_, _ , _ = plt.hist(labels_count, bins=100)\nlabels_count ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"Count for class new_whale: {}\".format(labels_count[0]))\n\nplt.hist(labels_count[1:],bins=100,range=[0,100])\nplt.hist(labels_count[1:],bins=100,range=[0,100])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dup = []\nfor idx,row in train_df.iterrows():\n    if labels_count[row['Id']] < 5:\n        dup.extend([idx]*math.ceil((5 - labels_count[row['Id']])/labels_count[row['Id']]))\ntrain_names = np.concatenate([train_names, dup])\ntrain_names = train_names[np.random.RandomState(seed=42).permutation(train_names.shape[0])]\nlen(train_names)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = Path(input_images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv(path/'train.csv')\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ndls = ImageDataLoaders.from_df(df, TRAIN, item_tfms=Resize(128),batch_tfms=aug_transforms(mult=2))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dls.show_batch()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fns = get_image_files(TEST)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"verify_images(fns)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn = cnn_learner(dls, resnet18, metrics=error_rate)\nlearn.fine_tune(4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\ninterp.plot_confusion_matrix()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"interp.plot_top_losses(16, nrows = 8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.export()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = Path()\npath.ls(file_exts='.pkl')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn_inf = load_learner(path/'export.pkl')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# need to make code for submission","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_images =get_image_files(TEST)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_images","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}