How to use the fastai.vision.models.resnet18 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 / similarity / test_similarity_model.py View on Github external
def test_compute_features_learner(tiny_ic_databunch):
    learn = cnn_learner(tiny_ic_databunch, models.resnet18)
    embedding_layer = learn.model[1][6]
    features = compute_features_learner(
        tiny_ic_databunch, DatasetType.Valid, 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 / computervision-recipes / tests / unit / similarity / test_similarity_model.py View on Github external
def test_compute_feature(tiny_ic_databunch):
    learn = cnn_learner(tiny_ic_databunch, models.resnet18)
    embedding_layer = learn.model[1][6]
    im_path = str(tiny_ic_databunch.valid_ds.x.items[0])
    feature = compute_feature(im_path, learn, embedding_layer)
    assert len(feature) == 512
github fastai / fastai / fastai / vision / learner.py View on Github external
def _resnet_split(m:nn.Module): return (m[0][6],m[1])
# Split squeezenet model on maxpool layers
def _squeezenet_split(m:nn.Module): return (m[0][0][5], m[0][0][8], m[1])
def _densenet_split(m:nn.Module): return (m[0][0][7],m[1])
def _vgg_split(m:nn.Module): return (m[0][0][22],m[1])
def _alexnet_split(m:nn.Module): return (m[0][0][6],m[1])

_default_meta    = {'cut':None, 'split':_default_split}
_resnet_meta     = {'cut':-2, 'split':_resnet_split }
_squeezenet_meta = {'cut':-1, 'split': _squeezenet_split}
_densenet_meta   = {'cut':-1, 'split':_densenet_split}
_vgg_meta        = {'cut':-1, 'split':_vgg_split}
_alexnet_meta    = {'cut':-1, 'split':_alexnet_split}

model_meta = {
    models.resnet18 :{**_resnet_meta}, models.resnet34: {**_resnet_meta},
    models.resnet50 :{**_resnet_meta}, models.resnet101:{**_resnet_meta},
    models.resnet152:{**_resnet_meta},

    models.squeezenet1_0:{**_squeezenet_meta},
    models.squeezenet1_1:{**_squeezenet_meta},

    models.densenet121:{**_densenet_meta}, models.densenet169:{**_densenet_meta},
    models.densenet201:{**_densenet_meta}, models.densenet161:{**_densenet_meta},
    models.vgg11_bn:{**_vgg_meta}, models.vgg13_bn:{**_vgg_meta}, models.vgg16_bn:{**_vgg_meta}, models.vgg19_bn:{**_vgg_meta},
    models.alexnet:{**_alexnet_meta}}

def cnn_config(arch):
    "Get the metadata associated with `arch`."
    torch.backends.cudnn.benchmark = True
    return model_meta.get(arch, _default_meta)
github microsoft / computervision-recipes / classification / python / 21_deployment_on_azure_container_instances.py View on Github external
from utils_cv.classification.model import IMAGENET_IM_SIZE, model_to_learner

# Check core SDK version number
print(f"Azure ML SDK Version: {azureml.core.VERSION}")


# ## 2. Model retrieval and export <a id="model"></a>
#
# For demonstration purposes, we will use here a ResNet18 model, pretrained on ImageNet. The following steps would be the same if we had trained a model locally (cf. [**01_training_introduction.ipynb**](01_training_introduction.ipynb) notebook for details).
#
# Let's first retrieve the model.

# In[2]:


learn = model_to_learner(models.resnet18(pretrained=True), IMAGENET_IM_SIZE)


# To be able to use this model, we need to export it to our local machine. We store it in an `outputs/` subfolder.

# In[3]:


output_folder = os.path.join(os.getcwd(), "outputs")
model_name = (
    "im_classif_resnet18"
)  # Name we will give our model both locally and on Azure
pickled_model_name = f"{model_name}.pkl"
os.makedirs(output_folder, exist_ok=True)

learn.export(os.path.join(output_folder, pickled_model_name))
github microsoft / computervision-recipes / classification / python / 00_webcam.py View on Github external
# In[3]:


labels = imagenet_labels()
print(f"Num labels = {len(labels)}")
print(f"{', '.join(labels[:5])}, ...")


# Note, Fastai's **[Learner](https://docs.fast.ai/basic_train.html#Learner)** is the trainer for model using data to minimize loss function with optimizer. We follow Fastai's naming convention, *'learn'*, for a `Learner` object variable.

# In[4]:


# Convert a pretrained imagenet model into Learner for prediction. 
# You can load an exported model by learn = load_learner(path) as well.
learn = model_to_learner(models.resnet18(pretrained=True), IMAGENET_IM_SIZE)


# ## 2. Classify Images
# 
# ### 2.1 Image file
# First, we prepare a coffee mug image to show an example of how to score a single image by using the model.

# In[5]:


# Download an example image
IM_URL = "https://cvbp.blob.core.windows.net/public/images/cvbp_cup.jpg"
urllib.request.urlretrieve(IM_URL, os.path.join(data_path(), "example.jpg"))

im = open_image(os.path.join(data_path(), "example.jpg"), convert_mode='RGB')
im
github microsoft / computervision-recipes / utils_cv / classification / parameter_sweeper.py View on Github external
from utils_cv.common.gpu import db_num_workers


Time = float
parameter_flag = "PARAMETERS"


class TrainingSchedule(Enum):
    head_only = ("head_only",)
    body_only = ("body_only",)
    head_first_then_body = "head_first_then_body"


class Architecture(Enum):
    resnet18 = partial(models.resnet18)
    resnet34 = partial(models.resnet34)
    resnet50 = partial(models.resnet50)
    squeezenet1_1 = partial(models.squeezenet1_1)


class DataFrameAlreadyCleaned(Exception):
    pass


def clean_sweeper_df(df: pd.DataFrame) -&gt; pd.DataFrame:
    """ Cleans up dataframe outputed from sweeper

    Cleans up experiment paramter strings in {df} by removing all experiment
    parameters that held constant through each experiment. This method uses a
    variable  to search for strings.
github microsoft / computervision-recipes / similarity / notebooks / script_exploring_hyperparameters.py View on Github external
# -------------------------------------------------------
    if False:
        # Set dataset, model and evaluation parameters
        DATA_PATH = (
            "C:/Users/pabuehle/Desktop/ComputerVision/data/tiny"
        )  # unzip_url(Urls.fridge_objects_tiny_path, exist_ok=True)

        # DNN configuration and learning parameters
        EPOCHS_HEAD = 0
        EPOCHS_BODY = 0
        LEARNING_RATE = 1e-4
        DROPOUT_RATE = 0.5
        BATCH_SIZE = (
            4
        )  # 16   #batch size has to be lower than nr of training ex
        ARCHITECTURE = models.resnet18
        IM_SIZE = 30  # 300

        # 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"""
        )
