Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
muellerzr committed Feb 6, 2020
2 parents 4cff7b5 + 32bc39c commit 6d38ed2
Show file tree
Hide file tree
Showing 12 changed files with 5,480 additions and 389 deletions.
116 changes: 54 additions & 62 deletions Computer Vision/03_Cross_Validation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"colab": {}
},
"source": [
"#Run once per session\n",
"#Run once per session\n",
"import os\n",
"!pip install -q feather-format kornia pyarrow wandb nbdev fastprogress fastai2 fastcore --upgrade \n",
"!pip install torch==1.3.1\n",
Expand Down Expand Up @@ -113,7 +113,7 @@
"source": [
"path = untar_data(URLs.IMAGEWOOF)"
],
"execution_count": 2,
"execution_count": 0,
"outputs": [
{
"output_type": "display_data",
Expand Down Expand Up @@ -145,7 +145,7 @@
"source": [
"path.ls()"
],
"execution_count": 3,
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
Expand Down Expand Up @@ -263,7 +263,7 @@
"source": [
"len(train_imgs)"
],
"execution_count": 105,
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
Expand Down Expand Up @@ -303,7 +303,7 @@
"source": [
"train_imgs"
],
"execution_count": 106,
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
Expand Down Expand Up @@ -385,7 +385,7 @@
"source": [
"len(train_imgs)"
],
"execution_count": 111,
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
Expand Down Expand Up @@ -432,16 +432,16 @@
"metadata": {
"id": "Ex9VxJvsq48u",
"colab_type": "code",
"outputId": "422a45b5-d2fd-41e2-b0aa-19c38a7cb7cc",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 68
},
"outputId": "422a45b5-d2fd-41e2-b0aa-19c38a7cb7cc"
}
},
"source": [
"split_list"
],
"execution_count": 114,
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
Expand Down Expand Up @@ -497,7 +497,7 @@
"source": [
"show_at(dsrc.train, 3)"
],
"execution_count": 91,
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
Expand Down Expand Up @@ -530,16 +530,16 @@
"metadata": {
"id": "vdJKCwAprEAg",
"colab_type": "code",
"outputId": "1ec961e6-8920-49f2-c4b0-92ad572fe596",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "1ec961e6-8920-49f2-c4b0-92ad572fe596"
}
},
"source": [
"dsrc.n_subsets"
],
"execution_count": 94,
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
Expand Down Expand Up @@ -582,7 +582,7 @@
"source": [
"dls.show_batch()"
],
"execution_count": 96,
"execution_count": 0,
"outputs": [
{
"output_type": "display_data",
Expand All @@ -603,16 +603,16 @@
"metadata": {
"id": "pxeObsZ2rROm",
"colab_type": "code",
"outputId": "300f2c1e-a71b-4f1b-ac9b-d0718e61eaa8",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "300f2c1e-a71b-4f1b-ac9b-d0718e61eaa8"
}
},
"source": [
"dls.n_subsets"
],
"execution_count": 99,
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
Expand Down Expand Up @@ -665,7 +665,7 @@
"source": [
"learn.fit_one_cycle(1)"
],
"execution_count": 101,
"execution_count": 0,
"outputs": [
{
"output_type": "display_data",
Expand Down Expand Up @@ -736,7 +736,7 @@
"source": [
"learn.validate(ds_idx=2)"
],
"execution_count": 102,
"execution_count": 0,
"outputs": [
{
"output_type": "display_data",
Expand Down Expand Up @@ -784,68 +784,59 @@
"colab": {}
},
"source": [
"from sklearn.model_selection import KFold"
"from sklearn.model_selection import StratifiedKFold"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "uH4hN22WwUq3",
"id": "ESll0JkfNgjQ",
"colab_type": "text"
},
"source": [
"We need to declare how many folds we want, and how random we want to be"
"First let's grab all the labels from our dataset"
]
},
{
"cell_type": "code",
"metadata": {
"id": "DCj9pqaZwSG_",
"id": "4lmrS_dNNkcP",
"colab_type": "code",
"colab": {}
},
"source": [
"kf = KFold(n_splits=10)"
"train_labels = L()\n",
"for i in range(len(dsrc.train)):\n",
" train_labels.append(dsrc.train[i][1])\n",
"train_labels"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "lHUqVW5UNloR",
"colab_type": "text"
},
"source": [
"Now let's make our K-Fold"
]
},
{
"cell_type": "code",
"metadata": {
"id": "MQ4iTy1xyuBQ",
"id": "DCj9pqaZwSG_",
"colab_type": "code",
"outputId": "4fd1f331-a19e-43f3-fe5a-92e21d89424e",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 187
}
"colab": {}
},
"source": [
"for train, test in kf.split(train_imgs):\n",
" print(len(train), len(test))"
"kf = StratifiedKFold(n_splits=5, shuffle=True)"
],
"execution_count": 116,
"outputs": [
{
"output_type": "stream",
"text": [
"8122 903\n",
"8122 903\n",
"8122 903\n",
"8122 903\n",
"8122 903\n",
"8123 902\n",
"8123 902\n",
"8123 902\n",
"8123 902\n",
"8123 902\n"
],
"name": "stdout"
}
]
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -899,17 +890,18 @@
"metadata": {
"id": "9orQlhlEn708",
"colab_type": "code",
"outputId": "dab35888-4491-4dfc-e6f9-1dd82d47abab",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 647
},
"outputId": "dab35888-4491-4dfc-e6f9-1dd82d47abab"
}
},
"source": [
"val_pct = []\n",
"tst_preds = []\n",
"for _, test in kf.split(train_imgs):\n",
" splits = IndexSplitter(test)\n",
"skf = StratifiedKFold(n_splits=10, shuffle=True)\n",
"for _, val_idx in kf.split(np.array(train_imgs), train_labels):\n",
" splits = IndexSplitter(val_idx)\n",
" split = splits(train_imgs)\n",
" split_list = [split[0], split[1]]\n",
" split_list.append(L(range(len(train_imgs), len(train_imgs)+len(tst_imgs))))\n",
Expand All @@ -922,7 +914,7 @@
" a,b = learn.get_preds(ds_idx=2)\n",
" tst_preds.append(a)"
],
"execution_count": 118,
"execution_count": 0,
"outputs": [
{
"output_type": "display_data",
Expand Down Expand Up @@ -1573,7 +1565,7 @@
"source": [
"accuracy(tst_preds_copy[0], b)"
],
"execution_count": 120,
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
Expand Down Expand Up @@ -1604,7 +1596,7 @@
"for i in tst_preds_copy:\n",
" print(accuracy(i, b))"
],
"execution_count": 121,
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
Expand Down Expand Up @@ -1654,16 +1646,16 @@
"metadata": {
"id": "EV8jGoDHSz7b",
"colab_type": "code",
"outputId": "9b016d87-1b6d-4d39-a3ec-334fed30ff55",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 136
},
"outputId": "9b016d87-1b6d-4d39-a3ec-334fed30ff55"
}
},
"source": [
"hat"
],
"execution_count": 130,
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
Expand Down Expand Up @@ -1712,7 +1704,7 @@
"source": [
"accuracy(hat, b)"
],
"execution_count": 132,
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
Expand Down
Loading

0 comments on commit 6d38ed2

Please sign in to comment.