{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from zipfile import ZipFile as zf\ntrain_zip = zf('/kaggle/input/dogs-vs-cats/train.zip', 'r')\ntrain_zip.extractall()\ntrain_zip.close()\ntest_zip = zf('/kaggle/input/dogs-vs-cats/test1.zip', 'r')\ntest_zip.extractall()\ntest_zip.close()","metadata":{"execution":{"iopub.status.busy":"2021-07-28T08:23:17.965752Z","iopub.execute_input":"2021-07-28T08:23:17.966096Z","iopub.status.idle":"2021-07-28T08:23:33.939389Z","shell.execute_reply.started":"2021-07-28T08:23:17.966068Z","shell.execute_reply":"2021-07-28T08:23:33.938552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.mkdir('./train1')\nos.mkdir('./train1/cats')\nos.mkdir('./train1/dogs')\nimport shutil\n\nfor file_name in os.listdir('./train'):\n    if file_name.split('.')[0] == 'cat':\n        shutil.copy(os.path.join('./train/', file_name) , os.path.join('./train1/cats', file_name))\n    elif file_name.split('.')[0] == 'dog':\n        shutil.copy(os.path.join('./train/', file_name) , os.path.join('./train1/dogs', file_name))","metadata":{"execution":{"iopub.status.busy":"2021-07-28T08:35:17.282278Z","iopub.execute_input":"2021-07-28T08:35:17.28268Z","iopub.status.idle":"2021-07-28T08:35:20.146154Z","shell.execute_reply.started":"2021-07-28T08:35:17.282645Z","shell.execute_reply":"2021-07-28T08:35:20.145313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(os.listdir('./train1/dogs')))\n","metadata":{"execution":{"iopub.status.busy":"2021-07-28T08:35:52.354351Z","iopub.execute_input":"2021-07-28T08:35:52.354705Z","iopub.status.idle":"2021-07-28T08:35:52.36969Z","shell.execute_reply.started":"2021-07-28T08:35:52.354678Z","shell.execute_reply":"2021-07-28T08:35:52.368857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('./training_set')\nos.mkdir('./training_set/cats')\nos.mkdir('./training_set/dogs')\nos.mkdir('./val_set')\nos.mkdir('./val_set/cats')\nos.mkdir('./val_set/dogs')\nimport random\ntraincats = os.listdir('./train1/cats')\nrandom.shuffle(traincats)\ntraindogs = os.listdir('./train1/dogs')\nrandom.shuffle(traindogs)\nfor file_name in traincats[:10000]:\n    shutil.copy(os.path.join('./train1/cats', file_name) , os.path.join('./training_set/cats', file_name))\nfor file_name in traincats[10000:]:\n    shutil.copy(os.path.join('./train1/cats', file_name) , os.path.join('./val_set/cats', file_name))\nfor file_name in traindogs[:10000]:\n    shutil.copy(os.path.join('./train1/dogs', file_name) , os.path.join('./training_set/dogs', file_name))\nfor file_name in traindogs[10000:]:\n    shutil.copy(os.path.join('./train1/dogs', file_name) , os.path.join('./val_set/dogs', file_name))","metadata":{"execution":{"iopub.status.busy":"2021-07-28T08:35:56.762704Z","iopub.execute_input":"2021-07-28T08:35:56.763026Z","iopub.status.idle":"2021-07-28T08:35:59.239853Z","shell.execute_reply.started":"2021-07-28T08:35:56.762998Z","shell.execute_reply":"2021-07-28T08:35:59.238924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(os.listdir('./training_set/cats/')))\nprint(len(os.listdir('./training_set/dogs/')))\nprint(len(os.listdir('./val_set/cats/')))\nprint(len(os.listdir('./val_set/dogs/')))","metadata":{"execution":{"iopub.status.busy":"2021-07-28T08:36:24.414915Z","iopub.execute_input":"2021-07-28T08:36:24.415328Z","iopub.status.idle":"2021-07-28T08:36:24.439325Z","shell.execute_reply.started":"2021-07-28T08:36:24.415293Z","shell.execute_reply":"2021-07-28T08:36:24.438512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nfrom matplotlib import pyplot\nfrom keras.utils import to_categorical\nfrom keras.applications.vgg16 import VGG16\nfrom keras.models import Model\nfrom keras.layers import Dense\nfrom keras.layers import Flatten\nfrom keras.layers import Dropout\nfrom keras.optimizers import Adam\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import CSVLogger\n# define cnn model\ndef define_model():\n    # load model\n    model = VGG16(include_top=False, input_shape=(224, 224, 3))\n    # mark loaded layers as not trainable\n    for layer in model.layers:\n        layer.trainable = False\n    # add new classifier layers\n    flat1 = Flatten()(model.layers[-1].output)\n    class1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1)\n    dropout = Dropout(0.5)(class1)\n    output = Dense(1, activation='sigmoid')(dropout)\n    # define new model\n    model = Model(inputs=model.inputs, outputs=output)\n    model.compile(optimizer=Adam(learning_rate = 0.001), loss='binary_crossentropy', metrics=['binary_accuracy'])\n    # compile model\n    return model\n \n# plot diagnostic learning curves\ndef summarize_diagnostics(history):\n    # plot loss\n    pyplot.subplot(211)\n    pyplot.title('Cross Entropy Loss')\n    pyplot.plot(history.history['loss'], color='blue', label='train')\n    pyplot.plot(history.history['val_loss'], color='orange', label='test')\n    # plot accuracy\n    pyplot.subplot(212)\n    pyplot.title('Classification Accuracy')\n    pyplot.plot(history.history['binary_accuracy'], color='blue', label='train')\n    pyplot.plot(history.history['val_binary_accuracy'], color='orange', label='test')\n    # save plot to file\n    filename = sys.argv[0].split('/')[-1]\n    pyplot.savefig(filename + '_plot.png')\n    pyplot.show()\n# run the test harness for evaluating a model\ndef run_test_harness():\n    model = define_model()\n    # create data generator\n    datagen = ImageDataGenerator(featurewise_center=True)\n    # specify imagenet mean values for centering\n    datagen.mean = [123.68, 116.779, 103.939]  \n    csv_logger = CSVLogger('training.log', separator=',', append=False)\n    # prepare iterator\n    train_it = datagen.flow_from_directory('./training_set',\n        class_mode='binary', batch_size=64, target_size=(224, 224))\n    test_it = datagen.flow_from_directory('./val_set/',\n        class_mode='binary', batch_size=64, target_size=(224, 224))\n    # fit model\n    history = model.fit(train_it, steps_per_epoch=len(train_it),\n        validation_data=test_it, validation_steps=len(test_it), epochs=10, verbose=1,callbacks=[csv_logger])\n    \n    _, acc = model.evaluate(test_it, steps=len(test_it), verbose=0)\n    print('> %.3f' % (acc * 100.0))\n    print(history.history)\n    # learning curves\n    summarize_diagnostics(history)\n    model.save('final_model.h5')\n    \n    \nrun_test_harness()","metadata":{"execution":{"iopub.status.busy":"2021-07-28T10:23:30.178432Z","iopub.execute_input":"2021-07-28T10:23:30.178818Z","iopub.status.idle":"2021-07-28T10:45:02.884311Z","shell.execute_reply.started":"2021-07-28T10:23:30.178787Z","shell.execute_reply":"2021-07-28T10:45:02.883487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir('./test1')[20:30]) # print the first ten file names in the training set","metadata":{"execution":{"iopub.status.busy":"2021-07-28T08:38:00.013399Z","iopub.execute_input":"2021-07-28T08:38:00.013763Z","iopub.status.idle":"2021-07-28T08:38:00.02694Z","shell.execute_reply.started":"2021-07-28T08:38:00.013732Z","shell.execute_reply":"2021-07-28T08:38:00.026046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom keras.preprocessing.image import load_img\nfrom matplotlib import pyplot\nfrom keras.preprocessing.image import img_to_array\nfrom keras.preprocessing.image import array_to_img\nfrom keras.models import load_model\nimg_file = './test1/8920.jpg'\ndef load_image(filename):\n    # load the image\n    img = load_img(filename, target_size=(224, 224))\n    # convert to array\n    img = img_to_array(img)\n    # reshape into a single sample with 3 channels\n    img = img.reshape(1, 224, 224, 3)\n    # center pixel data\n    img = img.astype('float32')\n    img = img - [123.68, 116.779, 103.939]\n    return img\nimg = load_image(img_file)\nmodel = load_model('./final_model.h5')\nresult = model.predict(img)\n\nimg_classes = ['cat','dog']\nprint(img_classes[int(result[0][0])])\n\nimg_test = load_img(img_file, target_size=(224, 224))\narr_ = np.squeeze(img_test) # you can give axis attribute if you wanna squeeze in specific dimension\npyplot.imshow(arr_)\npyplot.show()\n","metadata":{"execution":{"iopub.status.busy":"2021-07-28T10:47:32.758940Z","iopub.execute_input":"2021-07-28T10:47:32.759354Z","iopub.status.idle":"2021-07-28T10:47:33.329011Z","shell.execute_reply.started":"2021-07-28T10:47:32.759320Z","shell.execute_reply":"2021-07-28T10:47:33.328191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Confution Matrix and Classification Report\nfrom matplotlib import pyplot\nimport tensorflow as tf\nimport numpy as np\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.metrics import classification_report, confusion_matrix\nimport itertools\ndatagen = ImageDataGenerator(featurewise_center=True)\n    # specify imagenet mean values for centering\ndatagen.mean = [123.68, 116.779, 103.939]  \n\n# prepare iterator\n#train_it = datagen.flow_from_directory('./training_set',class_mode='binary', batch_size=64, target_size=(224, 224))\ntest_it = datagen.flow_from_directory('./val_set/', batch_size=10, target_size=(224, 224),shuffle=False)\nmodel = load_model('./final_model.h5')\n#Y_pred = model.predict_generator(test_it, len(test_it) // 64+1)\npredictions =model.predict(x=test_it,verbose=0)\n#print(test_it.classes[10:50])\n#print(predictions[0:10])\n#y_pred = np.argmax(predictions, axis=1)\n#print(predictions)\n#print(y_pred)\nprint('Confusion Matrix')\ncm = confusion_matrix(test_it.classes, np.round(predictions))\nprint(cm)\nprint('Classification Report')\ntarget_names = ['Cats', 'Dogs']\nprint(classification_report(test_it.classes, np.round(predictions), target_names=target_names))\ndef plot_confusion_matrix(cm, classes,\n                          normalize=False,\n                          title='Confusion matrix',\n                          cmap=pyplot.cm.Blues):\n    \"\"\"\n    This function prints and plots the confusion matrix.\n    Normalization can be applied by setting `normalize=True`.\n    \"\"\"\n    pyplot.imshow(cm, interpolation='nearest', cmap=cmap)\n    pyplot.title(title)\n    pyplot.colorbar()\n    tick_marks = np.arange(len(classes))\n    pyplot.xticks(tick_marks, classes, rotation=45)\n    pyplot.yticks(tick_marks, classes)\n\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n        print(\"Normalized confusion matrix\")\n    else:\n        print('Confusion matrix, without normalization')\n\n    print(cm)\n\n    thresh = cm.max() / 2.\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        pyplot.text(j, i, cm[i, j],\n            horizontalalignment=\"center\",\n            color=\"white\" if cm[i, j] > thresh else \"black\")\n\n    pyplot.tight_layout()\n    pyplot.ylabel('True label')\n    pyplot.xlabel('Predicted label')\n    \ncm_labels = [\"cat\",\"dog\"]\nplot_confusion_matrix(cm=cm,classes = cm_labels, title = \"CM\")","metadata":{"execution":{"iopub.status.busy":"2021-07-28T10:47:42.836342Z","iopub.execute_input":"2021-07-28T10:47:42.836740Z","iopub.status.idle":"2021-07-28T10:48:01.811965Z","shell.execute_reply.started":"2021-07-28T10:47:42.836705Z","shell.execute_reply":"2021-07-28T10:48:01.811197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-07-28T09:56:38.449501Z","iopub.status.idle":"2021-07-28T09:56:38.450431Z"},"trusted":true},"execution_count":null,"outputs":[]}]}