{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":5441,"databundleVersionId":38425,"sourceType":"competition"},{"sourceId":1003830,"sourceType":"datasetVersion","datasetId":550917},{"sourceId":4748798,"sourceType":"datasetVersion","datasetId":2716794}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport keras\nimport warnings\nfrom keras import layers\nfrom keras.models import Model\nfrom tensorflow.keras.applications import vgg19\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom random import shuffle\n\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2024-11-12T20:14:34.423254Z","iopub.execute_input":"2024-11-12T20:14:34.423763Z","iopub.status.idle":"2024-11-12T20:14:46.832222Z","shell.execute_reply.started":"2024-11-12T20:14:34.423709Z","shell.execute_reply":"2024-11-12T20:14:46.831238Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\ndevice = 'cuda' if torch.cuda.is_available() else 'cpu'\nprint(f\"Using device: {device}\")","metadata":{"execution":{"iopub.status.busy":"2024-11-12T20:15:25.606544Z","iopub.execute_input":"2024-11-12T20:15:25.607174Z","iopub.status.idle":"2024-11-12T20:15:28.969756Z","shell.execute_reply.started":"2024-11-12T20:15:25.607136Z","shell.execute_reply":"2024-11-12T20:15:28.968777Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for dirname ,_,_ in os.walk('/kaggle/working'):\n    print(dirname)","metadata":{"execution":{"iopub.status.busy":"2024-11-12T20:15:28.971469Z","iopub.execute_input":"2024-11-12T20:15:28.973124Z","iopub.status.idle":"2024-11-12T20:15:28.980916Z","shell.execute_reply.started":"2024-11-12T20:15:28.973087Z","shell.execute_reply":"2024-11-12T20:15:28.979836Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"try:\n    os.mkdir('/kaggle/working/cats-n-dogs')\nexcept OSError:\n    print('Failed to create folder')","metadata":{"execution":{"iopub.status.busy":"2024-11-12T20:15:31.820091Z","iopub.execute_input":"2024-11-12T20:15:31.820959Z","iopub.status.idle":"2024-11-12T20:15:31.825398Z","shell.execute_reply.started":"2024-11-12T20:15:31.820919Z","shell.execute_reply":"2024-11-12T20:15:31.824378Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import zipfile\nfrom pathlib import Path","metadata":{"execution":{"iopub.status.busy":"2024-11-12T20:15:45.426932Z","iopub.execute_input":"2024-11-12T20:15:45.427290Z","iopub.status.idle":"2024-11-12T20:15:45.431498Z","shell.execute_reply.started":"2024-11-12T20:15:45.427257Z","shell.execute_reply":"2024-11-12T20:15:45.430474Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Set the source and target directory\nzip_path = Path('/kaggle/input/dogs-vs-cats-redux-kernels-edition/')\nout_path = Path('/kaggle/working/cats-n-dogs')\n\nzip_files = [\"test.zip\", \"train.zip\"]\n\n# Open the Zip file\nfor file in zip_files:\n    with zipfile.ZipFile(os.path.join(zip_path,file),'r') as archive:\n        archive.extractall(out_path)\n    # Print a message indicating that the extraction is complete\n    print(f'Extracted contents from {file} to {out_path}/{file.split(\".\")[0]} directory.')","metadata":{"execution":{"iopub.status.busy":"2024-11-12T20:15:47.584454Z","iopub.execute_input":"2024-11-12T20:15:47.585169Z","iopub.status.idle":"2024-11-12T20:16:01.461297Z","shell.execute_reply.started":"2024-11-12T20:15:47.585131Z","shell.execute_reply":"2024-11-12T20:16:01.460228Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#check files location\n\nfor dirname,_,filenames in os.walk('/kaggle/working/cats-n-dogs'):\n    print(dirname,\": \", len(filenames))","metadata":{"execution":{"iopub.status.busy":"2024-11-12T20:16:04.982578Z","iopub.execute_input":"2024-11-12T20:16:04.983429Z","iopub.status.idle":"2024-11-12T20:16:05.032008Z","shell.execute_reply.started":"2024-11-12T20:16:04.983390Z","shell.execute_reply":"2024-11-12T20:16:05.031108Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#create seperate subfolder for eaсh class of train images\n\nimport shutil\n\ntrain_dir = '/kaggle/working/cats-n-dogs/train'\n\nkeywrd_1 = 'cat'\nkeywrd_2 = 'dog'\n        \ntrain_files = os.listdir(train_dir)\n\ntry:\n    os.mkdir(os.path.join(train_dir, keywrd_1))\n    os.mkdir(os.path.join(train_dir, keywrd_2))\nexcept OSError:\n    print('Failed to create folder.')\nexcept FileExistsError:\n    print('File/Folder already exists.')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T20:16:07.488320Z","iopub.execute_input":"2024-11-12T20:16:07.488925Z","iopub.status.idle":"2024-11-12T20:16:07.509073Z","shell.execute_reply.started":"2024-11-12T20:16:07.488887Z","shell.execute_reply":"2024-11-12T20:16:07.508358Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# move images to created folders\n\nfor file in train_files:\n    if \"jpg\" in file:  \n        if keywrd_1 in file:\n            shutil.move(os.path.join(train_dir, file), os.path.join(train_dir, keywrd_1, file))\n        elif keywrd_2 in file:\n            shutil.move(os.path.join(train_dir, file), os.path.join(train_dir, keywrd_2, file))","metadata":{"execution":{"iopub.status.busy":"2024-11-12T20:16:11.364538Z","iopub.execute_input":"2024-11-12T20:16:11.364939Z","iopub.status.idle":"2024-11-12T20:16:12.217061Z","shell.execute_reply.started":"2024-11-12T20:16:11.364901Z","shell.execute_reply":"2024-11-12T20:16:12.216082Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#create subfolder for test images for ImageDataGenerator\n\ntest_dir = '/kaggle/working/cats-n-dogs/test'\n\ntry:\n    os.mkdir(os.path.join(test_dir, \"test\"))\nexcept OSError:\n    print('Failed to create folder.')\nexcept FileExistsError:\n    print('File/Folder already exists.')\n    \ntest_files = os.listdir(test_dir)","metadata":{"execution":{"iopub.status.busy":"2024-11-12T20:16:13.534327Z","iopub.execute_input":"2024-11-12T20:16:13.535223Z","iopub.status.idle":"2024-11-12T20:16:13.547119Z","shell.execute_reply.started":"2024-11-12T20:16:13.535181Z","shell.execute_reply":"2024-11-12T20:16:13.546304Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# move images to created subfolder\n\nfor file in test_files:\n    if \"jpg\" in file: \n        shutil.move(os.path.join(test_dir, file), os.path.join(test_dir,\"test\", file))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T20:16:22.744700Z","iopub.execute_input":"2024-11-12T20:16:22.745361Z","iopub.status.idle":"2024-11-12T20:16:23.169043Z","shell.execute_reply.started":"2024-11-12T20:16:22.745321Z","shell.execute_reply":"2024-11-12T20:16:23.168273Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_files = os.listdir(os.path.join(test_dir,'test'))\n# test_files = glob('/kaggle/working/cats-n-dogs/test/*/*.jpg')\nlen(test_files)","metadata":{"execution":{"iopub.status.busy":"2024-11-12T20:16:25.072586Z","iopub.execute_input":"2024-11-12T20:16:25.073009Z","iopub.status.idle":"2024-11-12T20:16:25.087227Z","shell.execute_reply.started":"2024-11-12T20:16:25.072973Z","shell.execute_reply":"2024-11-12T20:16:25.086304Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_path = '/kaggle/working/cats-n-dogs/train'\ntest_path = '/kaggle/working/cats-n-dogs/test'","metadata":{"execution":{"iopub.status.busy":"2024-11-12T23:57:47.176730Z","iopub.execute_input":"2024-11-12T23:57:47.177371Z","iopub.status.idle":"2024-11-12T23:57:47.181552Z","shell.execute_reply.started":"2024-11-12T23:57:47.177333Z","shell.execute_reply":"2024-11-12T23:57:47.180587Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":" #Create sets of augmented images\n\ntrain_datagen = ImageDataGenerator(\n        rescale= 1./ 255.,\n        # rotation_range=20,\n        # zoom_range=0.05,\n        # horizontal_flip=True,\n        # fill_mode=\"nearest\",\n        validation_split=0.20)\n\nval_datagen = ImageDataGenerator(\n        rescale=1./255.,\n        validation_split=0.20)\n\ntest_datagen = ImageDataGenerator(\n            rescale=1./255.)","metadata":{"execution":{"iopub.status.busy":"2024-11-12T22:41:50.711076Z","iopub.execute_input":"2024-11-12T22:41:50.711770Z","iopub.status.idle":"2024-11-12T22:41:50.717157Z","shell.execute_reply.started":"2024-11-12T22:41:50.711725Z","shell.execute_reply":"2024-11-12T22:41:50.716078Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"batch_size = 32\nimg_shape = (224,224,3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T23:05:07.311489Z","iopub.execute_input":"2024-11-12T23:05:07.311896Z","iopub.status.idle":"2024-11-12T23:05:07.316416Z","shell.execute_reply.started":"2024-11-12T23:05:07.311857Z","shell.execute_reply":"2024-11-12T23:05:07.315469Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_gen = train_datagen.flow_from_directory(\n    directory=train_path,\n    target_size=img_shape[:2],\n    color_mode=\"rgb\",\n    batch_size= batch_size,\n    class_mode=\"binary\",\n    subset='training',\n    shuffle=True,\n    seed=42\n)\nval_gen = val_datagen.flow_from_directory(\n    directory=train_path,\n    target_size= img_shape[:2],\n    color_mode=\"rgb\",\n    batch_size= batch_size,\n    class_mode=\"binary\",\n    subset='validation',\n    shuffle=True,\n    seed=42\n)\ntest_gen = test_datagen.flow_from_directory(\n    directory=test_path,\n    target_size=img_shape[:2],\n    color_mode=\"rgb\",\n    batch_size= batch_size,\n    class_mode='binary',\n    shuffle=False,\n    seed=42\n)","metadata":{"execution":{"iopub.status.busy":"2024-11-12T23:05:08.684036Z","iopub.execute_input":"2024-11-12T23:05:08.684420Z","iopub.status.idle":"2024-11-12T23:05:09.714061Z","shell.execute_reply.started":"2024-11-12T23:05:08.684382Z","shell.execute_reply":"2024-11-12T23:05:09.713107Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#display random set of train images\n\npic_set = np.random.choice(train_files,10).tolist()\nfor i, pic in enumerate (pic_set):\n    pic = pic.split(\".\")[0] + \"/\" + pic\n    pic_set[i]= pic\n\nfig = plt.figure(figsize=(14, 7))\nfor i, pic in enumerate(pic_set, start=1):\n    subplot = fig.add_subplot(2, 5, i)\n    subplot.set_title('%s' % pic.split(\"/\")[1])\n    img = cv2.imread(os.path.join(train_path,pic))\n    img = cv2.resize(img, img_shape[:2])\n    plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2024-11-12T23:47:54.788008Z","iopub.execute_input":"2024-11-12T23:47:54.788851Z","iopub.status.idle":"2024-11-12T23:47:56.737788Z","shell.execute_reply.started":"2024-11-12T23:47:54.788810Z","shell.execute_reply":"2024-11-12T23:47:56.736843Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"base_model = vgg19.VGG19 (include_top = False, \n                              weights = 'imagenet', \n                              input_shape=(img_shape))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T22:41:06.453987Z","iopub.execute_input":"2024-11-12T22:41:06.454823Z","iopub.status.idle":"2024-11-12T22:41:06.766580Z","shell.execute_reply.started":"2024-11-12T22:41:06.454783Z","shell.execute_reply":"2024-11-12T22:41:06.765598Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"base_model.trainable=False","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T22:41:09.306668Z","iopub.execute_input":"2024-11-12T22:41:09.307043Z","iopub.status.idle":"2024-11-12T22:41:09.311880Z","shell.execute_reply.started":"2024-11-12T22:41:09.307008Z","shell.execute_reply":"2024-11-12T22:41:09.310981Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = keras.models.Sequential([\n    base_model,\n    layers.Flatten(),\n    layers.Dense(1, activation='sigmoid')], \n    name= base_model.name)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T23:04:20.476466Z","iopub.execute_input":"2024-11-12T23:04:20.476875Z","iopub.status.idle":"2024-11-12T23:04:20.484187Z","shell.execute_reply.started":"2024-11-12T23:04:20.476839Z","shell.execute_reply":"2024-11-12T23:04:20.483317Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer='adam', \n              loss='binary_crossentropy',  # функция потерь binary_crossentropy (log loss)\n              metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T23:04:24.000923Z","iopub.execute_input":"2024-11-12T23:04:24.001923Z","iopub.status.idle":"2024-11-12T23:04:24.012655Z","shell.execute_reply.started":"2024-11-12T23:04:24.001870Z","shell.execute_reply":"2024-11-12T23:04:24.011656Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T23:04:27.926884Z","iopub.execute_input":"2024-11-12T23:04:27.927593Z","iopub.status.idle":"2024-11-12T23:04:27.946373Z","shell.execute_reply.started":"2024-11-12T23:04:27.927551Z","shell.execute_reply":"2024-11-12T23:04:27.945567Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from keras.callbacks import ReduceLROnPlateau, EarlyStopping","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T23:24:23.045873Z","iopub.execute_input":"2024-11-12T23:24:23.046687Z","iopub.status.idle":"2024-11-12T23:24:23.051660Z","shell.execute_reply.started":"2024-11-12T23:24:23.046641Z","shell.execute_reply":"2024-11-12T23:24:23.050666Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learning_rate_reduction = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=2, min_lr=1e-6)\nearly_stoping = EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T23:24:24.178179Z","iopub.execute_input":"2024-11-12T23:24:24.178558Z","iopub.status.idle":"2024-11-12T23:24:24.183999Z","shell.execute_reply.started":"2024-11-12T23:24:24.178520Z","shell.execute_reply":"2024-11-12T23:24:24.182968Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fit_model = model.fit(train_gen,\n            validation_data = val_gen,\n            callbacks=[early_stoping,learning_rate_reduction],\n            epochs=15,\n            steps_per_epoch = 200,\n            validation_steps = 100)\n            # steps_per_epoch = train_gen.n//train_gen.batch_size,\n            # validation_steps = val_gen.n//val_gen.batch_size)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T23:31:32.131901Z","iopub.execute_input":"2024-11-12T23:31:32.132652Z","iopub.status.idle":"2024-11-12T23:36:54.567165Z","shell.execute_reply.started":"2024-11-12T23:31:32.132590Z","shell.execute_reply":"2024-11-12T23:36:54.566309Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(1, figsize = (15,8)) \n    \nplt.subplot(221)  \nplt.plot(fit_model.history['accuracy'])  \nplt.plot(fit_model.history['val_accuracy'])  \nplt.title('model accuracy')  \nplt.ylabel('accuracy')  \nplt.xlabel('epoch')  \nplt.legend(['train', 'valid']) \n    \nplt.subplot(222)  \nplt.plot(fit_model.history['loss'])  \nplt.plot(fit_model.history['val_loss'])  \nplt.title('model loss')  \nplt.ylabel('loss')  \nplt.xlabel('epoch')  \nplt.legend(['train', 'valid']) \n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T23:38:13.870297Z","iopub.execute_input":"2024-11-12T23:38:13.871188Z","iopub.status.idle":"2024-11-12T23:38:14.373419Z","shell.execute_reply.started":"2024-11-12T23:38:13.871148Z","shell.execute_reply":"2024-11-12T23:38:14.372554Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_pred = model.predict(test_gen, steps=test_gen.n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T23:38:30.256165Z","iopub.execute_input":"2024-11-12T23:38:30.257007Z","iopub.status.idle":"2024-11-12T23:39:26.701403Z","shell.execute_reply.started":"2024-11-12T23:38:30.256967Z","shell.execute_reply":"2024-11-12T23:39:26.700657Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"res = list(zip(sorted(test_files), test_pred))\nlen(res)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T23:42:56.290151Z","iopub.execute_input":"2024-11-12T23:42:56.291069Z","iopub.status.idle":"2024-11-12T23:42:56.308550Z","shell.execute_reply.started":"2024-11-12T23:42:56.291025Z","shell.execute_reply":"2024-11-12T23:42:56.307505Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import random","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T23:42:59.441557Z","iopub.execute_input":"2024-11-12T23:42:59.441954Z","iopub.status.idle":"2024-11-12T23:42:59.448524Z","shell.execute_reply.started":"2024-11-12T23:42:59.441918Z","shell.execute_reply":"2024-11-12T23:42:59.447539Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pic_set = random.sample(res, 20)\n\nfig = plt.figure(figsize=(14, 14))\nfor i, (pic, score) in enumerate(pic_set, 1):\n    # print(pic,score)\n    subplot = fig.add_subplot(4, 5, i)\n    # subplot.set_title('%.2f %s' % (score, os.path.basename(path)))\n    subplot.set_title('%s' % (\"cat\" if score <= .05 else \"dog\"))\n    img = cv2.imread(os.path.join(test_path,'test/', pic))\n    img = cv2.resize(img, img_shape[:2])\n    plt.imshow(img)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T00:02:35.125104Z","iopub.execute_input":"2024-11-13T00:02:35.125804Z","iopub.status.idle":"2024-11-13T00:02:38.342888Z","shell.execute_reply.started":"2024-11-13T00:02:35.125765Z","shell.execute_reply":"2024-11-13T00:02:38.341881Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import re\n\nwith open('/kaggle/working/cats-n-dogs/test/submit_Kaggle.csv', 'w') as dst:\n    dst.write('id,label\\n')\n    # for path, score in zip(sorted(test_files), test_pred):\n    for path, score in res:\n        dst.write('%s,%f\\n' % (re.search('(\\d+).jpg$', path).group(1), score))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-13T00:02:07.815598Z","iopub.execute_input":"2024-11-13T00:02:07.816547Z","iopub.status.idle":"2024-11-13T00:02:07.877549Z","shell.execute_reply.started":"2024-11-13T00:02:07.816504Z","shell.execute_reply":"2024-11-13T00:02:07.876807Z"}},"outputs":[],"execution_count":null}]}