> For the complete documentation index, see [llms.txt](https://docs.universaldatatool.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.universaldatatool.com/machine-learning/fastai/import-datasets-for-fast.ai-image-classification.md).

# Fast.ai Image Classification

## Example Dataset

We're going to use [udt-dataset-cats-and-dogs](https://github.com/UniversalDataTool/udt-dataset-cats-and-dogs), a dataset of labeled images of cats and dogs created from [COCO](https://cocodataset.org/). For this guide you don't need to download it directly, because we'll load it in right from our notebook.

{% hint style="info" %}
Don't like cats and dogs? You can also use any classification from the [Common Objects in Context with the Import COCO button](/importing-data/coco-images.md)! Maybe try to classify bears vs cats!
{% endhint %}

![coco\_dogs\_and\_cats.udt.csv](/files/-MHs7p0X2wwpnrnN5DOG)

## Import CSV Into Pandas Dataframe

We can begin by importing the fastai library, pandas, and our udt.csv file.

```python
from fastai.vision import *
import pandas as pd

url_to_csv = "https://raw.githubusercontent.com/UniversalDataTool/udt-dataset-cats-and-dogs/master/coco_dogs_and_cats.udt.csv"
udt_csv = pd.read_csv(url_to_csv)
```

{% hint style="info" %}
You can use the udt.json format too, tables are just a nice way to visualize the data!
{% endhint %}

## Download Images

UDT Datasets just have links to images, so we'll need to download the actual images. Let's do that using the [fast.ai download\_images function](https://docs.fast.ai/vision.utils#download_images).

```python
# Get the lines of our CSV that have sample data
samples = udt_csv[udt_csv["path"].str.contains("samples.")]

# Create two csvs that just have
# our cat image urls and dog image curls

dog_samples = samples[samples["annotation"] == "dog"]
cat_samples = samples[samples["annotation"] == "cat"]

dog_samples.to_csv("dog_urls.csv", columns=["imageUrl"], header=False, index=False)
cat_samples.to_csv("cat_urls.csv", columns=["imageUrl"], header=False, index=False)
```

![dog\_urls.csv](/files/-MHsA2usrUlYyKf_qWqB)

```python
# Now we can download the images!
download_images("dog_urls.csv", "images/dog" , max_pics=500)
download_images("cat_urls.csv", "images/cat" , max_pics=500)

# Let's make sure all the images are readable
verify_images("images/dog", delete=True, max_size=500)
verify_images("images/cat", delete=True, max_size=500)
```

## Create an ImageDataBunch

From here, everything should should seem pretty normal. We can create an ImageDataBunch from our `images` directory.

```python
data = ImageDataBunch.from_folder("images", train=".", valid_pct=0.2, ds_tfms=get_transforms(), size=224, num_workers=4).normalize(imagenet_stats)

# Let's take a look at the data
data.show_batch(rows=3, figsize=(7,8))
```

![show\_batch output](/files/-MHsAoB-zlbMSEeMQy3O)

## Train a Model

We can now train a model! This is just a simple one, don't forget to fine tune!

```python
learn = cnn_learner(data, models.resnet34, metrics=error_rate)
learn.fit_one_cycle(4)
```

![fit\_one\_cycle output](/files/-MHsB42Ekb2tBjoBTYFW)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.universaldatatool.com/machine-learning/fastai/import-datasets-for-fast.ai-image-classification.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
