{"cells":[{"metadata":{"trusted":true,"_uuid":"713f6d706d72771db0a1011ff13e98864f6d7788"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"888c6551e004535294c0e9ee2a9055f88d75de7c"},"cell_type":"code","source":"import glob\nimport shutil\nimport os\n\ninput_dir = \"../input/train/\"\ndest_dir = \"../input/\"\ntrain_cate = 'train/cate/'\ntrain_doge = 'train/doge/'\nvalid_cate = 'validate/cate/'\nvalid_doge = 'validate/doge/'\n    \nif not os.path.exists(train_cate):\n    os.makedirs(train_cate)\n    os.makedirs(train_doge)\n    \nif not os.path.exists(valid_cate):\n    os.makedirs(valid_cate)\n    os.makedirs(valid_doge)\n\nfor file in os.listdir(\"../input/train/\"):\n    if \"dog\" in file:\n        shutil.copyfile(input_dir+file, train_doge+file)\n    if \"cat\" in file:\n        shutil.copyfile(input_dir+file, train_cate+file)\n\nprint('files copied!')\n        \ndoge_Files = os.listdir(train_doge)\nfor file in doge_Files[0:2000]:\n    shutil.move(train_doge+file, valid_doge+file)\n    \ncate_Files = os.listdir(train_cate)\nfor file in cate_Files[0:2000]:\n    shutil.move(train_cate+file, valid_cate+file)\n    \nprint('Validation files separated!')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3de7518a0b4db858b92be075eed32b2109de2dcd"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"83ddaa35a49c5caecc4c27461ed3494c0c3ab3db"},"cell_type":"code","source":"import tensorflow as tf\nimport os\nimport cv2 as cv\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D\nfrom keras.layers import Activation, Dropout, Flatten, Dense\nfrom keras import backend as K\n\nfrom keras.backend.tensorflow_backend import set_session  \nconfig = tf.ConfigProto()  \nconfig.gpu_options.allow_growth = True  \nset_session(tf.Session(config=config)) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9fedd6810883ae6af50c1cf56021f1d9b84850dc"},"cell_type":"code","source":"# dimensions of our images.\nimg_width, img_height = 450, 450\n\n#train_data_dir = 'D:/NeuralNetworkData/MammographyImges/train/'\n#validation_data_dir = 'D:/NeuralNetworkData/MammographyImges/validate/'\n\ntrain_data_dir = 'train'\nvalidation_data_dir = 'validate'\nepochs = 5\nbatch_size = 16\n\nif K.image_data_format() == 'channels_first':\n    input_shape = (3, img_width, img_height)\nelse:\n    input_shape = (img_width, img_height, 3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"254414db46736a25d760043da60357a8d2192547"},"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32, (3, 3), input_shape=input_shape))\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Conv2D(32, (3, 3)))\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Conv2D(64, (3, 3)))\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(64))\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1))\nmodel.add(Activation('sigmoid'))\n\n\nmodel.compile(loss='binary_crossentropy',\n              optimizer='rmsprop',\n              metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"#https://towardsdatascience.com/keras-a-thing-you-should-know-about-keras-if-you-plan-to-train-a-deep-learning-model-on-a-large-fdd63ce66bd2\n#https://medium.com/@parthvadhadiya424/hello-world-program-in-keras-with-cnn-dog-vs-cat-classification-efc6f0da3cc5\n#https://towardsdatascience.com/build-your-first-deep-learning-classifier-using-tensorflow-dog-breed-example-964ed0689430\n\ntrain_datagen = tf.keras.preprocessing.image.ImageDataGenerator(\n        rescale=1./255,\n        shear_range=0.1,\n        zoom_range=0.1,\n        horizontal_flip=True)\n\ntest_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)\n\ntrain_generator = train_datagen.flow_from_directory(\n        train_data_dir,\n        target_size=(img_width, img_height),\n        batch_size=batch_size,\n        class_mode='binary')\n\nvalidation_generator = test_datagen.flow_from_directory(\n        validation_data_dir,\n        target_size=(img_width, img_height),\n        batch_size=batch_size,\n        class_mode='binary')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"edbb15c4746061e6f5b29c6220385465ebc1e538"},"cell_type":"code","source":"tbCallBack = tf.keras.callbacks.TensorBoard(log_dir='',\n                                            histogram_freq=0,\n                                            write_graph=True,\n                                            write_images=True)\n\nhistory = model.fit_generator(\n            train_generator,\n            steps_per_epoch=250,\n            epochs=epochs,\n            validation_data=validation_generator,\n            validation_steps=250,\n            callbacks=[tbCallBack])\n\nmodel.save('doggo_cate.h5')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8eb023ddbad3eff1a8a20426d0ea93563bfa8705"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}