image_output_1.png
from keras.utils import to_categorical

import cv2

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import os

import os

category = ['cat', 'dog']
EPOCHS = 50
IMGSIZE = 128
BATCH_SIZE = 32
STOPPING_PATIENCE = 15
VERBOSE = 1
MODEL_NAME = 'cnn_50epochs_imgsize128'
OPTIMIZER = 'adam'
TRAINING_DIR = '../input/dogs-vs-cats-redux-kernels-edition/train'
TEST_DIR = '../input/dogs-vs-cats-redux-kernels-edition/test'

for img in os.listdir(TRAINING_DIR)[7890:]:
    img_path = os.path.join(TRAINING_DIR, img)
    img_arr = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
    img_arr = cv2.resize(img_arr, (IMGSIZE, IMGSIZE))
    break

def create_train_data(path):
    X = []
    y = []
    for img in os.listdir(path):
        if img == os.listdir(path)[7889]:
            continue
        img_path = os.path.join(path, img)
        img_arr = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
        img_arr = cv2.resize(img_arr, (IMGSIZE, IMGSIZE))
        img_arr = img_arr / 255.0
        cat = np.where(img.split('.')[0] == 'dog', 1, 0)
        X.append(img_arr)
        y.append(cat)
    X = np.array(X).reshape(-1, IMGSIZE, IMGSIZE, 1)
    y = np.array(y)
    return (X, y)

X, y = create_train_data(TRAINING_DIR)

y = to_categorical(y, 2)
print(f'features shape {X.shape}.\nlabel shape {y.shape}.')