How to use the fastai.vision.cnn_learner function in fastai

To help you get started, we’ve selected a few fastai examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github microsoft / computervision-recipes / tests / unit / classification / test_classification_model.py View on Github external
tmr = TrainMetricsRecorder(learn)
        learn.callbacks.append(tmr)
        learn.unfreeze()
        learn.fit(epochs, lr)
        return tmr

    # multiple metrics
    learn = cnn_learner(tiny_ic_data, model, metrics=[accuracy, error_rate])
    cb = test_callback(learn)
    assert len(cb.train_metrics) == len(cb.valid_metrics) == epochs
    assert (
        len(cb.train_metrics[0]) == len(cb.valid_metrics[0]) == 2
    )  # we used 2 metrics

    # no metrics
    learn = cnn_learner(tiny_ic_data, model)
    cb = test_callback(learn)
    assert len(cb.train_metrics) == len(cb.valid_metrics) == 0  # no metrics

    # no validation set
    learn = cnn_learner(tiny_ic_data, model, metrics=accuracy)
    learn.data.valid_dl = None
    cb = test_callback(learn)
    assert len(cb.train_metrics) == epochs
    assert len(cb.train_metrics[0]) == 1  # we used 1 metrics
    assert len(cb.valid_metrics) == 0  # no validation
github microsoft / computervision-recipes / tests / unit / classification / test_classification_model.py View on Github external
# multiple metrics
    learn = cnn_learner(tiny_ic_data, model, metrics=[accuracy, error_rate])
    cb = test_callback(learn)
    assert len(cb.train_metrics) == len(cb.valid_metrics) == epochs
    assert (
        len(cb.train_metrics[0]) == len(cb.valid_metrics[0]) == 2
    )  # we used 2 metrics

    # no metrics
    learn = cnn_learner(tiny_ic_data, model)
    cb = test_callback(learn)
    assert len(cb.train_metrics) == len(cb.valid_metrics) == 0  # no metrics

    # no validation set
    learn = cnn_learner(tiny_ic_data, model, metrics=accuracy)
    learn.data.valid_dl = None
    cb = test_callback(learn)
    assert len(cb.train_metrics) == epochs
    assert len(cb.train_metrics[0]) == 1  # we used 1 metrics
    assert len(cb.valid_metrics) == 0  # no validation
github microsoft / computervision-recipes / tests / unit / similarity / test_similarity_model.py View on Github external
def test_compute_features(tiny_ic_databunch):
    learn = cnn_learner(tiny_ic_databunch, models.resnet18)
    embedding_layer = learn.model[1][6]
    features = compute_features(
        tiny_ic_databunch.valid_ds, learn, embedding_layer
    )
    im_paths = tiny_ic_databunch.valid_ds.x.items
    assert len(features) == len(im_paths)
    assert len(features[str(im_paths[1])]) == 512
github microsoft / MLOps / examples / Fastai / Image-classification / train.py View on Github external
import numpy as np

# get the Azure ML run object
run = Run.get_context()

# get images
path = Path('data')
np.random.seed(2)
data = ImageDataBunch.from_folder(path,
                                       train=".",
                                       valid_pct=0.2,
                                       ds_tfms=get_transforms(),
                                       size=224).normalize(imagenet_stats)

# build estimator based on ResNet 34
learn = cnn_learner(data, models.resnet34, metrics=accuracy)
learn.fit_one_cycle(2)

# do test time augmentation and get accuracy
acc = accuracy(*learn.TTA())


# log the accuracy to run
run.log('Accuracy', np.float(acc))
print("Accuracy: ", np.float(acc))
github microsoft / computervision-recipes / classification / python / 01_training_introduction.py View on Github external
# In a standard analysis, we would split the data into a train/validate/test data sets. For this example, we do not use a test set but this could be added using the [add_test](https://docs.fast.ai/data_block.html#LabelLists.add_test) method. Note that in the `fastai` framework, test sets do not include labels as this should be the unknown data to be predicted. The validation data set is a test set that includes labels that can be used to measure the model performance on new observations not used to train the model.

# # Train a Model

# For this image classifier, we use a **ResNet50** convolutional neural network (CNN) architecture. You can find more details about ResNet from [here](https://arxiv.org/abs/1512.03385).
#
# When training CNN, there are almost an infinite number of ways to construct the model architecture. We need to determine how many and what type of layers to include and how many nodes make up each layer. Other hyperparameters that control the training of those layers are also important and add to the overall complexity of neural net methods. With `fastai`, we use the `create_cnn` function to specify the model architecture and performance metric. We will use a transfer learning approach to reuse the CNN architecture and initialize the model parameters used to train on [ImageNet](http://www.image-net.org/).
#
# In this work, we use a custom callback `TrainMetricsRecorder` to track the model accuracy on the training set as we tune the model. This is for instruction only, as the standard approach in `fast.ai` [recorder class](https://docs.fast.ai/basic_train.html#Recorder) only supports tracking model accuracy on the validation set.

