{"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":"# 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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-19T10:33:20.604690Z","iopub.execute_input":"2023-06-19T10:33:20.605213Z","iopub.status.idle":"2023-06-19T10:33:20.641759Z","shell.execute_reply.started":"2023-06-19T10:33:20.605116Z","shell.execute_reply":"2023-06-19T10:33:20.640823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil","metadata":{"execution":{"iopub.status.busy":"2023-06-19T10:33:20.643530Z","iopub.execute_input":"2023-06-19T10:33:20.643972Z","iopub.status.idle":"2023-06-19T10:33:20.649211Z","shell.execute_reply.started":"2023-06-19T10:33:20.643934Z","shell.execute_reply":"2023-06-19T10:33:20.648105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\npath='/kaggle/input/dogs-vs-cats'\nwork_dir='/kaggle/working'\nz= zipfile.ZipFile(path+'/train.zip')\nz.extractall()\ntrain_dir='/kaggle/working/train'\n","metadata":{"execution":{"iopub.status.busy":"2023-06-19T10:33:20.651064Z","iopub.execute_input":"2023-06-19T10:33:20.651530Z","iopub.status.idle":"2023-06-19T10:33:36.774807Z","shell.execute_reply.started":"2023-06-19T10:33:20.651486Z","shell.execute_reply":"2023-06-19T10:33:36.773769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"z2= zipfile.ZipFile(path+'/test1.zip')\nz2.extractall()\ntest_dir='/kaggle/working/test1'","metadata":{"execution":{"iopub.status.busy":"2023-06-19T10:33:36.777987Z","iopub.execute_input":"2023-06-19T10:33:36.779079Z","iopub.status.idle":"2023-06-19T10:33:44.113391Z","shell.execute_reply.started":"2023-06-19T10:33:36.779040Z","shell.execute_reply":"2023-06-19T10:33:44.111728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_dir=os.path.join(work_dir,'validation')\nos.mkdir(validation_dir)","metadata":{"execution":{"iopub.status.busy":"2023-06-19T10:33:44.114848Z","iopub.execute_input":"2023-06-19T10:33:44.115217Z","iopub.status.idle":"2023-06-19T10:33:44.122227Z","shell.execute_reply.started":"2023-06-19T10:33:44.115185Z","shell.execute_reply":"2023-06-19T10:33:44.120480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cat_dir=os.path.join(train_dir,'cat')\nos.mkdir(train_cat_dir)\n\ntrain_dog_dir=os.path.join(train_dir,'dog')\nos.mkdir(train_dog_dir)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-06-19T10:33:44.124032Z","iopub.execute_input":"2023-06-19T10:33:44.124625Z","iopub.status.idle":"2023-06-19T10:33:44.140384Z","shell.execute_reply.started":"2023-06-19T10:33:44.124575Z","shell.execute_reply":"2023-06-19T10:33:44.139084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_cat_dir=os.path.join(validation_dir,'cat')\nos.mkdir(validation_cat_dir)\n\nvalidation_dog_dir=os.path.join(validation_dir,'dog')\nos.mkdir(validation_dog_dir)","metadata":{"execution":{"iopub.status.busy":"2023-06-19T10:33:44.143682Z","iopub.execute_input":"2023-06-19T10:33:44.144149Z","iopub.status.idle":"2023-06-19T10:33:44.155409Z","shell.execute_reply.started":"2023-06-19T10:33:44.144112Z","shell.execute_reply":"2023-06-19T10:33:44.154038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_cat_dir=os.path.join(test_dir,'cat')\nos.mkdir(test_cat_dir)\n\ntest_dog_dir=os.path.join(test_dir,'dog')\nos.mkdir(test_dog_dir)","metadata":{"execution":{"iopub.status.busy":"2023-06-19T10:33:44.157869Z","iopub.execute_input":"2023-06-19T10:33:44.158399Z","iopub.status.idle":"2023-06-19T10:33:44.177201Z","shell.execute_reply.started":"2023-06-19T10:33:44.158347Z","shell.execute_reply":"2023-06-19T10:33:44.175624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#kopiujemy z test do validation\nfnames=['dog.{}.jpg'.format(i) for i in range(1000)]\nfor fname in fnames:\n    src=os.path.join(train_dir,fname)\n    dst=os.path.join(validation_dog_dir,fname)\n    shutil.copyfile(src,dst)\nfnames=['cat.{}.jpg'.format(i) for i in range(1000)]\nfor fname in fnames:\n    src=os.path.join(train_dir,fname)\n    dst=os.path.join(validation_cat_dir,fname)\n    shutil.copyfile(src,dst)","metadata":{"execution":{"iopub.status.busy":"2023-06-19T10:33:44.181410Z","iopub.execute_input":"2023-06-19T10:33:44.181812Z","iopub.status.idle":"2023-06-19T10:33:44.476552Z","shell.execute_reply.started":"2023-06-19T10:33:44.181777Z","shell.execute_reply":"2023-06-19T10:33:44.475282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for file in os.listdir(train_dir):\n    if file[-3]=='j':\n        if file[0]=='c':\n            os.replace(os.path.join(train_dir,file),os.path.join(train_cat_dir,file))\n        else:\n            os.replace(os.path.join(train_dir,file),os.path.join(train_dog_dir,file))","metadata":{"execution":{"iopub.status.busy":"2023-06-19T10:33:44.480843Z","iopub.execute_input":"2023-06-19T10:33:44.481244Z","iopub.status.idle":"2023-06-19T10:33:45.326488Z","shell.execute_reply.started":"2023-06-19T10:33:44.481210Z","shell.execute_reply":"2023-06-19T10:33:45.325082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_train_len=len(os.listdir(train_cat_dir))\nprint(cat_train_len)\n\ndog_train_len=len(os.listdir(train_dog_dir))\nprint(dog_train_len)","metadata":{"execution":{"iopub.status.busy":"2023-06-19T10:33:45.328189Z","iopub.execute_input":"2023-06-19T10:33:45.328569Z","iopub.status.idle":"2023-06-19T10:33:45.356102Z","shell.execute_reply.started":"2023-06-19T10:33:45.328535Z","shell.execute_reply":"2023-06-19T10:33:45.354262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"tworzenie sieci ","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras import layers,models,optimizers","metadata":{"execution":{"iopub.status.busy":"2023-06-19T10:33:45.358341Z","iopub.execute_input":"2023-06-19T10:33:45.358788Z","iopub.status.idle":"2023-06-19T10:33:53.529827Z","shell.execute_reply.started":"2023-06-19T10:33:45.358751Z","shell.execute_reply":"2023-06-19T10:33:53.528344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=models.Sequential()\nmodel.add(layers.Conv2D(32,(3,3),activation='relu',input_shape=(150,150,3)))\n\nmodel.add(layers.MaxPool2D((2,2)))\nmodel.add(layers.Conv2D(64,(3,3),activation='relu'))\n\nmodel.add(layers.MaxPool2D((2,2)))\nmodel.add(layers.Conv2D(128,(3,3),activation='relu'))\n\nmodel.add(layers.MaxPool2D((2,2)))\nmodel.add(layers.Conv2D(128,(3,3),activation='relu'))\n\nmodel.add(layers.MaxPool2D((2,2)))\nmodel.add(layers.Flatten())\n\nmodel.add(layers.Dense(512,activation='relu'))\nmodel.add(layers.Dense(1,activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2023-06-19T10:33:53.531420Z","iopub.execute_input":"2023-06-19T10:33:53.532137Z","iopub.status.idle":"2023-06-19T10:33:53.785979Z","shell.execute_reply.started":"2023-06-19T10:33:53.532098Z","shell.execute_reply":"2023-06-19T10:33:53.784942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='rmsprop',\n    loss='binary_crossentropy',\n    metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-06-19T10:33:53.788060Z","iopub.execute_input":"2023-06-19T10:33:53.788948Z","iopub.status.idle":"2023-06-19T10:33:53.808833Z","shell.execute_reply.started":"2023-06-19T10:33:53.788899Z","shell.execute_reply":"2023-06-19T10:33:53.806452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2023-06-19T10:33:53.810486Z","iopub.execute_input":"2023-06-19T10:33:53.810966Z","iopub.status.idle":"2023-06-19T10:33:53.818496Z","shell.execute_reply.started":"2023-06-19T10:33:53.810928Z","shell.execute_reply":"2023-06-19T10:33:53.816943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen=ImageDataGenerator(rescale=1./255)#przeskalowanie 1/255\nvalidation_datagen=ImageDataGenerator(rescale=1./255)#przeskalowanie 1/255\n\ntrain_generator=train_datagen.flow_from_directory(\n    train_dir,\n    target_size=(150,150),\n    batch_size=20,\n    class_mode='binary')\n\nvalidation_generator=validation_datagen.flow_from_directory(\n    train_dir,\n    target_size=(150,150),\n    batch_size=20,\n    class_mode='binary')","metadata":{"execution":{"iopub.status.busy":"2023-06-19T10:33:53.821133Z","iopub.execute_input":"2023-06-19T10:33:53.822183Z","iopub.status.idle":"2023-06-19T10:33:55.651039Z","shell.execute_reply.started":"2023-06-19T10:33:53.822130Z","shell.execute_reply":"2023-06-19T10:33:55.649428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit_generator(\n    train_generator,\n    steps_per_epoch=100,\n    epochs=30,\n    validation_data=validation_generator,\n    validation_steps=50)\n#     verbose=0)\nmodel.save('cat_and_dog.h5')","metadata":{"execution":{"iopub.status.busy":"2023-06-19T10:33:55.652943Z","iopub.execute_input":"2023-06-19T10:33:55.653483Z","iopub.status.idle":"2023-06-19T11:09:30.008597Z","shell.execute_reply.started":"2023-06-19T10:33:55.653431Z","shell.execute_reply":"2023-06-19T11:09:30.007243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#image data generator\ndatagen=ImageDataGenerator( \n                            rotation_range=40,\n                            width_shift_range=0.2,\n                            height_shift_range=0.2,\n                            shear_range=0.2,\n                            zoom_range=0.2,\n                            horizontal_flip=True,\n                            fill_mode='nearest')","metadata":{"execution":{"iopub.status.busy":"2023-06-19T11:09:30.010931Z","iopub.execute_input":"2023-06-19T11:09:30.011334Z","iopub.status.idle":"2023-06-19T11:09:30.019383Z","shell.execute_reply.started":"2023-06-19T11:09:30.011291Z","shell.execute_reply":"2023-06-19T11:09:30.017708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing import image #edycja obrazów","metadata":{"execution":{"iopub.status.busy":"2023-06-19T11:09:30.020686Z","iopub.execute_input":"2023-06-19T11:09:30.021576Z","iopub.status.idle":"2023-06-19T11:09:30.035367Z","shell.execute_reply.started":"2023-06-19T11:09:30.021533Z","shell.execute_reply":"2023-06-19T11:09:30.033793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frames=[os.path.join(train_cat_dir,fname) for fname in os.listdir(train_cat_dir)]\nimg_path=frames[3]\nimg=image.load_img(img_path, target_size=(150,150))\nx=image.img_to_array(img)\nx=x.reshape((1,)+x.shape)\n\ni=0\nfor batch in datagen.flow(x,batch_size=1):\n    plt.figure(i)\n    imgplot=plt.imshow(image.array_to_img(batch[0]))\n    i+=1\n    if i%4==0:break\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-06-19T11:09:30.037050Z","iopub.execute_input":"2023-06-19T11:09:30.037537Z","iopub.status.idle":"2023-06-19T11:09:31.024050Z","shell.execute_reply.started":"2023-06-19T11:09:30.037504Z","shell.execute_reply":"2023-06-19T11:09:31.022825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# nowy model z dropout\nmodel=models.Sequential()\nmodel.add(layers.Conv2D(32,(3,3),activation='relu',input_shape=(150,150,3)))\n\nmodel.add(layers.MaxPool2D((2,2)))\nmodel.add(layers.Conv2D(64,(3,3),activation='relu'))\n\nmodel.add(layers.MaxPool2D((2,2)))\nmodel.add(layers.Conv2D(128,(3,3),activation='relu'))\n\nmodel.add(layers.MaxPool2D((2,2)))\nmodel.add(layers.Conv2D(128,(3,3),activation='relu'))\n\nmodel.add(layers.MaxPool2D((2,2)))\nmodel.add(layers.Flatten())\n\nmodel.add(layers.Dropout(0.5))\n\nmodel.add(layers.Dense(512,activation='relu'))\nmodel.add(layers.Dense(1,activation='sigmoid'))\n\n#kompilacja\nmodel.compile(optimizer='rmsprop',\n    loss='binary_crossentropy',\n    metrics=['accuracy'])\n\ntrain_datagen=ImageDataGenerator(rescale=1./255,\n                                rotation_range=40,\n                                width_shift_range=0.2,\n                                height_shift_range=0.2,\n                                shear_range=0.2,\n                                zoom_range=0.2,\n                                horizontal_flip=True)\n\ntest_datagem=ImageDataGenerator(rescale=1./255)\n\n\ntrain_generator=train_datagen.flow_from_directory(\n    train_dir,\n    target_size=(150,150),\n    batch_size=32,\n    class_mode='binary')\n\nvalidation_generator=validation_datagen.flow_from_directory(\n    train_dir,\n    target_size=(150,150),\n    batch_size=32,\n    class_mode='binary')\n\n#trening\nhistory=model.fit_generator(\n    train_generator,\n    steps_per_epoch=100,\n    epochs=100,\n    validation_data=validation_generator,\n    validation_steps=50,\n    verbose=0)\nmodel.save('cat_and_dog_2.h5')","metadata":{"execution":{"iopub.status.busy":"2023-06-19T11:09:31.026093Z","iopub.execute_input":"2023-06-19T11:09:31.027298Z","iopub.status.idle":"2023-06-19T13:48:24.773349Z","shell.execute_reply.started":"2023-06-19T11:09:31.027211Z","shell.execute_reply":"2023-06-19T13:48:24.771960Z"},"trusted":true},"execution_count":null,"outputs":[]}]}