image_output_11.png image_output_98.png image_output_74.png image_output_82.png image_output_24.png image_output_46.png image_output_85.png image_output_25.png image_output_77.png image_output_47.png image_output_78.png image_output_17.png image_output_30.png image_output_73.png image_output_72.png image_output_14.png image_output_59.png image_output_39.png image_output_97.png image_output_28.png image_output_86.png image_output_84.png image_output_81.png image_output_23.png image_output_34.png image_output_64.png image_output_13.png image_output_40.png image_output_5.png image_output_48.png image_output_68.png image_output_75.png image_output_18.png image_output_58.png image_output_92.png image_output_21.png image_output_52.png image_output_60.png image_output_7.png image_output_62.png image_output_96.png image_output_56.png image_output_31.png image_output_65.png image_output_20.png image_output_69.png image_output_32.png image_output_53.png image_output_4.png image_output_51.png image_output_83.png image_output_42.png image_output_35.png image_output_90.png image_output_41.png image_output_57.png image_output_36.png image_output_8.png image_output_37.png image_output_66.png image_output_16.png image_output_91.png image_output_70.png image_output_67.png image_output_27.png image_output_54.png image_output_6.png image_output_45.png image_output_63.png image_output_71.png image_output_80.png image_output_95.png image_output_93.png image_output_12.png image_output_22.png image_output_89.png image_output_55.png text_plain_output_1.png image_output_94.png image_output_3.png image_output_29.png image_output_44.png image_output_43.png image_output_2.png image_output_1.png image_output_10.png image_output_88.png image_output_33.png image_output_87.png image_output_50.png image_output_15.png image_output_99.png image_output_49.png image_output_100.png image_output_76.png image_output_9.png image_output_19.png image_output_79.png image_output_61.png image_output_38.png image_output_26.png
from os import listdir

from os.path import isfile, join

from tensorflow.keras.callbacks import EarlyStopping

from tensorflow.keras.callbacks import ModelCheckpoint

from tensorflow.keras.layers import Conv2D

from tensorflow.keras.layers import Dense

from tensorflow.keras.layers import Flatten

from tensorflow.keras.layers import MaxPooling2D

from tensorflow.keras.models import Sequential

from tensorflow.keras.models import load_model

from tensorflow.keras.optimizers import SGD

from tensorflow.keras.preprocessing import image

from tensorflow.keras.preprocessing.image import ImageDataGenerator

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import random

K.clear_session()

img_width = img_height = 224
training_data_dir = '../input/my-dogs-vs-cats/my-dogs-vs-cats/training'
validation_data_dir = '../input/my-dogs-vs-cats/my-dogs-vs-cats/validation'
batch_size = 32

train_datagen = ImageDataGenerator(rescale=1.0 / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)

train_generator = train_datagen.flow_from_directory(training_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='binary')

valid_datagen = ImageDataGenerator(rescale=1.0 / 255)

validation_generator = valid_datagen.flow_from_directory(validation_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='binary')

if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
else:
    input_shape = (img_width, img_height, 3)

def VGG16():
    model = Sequential()
    model.add(Conv2D(filters=64, kernel_size=(3, 3), padding='same', input_shape=input_shape, activation='relu', kernel_initializer='he_uniform'))
    model.add(Conv2D(filters=64, kernel_size=(3, 3), padding='same', activation='relu', kernel_initializer='he_uniform'))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
    model.add(Conv2D(filters=128, kernel_size=(3, 3), padding='same', activation='relu', kernel_initializer='he_uniform'))
    model.add(Conv2D(filters=128, kernel_size=(3, 3), padding='same', activation='relu', kernel_initializer='he_uniform'))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
    model.add(Conv2D(filters=256, kernel_size=(3, 3), padding='same', activation='relu', kernel_initializer='he_uniform'))
    model.add(Conv2D(filters=256, kernel_size=(3, 3), padding='same', activation='relu', kernel_initializer='he_uniform'))
    model.add(Conv2D(filters=256, kernel_size=(3, 3), padding='same', activation='relu', kernel_initializer='he_uniform'))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
    model.add(Conv2D(filters=512, kernel_size=(3, 3), padding='same', activation='relu', kernel_initializer='he_uniform'))
    model.add(Conv2D(filters=512, kernel_size=(3, 3), padding='same', activation='relu', kernel_initializer='he_uniform'))
    model.add(Conv2D(filters=512, kernel_size=(3, 3), padding='same', activation='relu', kernel_initializer='he_uniform'))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
    model.add(Conv2D(filters=512, kernel_size=(3, 3), padding='same', activation='relu', kernel_initializer='he_uniform'))
    model.add(Conv2D(filters=512, kernel_size=(3, 3), padding='same', activation='relu', kernel_initializer='he_uniform'))
    model.add(Conv2D(filters=512, kernel_size=(3, 3), padding='same', activation='relu', kernel_initializer='he_uniform'))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
    model.add(Flatten(name='flatten'))
    model.add(Dense(4096, activation='relu', name='fc1'))
    model.add(Dense(4096, activation='relu', name='fc2'))
    model.add(Dense(1, activation='sigmoid', name='output'))
    return model

model = VGG16()

early_stopping = EarlyStopping(monitor='val_accuracy', mode='auto', verbose=1, patience=3)

model_checkpoint = ModelCheckpoint(filepath='./checkpoint.h5', monitor='val_accuracy', save_best_only=True, mode='auto')
callbacks = [early_stopping, model_checkpoint]

opt = SGD(learning_rate=0.001, momentum=0.9)
model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])

model.summary()

epochs = 2
steps_per_epoch = len(train_generator)
validation_steps = len(validation_generator)

history = model.fit_generator(generator=train_generator, validation_data=validation_generator, epochs=epochs, steps_per_epoch=steps_per_epoch, validation_steps=validation_steps, callbacks=callbacks, verbose=1)

model = load_model('../input/modelcheckpoint/final_model.h5')

import matplotlib.pyplot as plt
import random
import pandas as pd
from tensorflow.keras.preprocessing import image
df = pd.read_csv('../input/my-dogs-vs-cats/my-dogs-vs-cats/sampleSubmission.csv')
def updateValue(df: pd.DataFrame, id: int, value: str):
    df.loc[df['id'] == id, 'label'] = value
predict_dir_path = '../input/my-dogs-vs-cats/my-dogs-vs-cats/test'
onlyfiles = [f for f in listdir(predict_dir_path) if isfile(join(predict_dir_path, f))]
random.shuffle(onlyfiles)
dog_counter = 0
cat_counter = 0
counter = 0
times = 100
for file, counter in zip(onlyfiles, range(times)):
    img = image.load_img(predict_dir_path + '/' + file, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    images = np.vstack([x])
    classes = model.predict(images, batch_size=10)
    classes = classes[0][0]
    id = str(file).split('.')
    id = int(id[0])
    if classes == 0:
        updateValue(df, id, 'cat')
        if cat_counter < 100:
            plt.title('cat')
            plt.imshow(img)
            plt.show()
        cat_counter += 1
    else:
        updateValue(df, id, 'dog')
        if dog_counter < 100:
            plt.title('dog')
            plt.imshow(img)
            plt.show()
        dog_counter += 1
df.to_csv('./sampleSubmission.csv', index=False)
print('Total Dogs :', dog_counter)
print('Total Cats :', cat_counter)