Cainvas
Model Files
sheep.h5
keras
Model
deepSea Compiled Models
sheep.exe
deepSea
Ubuntu

Sheep breed classification

Credit: AITS Cainvas Community

Photo by Petter Pentilä on Dribbble

Identify the breed of the sheep in the image using neural networks.

In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
from keras import layers, optimizers, models, preprocessing, losses, callbacks, Input, Model, applications
import os
import random
from PIL import Image
In [2]:
!wget https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/SheepFaceImages.zip
 
!unzip -qo SheepFaceImages.zip

!rm SheepFaceImages.zip
--2021-01-26 10:23:26--  https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/SheepFaceImages.zip
Resolving cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)... 52.219.62.92
Connecting to cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)|52.219.62.92|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 11600085 (11M) [application/zip]
Saving to: ‘SheepFaceImages.zip’

SheepFaceImages.zip 100%[===================>]  11.06M  --.-KB/s    in 0.1s    

2021-01-26 10:23:26 (115 MB/s) - ‘SheepFaceImages.zip’ saved [11600085/11600085]

The dataset

On Kaggle by Divyansh Agrawal

This data is originally the efforts of Abu Jwade Sanabel et al. The team collected the data from a real farm in Australia. Data

The dataset has images of sheep, focussed on their faces, belonging to 4 classes -

In [3]:
# Loading the dataset

path = 'SheepFaceImages'

train_df = preprocessing.image_dataset_from_directory(path, label_mode = 'categorical', validation_split = 0.2, subset = 'training', seed = 113)

test_df = preprocessing.image_dataset_from_directory(path, label_mode = 'categorical', validation_split = 0.2, subset = 'validation', seed = 113)
Found 1680 files belonging to 4 classes.
Using 1344 files for training.
Found 1680 files belonging to 4 classes.
Using 336 files for validation.
In [4]:
# How many samples in each class

for x in os.listdir(path):
    print(x, ' - ', len(os.listdir(path + '/' + x)))
Poll Dorset  -  420
Marino  -  420
Suffolk  -  420
White Suffolk  -  420

This is a balanced dataset!

In [5]:
# Looking into the class labels

class_names = train_df.class_names

print("Train class names: ", train_df.class_names)
print("Test class names: ", test_df.class_names)
Train class names:  ['Marino', 'Poll Dorset', 'Suffolk', 'White Suffolk']
Test class names:  ['Marino', 'Poll Dorset', 'Suffolk', 'White Suffolk']

Visualization

In [6]:
num_samples = 4    # the number of samples to be displayed in each class

for x in class_names:
    plt.figure(figsize=(20, 20))

    filenames = os.listdir(path + '/' + x)

    for i in range(num_samples):
        ax = plt.subplot(1, num_samples, i + 1)
        img = Image.open(path +'/' + x + '/' + filenames[i])
        plt.imshow(img)
        plt.title(x)
        plt.axis("off")