Importing Dataset
The dataset which we will use here has been taken from – https://www.kaggle.com/competitions/dog-breed-identification/data. This dataset includes 10,000 images of 120 different breeds of dogs. In this data set, we have a training images folder. test image folder and a CSV file that contains information regarding the image and the breed it belongs to.
Python3
from zipfile import ZipFile data_path = 'dog-breed-identification.zip' with ZipFile(data_path, 'r' ) as zip : zip .extractall() print ( 'The data set has been extracted.' ) |
Output:
The data set has been extracted.
Python3
df = pd.read_csv( 'labels.csv' ) df.head() |
Output:
Python3
df.shape |
Output:
(10222, 2)
Let’s check the number of unique breeds of dog images we have in the training data.
Python3
df[ 'breed' ].nunique() |
Output:
120
So, here we can see that there are 120 unique breed data which has been provided to us.
Python3
plt.figure(figsize = ( 10 , 5 )) df[ 'breed' ].value_counts().plot.bar() plt.axis( 'off' ) plt.show() |
Output:
Here we can observe that there is a data imbalance between the classes of different breeds of dogs.
Python3
df[ 'filepath' ] = 'train/' + df[ 'id' ] + '.jpg' df.head() |
Output:
Although visualizing one image from each class is not feasible but let’s view some of them.
Python3
plt.subplots(figsize = ( 10 , 10 )) for i in range ( 12 ): plt.subplot( 4 , 3 , i + 1 ) # Selecting a random image # index from the dataframe. k = np.random.randint( 0 , len (df)) img = cv2.imread(df.loc[k, 'filepath' ]) plt.imshow(img) plt.title(df.loc[k, 'breed' ]) plt.axis( 'off' ) plt.show() |
Output:
The images are not of the same size which is natural as real-world images tend to be of different sizes and shapes. We will take care of this while loading and processing the images.
Python3
le = LabelEncoder() df[ 'breed' ] = le.fit_transform(df[ 'breed' ]) df.head() |
Output:
Dog Breed Classification using Transfer Learning
In this article, we will learn how to build a classifier using the Transfer Learning technique which can classify among different breeds of dogs. This project has been developed using collab and the dataset has been taken from Kaggle whose link has been provided as well.
Contact Us