{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport tensorflow as tf\nfrom cv2 import cv2\nimport zipfile\nimport os\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"TEST_DIR = '../input/dogs-vs-cats-redux-kernels-edition/test.zip'\nTRAIN_DIR = '../input/dogs-vs-cats-redux-kernels-edition/train.zip'","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"now we extract the images (i have no idea on how to work with elements of zip files without extracting them)","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"#you can execute this  only once\nwith zipfile.ZipFile(TRAIN_DIR,'r') as trainfile:\n    trainfile.extractall()\nwith zipfile.ZipFile(TEST_DIR,'r') as trainfile:\n    trainfile.extractall()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now lets see if the files are extracted or not","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"testdir ='test/'\ntraindir = 'train/'","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"now lets get the full path of train images and test images","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"test_images = [testdir+i for i in os.listdir(testdir)]\nall_images = [traindir+i for i in os.listdir(traindir)]\n\nlimit = int( 0.8* len(all_images))\n\ntrain_images = all_images[0:limit]\nvalidation_images = all_images[limit:]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"small visualization","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"img = cv2.imread(train_images[1])\nplt.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"lets create a function which will read the image using opencv library","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"rows, columns = 160,160","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def getallimages(path):\n    actualdata = np.ndarray((len(path),rows,columns,3),dtype=np.uint8)\n    for index , file in enumerate(path):\n        img = cv2.imread(file)\n        img= cv2.resize(img, (rows, columns), interpolation=cv2.INTER_CUBIC)\n#         img = tf.cast(img,tf.float32)\n#         img = (img/127.5) - 1\n#         img = tf.image.resize(img, (rows, columns))\n        actualdata[index] = img\n    return actualdata\ntrain = getallimages(train_images)\ntest = getallimages(test_images)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"validation = getallimages(validation_images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"we need some labels too so lets add y which will be our label ","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"label = [1 if 'dog' in i else 0 for i in train_images]\nvalidation_label = [1 if 'dog' in i else 0 for i in validation_images]\n\nvalidation_label[:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_shape = (rows,rows,3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"type(train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"base_model = tf.keras.applications.ResNet101(\n    weights = 'imagenet', include_top=False, input_shape=image_shape)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**We freeze the base layer ie its weights are not going to be retraning the model**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"base_model.trainable=False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"base_model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = tf.keras.Sequential([\n    base_model,\n    tf.keras.layers.GlobalAveragePooling2D(),\n   \n    tf.keras.layers.Dense(1,activation='sigmoid')\n    \n])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"we will create a training batch now\n","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# train_dataset = tf.data.Dataset.from_tensor_slices((train,label))\n# validation_ds = tf.data.Dataset.from_tensor_slices((validation,validation_label))\n\n# BATCH_SIZE = 64\n# SHUFFLE_BUFFER_SIZE = 10000\n# validation_batches = validation_ds.batch(BATCH_SIZE)\n# train_dataset = train_dataset.shuffle(SHUFFLE_BUFFER_SIZE).batch(BATCH_SIZE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"base_learning_rate = 0.001\n# you can use this tootf.keras.optimizers.RMSprop(learning_rate=base_learning_rate\nmodel.compile(optimizer='adam',\n              loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),\n              metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"epochs = 10\nvalidation_steps=20\n\n# model.evaluate(validation,validation_label, steps = validation_steps)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(x=np.array(train),y=np.array(label),validation_data=(np.array(validation),np.array(validation_label)) ,batch_size=128,epochs=epochs,shuffle=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"prediction  = model.predict_proba(test,verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.xlabel(prediction[4][0])\nplt.imshow(test[4])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# model.save('dgvscat.h5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":" \ntest_id = [i.split('/')[1][:-4] for i in test_images]\n\npredictions_df = pd.DataFrame({'id': test_id, 'label': prediction[:,0]})\npredictions_df\npredictions_df.to_csv(\"submission.csv\", index=False,header=True)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}