image_output_1.png
from sklearn.utils import shuffle

import cv2

import matplotlib.pyplot as plt

import numpy as np

import os

import seaborn as sns

seed = 42
np.random.seed = seed

labels = ['NORMAL', 'PNEUMONIA']
folders = ['train', 'test', 'val']
def load_images_from_directory(main_dirictory, foldername):
    total_labels = []
    images = []
    total_normal = 0
    total_pneumonia = 0
    path = os.path.join(main_dirictory, foldername)
    for lab in labels:
        full_path = os.path.join(path, lab)
        for image in os.listdir(full_path):
            img = cv2.imread(full_path + '/' + image)
            img = cv2.resize(img, (150, 150))
            images.append(img)
            if lab == 'NORMAL':
                label = 0
                total_normal += 1
            elif lab == 'PNEUMONIA':
                label = 1
                total_pneumonia += 1
            total_labels.append(label)
    return shuffle(images, total_labels, random_state=756349782)
def get_Label(number):
    labels = {0: 'NORMAL', 1: 'PNEUMONIA'}
    return labels[number]
def plot_predection(model_name):
    images = []
    count = 0
    for i, files in enumerate(val_images):
        img = cv2.resize(files, (150, 150))
        img = np.expand_dims(files, axis=0)
        feature = model_name.predict(img)
        predection = np.argmax(feature, axis=1)
        plt.xticks([])
        plt.yticks([])
        count += 1
        if count == 30:
            break
def freezing_layers(model_name):
    for layer in model_name.layers:
        layer.trainable = False

train_images = np.asarray(train_images, np.float32) / 255
train_labels = np.asarray(train_labels)

for i in range(30):
    plt.xticks([])
    plt.yticks([])

plt.title('Train Labels Visualization')
sns.countplot(x=train_labels, palette='flare')
plt.show()