<jupyter_start><jupyter_text>Training a classification model with ResNet<jupyter_code>import datetime
import matplotlib as mpl
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import os
import dtlpy as dl
from dtlpy.ml import train_utils<jupyter_output><empty_output><jupyter_text>Get Global Model and Pretrained Snapshot<jupyter_code>model = dl.models.get(model_name='ResNet')
snapshot = model.snapshots.get('pretrained-resnet18')
model.snapshots.list().to_df()<jupyter_output><empty_output><jupyter_text>Upload Sheep Dataset  We will upload a new classification dataset with 4 labels of sheep.<jupyter_code>project = dl.projects.get('Sheeps Face Proj')
dataset = project.datasets.create('Sheep Face')
dataset.to_df()
_ = dataset.items.upload(local_path='../../assets/sample_datasets/SheepFace/items/*',
                         local_annotations_path='../../assets/sample_datasets/SheepFace/json')
dataset.add_labels(label_list=['Marino', 'Poll Dorset', 'Suffolk', 'White Suffolk'])<jupyter_output><empty_output><jupyter_text>Run Pretrained Model  Load the pretrained snapshot into the model adapter<jupyter_code>adapter = model.build()
adapter.load_from_snapshot(snapshot=snapshot)<jupyter_output><empty_output><jupyter_text>Get an item and predict with upload.  You can also open the item in the platform to view and edit annotations easily.<jupyter_code>item = dl.items.get(item_id='611e174e4c09acc3c5bb81d3')
annotations = adapter.predict_items([item], with_upload=True)
image = Image.open(item.download())
plt.imshow(item.annotations.show(np.asarray(image),
                                 thickness=5))
print('Classification: {}'.format(annotations[0][0].label))<jupyter_output><empty_output><jupyter_text>Train on new dataset  Here we will train on the Sheep dataset.  First we will clone and split the dataset to 2 partitions - train and validation.  After that we will clone the pretrained snapshot<jupyter_code>dataset = project.datasets.get(dataset_name='Sheep Face')
partitions = {dl.SnapshotPartitionType.TRAIN: 0.8,
              dl.SnapshotPartitionType.VALIDATION: 0.2}
cloned_dataset = train_utils.prepare_dataset(dataset,
                                             filters=None,
                                             partitions=partitions)
cloned_dataset.add_labels(label_list=list(dataset.instance_map.keys()))
snapshot_name = 'sheep-soft-augmentations'
# create an Item Bucket to save snapshot in your project
bucket = project.buckets.create(bucket_type=dl.BucketType.ITEM,
                                model_name=model.name,
                                snapshot_name=snapshot_name)
new_snapshot = snapshot.clone(snapshot_name=snapshot_name,
                              dataset_id=cloned_dataset.id,
                              bucket=bucket,
                              configuration={'batch_size': 16,
                                             'start_epoch': 0,
                                             'num_epochs': 2,
                                             'input_size': 256,
                                             'id_to_label_map': {(v - 1): k for k, v in
                                                                 dataset.instance_map.items()}
                                             })
new_snapshot = model.snapshots.get(snapshot_name=snapshot_name)<jupyter_output><empty_output><jupyter_text>We'll load the new un-trained snapshot to the adapter and prepare the training local dataset<jupyter_code>adapter.load_from_snapshot(snapshot=new_snapshot)
root_path, data_path, output_path = adapter.prepare_training()<jupyter_output><empty_output><jupyter_text>Start The Train  Finally we are ready to train!<jupyter_code>print("Training {!r} with snapshot {!r} on data {!r}".format(model.name, new_snapshot.id, data_path))
adapter.train(data_path=data_path,
              output_path=output_path)<jupyter_output><empty_output><jupyter_text>Saving the snapshot - will upload the trained weights to the Item Bucket<jupyter_code>adapter.save_to_snapshot(local_path=output_path,
                         replace=True)<jupyter_output><empty_output><jupyter_text>And list the bucket content<jupyter_code>adapter.snapshot.bucket.list_content()<jupyter_output><empty_output><jupyter_text>Predict On Out New Trained Snapshot  We will create a load and visualize the prediction of the snapshot<jupyter_code>item = dl.items.get(item_id='611e174e4c09acc3c5bb81d3')
annotations = adapter.predict_items([item], with_upload=True)
image = Image.open(item.download())
plt.imshow(item.annotations.show(np.asarray(image),
                                 thickness=5))
print('Classification: {}'.format(annotations[0][0].label))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09554946213878929, 'fraction_numerical': 0.01054629824931449, 'mean_word_length': 3.330593607305936, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '593914', 'license': 'MIT', 'n_tokens_mistral': 1385, 'n_tokens_neox': 1364, 'n_words': 278, 'path': 'tutorials/model_management/use_dataloop_zoo_models/classification/chapter.ipynb', 'repo_name': 'dataloop-ai/dtlpy-documentation', 'size': '4741'}