{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide_input": false
},
"outputs": [],
"source": [
"from fastai.vision import * # Quick access to computer vision functionality"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Vision example"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Images can be in labeled folders, or a single folder with a CSV."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"PosixPath('/home/ubuntu/.fastai/data/mnist_sample')"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"path = untar_data(URLs.MNIST_SAMPLE)\n",
"path"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Image folder version"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a `DataBunch`, optionally with transforms:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"image/jpeg": "/9j/4AAQSkZJRgABAQEAZABkAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/wAARCAAcABwDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQF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cU7PZDMwFEe90OvA8L9BPpxPq9frDPJ+iFdq2TYPBwP8ul8ukadpdnKqqxBj7eYXfnTGGUqmEWq12U+FisUA6nX6mlQMSETRNw3q9vgEWi8UgXXhQPB6HaZrY7XY+aL/fI5fLIRqN/j5wOBzeNYvruuj1ekHHGR7Y7XZxPp8fdul8Pn+qDz34iqJQJPJYlkgknuqFx+JirVaLUqmUn7jf71M+nyfTNImIyLKswBzSXUpEUBQFtm2/tksvns1mb2CO4yCTybwGmEwmsVqtfNhmswFjTEQrB7weDcdxRGFyQMMwcDwe4XkettutyDHKA3Vdh+u6fnWGYYTdsHiwqqoYj8c+bDqdIhaLvQao6zomk4kPG41GqFarMvcvFnj9Az4cDqhUKlLNJvWmaTQaNJvNZKT0cSnzXfb2V9sXF0CiY/TP1uMAAAAASUVORK5CYII=\n",
"text/plain": [
"Image (3, 28, 28)"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = ImageDataBunch.from_folder(path, ds_tfms=(rand_pad(2, 28), []), bs=64)\n",
"data.normalize(imagenet_stats)\n",
"img,label = data.train_ds[0]\n",
"img"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create and fit a `Learner`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
" \n",
" \n",
" epoch | \n",
" train_loss | \n",
" valid_loss | \n",
" accuracy | \n",
" time | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 0.046350 | \n",
" 0.008395 | \n",
" 0.997547 | \n",
" 00:06 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"learn = cnn_learner(data, models.resnet18, metrics=accuracy)\n",
"learn.fit_one_cycle(1, 0.01)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor(0.9975)"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"accuracy(*learn.get_preds())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### CSV version"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Same as above, using CSV instead of folder name for labels"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"image/jpeg": "/9j/4AAQSkZJRgABAQEAZABkAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/wAARCAAcABwDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD8AER5XEcaFmY4VVGST6V7Nrv/AATi/wCChvhfwPcfE7xN+wb8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upoezJEkSSqUSmBntdhvhcHhR2P+BpmkikUjAtm00m83pxB4dHS0Dmw9UFAUXFxd4fX3962p8qFqtIhKJrA54dnYGZsZoNML19TVOT0+xv7+PdDqNp6cnMDNOTk5WBzRNE5lMBru7uz5fNpsFM6Pb7UKW5Z8bmg9tb2/DcRwwM5LJ5M8DiQiXl5dgZtRqtfUAo9EomBnD4RCapn05bunv6eXlhYiIZFmmvb29L8ctDczn80RE5HkePTw8LBTrSzsYDP5z+hRFQbvdBjPj/Pz8ez3c2NhApVLB3d0dBEH4U/tAAJIkIRaLoVgsgpnheR4Mw1gIGPycrmVZZFkWCYJA5XKZJpMJERFtbW1RPB6f7mNmyuVy1Gq1FirnzJKmUikMBgPfc9ZoNNDpdHBzcwPLspaabuGjruuydySsnEwSMpccAAAAAElFTkSuQmCC\n",
"text/plain": [
"Image (3, 28, 28)"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = ImageDataBunch.from_csv(path, ds_tfms=(rand_pad(2, 28), []), bs=64)\n",
"data.normalize(imagenet_stats)\n",
"img,label = data.train_ds[0]\n",
"img"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" epoch | \n",
" train_loss | \n",
" valid_loss | \n",
" accuracy | \n",
" time | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 0.055503 | \n",
" 0.007956 | \n",
" 0.996881 | \n",
" 00:05 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"learn = cnn_learner(data, models.resnet18, metrics=accuracy)\n",
"learn.fit_one_cycle(1, 0.01)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}