{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":34426,"databundleVersionId":3218632,"sourceType":"competition"},{"sourceId":64148,"databundleVersionId":7669720,"sourceType":"competition"}],"dockerImageVersionId":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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)\n\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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget --no-check-certificate \\\n    https://storage.googleapis.com/tensorflow/keras-applications/vgg16/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5 \\\n    -O /tmp/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5","metadata":{"execution":{"iopub.status.busy":"2024-02-23T03:45:43.041012Z","iopub.execute_input":"2024-02-23T03:45:43.041904Z","iopub.status.idle":"2024-02-23T03:45:46.196935Z","shell.execute_reply.started":"2024-02-23T03:45:43.041863Z","shell.execute_reply":"2024-02-23T03:45:46.195832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom sklearn.model_selection import train_test_split\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\n# base_dir =\"/kaggle/input/dog-vs-cat-classification/train/\"\n# base_dir1 =\"/kaggle/input/dog-vs-cat-classification/test/\"\n# train_dir = os.path.join(base_dir,'train')\n# test_dir = os.path.join(base_dir1,'test')\ntrain_path = \"/kaggle/input/dog-vs-cat-classification/train/train\"\ntrain_path1 = \"/kaggle/input/dog-vs-cat-classification/train/train/cats\"\ntrain_path2 = \"/kaggle/input/dog-vs-cat-classification/train/train/dogs\"\ntest_path = \"/kaggle/input/dog-vs-cat-classification/test/test\"\ntrain_file_names1 = os.listdir(train_path1)\ntrain_file_names2 = os.listdir(train_path2)\n\nimport pandas\nimport numpy\nimage_paths = []\nlabels = []\nfor train_file in train_file_names1:\n    label = train_file.split('.')[0]\n    labels.append(label)\n    image_path = os.path.join(train_path1, train_file)\n    image_paths.append(image_path)\n    \n#     print(image_path)\n    \nfor train_file in train_file_names2:\n    label = train_file.split('.')[0]\n    labels.append(label)\n    image_path = os.path.join(train_path2, train_file)\n    image_paths.append(image_path)    \n    \ntrain_df = pandas.DataFrame()\ntrain_df['train_image_path'] = image_paths #为自定义属性名添加列表\ntrain_df['label'] = labels\ntrain_set, val_set = train_test_split(train_df, random_state=42, stratify=train_df['label'])\n\nprint(train_df.head(1))\n\n#训练集\n# train_cats_dir = os.path.join(train_dir,'cats')\n# train_dogs_dir = os.path.join(train_dir,'dogs')\ntest_file_names = os.listdir(test_path)\nimage_paths1 = []\ntest_idx1 = []\nfor test_file in test_file_names:\n    idx = test_file\n    test_idx1.append(idx)\n    image_path1 = os.path.join(test_path, test_file)\n    image_paths1.append(image_path1)\n    \ntest_df2 = pandas.DataFrame()\ntest_df2['test_image_path'] = image_paths1\ntest_df2['test_idx'] = test_idx1\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-02-23T03:46:09.182750Z","iopub.execute_input":"2024-02-23T03:46:09.183693Z","iopub.status.idle":"2024-02-23T03:46:09.371450Z","shell.execute_reply.started":"2024-02-23T03:46:09.183653Z","shell.execute_reply":"2024-02-23T03:46:09.370496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nimport tensorflow as tf\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications.vgg16 import VGG16\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D,MaxPooling2D,Activation,Dropout,Flatten,Dense\ntrain_gen = ImageDataGenerator(\n    zoom_range=0.2,\n    rotation_range=40,\n    rescale=1./255,\n    shear_range=0.2,\n    horizontal_flip=True,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    fill_mode=\"nearest\"\n)\ntrain_generator = train_gen.flow_from_dataframe(\n    dataframe=train_set,\n    x_col='train_image_path',\n    y_col='label',\n    target_size=(150,150),\n    class_mode='binary',\n    batch_size=50,\n    shuffle=False\n)\n\nval_gen = ImageDataGenerator(\n    rescale=1./255\n)\nvalidation_generator = val_gen.flow_from_dataframe(\n    dataframe=val_set,\n    x_col='train_image_path',\n    y_col='label',\n    target_size=(150,150),\n    class_mode='binary',\n    batch_size=50,\n    shuffle=False\n)\n\n# print(len(train_generator))\n\n","metadata":{"execution":{"iopub.status.busy":"2024-02-23T03:46:18.419199Z","iopub.execute_input":"2024-02-23T03:46:18.419941Z","iopub.status.idle":"2024-02-23T03:47:34.504941Z","shell.execute_reply.started":"2024-02-23T03:46:18.419904Z","shell.execute_reply":"2024-02-23T03:47:34.503887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nimport tensorflow as tf\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications.vgg16 import VGG16\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D,MaxPooling2D,Activation,Dropout,Flatten,Dense\n\nvgg16_model =VGG16(weights=None,include_top=False,input_shape=(150,150,3))\nvgg16_model.load_weights('/tmp/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5')\nfor layer in vgg16_model.layers:\n    layer.trainable = False    # freeze VGG16卷积层的参数\n#搭建全连接层\ntop_model = tf.keras.Sequential([\n    vgg16_model,\ntf.keras.layers.Flatten(input_shape=vgg16_model.output_shape[1:]),\ntf.keras.layers.Dense(512,activation=\"relu\"),\ntf.keras.layers.Dropout(0.5),\ntf.keras.layers.Dense(1,activation=\"sigmoid\")\n])\n\ntop_model.compile(loss='binary_crossentropy',optimizer=Adam(lr=1e-4),\nmetrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2024-02-23T03:47:39.249705Z","iopub.execute_input":"2024-02-23T03:47:39.250566Z","iopub.status.idle":"2024-02-23T03:47:39.614216Z","shell.execute_reply.started":"2024-02-23T03:47:39.250533Z","shell.execute_reply":"2024-02-23T03:47:39.613450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = top_model.fit_generator(\n    train_generator,\n    steps_per_epoch=100,\n    epochs=20,\n    validation_data=validation_generator,\n    validation_steps=50,\n    verbose=2\n)\n\ntop_model.save(\"cats_dogs_vgg16.h5\")","metadata":{"execution":{"iopub.status.busy":"2024-02-23T03:47:49.060916Z","iopub.execute_input":"2024-02-23T03:47:49.061621Z","iopub.status.idle":"2024-02-23T04:02:32.889336Z","shell.execute_reply.started":"2024-02-23T03:47:49.061587Z","shell.execute_reply":"2024-02-23T04:02:32.888484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(\n    rescale=1./255\n)\ntest_generator = test_datagen.flow_from_dataframe(\n    dataframe=test_df2,\n    x_col='test_image_path',\n    y_col=None,\n    target_size=(150,150),\n    class_mode=None,\n    batch_size=50,\n    shuffle=False\n)\nprint(len(test_generator))\n","metadata":{"execution":{"iopub.status.busy":"2024-02-23T04:03:07.429241Z","iopub.execute_input":"2024-02-23T04:03:07.429704Z","iopub.status.idle":"2024-02-23T04:03:30.794650Z","shell.execute_reply.started":"2024-02-23T04:03:07.429664Z","shell.execute_reply":"2024-02-23T04:03:30.793749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = top_model.predict(test_generator)\npredictions = numpy.round(predictions.flatten()).astype(int)\nprint(numpy.size(predictions))","metadata":{"execution":{"iopub.status.busy":"2024-02-23T04:03:34.103099Z","iopub.execute_input":"2024-02-23T04:03:34.103569Z","iopub.status.idle":"2024-02-23T04:04:39.421417Z","shell.execute_reply.started":"2024-02-23T04:03:34.103531Z","shell.execute_reply":"2024-02-23T04:04:39.420474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pandas.DataFrame({'id':test_idx1,'labels':predictions})\nsubmission.to_csv(\"submission.csv\",index=False)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-02-23T04:05:06.555867Z","iopub.execute_input":"2024-02-23T04:05:06.556903Z","iopub.status.idle":"2024-02-23T04:05:06.573740Z","shell.execute_reply.started":"2024-02-23T04:05:06.556865Z","shell.execute_reply":"2024-02-23T04:05:06.572770Z"},"trusted":true},"execution_count":null,"outputs":[]}]}