github microsoft / computervision-recipes / classification / python / 02_multilabel_classification.py View on Github external
print(f"Fast.ai version = {fastai.__version__}")
which_processor()


# Like before, we set some parameters. This time, we can use one of the multi-label datasets that come with this repo.

# In[3]:


DATA_PATH = unzip_url(Urls.multilabel_fridge_objects_path, exist_ok=True)
EPOCHS = 10
LEARNING_RATE = 1e-4
IM_SIZE = 300
BATCH_SIZE = 16
ARCHITECTURE = models.resnet18


# ---

# ## 1. Prepare Image Data for Multi-label Classification
#
# In this notebook, we'll look at different kinds of beverages. In the repo, under `data`, we've downloaded a directory titled: __multilabelFridgeObjects__.
#
# Lets set that directory to our `path` variable, which we'll use throughout the notebook. We'll also inspect what's inside to get an understanding of how to structure images for multi-label classification.

# In[4]:


path = Path(DATA_PATH)
path.ls()
github microsoft / computervision-recipes / classification / python / 03_training_accuracy_vs_speed.py View on Github external
assert MODEL_TYPE in ["high_accuracy", "fast_inference", "small_size"]


# Set parameters based on your selected model.

# In[6]:


if MODEL_TYPE == "high_accuracy":
    ARCHITECTURE = models.resnet50
    IM_SIZE = 500

if MODEL_TYPE == "fast_inference":
    ARCHITECTURE = models.resnet18
    IM_SIZE = 300

if MODEL_TYPE == "small_size":
    ARCHITECTURE = models.squeezenet1_1
    IM_SIZE = 300


# We'll automatically determine if your dataset is a multi-label or traditional (single-label) classification problem. To do so, we'll use the `is_data_multilabel` helper function. In order to detect whether or not a dataset is multi-label, the helper function will check to see if the datapath contains a csv file that has a column 'labels' where the values are space-delimited. You can inspect the function by calling `is_data_multilabel??`.
#
# This function assumes that your multi-label dataset is structured in the recommended format shown in the [multilabel notebook](02_multilabel_classification.ipynb).

# In[7]:


multilabel = is_data_multilabel(DATA_PATH)
metric = accuracy if not multilabel else hamming_accuracy