{"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":"import os, shutil","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-18T08:08:36.055115Z","iopub.execute_input":"2023-05-18T08:08:36.055664Z","iopub.status.idle":"2023-05-18T08:08:36.062016Z","shell.execute_reply.started":"2023-05-18T08:08:36.055625Z","shell.execute_reply":"2023-05-18T08:08:36.060341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"from zipfile import ZipFile\n\ndataset = '/kaggle/input/dogs-vs-cats/train.zip'\n\nwith ZipFile(dataset, 'r') as zip:\n  zip.extractall()\n  print('The dataset is extracted')","metadata":{"execution":{"iopub.status.busy":"2023-05-18T08:01:58.469137Z","iopub.execute_input":"2023-05-18T08:01:58.469556Z","iopub.status.idle":"2023-05-18T08:02:11.090837Z","shell.execute_reply.started":"2023-05-18T08:01:58.469526Z","shell.execute_reply":"2023-05-18T08:02:11.089408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path, dirs, files = next(os.walk('/kaggle/working/train'))\nfile_count = len(files)\nprint('Number of images: ', file_count)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_names = os.listdir('/kaggle/working/train/')\n","metadata":{"execution":{"iopub.status.busy":"2023-05-15T09:48:08.541829Z","iopub.execute_input":"2023-05-15T09:48:08.542204Z","iopub.status.idle":"2023-05-15T09:48:08.562890Z","shell.execute_reply.started":"2023-05-15T09:48:08.542173Z","shell.execute_reply":"2023-05-15T09:48:08.562014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cats_dir = os.path.join(path, 'cats')\nos.mkdir(train_cats_dir)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T09:48:12.169737Z","iopub.execute_input":"2023-05-15T09:48:12.170075Z","iopub.status.idle":"2023-05-15T09:48:12.175527Z","shell.execute_reply.started":"2023-05-15T09:48:12.170049Z","shell.execute_reply":"2023-05-15T09:48:12.174461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dogs_dir = os.path.join(path, 'dogs')\nos.mkdir(train_dogs_dir)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T09:48:16.465516Z","iopub.execute_input":"2023-05-15T09:48:16.465877Z","iopub.status.idle":"2023-05-15T09:48:16.470756Z","shell.execute_reply.started":"2023-05-15T09:48:16.465849Z","shell.execute_reply":"2023-05-15T09:48:16.469980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fnames = ['cat.{}.jpg'.format(i) for i in range(1000)]\nfor fname in fnames:\n    src = os.path.join(path, fname)\n    dst = os.path.join(train_cats_dir, fname)\n    shutil.copyfile(src, dst)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T09:49:24.592664Z","iopub.execute_input":"2023-05-15T09:49:24.593064Z","iopub.status.idle":"2023-05-15T09:49:24.721230Z","shell.execute_reply.started":"2023-05-15T09:49:24.593032Z","shell.execute_reply":"2023-05-15T09:49:24.720374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fnames = ['dog.{}.jpg'.format(i) for i in range(1000)]\nfor fname in fnames:\n    src = os.path.join(path, fname)\n    dst = os.path.join(train_dogs_dir, fname)\n    shutil.copyfile(src, dst)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T09:50:01.508436Z","iopub.execute_input":"2023-05-15T09:50:01.509392Z","iopub.status.idle":"2023-05-15T09:50:01.639327Z","shell.execute_reply.started":"2023-05-15T09:50:01.509360Z","shell.execute_reply":"2023-05-15T09:50:01.638496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\ntrain_datagen = ImageDataGenerator(rescale=1./255)\ntest_datagen = ImageDataGenerator(rescale=1./255)\ntrain_generator = train_datagen.flow_from_directory(path,target_size=(150, 150),batch_size=20,class_mode='binary')","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:00:46.193093Z","iopub.execute_input":"2023-05-15T10:00:46.193510Z","iopub.status.idle":"2023-05-15T10:00:46.474567Z","shell.execute_reply.started":"2023-05-15T10:00:46.193477Z","shell.execute_reply":"2023-05-15T10:00:46.473551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for data_batch, labels_batch in train_generator:\n    print('data batch shape:', data_batch.shape)\n    print('labels batch shape:', labels_batch.shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2023-05-18T08:08:43.562248Z","iopub.execute_input":"2023-05-18T08:08:43.562661Z","iopub.status.idle":"2023-05-18T08:08:43.627146Z","shell.execute_reply.started":"2023-05-18T08:08:43.562629Z","shell.execute_reply":"2023-05-18T08:08:43.626030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import layers\nfrom keras import models\nmodel = models.Sequential()\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu',\ninput_shape=(150, 150, 3)))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(128, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(128, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(512, activation='relu'))\nmodel.add(layers.Dense(1, activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2023-05-18T08:08:45.693708Z","iopub.execute_input":"2023-05-18T08:08:45.694195Z","iopub.status.idle":"2023-05-18T08:08:46.030616Z","shell.execute_reply.started":"2023-05-18T08:08:45.694156Z","shell.execute_reply":"2023-05-18T08:08:46.029495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import optimizers\nmodel.compile(loss='binary_crossentropy',\noptimizer=optimizers.RMSprop(lr=1e-4),\nmetrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2023-05-18T08:08:51.526453Z","iopub.execute_input":"2023-05-18T08:08:51.527257Z","iopub.status.idle":"2023-05-18T08:08:51.554158Z","shell.execute_reply.started":"2023-05-18T08:08:51.527203Z","shell.execute_reply":"2023-05-18T08:08:51.552602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(\ntrain_generator,\nsteps_per_epoch=100,\nepochs=10,\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-18T08:09:07.402938Z","iopub.execute_input":"2023-05-18T08:09:07.403468Z","iopub.status.idle":"2023-05-18T08:21:33.269532Z","shell.execute_reply.started":"2023-05-18T08:09:07.403429Z","shell.execute_reply":"2023-05-18T08:21:33.268478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('cats_and_dogs_small_1.h5')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications import VGG16\nconv_base = VGG16(weights='imagenet',\ninclude_top=False,\ninput_shape=(150, 150, 3))","metadata":{"execution":{"iopub.status.busy":"2023-05-18T09:07:50.337025Z","iopub.execute_input":"2023-05-18T09:07:50.337480Z","iopub.status.idle":"2023-05-18T09:07:51.312005Z","shell.execute_reply.started":"2023-05-18T09:07:50.337444Z","shell.execute_reply":"2023-05-18T09:07:51.310167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nfrom keras.preprocessing.image import ImageDataGenerator\n\ndatagen = ImageDataGenerator(rescale=1./255)\nbatch_size = 20\ndef extract_features(directory, sample_count):\n    features = np.zeros(shape=(sample_count, 4, 4, 512))\n    labels = np.zeros(shape=(sample_count))\n    generator = datagen.flow_from_directory(directory,target_size=(150, 150),batch_size=batch_size,class_mode='binary')\n    i=0\n    for inputs_batch, labels_batch in generator:\n        features_batch = conv_base.predict(inputs_batch)\n        features[i * batch_size : (i + 1) * batch_size] = features_batch\n        labels[i * batch_size : (i + 1) * batch_size] = labels_batch\n        i += 1\n        if i * batch_size >= sample_count:\n            break\n    return features, labels\ntrain_features, train_labels = extract_features(path, 2000)","metadata":{"execution":{"iopub.status.busy":"2023-05-18T09:10:16.793301Z","iopub.execute_input":"2023-05-18T09:10:16.793769Z","iopub.status.idle":"2023-05-18T09:14:29.379040Z","shell.execute_reply.started":"2023-05-18T09:10:16.793736Z","shell.execute_reply":"2023-05-18T09:14:29.378075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_features = np.reshape(train_features, (2000, 4*4* 512))","metadata":{"execution":{"iopub.status.busy":"2023-05-18T09:17:31.980125Z","iopub.execute_input":"2023-05-18T09:17:31.980682Z","iopub.status.idle":"2023-05-18T09:17:31.987223Z","shell.execute_reply.started":"2023-05-18T09:17:31.980642Z","shell.execute_reply":"2023-05-18T09:17:31.985851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import models\nfrom keras import layers\nfrom keras import optimizers\nmodel = models.Sequential()\nmodel.add(layers.Dense(256, activation='relu', input_dim=4 * 4 * 512))\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.Dense(1, activation='sigmoid'))\nmodel.compile(optimizer=optimizers.RMSprop(lr=2e-5),\nloss='binary_crossentropy',\nmetrics=['acc'])\nhistory = model.fit(train_features, train_labels,\nepochs=30,\nbatch_size=20)","metadata":{"execution":{"iopub.status.busy":"2023-05-18T09:17:56.210483Z","iopub.execute_input":"2023-05-18T09:17:56.210979Z","iopub.status.idle":"2023-05-18T09:18:42.203128Z","shell.execute_reply.started":"2023-05-18T09:17:56.210943Z","shell.execute_reply":"2023-05-18T09:18:42.201842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}