/
INET4710_FinalProject_Helper_Functions.ipynb
203 lines (203 loc) · 8.04 KB
/
INET4710_FinalProject_Helper_Functions.ipynb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Appendix: Helper Functions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Download NAIP Images"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import boto3\n",
"import os\n",
"\n",
"def download_naip_image(state, year, lat, lon, filename, save_directory):\n",
" '''\n",
" Given a year indicator, latitude, longitude, and NAIP filename, this function makes a call to the AWS S3 public\n",
" bucket that hosts the NAIP imagery and download the selected image to a specified save directory on the\n",
" user's computer.\n",
" '''\n",
" # AWS doesn't include the version date in the filename, so splice that off before appending to path_to_download\n",
" filename_shortened = filename[0:26] + \".tif\"\n",
"\n",
" # Define the folder to search on AWS\n",
" path_to_download = state + '/' + str(year) + '/100cm/rgbir/' + str(lat) + '0' + str(lon) + '/' + filename_shortened\n",
" \n",
" # Initialize boto3 S3 client\n",
" #s3_client = boto3.client('s3')\n",
" s3_client = boto3.client(\n",
" 's3',\n",
" # Hard coded strings as credentials, not recommended.\n",
" aws_access_key_id='<fill in here>',\n",
" aws_secret_access_key='<fill in here>'\n",
" )\n",
"\n",
" # Define the path where we want to save the file\n",
" # Note: We'll make sure images are stored in separate directories by year\n",
" save_path = save_directory + '/' + str(year) + '/' + filename\n",
"\n",
" # Check if this file already exists at the save path; if not, download it\n",
" if not os.path.exists(save_path):\n",
" save_path = save_directory + '/' + str(year) + '/' + filename\n",
" print(\"Downloading image: \" + path_to_download)\n",
" s3_client.download_file('naip-source', path_to_download, save_path, {'RequestPayer':'requester'})\n",
" else:\n",
" print(\"Looks like you've already downloaded this file.\")\n",
"\n",
" print(\"Finished downloading image: \" + filename_shortened)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Data for Duluth, MN\n",
"mn_files_to_download = [\n",
" ('mn', 2013, 46, 92, 'm_4609215_sw_15_1_20130618_20130930.tif'),\n",
" ('mn', 2013, 46, 92, 'm_4609215_se_15_1_20130618_20130930.tif'),\n",
" ('mn', 2013, 46, 92, 'm_4609216_sw_15_1_20130618_20130930.tif'),\n",
" ('mn', 2013, 46, 92, 'm_4609223_nw_15_1_20130618_20130930.tif'),\n",
" ('mn', 2013, 46, 92, 'm_4609223_ne_15_1_20130618_20130930.tif'),\n",
" ('mn', 2013, 46, 92, 'm_4609224_nw_15_1_20130618_20130930.tif'),\n",
" ('mn', 2015, 46, 92, 'm_4609215_sw_15_1_20150922_20151221.tif'),\n",
" ('mn', 2015, 46, 92, 'm_4609215_se_15_1_20150922_20151221.tif'),\n",
" ('mn', 2015, 46, 92, 'm_4609216_sw_15_1_20150922_20151221.tif'),\n",
" ('mn', 2015, 46, 92, 'm_4609223_nw_15_1_20150922_20151221.tif'),\n",
" ('mn', 2015, 46, 92, 'm_4609223_ne_15_1_20150922_20151221.tif'),\n",
" ('mn', 2015, 46, 92, 'm_4609224_nw_15_1_20150922_20151221.tif')\n",
"]\n",
"\n",
"# Data for Flint, Michigan\n",
"mi_to_download = [\n",
" ('mi', 2014, 43, 83, 'm_4308358_ne_17_1_20140803_20141021.tif'), \n",
" ('mi', 2014, 43, 83, 'm_4308359_nw_17_1_20140722_20141021.tif'),\n",
" ('mi', 2014, 43, 83, 'm_4308359_ne_17_1_20140722_20141021.tif'),\n",
" ('mi', 2014, 43, 83, 'm_4308358_se_17_1_20140803_20141021.tif'),\n",
" ('mi', 2014, 43, 83, 'm_4308359_sw_17_1_20140722_20141021.tif'),\n",
" ('mi', 2014, 43, 83, 'm_4308359_se_17_1_20140722_20141021.tif'),\n",
" ('mi', 2014, 42, 83, 'm_4208302_ne_17_1_20140803_20141021.tif'),\n",
" ('mi', 2014, 42, 83, 'm_4208303_nw_17_1_20140722_20141021.tif'),\n",
" ('mi', 2014, 42, 83, 'm_4208303_ne_17_1_20140722_20141021.tif'),\n",
" ('mi', 2012, 43, 83, 'm_4308358_ne_17_1_20120627_20120911.tif'),\n",
" ('mi', 2012, 43, 83, 'm_4308359_nw_17_1_20120629_20120911.tif'),\n",
" ('mi', 2012, 43, 83, 'm_4308359_ne_17_1_20120629_20120911.tif'),\n",
" ('mi', 2012, 43, 83, 'm_4308358_se_17_1_20120627_20120911.tif'),\n",
" ('mi', 2012, 43, 83, 'm_4308359_sw_17_1_20120629_20120911.tif'),\n",
" ('mi', 2012, 43, 83, 'm_4308359_se_17_1_20120629_20120911.tif'),\n",
" ('mi', 2012, 42, 83, 'm_4208302_ne_17_1_20120627_20120911.tif'),\n",
" ('mi', 2012, 42, 83, 'm_4208303_nw_17_1_20120629_20120911.tif'),\n",
" ('mi', 2012, 42, 83, 'm_4208303_ne_17_1_20120629_20120911.tif')\n",
"]\n",
"\n",
"for file in mn_files_to_download:\n",
" download_naip_image(file[0], file[1], file[2], file[3], file[4], \"./images/naip\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Misclassification Table"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"from sklearn.metrics import confusion_matrix\n",
"\n",
"test_train_dict = {\n",
" 0: 'water',\n",
" 1: 'grass/agriculture',\n",
" 2: 'turf/fields',\n",
" 3: 'trees',\n",
" 4: 'dirt/soil/sand',\n",
" 5: 'asphalt/buildings',\n",
" 6: 'dirt/soil/sand'\n",
"}\n",
"\n",
"gt_dict = {\n",
" 1: 'grass/agriculture',\n",
" 2: 'dirt/soil/sand',\n",
" 3: 'asphalt/buildings',\n",
" 4: 'asphalt/buildings',\n",
" 5: 'water',\n",
" 6: 'trees',\n",
" 7: 'trees',\n",
" 8: 'grass/agriculture',\n",
" 9: 'grass/agriculture',\n",
" 10: 'water',\n",
" 11: 'water',\n",
" 12: 'dirt/soil/sand'\n",
"}\n",
"\n",
"def misclassification_table(img_sets, test_train_dict, gt_dict):\n",
" for i in range(len(img_sets)):\n",
" classified_img = rasterio.open(img_sets[i][1])\n",
" gt_img = rasterio.open(img_sets[i][2])\n",
"\n",
" classification = classified_img.read(1).astype('int')\n",
" gt_class = gt_img.read(1).astype('int')\n",
" \n",
" # https://stackoverflow.com/questions/16992713/translate-every-element-in-numpy-array-according-to-key\n",
" def vec_translate(a, my_dict): \n",
" return np.vectorize(my_dict.__getitem__)(a)\n",
" \n",
" classification_text = vec_translate(classification, test_train_dict)\n",
" \n",
" gt_class_text = vec_translate(gt_class, gt_dict)\n",
" \n",
" #df_confusion = pd.crosstab(list(gt_class_text.reshape(-1, 1)), list(classification_text.reshape(-1, 1)), rownames=['Ground_Truth_Classes'], colnames=['Predicted_Classes'], margins=True)\n",
" \n",
" pred_accuracy = confusion_matrix(gt_class_text.reshape(-1, 1), classification_text.reshape(-1, 1),\n",
" labels=['water',\n",
" 'grass/agriculture',\n",
" 'turf/fields',\n",
" 'trees',\n",
" 'dirt/soil/sand', \n",
" 'asphalt/buildings'])\n",
" \n",
" return(pred_accuracy)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}