├── codes.txt
├── README.md
├── valid.txt
├── LICENSE
└── RoadSurfaceSegmentation.ipynb
/codes.txt:
--------------------------------------------------------------------------------
1 | background
2 | roadAsphalt
3 | roadPaved
4 | roadUnpaved
5 | roadMarking
6 | speedBump
7 | catsEye
8 | stormDrain
9 | manholeCover
10 | patchs
11 | waterPuddle
12 | pothole
13 | craks
14 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Road surface detection and differentiation considering surface damages
2 |
3 | [](https://youtu.be/uUchRHWOEF4 "Road Surface Semantic Segmentation")
4 |
5 | The semantic segmentation GT for road surfaces contains 701 frames from [RTK dataset](http://www.lapix.ufsc.br/pesquisas/projeto-veiculo-autonomo/datasets/?lang=en). Classes are defined as follows:
6 |
7 | - Background, everything being unrelated to the road surface;
8 | - Asphalt, roads with asphalt surface;
9 | - Paved, different pavements (eg.: Cobblestone);
10 | - Unpaved, for unpaved roads;
11 | - Markings, to the road markings;
12 | - Speed-Bump, for the speed-bumps on the road;
13 | - Cats-Eye, for the cats-eye found on the road, both on the side and in the center of the path;
14 | - Storm-Drain, usually at the side edges of the road;
15 | - Patch, for the various patches found on asphalt road;
16 | - Water-Puddle, we use this class also for muddy regions;
17 | - Pothole, for different types and sizes of potholes, no matter if they are on asphalt, paved or unpaved roads;
18 | - Cracks, used in different road damages, like ruptures.
19 |
20 | ## Citation:
21 | ```
22 | @misc{rateke:2020.3,
23 | title = {Road surface detection and differentiation considering surface damages},
24 | author = {Thiago Rateke and Aldo von Wangenheim},
25 | journal={Autonomous Robots},
26 | year={2021},
27 | month={Jan},
28 | day={11},
29 | issn={1573-7527},
30 | doi={10.1007/s10514-020-09964-3},
31 | url={https://doi.org/10.1007/s10514-020-09964-3}
32 | }
33 | ```
34 | - [Original Paper](https://rdcu.be/cdpxi)
35 | - [Step-by-step](https://towardsdatascience.com/road-surface-semantic-segmentation-4d65b045245?source=friends_link&sk=f1fbe72fecd65331eb326a513b1f464f)
36 |
--------------------------------------------------------------------------------
/valid.txt:
--------------------------------------------------------------------------------
1 | 000000001.png
2 | 000000004.png
3 | 000000010.png
4 | 000000012.png
5 | 000000019.png
6 | 000000025.png
7 | 000000029.png
8 | 000000038.png
9 | 000000045.png
10 | 000000050.png
11 | 000000052.png
12 | 000000060.png
13 | 000000072.png
14 | 000000079.png
15 | 000000085.png
16 | 000000087.png
17 | 000000094.png
18 | 000000100.png
19 | 000000103.png
20 | 000000106.png
21 | 000000116.png
22 | 000000120.png
23 | 000000123.png
24 | 000000130.png
25 | 000000134.png
26 | 000000137.png
27 | 000000150.png
28 | 000000152.png
29 | 000000159.png
30 | 000000178.png
31 | 000000180.png
32 | 000000186.png
33 | 000000192.png
34 | 000000194.png
35 | 000000198.png
36 | 000000204.png
37 | 000000207.png
38 | 000000214.png
39 | 000000221.png
40 | 000000224.png
41 | 000000225.png
42 | 000000228.png
43 | 000000231.png
44 | 000000233.png
45 | 000000237.png
46 | 000000240.png
47 | 000000243.png
48 | 000000244.png
49 | 000000248.png
50 | 000000250.png
51 | 000000265.png
52 | 000000266.png
53 | 000000269.png
54 | 000000272.png
55 | 000000276.png
56 | 000000282.png
57 | 000000284.png
58 | 000000289.png
59 | 000000290.png
60 | 000000298.png
61 | 000000299.png
62 | 000000323.png
63 | 000000327.png
64 | 000000339.png
65 | 000000344.png
66 | 000000346.png
67 | 000000351.png
68 | 000000356.png
69 | 000000365.png
70 | 000000367.png
71 | 000000374.png
72 | 000000381.png
73 | 000000395.png
74 | 000000398.png
75 | 000000399.png
76 | 000000402.png
77 | 000000407.png
78 | 000000409.png
79 | 000000412.png
80 | 000000414.png
81 | 000000417.png
82 | 000000423.png
83 | 000000425.png
84 | 000000429.png
85 | 000000430.png
86 | 000000439.png
87 | 000000447.png
88 | 000000449.png
89 | 000000451.png
90 | 000000460.png
91 | 000000463.png
92 | 000000465.png
93 | 000000471.png
94 | 000000472.png
95 | 000000479.png
96 | 000000481.png
97 | 000000486.png
98 | 000000500.png
99 | 000000502.png
100 | 000000506.png
101 | 000000507.png
102 | 000000515.png
103 | 000000517.png
104 | 000000522.png
105 | 000000527.png
106 | 000000529.png
107 | 000000543.png
108 | 000000549.png
109 | 000000556.png
110 | 000000563.png
111 | 000000577.png
112 | 000000598.png
113 | 000000602.png
114 | 000000605.png
115 | 000000612.png
116 | 000000620.png
117 | 000000626.png
118 | 000000632.png
119 | 000000635.png
120 | 000000642.png
121 | 000000646.png
122 | 000000653.png
123 | 000000655.png
124 | 000000656.png
125 | 000000660.png
126 | 000000662.png
127 | 000000663.png
128 | 000000666.png
129 | 000000668.png
130 | 000000669.png
131 | 000000675.png
132 | 000000678.png
133 | 000000681.png
134 | 000000684.png
135 | 000000685.png
136 | 000000687.png
137 | 000000690.png
138 | 000000694.png
139 | 000000698.png
140 | 000000699.png
141 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/RoadSurfaceSegmentation.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "Copy of RoadSurfaceSegmentation.ipynb",
7 | "provenance": [],
8 | "collapsed_sections": [
9 | "mk2r-gCRCVb_",
10 | "nBWj9_LSC2G5",
11 | "UbxyIcZkDqBJ",
12 | "K7W20An-GnNm",
13 | "VE4zF162G-4z",
14 | "tRZoEiB8HJhI",
15 | "3oW77qUxHhkp",
16 | "3ojFnvRBHx8v",
17 | "KgZkM8M1IAxD",
18 | "_siWJq89IOdJ",
19 | "Uh1ZPx64gR56",
20 | "aT8l81QTjBrE",
21 | "33FD1Sc2j-k0",
22 | "4TAIZ9s4msq5",
23 | "D8N5E7BxnK7n",
24 | "XI-bgt6voXi9",
25 | "wAZbjvb40iMJ",
26 | "g5RVRyXL0_Uh",
27 | "eZ7SZZBN1KrS",
28 | "S0FHwE1p1RmR",
29 | "IO3-KXDsp4VB",
30 | "Ke8FATsZW1n_"
31 | ]
32 | },
33 | "kernelspec": {
34 | "name": "python3",
35 | "display_name": "Python 3"
36 | },
37 | "accelerator": "GPU"
38 | },
39 | "cells": [
40 | {
41 | "cell_type": "markdown",
42 | "metadata": {
43 | "id": "GGg9kEr0mzrf",
44 | "colab_type": "text"
45 | },
46 | "source": [
47 | "# Road surface detection and differentiation considering surface damages"
48 | ]
49 | },
50 | {
51 | "cell_type": "markdown",
52 | "metadata": {
53 | "id": "mk2r-gCRCVb_",
54 | "colab_type": "text"
55 | },
56 | "source": [
57 | "## 1. Initial settings\n",
58 | "\n",
59 | "---\n",
60 | "\n"
61 | ]
62 | },
63 | {
64 | "cell_type": "code",
65 | "metadata": {
66 | "id": "woWwcX5eznYL",
67 | "colab_type": "code",
68 | "colab": {}
69 | },
70 | "source": [
71 | "%reload_ext autoreload\n",
72 | "%autoreload 2\n",
73 | "%matplotlib inline\n",
74 | "!/opt/bin/nvidia-smi\n",
75 | "!nvcc --version"
76 | ],
77 | "execution_count": null,
78 | "outputs": []
79 | },
80 | {
81 | "cell_type": "code",
82 | "metadata": {
83 | "id": "rUlUjSarCbeu",
84 | "colab_type": "code",
85 | "colab": {}
86 | },
87 | "source": [
88 | "from fastai.vision import *\n",
89 | "from fastai.vision.interpret import *\n",
90 | "from fastai.callbacks.hooks import *\n",
91 | "from pathlib import Path\n",
92 | "from fastai.utils.mem import *\n",
93 | "torch.backends.cudnn.benchmark=True"
94 | ],
95 | "execution_count": null,
96 | "outputs": []
97 | },
98 | {
99 | "cell_type": "code",
100 | "metadata": {
101 | "id": "fbNQPOrsCdRX",
102 | "colab_type": "code",
103 | "colab": {}
104 | },
105 | "source": [
106 | "import warnings\n",
107 | "warnings.filterwarnings(\"ignore\", category=UserWarning, module=\"torch.nn.functional\")"
108 | ],
109 | "execution_count": null,
110 | "outputs": []
111 | },
112 | {
113 | "cell_type": "code",
114 | "metadata": {
115 | "id": "D3GdYMqtCexi",
116 | "colab_type": "code",
117 | "colab": {}
118 | },
119 | "source": [
120 | "from google.colab import drive\n",
121 | "drive.mount('/content/gdrive')"
122 | ],
123 | "execution_count": null,
124 | "outputs": []
125 | },
126 | {
127 | "cell_type": "markdown",
128 | "metadata": {
129 | "id": "nBWj9_LSC2G5",
130 | "colab_type": "text"
131 | },
132 | "source": [
133 | "## 2. Preparing the data\n",
134 | "\n",
135 | "---\n",
136 | "\n"
137 | ]
138 | },
139 | {
140 | "cell_type": "code",
141 | "metadata": {
142 | "id": "6yMPQ7v7CtbG",
143 | "colab_type": "code",
144 | "colab": {}
145 | },
146 | "source": [
147 | "path = Path('gdrive/My Drive/Colab Notebooks/data/')\n",
148 | "path.ls()"
149 | ],
150 | "execution_count": null,
151 | "outputs": []
152 | },
153 | {
154 | "cell_type": "code",
155 | "metadata": {
156 | "id": "Z1BpN93iC8J7",
157 | "colab_type": "code",
158 | "colab": {}
159 | },
160 | "source": [
161 | "codes = np.loadtxt(path/'codes.txt', dtype=str); codes"
162 | ],
163 | "execution_count": null,
164 | "outputs": []
165 | },
166 | {
167 | "cell_type": "code",
168 | "metadata": {
169 | "id": "kfT8JAUYC-tp",
170 | "colab_type": "code",
171 | "colab": {}
172 | },
173 | "source": [
174 | "path_lbl = path/'labels'\n",
175 | "path_img = path/'images'"
176 | ],
177 | "execution_count": null,
178 | "outputs": []
179 | },
180 | {
181 | "cell_type": "code",
182 | "metadata": {
183 | "id": "N83tmLjBDAII",
184 | "colab_type": "code",
185 | "colab": {}
186 | },
187 | "source": [
188 | "fnames = get_image_files(path_img)\n",
189 | "fnames[:3]\n",
190 | "len(fnames)"
191 | ],
192 | "execution_count": null,
193 | "outputs": []
194 | },
195 | {
196 | "cell_type": "code",
197 | "metadata": {
198 | "id": "ZvTFNcwxDB6C",
199 | "colab_type": "code",
200 | "colab": {}
201 | },
202 | "source": [
203 | "lbl_names = get_image_files(path_lbl)\n",
204 | "lbl_names[:3]\n",
205 | "len(lbl_names)"
206 | ],
207 | "execution_count": null,
208 | "outputs": []
209 | },
210 | {
211 | "cell_type": "code",
212 | "metadata": {
213 | "id": "Dzk-MfPYDD7p",
214 | "colab_type": "code",
215 | "colab": {}
216 | },
217 | "source": [
218 | "img_f = fnames[139]\n",
219 | "img = open_image(img_f)\n",
220 | "img.show(figsize=(5,5))"
221 | ],
222 | "execution_count": null,
223 | "outputs": []
224 | },
225 | {
226 | "cell_type": "code",
227 | "metadata": {
228 | "id": "KxBbLefJDFsd",
229 | "colab_type": "code",
230 | "colab": {}
231 | },
232 | "source": [
233 | "get_y_fn = lambda x: path_lbl/f'{x.stem}{x.suffix}'"
234 | ],
235 | "execution_count": null,
236 | "outputs": []
237 | },
238 | {
239 | "cell_type": "code",
240 | "metadata": {
241 | "id": "P73xHjqaDHI7",
242 | "colab_type": "code",
243 | "colab": {}
244 | },
245 | "source": [
246 | "mask = open_mask(get_y_fn(img_f))\n",
247 | "mask.show(figsize=(5,5), alpha=1)"
248 | ],
249 | "execution_count": null,
250 | "outputs": []
251 | },
252 | {
253 | "cell_type": "code",
254 | "metadata": {
255 | "id": "a_2SmfMCDI7n",
256 | "colab_type": "code",
257 | "colab": {}
258 | },
259 | "source": [
260 | "src_size = np.array(mask.shape[1:])\n",
261 | "src_size,mask.data"
262 | ],
263 | "execution_count": null,
264 | "outputs": []
265 | },
266 | {
267 | "cell_type": "markdown",
268 | "metadata": {
269 | "id": "NmrXBAhNf5mC",
270 | "colab_type": "text"
271 | },
272 | "source": [
273 | "## 3. First Step - Without weights\n",
274 | "\n",
275 | "---\n",
276 | "\n"
277 | ]
278 | },
279 | {
280 | "cell_type": "markdown",
281 | "metadata": {
282 | "id": "UbxyIcZkDqBJ",
283 | "colab_type": "text"
284 | },
285 | "source": [
286 | "### 3.1. First step Datasets\n",
287 | "\n",
288 | "---\n",
289 | "\n"
290 | ]
291 | },
292 | {
293 | "cell_type": "code",
294 | "metadata": {
295 | "id": "KoQLPTpZDZQZ",
296 | "colab_type": "code",
297 | "colab": {}
298 | },
299 | "source": [
300 | "size = src_size\n",
301 | "\n",
302 | "free = gpu_mem_get_free_no_cache()\n",
303 | "# the max size of bs depends on the available GPU RAM\n",
304 | "if free > 8200: bs=8\n",
305 | "else: bs=4\n",
306 | "print(f\"using bs={bs}, have {free}MB of GPU RAM free\")"
307 | ],
308 | "execution_count": null,
309 | "outputs": []
310 | },
311 | {
312 | "cell_type": "code",
313 | "metadata": {
314 | "id": "ypW12JvVDxMk",
315 | "colab_type": "code",
316 | "colab": {}
317 | },
318 | "source": [
319 | "src = (SegmentationItemList.from_folder(path_img)\n",
320 | " .split_by_fname_file('../valid.txt')\n",
321 | " .label_from_func(get_y_fn, classes=codes))"
322 | ],
323 | "execution_count": null,
324 | "outputs": []
325 | },
326 | {
327 | "cell_type": "code",
328 | "metadata": {
329 | "id": "-qSJBnFCDy7t",
330 | "colab_type": "code",
331 | "colab": {}
332 | },
333 | "source": [
334 | "data = (src.transform(get_transforms(), size=size, tfm_y=True)\n",
335 | " .databunch(bs=bs)\n",
336 | " .normalize(imagenet_stats))"
337 | ],
338 | "execution_count": null,
339 | "outputs": []
340 | },
341 | {
342 | "cell_type": "code",
343 | "metadata": {
344 | "id": "-HeHMvl3D0vH",
345 | "colab_type": "code",
346 | "colab": {}
347 | },
348 | "source": [
349 | "data.show_batch(2, figsize=(10,10))"
350 | ],
351 | "execution_count": null,
352 | "outputs": []
353 | },
354 | {
355 | "cell_type": "code",
356 | "metadata": {
357 | "id": "eFnPc-sND2fW",
358 | "colab_type": "code",
359 | "colab": {}
360 | },
361 | "source": [
362 | "data.show_batch(2, figsize=(10,7), ds_type=DatasetType.Valid)"
363 | ],
364 | "execution_count": null,
365 | "outputs": []
366 | },
367 | {
368 | "cell_type": "markdown",
369 | "metadata": {
370 | "id": "K7W20An-GnNm",
371 | "colab_type": "text"
372 | },
373 | "source": [
374 | "### 3.2. First step Model\n",
375 | "\n",
376 | "---\n",
377 | "\n"
378 | ]
379 | },
380 | {
381 | "cell_type": "code",
382 | "metadata": {
383 | "id": "V-paASzNELtK",
384 | "colab_type": "code",
385 | "colab": {}
386 | },
387 | "source": [
388 | "name2id = {v:k for k,v in enumerate(codes)}\n",
389 | "\n",
390 | "def acc_rtk(input, target):\n",
391 | " target = target.squeeze(1)\n",
392 | " mask = target != 0\n",
393 | " return (input.argmax(dim=1)[mask]==target[mask]).float().mean()"
394 | ],
395 | "execution_count": null,
396 | "outputs": []
397 | },
398 | {
399 | "cell_type": "code",
400 | "metadata": {
401 | "id": "NcAvDEryGuwq",
402 | "colab_type": "code",
403 | "colab": {}
404 | },
405 | "source": [
406 | "metrics=acc_rtk\n",
407 | "wd=1e-2"
408 | ],
409 | "execution_count": null,
410 | "outputs": []
411 | },
412 | {
413 | "cell_type": "code",
414 | "metadata": {
415 | "id": "F9rbvQ8AGw0a",
416 | "colab_type": "code",
417 | "colab": {}
418 | },
419 | "source": [
420 | "learn = unet_learner(data, models.resnet34, metrics=metrics, wd=wd)"
421 | ],
422 | "execution_count": null,
423 | "outputs": []
424 | },
425 | {
426 | "cell_type": "code",
427 | "metadata": {
428 | "id": "wpjCmT8mGy1W",
429 | "colab_type": "code",
430 | "colab": {}
431 | },
432 | "source": [
433 | "#CUDA_LAUNCH_BLOCKING=1\n",
434 | "lr_find(learn)\n",
435 | "learn.recorder.plot()"
436 | ],
437 | "execution_count": null,
438 | "outputs": []
439 | },
440 | {
441 | "cell_type": "code",
442 | "metadata": {
443 | "id": "GS7-jnS0G1DM",
444 | "colab_type": "code",
445 | "colab": {}
446 | },
447 | "source": [
448 | "lr=1e-4"
449 | ],
450 | "execution_count": null,
451 | "outputs": []
452 | },
453 | {
454 | "cell_type": "code",
455 | "metadata": {
456 | "id": "u0Y52M-qG3ID",
457 | "colab_type": "code",
458 | "colab": {}
459 | },
460 | "source": [
461 | "learn.fit_one_cycle(10, slice(lr), pct_start=0.9)"
462 | ],
463 | "execution_count": null,
464 | "outputs": []
465 | },
466 | {
467 | "cell_type": "code",
468 | "metadata": {
469 | "id": "Ym9mEJVVG4zE",
470 | "colab_type": "code",
471 | "colab": {}
472 | },
473 | "source": [
474 | "learn.save('stage-1')"
475 | ],
476 | "execution_count": null,
477 | "outputs": []
478 | },
479 | {
480 | "cell_type": "code",
481 | "metadata": {
482 | "id": "r_WCYmq3G6Uf",
483 | "colab_type": "code",
484 | "colab": {}
485 | },
486 | "source": [
487 | "learn.load('stage-1');"
488 | ],
489 | "execution_count": null,
490 | "outputs": []
491 | },
492 | {
493 | "cell_type": "code",
494 | "metadata": {
495 | "id": "Gb-EFFVpG7-q",
496 | "colab_type": "code",
497 | "colab": {}
498 | },
499 | "source": [
500 | "learn.show_results(rows=5, figsize=(15,15))"
501 | ],
502 | "execution_count": null,
503 | "outputs": []
504 | },
505 | {
506 | "cell_type": "markdown",
507 | "metadata": {
508 | "id": "VE4zF162G-4z",
509 | "colab_type": "text"
510 | },
511 | "source": [
512 | "### 3.3. Interpret\n",
513 | "\n",
514 | "---\n",
515 | "\n"
516 | ]
517 | },
518 | {
519 | "cell_type": "code",
520 | "metadata": {
521 | "id": "kJu9vBlfHA5B",
522 | "colab_type": "code",
523 | "colab": {}
524 | },
525 | "source": [
526 | "interp = SegmentationInterpretation.from_learner(learn)"
527 | ],
528 | "execution_count": null,
529 | "outputs": []
530 | },
531 | {
532 | "cell_type": "code",
533 | "metadata": {
534 | "id": "rea5Nz9yHCxp",
535 | "colab_type": "code",
536 | "colab": {}
537 | },
538 | "source": [
539 | "top_losses, top_idxs = interp.top_losses((288,352))"
540 | ],
541 | "execution_count": null,
542 | "outputs": []
543 | },
544 | {
545 | "cell_type": "code",
546 | "metadata": {
547 | "id": "VSCFhKzzHEfS",
548 | "colab_type": "code",
549 | "colab": {}
550 | },
551 | "source": [
552 | "# plot loss distribution\n",
553 | "plt.hist(to_np(top_losses), bins=20)"
554 | ],
555 | "execution_count": null,
556 | "outputs": []
557 | },
558 | {
559 | "cell_type": "code",
560 | "metadata": {
561 | "id": "_MHIJS_yHGa7",
562 | "colab_type": "code",
563 | "colab": {}
564 | },
565 | "source": [
566 | "# top loss idxs of images\n",
567 | "top_idxs[:5]"
568 | ],
569 | "execution_count": null,
570 | "outputs": []
571 | },
572 | {
573 | "cell_type": "markdown",
574 | "metadata": {
575 | "id": "tRZoEiB8HJhI",
576 | "colab_type": "text"
577 | },
578 | "source": [
579 | "### 3.4. Confusion Matrix\n",
580 | "\n",
581 | "---\n",
582 | "\n"
583 | ]
584 | },
585 | {
586 | "cell_type": "code",
587 | "metadata": {
588 | "id": "mRo-rEthHJ6A",
589 | "colab_type": "code",
590 | "colab": {}
591 | },
592 | "source": [
593 | "mean_cm, single_img_cm = interp._generate_confusion()"
594 | ],
595 | "execution_count": null,
596 | "outputs": []
597 | },
598 | {
599 | "cell_type": "code",
600 | "metadata": {
601 | "id": "mTum9BXpHNBn",
602 | "colab_type": "code",
603 | "colab": {}
604 | },
605 | "source": [
606 | "mean_cm.shape, single_img_cm.shape"
607 | ],
608 | "execution_count": null,
609 | "outputs": []
610 | },
611 | {
612 | "cell_type": "code",
613 | "metadata": {
614 | "id": "9uFc-7KcHO2l",
615 | "colab_type": "code",
616 | "colab": {}
617 | },
618 | "source": [
619 | "# global class performance\n",
620 | "df = interp._plot_intersect_cm(mean_cm, \"Mean of Ratio of Intersection given True Label\")"
621 | ],
622 | "execution_count": null,
623 | "outputs": []
624 | },
625 | {
626 | "cell_type": "code",
627 | "metadata": {
628 | "id": "D4bA9Qw4HXEB",
629 | "colab_type": "code",
630 | "colab": {}
631 | },
632 | "source": [
633 | "# single image class performance\n",
634 | "i = 10\n",
635 | "df = interp._plot_intersect_cm(single_img_cm[i], f\"Ratio of Intersection given True Label, Image:{i}\")"
636 | ],
637 | "execution_count": null,
638 | "outputs": []
639 | },
640 | {
641 | "cell_type": "code",
642 | "metadata": {
643 | "id": "tbFVrVCVHaK6",
644 | "colab_type": "code",
645 | "colab": {}
646 | },
647 | "source": [
648 | "# show xyz\n",
649 | "interp.show_xyz(i)"
650 | ],
651 | "execution_count": null,
652 | "outputs": []
653 | },
654 | {
655 | "cell_type": "markdown",
656 | "metadata": {
657 | "id": "3oW77qUxHhkp",
658 | "colab_type": "text"
659 | },
660 | "source": [
661 | "### 3.5. First model continuation\n",
662 | "\n",
663 | "---\n",
664 | "\n"
665 | ]
666 | },
667 | {
668 | "cell_type": "code",
669 | "metadata": {
670 | "id": "DHhM3YfjHjmV",
671 | "colab_type": "code",
672 | "colab": {}
673 | },
674 | "source": [
675 | "learn.unfreeze()"
676 | ],
677 | "execution_count": null,
678 | "outputs": []
679 | },
680 | {
681 | "cell_type": "code",
682 | "metadata": {
683 | "id": "vpf9gZYgHlnI",
684 | "colab_type": "code",
685 | "colab": {}
686 | },
687 | "source": [
688 | "lrs = slice(lr/400,lr/4)"
689 | ],
690 | "execution_count": null,
691 | "outputs": []
692 | },
693 | {
694 | "cell_type": "code",
695 | "metadata": {
696 | "id": "h84SZCadHnaY",
697 | "colab_type": "code",
698 | "colab": {}
699 | },
700 | "source": [
701 | "learn.fit_one_cycle(100, lrs, pct_start=0.9)"
702 | ],
703 | "execution_count": null,
704 | "outputs": []
705 | },
706 | {
707 | "cell_type": "code",
708 | "metadata": {
709 | "id": "iJxgf68WHqD8",
710 | "colab_type": "code",
711 | "colab": {}
712 | },
713 | "source": [
714 | "learn.save('stage-2')"
715 | ],
716 | "execution_count": null,
717 | "outputs": []
718 | },
719 | {
720 | "cell_type": "code",
721 | "metadata": {
722 | "id": "t_rkQMWVHrmf",
723 | "colab_type": "code",
724 | "colab": {}
725 | },
726 | "source": [
727 | "learn.load('stage-2');"
728 | ],
729 | "execution_count": null,
730 | "outputs": []
731 | },
732 | {
733 | "cell_type": "code",
734 | "metadata": {
735 | "id": "rwQbJFU2HtMx",
736 | "colab_type": "code",
737 | "colab": {}
738 | },
739 | "source": [
740 | "learn.show_results(rows=25, figsize=(20,20))"
741 | ],
742 | "execution_count": null,
743 | "outputs": []
744 | },
745 | {
746 | "cell_type": "markdown",
747 | "metadata": {
748 | "id": "3ojFnvRBHx8v",
749 | "colab_type": "text"
750 | },
751 | "source": [
752 | "### 3.6. Confusion Matrix\n",
753 | "\n",
754 | "---\n",
755 | "\n"
756 | ]
757 | },
758 | {
759 | "cell_type": "code",
760 | "metadata": {
761 | "id": "Sdx-NrEBiPjh",
762 | "colab_type": "code",
763 | "colab": {}
764 | },
765 | "source": [
766 | "interp = SegmentationInterpretation.from_learner(learn)"
767 | ],
768 | "execution_count": null,
769 | "outputs": []
770 | },
771 | {
772 | "cell_type": "code",
773 | "metadata": {
774 | "id": "khTXOjeCHz4U",
775 | "colab_type": "code",
776 | "colab": {}
777 | },
778 | "source": [
779 | "mean_cm, single_img_cm = interp._generate_confusion()"
780 | ],
781 | "execution_count": null,
782 | "outputs": []
783 | },
784 | {
785 | "cell_type": "code",
786 | "metadata": {
787 | "id": "LvilgGWHH2MX",
788 | "colab_type": "code",
789 | "colab": {}
790 | },
791 | "source": [
792 | "mean_cm.shape, single_img_cm.shape"
793 | ],
794 | "execution_count": null,
795 | "outputs": []
796 | },
797 | {
798 | "cell_type": "code",
799 | "metadata": {
800 | "id": "0b1RhFsxH4PW",
801 | "colab_type": "code",
802 | "colab": {}
803 | },
804 | "source": [
805 | "# global class performance\n",
806 | "df = interp._plot_intersect_cm(mean_cm, \"Mean of Ratio of Intersection given True Label\")"
807 | ],
808 | "execution_count": null,
809 | "outputs": []
810 | },
811 | {
812 | "cell_type": "code",
813 | "metadata": {
814 | "id": "XuneWk4BH64P",
815 | "colab_type": "code",
816 | "colab": {}
817 | },
818 | "source": [
819 | "# single image class performance\n",
820 | "i = 0\n",
821 | "df = interp._plot_intersect_cm(single_img_cm[i], f\"Ratio of Intersection given True Label, Image:{i}\")"
822 | ],
823 | "execution_count": null,
824 | "outputs": []
825 | },
826 | {
827 | "cell_type": "code",
828 | "metadata": {
829 | "id": "G9ZMMt2IH85B",
830 | "colab_type": "code",
831 | "colab": {}
832 | },
833 | "source": [
834 | "# show xyz\n",
835 | "interp.show_xyz(i)"
836 | ],
837 | "execution_count": null,
838 | "outputs": []
839 | },
840 | {
841 | "cell_type": "markdown",
842 | "metadata": {
843 | "id": "KgZkM8M1IAxD",
844 | "colab_type": "text"
845 | },
846 | "source": [
847 | "### 3.7. Interpret\n",
848 | "\n",
849 | "---\n",
850 | "\n"
851 | ]
852 | },
853 | {
854 | "cell_type": "code",
855 | "metadata": {
856 | "id": "BXla1Fq0IBMS",
857 | "colab_type": "code",
858 | "colab": {}
859 | },
860 | "source": [
861 | "learn.interpret"
862 | ],
863 | "execution_count": null,
864 | "outputs": []
865 | },
866 | {
867 | "cell_type": "markdown",
868 | "metadata": {
869 | "id": "_siWJq89IOdJ",
870 | "colab_type": "text"
871 | },
872 | "source": [
873 | "### 3.8. Saving\n",
874 | "\n",
875 | "---\n",
876 | "\n"
877 | ]
878 | },
879 | {
880 | "cell_type": "code",
881 | "metadata": {
882 | "id": "m3WfZ7bTIQOW",
883 | "colab_type": "code",
884 | "colab": {}
885 | },
886 | "source": [
887 | "learn.save('stage-2')"
888 | ],
889 | "execution_count": null,
890 | "outputs": []
891 | },
892 | {
893 | "cell_type": "code",
894 | "metadata": {
895 | "id": "HvexEcm9ISE_",
896 | "colab_type": "code",
897 | "colab": {}
898 | },
899 | "source": [
900 | "data=None\n",
901 | "learn=None\n",
902 | "gc.collect()"
903 | ],
904 | "execution_count": null,
905 | "outputs": []
906 | },
907 | {
908 | "cell_type": "markdown",
909 | "metadata": {
910 | "id": "Uh1ZPx64gR56",
911 | "colab_type": "text"
912 | },
913 | "source": [
914 | "## 4. Second Step - With weights\n",
915 | "\n",
916 | "---\n",
917 | "\n"
918 | ]
919 | },
920 | {
921 | "cell_type": "markdown",
922 | "metadata": {
923 | "id": "aT8l81QTjBrE",
924 | "colab_type": "text"
925 | },
926 | "source": [
927 | "### 4.1. Second step Datasets\n",
928 | "\n",
929 | "---\n",
930 | "\n"
931 | ]
932 | },
933 | {
934 | "cell_type": "code",
935 | "metadata": {
936 | "id": "FZP0o8F9i5-o",
937 | "colab_type": "code",
938 | "colab": {}
939 | },
940 | "source": [
941 | "size = src_size\n",
942 | "\n",
943 | "free = gpu_mem_get_free_no_cache()\n",
944 | "# the max size of bs depends on the available GPU RAM\n",
945 | "if free > 8200: bs=8\n",
946 | "else: bs=4\n",
947 | "print(f\"using bs={bs}, have {free}MB of GPU RAM free\")"
948 | ],
949 | "execution_count": null,
950 | "outputs": []
951 | },
952 | {
953 | "cell_type": "code",
954 | "metadata": {
955 | "id": "FgHlUXXIjMKU",
956 | "colab_type": "code",
957 | "colab": {}
958 | },
959 | "source": [
960 | "src = (SegmentationItemList.from_folder(path_img)\n",
961 | " .split_by_fname_file('../valid.txt')\n",
962 | " .label_from_func(get_y_fn, classes=codes))"
963 | ],
964 | "execution_count": null,
965 | "outputs": []
966 | },
967 | {
968 | "cell_type": "code",
969 | "metadata": {
970 | "id": "0WjndpsWjN85",
971 | "colab_type": "code",
972 | "colab": {}
973 | },
974 | "source": [
975 | "data = (src.transform(get_transforms(), size=size, tfm_y=True)\n",
976 | " .databunch(bs=bs)\n",
977 | " .normalize(imagenet_stats))"
978 | ],
979 | "execution_count": null,
980 | "outputs": []
981 | },
982 | {
983 | "cell_type": "code",
984 | "metadata": {
985 | "id": "BIaF-c96jRC4",
986 | "colab_type": "code",
987 | "colab": {}
988 | },
989 | "source": [
990 | "data.show_batch(2, figsize=(10,10))"
991 | ],
992 | "execution_count": null,
993 | "outputs": []
994 | },
995 | {
996 | "cell_type": "code",
997 | "metadata": {
998 | "id": "amJs-yS0jTj2",
999 | "colab_type": "code",
1000 | "colab": {}
1001 | },
1002 | "source": [
1003 | "data.show_batch(2, figsize=(10,7), ds_type=DatasetType.Valid)"
1004 | ],
1005 | "execution_count": null,
1006 | "outputs": []
1007 | },
1008 | {
1009 | "cell_type": "markdown",
1010 | "metadata": {
1011 | "id": "33FD1Sc2j-k0",
1012 | "colab_type": "text"
1013 | },
1014 | "source": [
1015 | "### 4.2. Second step Model\n",
1016 | "\n",
1017 | "---\n",
1018 | "\n"
1019 | ]
1020 | },
1021 | {
1022 | "cell_type": "code",
1023 | "metadata": {
1024 | "id": "51MWvFIcj8PK",
1025 | "colab_type": "code",
1026 | "colab": {}
1027 | },
1028 | "source": [
1029 | "name2id = {v:k for k,v in enumerate(codes)}\n",
1030 | "void_code = name2id['manholeCover']\n",
1031 | "\n",
1032 | "def acc_rtk(input, target):\n",
1033 | " target = target.squeeze(1)\n",
1034 | " mask = target != void_code\n",
1035 | " return (input.argmax(dim=1)[mask]==target[mask]).float().mean()"
1036 | ],
1037 | "execution_count": null,
1038 | "outputs": []
1039 | },
1040 | {
1041 | "cell_type": "code",
1042 | "metadata": {
1043 | "id": "U-TFrct9kEI7",
1044 | "colab_type": "code",
1045 | "colab": {}
1046 | },
1047 | "source": [
1048 | "metrics=acc_rtk\n",
1049 | "wd=1e-2"
1050 | ],
1051 | "execution_count": null,
1052 | "outputs": []
1053 | },
1054 | {
1055 | "cell_type": "code",
1056 | "metadata": {
1057 | "id": "XhlXrkb3kGqL",
1058 | "colab_type": "code",
1059 | "colab": {}
1060 | },
1061 | "source": [
1062 | "balanced_loss = CrossEntropyFlat(axis=1, weight=torch.tensor([1.0,5.0,6.0,7.0,75.0,1000.0,3100.0,3300.0,0.0,270.0,2200.0,1000.0,180.0]).cuda())"
1063 | ],
1064 | "execution_count": null,
1065 | "outputs": []
1066 | },
1067 | {
1068 | "cell_type": "code",
1069 | "metadata": {
1070 | "id": "r53gfgy6kKsP",
1071 | "colab_type": "code",
1072 | "colab": {}
1073 | },
1074 | "source": [
1075 | "learn = unet_learner(data, models.resnet34, metrics=metrics, loss_func=balanced_loss, wd=wd)"
1076 | ],
1077 | "execution_count": null,
1078 | "outputs": []
1079 | },
1080 | {
1081 | "cell_type": "code",
1082 | "metadata": {
1083 | "id": "QZJ69o4ZkRW3",
1084 | "colab_type": "code",
1085 | "colab": {}
1086 | },
1087 | "source": [
1088 | "learn.load('stage-2')"
1089 | ],
1090 | "execution_count": null,
1091 | "outputs": []
1092 | },
1093 | {
1094 | "cell_type": "code",
1095 | "metadata": {
1096 | "id": "LdbVHXsWkVLJ",
1097 | "colab_type": "code",
1098 | "colab": {}
1099 | },
1100 | "source": [
1101 | "!/opt/bin/nvidia-smi"
1102 | ],
1103 | "execution_count": null,
1104 | "outputs": []
1105 | },
1106 | {
1107 | "cell_type": "code",
1108 | "metadata": {
1109 | "id": "BrjsLWBnkXM4",
1110 | "colab_type": "code",
1111 | "colab": {}
1112 | },
1113 | "source": [
1114 | "#CUDA_LAUNCH_BLOCKING=1\n",
1115 | "lr_find(learn)\n",
1116 | "learn.recorder.plot()"
1117 | ],
1118 | "execution_count": null,
1119 | "outputs": []
1120 | },
1121 | {
1122 | "cell_type": "code",
1123 | "metadata": {
1124 | "id": "vF7XVkIhkZIf",
1125 | "colab_type": "code",
1126 | "colab": {}
1127 | },
1128 | "source": [
1129 | "lr=1e-4"
1130 | ],
1131 | "execution_count": null,
1132 | "outputs": []
1133 | },
1134 | {
1135 | "cell_type": "code",
1136 | "metadata": {
1137 | "id": "MmG820FAkazZ",
1138 | "colab_type": "code",
1139 | "colab": {}
1140 | },
1141 | "source": [
1142 | "learn.fit_one_cycle(10, slice(lr), pct_start=0.9)"
1143 | ],
1144 | "execution_count": null,
1145 | "outputs": []
1146 | },
1147 | {
1148 | "cell_type": "code",
1149 | "metadata": {
1150 | "id": "IV2AM1tVkdNy",
1151 | "colab_type": "code",
1152 | "colab": {}
1153 | },
1154 | "source": [
1155 | "learn.save('stage-1-weights')"
1156 | ],
1157 | "execution_count": null,
1158 | "outputs": []
1159 | },
1160 | {
1161 | "cell_type": "code",
1162 | "metadata": {
1163 | "id": "cnrMAAJpkfNM",
1164 | "colab_type": "code",
1165 | "colab": {}
1166 | },
1167 | "source": [
1168 | "learn.load('stage-1-weights');"
1169 | ],
1170 | "execution_count": null,
1171 | "outputs": []
1172 | },
1173 | {
1174 | "cell_type": "code",
1175 | "metadata": {
1176 | "id": "JjrNgXZXkg9_",
1177 | "colab_type": "code",
1178 | "colab": {}
1179 | },
1180 | "source": [
1181 | "learn.show_results(rows=5, figsize=(15,15))"
1182 | ],
1183 | "execution_count": null,
1184 | "outputs": []
1185 | },
1186 | {
1187 | "cell_type": "markdown",
1188 | "metadata": {
1189 | "id": "4TAIZ9s4msq5",
1190 | "colab_type": "text"
1191 | },
1192 | "source": [
1193 | "### 4.3. Interpret\n",
1194 | "\n",
1195 | "---\n",
1196 | "\n"
1197 | ]
1198 | },
1199 | {
1200 | "cell_type": "code",
1201 | "metadata": {
1202 | "id": "W74hI7tWmncC",
1203 | "colab_type": "code",
1204 | "colab": {}
1205 | },
1206 | "source": [
1207 | "interp = SegmentationInterpretation.from_learner(learn)"
1208 | ],
1209 | "execution_count": null,
1210 | "outputs": []
1211 | },
1212 | {
1213 | "cell_type": "code",
1214 | "metadata": {
1215 | "id": "yel6fEhlmxwA",
1216 | "colab_type": "code",
1217 | "colab": {}
1218 | },
1219 | "source": [
1220 | "top_losses, top_idxs = interp.top_losses((288,352))"
1221 | ],
1222 | "execution_count": null,
1223 | "outputs": []
1224 | },
1225 | {
1226 | "cell_type": "code",
1227 | "metadata": {
1228 | "id": "6yfQcvPvmzd6",
1229 | "colab_type": "code",
1230 | "colab": {}
1231 | },
1232 | "source": [
1233 | "# plot loss distribution\n",
1234 | "plt.hist(to_np(top_losses), bins=20)"
1235 | ],
1236 | "execution_count": null,
1237 | "outputs": []
1238 | },
1239 | {
1240 | "cell_type": "code",
1241 | "metadata": {
1242 | "id": "dVEVTr0Vm2cj",
1243 | "colab_type": "code",
1244 | "colab": {}
1245 | },
1246 | "source": [
1247 | "# top loss idxs of images\n",
1248 | "top_idxs[:5]"
1249 | ],
1250 | "execution_count": null,
1251 | "outputs": []
1252 | },
1253 | {
1254 | "cell_type": "markdown",
1255 | "metadata": {
1256 | "id": "D8N5E7BxnK7n",
1257 | "colab_type": "text"
1258 | },
1259 | "source": [
1260 | "### 4.4. Confusion Matrix\n",
1261 | "\n",
1262 | "---\n",
1263 | "\n"
1264 | ]
1265 | },
1266 | {
1267 | "cell_type": "code",
1268 | "metadata": {
1269 | "id": "WzpLXsjAnCzg",
1270 | "colab_type": "code",
1271 | "colab": {}
1272 | },
1273 | "source": [
1274 | "mean_cm, single_img_cm = interp._generate_confusion()"
1275 | ],
1276 | "execution_count": null,
1277 | "outputs": []
1278 | },
1279 | {
1280 | "cell_type": "code",
1281 | "metadata": {
1282 | "id": "qlGEqxtKnPcH",
1283 | "colab_type": "code",
1284 | "colab": {}
1285 | },
1286 | "source": [
1287 | "mean_cm.shape, single_img_cm.shape"
1288 | ],
1289 | "execution_count": null,
1290 | "outputs": []
1291 | },
1292 | {
1293 | "cell_type": "code",
1294 | "metadata": {
1295 | "id": "ypwBccNlnRxJ",
1296 | "colab_type": "code",
1297 | "colab": {}
1298 | },
1299 | "source": [
1300 | "# global class performance\n",
1301 | "df = interp._plot_intersect_cm(mean_cm, \"Mean of Ratio of Intersection given True Label\")"
1302 | ],
1303 | "execution_count": null,
1304 | "outputs": []
1305 | },
1306 | {
1307 | "cell_type": "code",
1308 | "metadata": {
1309 | "id": "_4CNorVgnU2D",
1310 | "colab_type": "code",
1311 | "colab": {}
1312 | },
1313 | "source": [
1314 | "# single image class performance\n",
1315 | "i = 10\n",
1316 | "df = interp._plot_intersect_cm(single_img_cm[i], f\"Ratio of Intersection given True Label, Image:{i}\")"
1317 | ],
1318 | "execution_count": null,
1319 | "outputs": []
1320 | },
1321 | {
1322 | "cell_type": "code",
1323 | "metadata": {
1324 | "id": "V2h5ich-nXmo",
1325 | "colab_type": "code",
1326 | "colab": {}
1327 | },
1328 | "source": [
1329 | "# show xyz\n",
1330 | "interp.show_xyz(i)"
1331 | ],
1332 | "execution_count": null,
1333 | "outputs": []
1334 | },
1335 | {
1336 | "cell_type": "markdown",
1337 | "metadata": {
1338 | "id": "XI-bgt6voXi9",
1339 | "colab_type": "text"
1340 | },
1341 | "source": [
1342 | "### 4.5. Second model continuation\n",
1343 | "\n",
1344 | "---\n",
1345 | "\n"
1346 | ]
1347 | },
1348 | {
1349 | "cell_type": "code",
1350 | "metadata": {
1351 | "id": "6BuzgDOYoS60",
1352 | "colab_type": "code",
1353 | "colab": {}
1354 | },
1355 | "source": [
1356 | "learn.unfreeze()"
1357 | ],
1358 | "execution_count": null,
1359 | "outputs": []
1360 | },
1361 | {
1362 | "cell_type": "code",
1363 | "metadata": {
1364 | "id": "Zili8A1rojNi",
1365 | "colab_type": "code",
1366 | "colab": {}
1367 | },
1368 | "source": [
1369 | "lrs = slice(lr/400,lr/4)"
1370 | ],
1371 | "execution_count": null,
1372 | "outputs": []
1373 | },
1374 | {
1375 | "cell_type": "code",
1376 | "metadata": {
1377 | "id": "6C5gFMv7olCX",
1378 | "colab_type": "code",
1379 | "colab": {}
1380 | },
1381 | "source": [
1382 | "learn.fit_one_cycle(100, lrs, pct_start=0.8)"
1383 | ],
1384 | "execution_count": null,
1385 | "outputs": []
1386 | },
1387 | {
1388 | "cell_type": "code",
1389 | "metadata": {
1390 | "id": "DwdGGDd3ooXx",
1391 | "colab_type": "code",
1392 | "colab": {}
1393 | },
1394 | "source": [
1395 | "learn.save('stage-2-weights')"
1396 | ],
1397 | "execution_count": null,
1398 | "outputs": []
1399 | },
1400 | {
1401 | "cell_type": "code",
1402 | "metadata": {
1403 | "id": "Oa5iJP78osiY",
1404 | "colab_type": "code",
1405 | "colab": {}
1406 | },
1407 | "source": [
1408 | "learn.load('stage-2-weights');"
1409 | ],
1410 | "execution_count": null,
1411 | "outputs": []
1412 | },
1413 | {
1414 | "cell_type": "code",
1415 | "metadata": {
1416 | "id": "N4yDmcE7ouv6",
1417 | "colab_type": "code",
1418 | "colab": {}
1419 | },
1420 | "source": [
1421 | "learn.show_results(rows=25, figsize=(20,20))"
1422 | ],
1423 | "execution_count": null,
1424 | "outputs": []
1425 | },
1426 | {
1427 | "cell_type": "markdown",
1428 | "metadata": {
1429 | "id": "wAZbjvb40iMJ",
1430 | "colab_type": "text"
1431 | },
1432 | "source": [
1433 | "### 4.6. Confusion Matrix\n",
1434 | "\n",
1435 | "---\n",
1436 | "\n"
1437 | ]
1438 | },
1439 | {
1440 | "cell_type": "code",
1441 | "metadata": {
1442 | "id": "V1zqX1rEicia",
1443 | "colab_type": "code",
1444 | "colab": {}
1445 | },
1446 | "source": [
1447 | "interp = SegmentationInterpretation.from_learner(learn)"
1448 | ],
1449 | "execution_count": null,
1450 | "outputs": []
1451 | },
1452 | {
1453 | "cell_type": "code",
1454 | "metadata": {
1455 | "id": "s4K_t8a30QW4",
1456 | "colab_type": "code",
1457 | "colab": {}
1458 | },
1459 | "source": [
1460 | "mean_cm, single_img_cm = interp._generate_confusion()"
1461 | ],
1462 | "execution_count": null,
1463 | "outputs": []
1464 | },
1465 | {
1466 | "cell_type": "code",
1467 | "metadata": {
1468 | "id": "P2jgTPz60mMm",
1469 | "colab_type": "code",
1470 | "colab": {}
1471 | },
1472 | "source": [
1473 | "mean_cm.shape, single_img_cm.shape"
1474 | ],
1475 | "execution_count": null,
1476 | "outputs": []
1477 | },
1478 | {
1479 | "cell_type": "code",
1480 | "metadata": {
1481 | "id": "k149KJTd0oCk",
1482 | "colab_type": "code",
1483 | "colab": {}
1484 | },
1485 | "source": [
1486 | "# global class performance\n",
1487 | "df = interp._plot_intersect_cm(mean_cm, \"Mean of Ratio of Intersection given True Label\")"
1488 | ],
1489 | "execution_count": null,
1490 | "outputs": []
1491 | },
1492 | {
1493 | "cell_type": "code",
1494 | "metadata": {
1495 | "id": "ccTzua6t0q3z",
1496 | "colab_type": "code",
1497 | "colab": {}
1498 | },
1499 | "source": [
1500 | "# single image class performance\n",
1501 | "i = 130\n",
1502 | "df = interp._plot_intersect_cm(single_img_cm[i], f\"Ratio of Intersection given True Label, Image:{i}\")"
1503 | ],
1504 | "execution_count": null,
1505 | "outputs": []
1506 | },
1507 | {
1508 | "cell_type": "code",
1509 | "metadata": {
1510 | "id": "StyO909Y0tbq",
1511 | "colab_type": "code",
1512 | "colab": {}
1513 | },
1514 | "source": [
1515 | "# show xyz\n",
1516 | "interp.show_xyz(i)"
1517 | ],
1518 | "execution_count": null,
1519 | "outputs": []
1520 | },
1521 | {
1522 | "cell_type": "markdown",
1523 | "metadata": {
1524 | "id": "g5RVRyXL0_Uh",
1525 | "colab_type": "text"
1526 | },
1527 | "source": [
1528 | "### 4.7. Interpret\n",
1529 | "\n",
1530 | "---\n",
1531 | "\n"
1532 | ]
1533 | },
1534 | {
1535 | "cell_type": "code",
1536 | "metadata": {
1537 | "id": "DHSOVNYk0_yI",
1538 | "colab_type": "code",
1539 | "colab": {}
1540 | },
1541 | "source": [
1542 | "learn.interpret"
1543 | ],
1544 | "execution_count": null,
1545 | "outputs": []
1546 | },
1547 | {
1548 | "cell_type": "markdown",
1549 | "metadata": {
1550 | "id": "eZ7SZZBN1KrS",
1551 | "colab_type": "text"
1552 | },
1553 | "source": [
1554 | "### 4.8. Saving\n",
1555 | "\n",
1556 | "---\n",
1557 | "\n"
1558 | ]
1559 | },
1560 | {
1561 | "cell_type": "code",
1562 | "metadata": {
1563 | "id": "L-jfTKtS1GwG",
1564 | "colab_type": "code",
1565 | "colab": {}
1566 | },
1567 | "source": [
1568 | "learn.save('stage-2-weights')"
1569 | ],
1570 | "execution_count": null,
1571 | "outputs": []
1572 | },
1573 | {
1574 | "cell_type": "markdown",
1575 | "metadata": {
1576 | "id": "S0FHwE1p1RmR",
1577 | "colab_type": "text"
1578 | },
1579 | "source": [
1580 | "## 5. Using a saved model to Predict\n",
1581 | "\n",
1582 | "---\n",
1583 | "\n"
1584 | ]
1585 | },
1586 | {
1587 | "cell_type": "code",
1588 | "metadata": {
1589 | "id": "zs0LgfQr1NQ1",
1590 | "colab_type": "code",
1591 | "colab": {}
1592 | },
1593 | "source": [
1594 | "size = src_size\n",
1595 | "\n",
1596 | "free = gpu_mem_get_free_no_cache()\n",
1597 | "# the max size of bs depends on the available GPU RAM\n",
1598 | "if free > 8200: bs=8\n",
1599 | "else: bs=4\n",
1600 | "print(f\"using bs={bs}, have {free}MB of GPU RAM free\")"
1601 | ],
1602 | "execution_count": null,
1603 | "outputs": []
1604 | },
1605 | {
1606 | "cell_type": "code",
1607 | "metadata": {
1608 | "id": "kcPVDg1o1ZyY",
1609 | "colab_type": "code",
1610 | "colab": {}
1611 | },
1612 | "source": [
1613 | "src = (SegmentationItemList.from_folder(path_img)\n",
1614 | " .split_by_fname_file('../valid.txt')\n",
1615 | " .label_from_func(get_y_fn, classes=codes))"
1616 | ],
1617 | "execution_count": null,
1618 | "outputs": []
1619 | },
1620 | {
1621 | "cell_type": "code",
1622 | "metadata": {
1623 | "id": "Xhppsx_R1bvI",
1624 | "colab_type": "code",
1625 | "colab": {}
1626 | },
1627 | "source": [
1628 | "data = (src.transform(get_transforms(), size=size, tfm_y=True)\n",
1629 | " .databunch(bs=bs)\n",
1630 | " .normalize(imagenet_stats))"
1631 | ],
1632 | "execution_count": null,
1633 | "outputs": []
1634 | },
1635 | {
1636 | "cell_type": "code",
1637 | "metadata": {
1638 | "id": "ViZg6ouZOdcm",
1639 | "colab_type": "code",
1640 | "colab": {}
1641 | },
1642 | "source": [
1643 | "learn = unet_learner(data, models.resnet34)"
1644 | ],
1645 | "execution_count": null,
1646 | "outputs": []
1647 | },
1648 | {
1649 | "cell_type": "code",
1650 | "metadata": {
1651 | "id": "wkRfgXLd1sMe",
1652 | "colab_type": "code",
1653 | "colab": {}
1654 | },
1655 | "source": [
1656 | "learn.load('stage-2-weights');"
1657 | ],
1658 | "execution_count": null,
1659 | "outputs": []
1660 | },
1661 | {
1662 | "cell_type": "code",
1663 | "metadata": {
1664 | "id": "ABRotkkd1vSU",
1665 | "colab_type": "code",
1666 | "colab": {}
1667 | },
1668 | "source": [
1669 | "interp = SegmentationInterpretation.from_learner(learn)"
1670 | ],
1671 | "execution_count": null,
1672 | "outputs": []
1673 | },
1674 | {
1675 | "cell_type": "code",
1676 | "metadata": {
1677 | "id": "gE8c7YSX1xOH",
1678 | "colab_type": "code",
1679 | "colab": {}
1680 | },
1681 | "source": [
1682 | "mean_cm, single_img_cm = interp._generate_confusion()"
1683 | ],
1684 | "execution_count": null,
1685 | "outputs": []
1686 | },
1687 | {
1688 | "cell_type": "code",
1689 | "metadata": {
1690 | "id": "OWUm7CsA1yxT",
1691 | "colab_type": "code",
1692 | "colab": {}
1693 | },
1694 | "source": [
1695 | "mean_cm.shape, single_img_cm.shape"
1696 | ],
1697 | "execution_count": null,
1698 | "outputs": []
1699 | },
1700 | {
1701 | "cell_type": "code",
1702 | "metadata": {
1703 | "id": "6ZejuHxi106m",
1704 | "colab_type": "code",
1705 | "colab": {}
1706 | },
1707 | "source": [
1708 | "# global class performance\n",
1709 | "df = interp._plot_intersect_cm(mean_cm, \"Mean of Ratio of Intersection given True Label\")"
1710 | ],
1711 | "execution_count": null,
1712 | "outputs": []
1713 | },
1714 | {
1715 | "cell_type": "code",
1716 | "metadata": {
1717 | "id": "vGajg-Q_13xU",
1718 | "colab_type": "code",
1719 | "colab": {}
1720 | },
1721 | "source": [
1722 | "# single image class performance\n",
1723 | "i = 130\n",
1724 | "df = interp._plot_intersect_cm(single_img_cm[i], f\"Ratio of Intersection given True Label, Image:{i}\")"
1725 | ],
1726 | "execution_count": null,
1727 | "outputs": []
1728 | },
1729 | {
1730 | "cell_type": "code",
1731 | "metadata": {
1732 | "id": "KmLQEz2w16H6",
1733 | "colab_type": "code",
1734 | "colab": {}
1735 | },
1736 | "source": [
1737 | "# show xyz\n",
1738 | "interp.show_xyz(i)"
1739 | ],
1740 | "execution_count": null,
1741 | "outputs": []
1742 | },
1743 | {
1744 | "cell_type": "code",
1745 | "metadata": {
1746 | "id": "J66cvtAP181a",
1747 | "colab_type": "code",
1748 | "colab": {}
1749 | },
1750 | "source": [
1751 | "learn.show_results()"
1752 | ],
1753 | "execution_count": null,
1754 | "outputs": []
1755 | },
1756 | {
1757 | "cell_type": "markdown",
1758 | "metadata": {
1759 | "id": "IO3-KXDsp4VB",
1760 | "colab_type": "text"
1761 | },
1762 | "source": [
1763 | "## 6. Saving the Results\n",
1764 | "\n",
1765 | "---\n",
1766 | "\n"
1767 | ]
1768 | },
1769 | {
1770 | "cell_type": "code",
1771 | "metadata": {
1772 | "id": "BMj7IsIep6oZ",
1773 | "colab_type": "code",
1774 | "colab": {}
1775 | },
1776 | "source": [
1777 | "img_f = fnames[655]\n",
1778 | "img = open_image(img_f)\n",
1779 | "img.show(figsize=(5,5))"
1780 | ],
1781 | "execution_count": null,
1782 | "outputs": []
1783 | },
1784 | {
1785 | "cell_type": "code",
1786 | "metadata": {
1787 | "id": "m5j5g8nuqCd5",
1788 | "colab_type": "code",
1789 | "colab": {}
1790 | },
1791 | "source": [
1792 | "prediction = learn.predict(img)"
1793 | ],
1794 | "execution_count": null,
1795 | "outputs": []
1796 | },
1797 | {
1798 | "cell_type": "code",
1799 | "metadata": {
1800 | "id": "r8F26wXwqEIL",
1801 | "colab_type": "code",
1802 | "colab": {}
1803 | },
1804 | "source": [
1805 | "prediction[0].show(figsize=(5,5))"
1806 | ],
1807 | "execution_count": null,
1808 | "outputs": []
1809 | },
1810 | {
1811 | "cell_type": "code",
1812 | "metadata": {
1813 | "id": "S47GvGCLqGTe",
1814 | "colab_type": "code",
1815 | "colab": {}
1816 | },
1817 | "source": [
1818 | "results_save = 'results'\n",
1819 | "path_rst = path/results_save\n",
1820 | "path_rst.mkdir(exist_ok=True)"
1821 | ],
1822 | "execution_count": null,
1823 | "outputs": []
1824 | },
1825 | {
1826 | "cell_type": "code",
1827 | "metadata": {
1828 | "id": "J8KKlUcSqLZ1",
1829 | "colab_type": "code",
1830 | "colab": {}
1831 | },
1832 | "source": [
1833 | "def save_preds(names):\n",
1834 | " i=0\n",
1835 | " #names = dl.dataset.items\n",
1836 | " \n",
1837 | " for b in names:\n",
1838 | " img_s = fnames[i]\n",
1839 | " img_toSave = open_image(img_s)\n",
1840 | " img_split = f'{img_s}'\n",
1841 | " img_split = img_split[44:]\n",
1842 | " predictionSave = learn.predict(img_toSave)\n",
1843 | " predictionSave[0].save(path_rst/img_split) #Save Image\n",
1844 | " i += 1\n",
1845 | " print(i)"
1846 | ],
1847 | "execution_count": null,
1848 | "outputs": []
1849 | },
1850 | {
1851 | "cell_type": "code",
1852 | "metadata": {
1853 | "id": "sGOALMQJqNT0",
1854 | "colab_type": "code",
1855 | "colab": {}
1856 | },
1857 | "source": [
1858 | "save_preds(fnames)"
1859 | ],
1860 | "execution_count": null,
1861 | "outputs": []
1862 | },
1863 | {
1864 | "cell_type": "markdown",
1865 | "metadata": {
1866 | "id": "Ke8FATsZW1n_",
1867 | "colab_type": "text"
1868 | },
1869 | "source": [
1870 | "## 7. Coloring the Results\n",
1871 | "\n",
1872 | "---\n",
1873 | "\n",
1874 | "\n"
1875 | ]
1876 | },
1877 | {
1878 | "cell_type": "code",
1879 | "metadata": {
1880 | "id": "8ODnS4N8W6yF",
1881 | "colab_type": "code",
1882 | "colab": {}
1883 | },
1884 | "source": [
1885 | "import os\n",
1886 | "import glob\n",
1887 | "import base64\n",
1888 | "import cv2 as cv\n",
1889 | "cv.__version__"
1890 | ],
1891 | "execution_count": null,
1892 | "outputs": []
1893 | },
1894 | {
1895 | "cell_type": "code",
1896 | "metadata": {
1897 | "id": "H5ZL2LkXX9Ij",
1898 | "colab_type": "code",
1899 | "colab": {}
1900 | },
1901 | "source": [
1902 | "colored_results = 'results_color'\n",
1903 | "path_crst = path/colored_results\n",
1904 | "path_crst.mkdir(exist_ok=True)"
1905 | ],
1906 | "execution_count": null,
1907 | "outputs": []
1908 | },
1909 | {
1910 | "cell_type": "code",
1911 | "metadata": {
1912 | "id": "CaMwRbGJzFyG",
1913 | "colab_type": "code",
1914 | "colab": {}
1915 | },
1916 | "source": [
1917 | "%load_ext cython"
1918 | ],
1919 | "execution_count": null,
1920 | "outputs": []
1921 | },
1922 | {
1923 | "cell_type": "code",
1924 | "metadata": {
1925 | "id": "5tdvtka_zJg7",
1926 | "colab_type": "code",
1927 | "colab": {}
1928 | },
1929 | "source": [
1930 | "%%cython -a\n",
1931 | "import cython\n",
1932 | "cimport numpy\n",
1933 | "import cv2 as cv\n",
1934 | "import numpy as np\n",
1935 | "\n",
1936 | "@cython.boundscheck(False)\n",
1937 | "@cython.wraparound(False)\n",
1938 | "#def colorfull_fast(numpy.ndarray[numpy.uint8_t, ndim=3, mode=\"c\"] frame):\n",
1939 | "cpdef numpy.ndarray[numpy.uint8_t, ndim=3, mode=\"c\"] colorfull_fast(numpy.ndarray[numpy.uint8_t, ndim=3, mode=\"c\"] frame):\n",
1940 | " # set the variable extension types\n",
1941 | " cdef int x, y, width, height, b, g, r\n",
1942 | "\n",
1943 | " #frame = cv.imdecode(np.frombuffer(byteframe, np.uint8), -1)\n",
1944 | "\n",
1945 | " # grab the image dimensions\n",
1946 | " width = 288\n",
1947 | " height = 352\n",
1948 | " \n",
1949 | " # loop over the image, pixel by pixel\n",
1950 | " for x in range(width):\n",
1951 | " for y in range(height):\n",
1952 | " b, g, r = frame[x, y]\n",
1953 | " if (b, g, r) == (0,0,0): #background\n",
1954 | " frame[x, y] = (0,0,0)\n",
1955 | " elif (b, g, r) == (1,1,1): #roadAsphalt\n",
1956 | " frame[x, y] = (85,85,255)\n",
1957 | " elif (b, g, r) == (2,2,2): #roadPaved\n",
1958 | " frame[x, y] = (85,170,127)\n",
1959 | " elif (b, g, r) == (3,3,3): #roadUnpaved\n",
1960 | " frame[x, y] = (255,170,127) \n",
1961 | " elif (b, g, r) == (4,4,4): #roadMarking\n",
1962 | " frame[x, y] = (255,255,255) \n",
1963 | " elif (b, g, r) == (5,5,5): #speedBump\n",
1964 | " frame[x, y] = (255,85,255)\n",
1965 | " elif (b, g, r) == (6,6,6): #catsEye\n",
1966 | " frame[x, y] = (255,255,127) \n",
1967 | " elif (b, g, r) == (7,7,7): #stormDrain\n",
1968 | " frame[x, y] = (170,0,127) \n",
1969 | " elif (b, g, r) == (8,8,8): #manholeCover\n",
1970 | " frame[x, y] = (0,255,255) \n",
1971 | " elif (b, g, r) == (9,9,9): #patchs\n",
1972 | " frame[x, y] = (0,0,127) \n",
1973 | " elif (b, g, r) == (10,10,10): #waterPuddle\n",
1974 | " frame[x, y] = (170,0,0)\n",
1975 | " elif (b, g, r) == (11,11,11): #pothole\n",
1976 | " frame[x, y] = (255,0,0)\n",
1977 | " elif (b, g, r) == (12,12,12): #cracks\n",
1978 | " frame[x, y] = (255,85,0)\n",
1979 | " \n",
1980 | " frame = cv.cvtColor(frame,cv.COLOR_BGR2RGB)\n",
1981 | " \n",
1982 | " # return the colored image\n",
1983 | " return frame"
1984 | ],
1985 | "execution_count": null,
1986 | "outputs": []
1987 | },
1988 | {
1989 | "cell_type": "code",
1990 | "metadata": {
1991 | "id": "kAidoH1KzMsd",
1992 | "colab_type": "code",
1993 | "colab": {}
1994 | },
1995 | "source": [
1996 | "import timeit\n",
1997 | "# Count variables\n",
1998 | "fqtd = 0\n",
1999 | "\n",
2000 | "filenames = [img for img in glob.glob(str(path_rst/\"*.png\"))]\n",
2001 | "\n",
2002 | "filenames.sort() # ADD THIS LINE\n",
2003 | "\n",
2004 | "for img in filenames:\n",
2005 | " frame = cv.imread(img)\n",
2006 | "\n",
2007 | " #%timeit colorfull_fast(frame)\n",
2008 | " \n",
2009 | " frame = colorfull_fast(frame)\n",
2010 | " name = \"%09d.png\"%fqtd\n",
2011 | " cv.imwrite(os.path.join(path_crst, name), frame)\n",
2012 | "\n",
2013 | " fqtd += 1\n",
2014 | " print(fqtd)\n",
2015 | "\n",
2016 | "print(\"Done!\")"
2017 | ],
2018 | "execution_count": null,
2019 | "outputs": []
2020 | }
2021 | ]
2022 | }
--------------------------------------------------------------------------------