text_plain_output_1.png
from tensorflow.keras.utils import load_img

import cv2

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import random

def load_image(filename):
    image = load_img('../input/plant-pathology-2021-fgvc8/train_images/' + filename)
def load_random_image(filenames):
    sample = random.choice(filenames)
    image = load_img('../input/plant-pathology-2021-fgvc8/train_images/' + sample)
def load_augmented_random_image(filenames):
    sample = random.choice(filenames)
    image = load_img('/kaggle/working/' + sample)
def load_image_for_augmentation(image_path):
    image = cv2.imread(image_path)
    if image.shape[-1] == 1:
        image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
    image = np.array(image)
    return image
def format_path_gcs(st):
    return GCS_DS_PATH + '/train_images/' + st
def format_resized_image_path_gcs(st):
    return GCS_DS_PATH + '/img_sz_384/' + st
def format_tpu_path(st):
    return '/kaggle/input/resized-plant2021' + '/img_sz_384/' + st

# Defining mem usage reduction function to try to make dataframes lighter

def reduce_mem_usage(df, verbose=True):
    numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
    start_mem = df.memory_usage().sum() / 1024**2
    for col in df.columns:
        col_type = df[col].dtypes
        if col_type in numerics:
            c_min = df[col].min()
            c_max = df[col].max()
            if str(col_type)[:3] == 'int':
                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:
                    df[col] = df[col].astype(np.int8)
                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:
                    df[col] = df[col].astype(np.int16)
                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:
                    df[col] = df[col].astype(np.int32)
                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:
                    df[col] = df[col].astype(np.int64)
            else:
                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:
                    df[col] = df[col].astype(np.float16)
                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:
                    df[col] = df[col].astype(np.float32)
                else:
                    df[col] = df[col].astype(np.float64)

    end_mem = df.memory_usage().sum() / 1024**2
    print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))
    print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))

    return df

train_df = pd.read_csv('../input/plant-pathology-2021-fgvc8/train.csv')
IMAGE_PATH = '../input/plant-pathology-2021-fgvc8/test-images/'
train_df = reduce_mem_usage(train_df)

train_df.labels.value_counts()

label_counts = train_df['labels'].value_counts()
colormap = plt.cm.get_cmap('tab10')
num_labels = len(label_counts)
colors = [colormap(i) for i in range(num_labels)]
plt.figure(figsize=(10, 6))
plt.bar(label_counts.index, label_counts.values, color=colors)
plt.xlabel('Labels')
plt.ylabel('Count')
plt.title('Label Distribution')
plt.xticks(rotation=45)
for i, count in enumerate(label_counts):
    plt.text(i, count + 50, str(count), ha='center')
total_count = label_counts.sum()
plt.axhline(total_count, color='black', linestyle='--', alpha=0.5)
plt.text(0.5, 1.08, f'Total Count: {total_count}', transform=plt.gca().transAxes, ha='center', fontsize=12, fontweight='bold')
max_count = max(label_counts)
y_limit = max_count + max_count * 0.1
plt.ylim(top=y_limit)
plt.show()