# In[9]:


learn = cnn_learner(
    data,
    ARCHITECTURE,
    metrics=[accuracy],
    callback_fns=[partial(TrainMetricsRecorder, show_graph=True)],
)


# Use the `unfreeze` method to allow us to retrain all the CNN layers with the <i>Fridge Objects</i> data set.

# In[10]:


learn.unfreeze()


# The `fit` function trains the CNN using the parameters specified above.
github microsoft / computervision-recipes / similarity / notebooks / script_exploring_hyperparameters.py View on Github external
# Load images into fast.ai's ImageDataBunch object
        random.seed(642)
        data = (
            ImageList.from_folder(DATA_PATH)
            .split_by_rand_pct(valid_pct=0.5, seed=20)
            .label_from_folder()
            .transform(size=IM_SIZE)
            .databunch(bs=BATCH_SIZE)
            .normalize(imagenet_stats)
        )
        print(
            f"""Training set: {len(data.train_ds.x)} images\nValidation set: {len(data.valid_ds.x)} images"""
        )

        # Init learner
        learn = cnn_learner(
            data,
            ARCHITECTURE,
            metrics=[accuracy],
            callback_fns=[partial(TrainMetricsRecorder, show_graph=True)],
            ps=DROPOUT_RATE,
        )

        # Train the last layer
        learn.fit_one_cycle(EPOCHS_HEAD, LEARNING_RATE)
        learn.unfreeze()
        learn.fit_one_cycle(EPOCHS_BODY, LEARNING_RATE)

        # Build multiple sets of comparative images from the validation images
        comparative_sets = comparative_set_builder(
            data.valid_ds, num_sets=1000, num_negatives=99
        )
github microsoft / computervision-recipes / classification / python / 02_multilabel_classification.py View on Github external
# ---

# The following section covers training a model with fastai. It is very similar to the previous notebook, except for the evaluation metric that is used.
#
# Since this is a multi-label classification problem, we'll want to use hamming and zero-one accuracy, as our evalution metric. Unlike traditional accuracy, fast.ai does not provide these metrics in their library, so we have to define them. To create our own metrics, we'll need to define a custom function that will take `y_pred` and `y_true`, and return a single metric.
#
# We've defined the hamming and zero-one accuracy functions in the `utils_cv.classification.models` module so we can use them directly when defining our `cnn_learner`.
#
# > To inspect the implementation of these functions, you can run:
# > - `print(inspect.getsource(hamming_accuracy))`
# > - `print(inpsect.getsource(zero_one_accuracy))`

# In[11]:


learn = cnn_learner(
    data,
    ARCHITECTURE,
    metrics=[hamming_accuracy, zero_one_accuracy],
    callback_fns=[partial(TrainMetricsRecorder, show_graph=True)],
)


# For multi-label classification, we need to use a different loss function, but you'll notice that we do not specify which to use. This is because fast.ai uses the passed-in databunch to detect that this is a multi-label classification problem (that each x maps to a y that has multiple labels) and automatically sets the appropriate loss function. In this case, we see that fast.ai has chosen to use Pytorch's [`BCEWithLogitsLoss`](https://pytorch.org/docs/0.3.0/nn.html#bcewithlogitsloss), which uses the sigmoid activation instead of a softmax.
#
# For further details, we can inspect the loss function by calling `learn.loss_func??`. You can read more about how the [loss function differs in multi-label classification](#appendix-loss-function) in the appendix at the bottom of this notebook.

# In[12]:


learn.loss_func
github microsoft / computervision-recipes / classification / python / 03_training_accuracy_vs_speed.py View on Github external
# In[9]:


data = (
    label_list.transform(tfms=get_transforms(), size=IM_SIZE)
    .databunch(bs=BATCH_SIZE)
    .normalize(imagenet_stats)
)


# Create the learner.

# In[10]:


learn = cnn_learner(data, ARCHITECTURE, metrics=metric)


# Train the last layer for a few epochs.

# In[11]:


learn.fit_one_cycle(EPOCHS_HEAD, LEARNING_RATE)


# Unfreeze the layers.

# In[12]:


learn.unfreeze()
github developmentseed / fastai-serving / benchmark / make_fixture_model.py View on Github external
import torchvision
from fastai.vision import ImageDataBunch, cnn_learner

data = ImageDataBunch.from_csv('fixtures')
learner = cnn_learner(data, torchvision.models.resnet34)
learner.export()