{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"## course work: https://www.coursera.org/learn/convolutional-neural-networks-tensorflow/supplement/4ijuU/the-cats-vs-dogs-dataset\n\n# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport random\nimport tensorflow as tf\nfrom tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras.preprocessing import image\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nmodel = tf.keras.models.Sequential([\n# Note the input shape is the desired size of the image 150x150 with 3 bytes color\n    tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(150, 150, 3)),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Conv2D(32, (3,3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2), \n    tf.keras.layers.Conv2D(64, (3,3), activation='relu'), \n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Conv2D(128, (3,3), activation='relu'), \n    tf.keras.layers.MaxPooling2D(2,2),\n    # Flatten the results to feed into a DNN\n    tf.keras.layers.Flatten(), \n    # 512 neuron hidden layer\n    tf.keras.layers.Dense(512, activation='relu'), \n    # Only 1 output neuron. It will contain a value from 0-1 where 0 for 1 class ('cats') and 1 for the other ('dogs')\n    tf.keras.layers.Dense(1, activation='sigmoid')  \n])\n\nmodel.compile(optimizer=RMSprop(lr=0.001), loss='binary_crossentropy', metrics=['acc'])\n\nTRAINING_DIR = '../input/train' \ntrain_datagen = ImageDataGenerator(rescale=1.0/225.,rotation_range=40, width_shift_range=0.2,height_shift_range=0.2,shear_range=0.2, zoom_range=0.2,horizontal_flip=True,fill_mode='nearest')\ntrain_generator = train_datagen.flow_from_directory(TRAINING_DIR,batch_size=20,class_mode='binary',target_size=(150, 150)) \nVALIDATION_DIR = '../input/test1' \nvalidation_datagen = ImageDataGenerator( rescale = 1.0/255.,rotation_range=40, width_shift_range=0.2,height_shift_range=0.2,shear_range=0.2, zoom_range=0.2,horizontal_flip=True,fill_mode='nearest')\nvalidation_generator = validation_datagen.flow_from_directory(VALIDATION_DIR,batch_size=20,class_mode  = 'binary',target_size = (150, 150))\n\nhistory = model.fit_generator(train_generator,epochs=5,verbose=1,validation_data=validation_generator)\n\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"count=0;\nclasses=np.array(['id','label'])\nfor i in os.listdir('../input/test1/test1/'):\n    img = image.load_img('../input/test1/test1/'+i, target_size=(150,150))\n    x = image.img_to_array(img)\n    x = np.expand_dims(x, axis=0)\n    images=np.vstack([x])\n    resultt = model.predict(images)\n    if resultt>0.5:\n        print('1')\n        classes = np.vstack((classes, [count+1,1]))\n    else:\n        print('0')\n        classes = np.vstack((classes, [count+1,0]))\n    \n    count=count+1;\n\n    output=pd.DataFrame(data=classes[1:,1:],index=classes[1:,0],columns=classes[0,1:])  # 1st row as the column names\n    \n    output.to_csv('../sampleSubmission.csv')\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}