{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":5441,"databundleVersionId":38425,"sourceType":"competition"}],"dockerImageVersionId":30747,"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","execution":{"iopub.status.busy":"2024-07-25T13:49:41.253056Z","iopub.execute_input":"2024-07-25T13:49:41.253436Z","iopub.status.idle":"2024-07-25T13:49:41.261018Z","shell.execute_reply.started":"2024-07-25T13:49:41.253409Z","shell.execute_reply":"2024-07-25T13:49:41.260091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile # ZIPファイルをインポート\n\n# train.zipの展開\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip', 'r') as zip_ref:\n    zip_ref.extractall('/kaggle/working/train')\n\n# test.zipの展開\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip', 'r') as zip_ref:\n    zip_ref.extractall('/kaggle/working/test')","metadata":{"execution":{"iopub.status.busy":"2024-07-25T13:49:41.262691Z","iopub.execute_input":"2024-07-25T13:49:41.263014Z","iopub.status.idle":"2024-07-25T13:49:53.056532Z","shell.execute_reply.started":"2024-07-25T13:49:41.262980Z","shell.execute_reply":"2024-07-25T13:49:53.055709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # numpyをインポート\nimport pandas as pd\nimport os\nimport shutil\nimport cv2\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport tensorflow.keras as keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import load_model\nimport csv","metadata":{"execution":{"iopub.status.busy":"2024-07-25T13:49:53.057654Z","iopub.execute_input":"2024-07-25T13:49:53.058000Z","iopub.status.idle":"2024-07-25T13:49:53.064039Z","shell.execute_reply.started":"2024-07-25T13:49:53.057968Z","shell.execute_reply":"2024-07-25T13:49:53.063105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# パス管理\n# trainデータのディレクトリのパスを決める\ntrain_dir = '/kaggle/working/train/train'","metadata":{"execution":{"iopub.status.busy":"2024-07-25T13:49:53.065171Z","iopub.execute_input":"2024-07-25T13:49:53.065446Z","iopub.status.idle":"2024-07-25T13:49:53.407860Z","shell.execute_reply.started":"2024-07-25T13:49:53.065422Z","shell.execute_reply":"2024-07-25T13:49:53.407085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# testデータのディレクトリのパスを決める\ntest_dir = '/kaggle/working/test/test'","metadata":{"execution":{"iopub.status.busy":"2024-07-25T13:49:53.410475Z","iopub.execute_input":"2024-07-25T13:49:53.410745Z","iopub.status.idle":"2024-07-25T13:49:53.418346Z","shell.execute_reply.started":"2024-07-25T13:49:53.410722Z","shell.execute_reply":"2024-07-25T13:49:53.417453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 画像のサイズをすべて統一する\nIMG_SIZE = 64","metadata":{"execution":{"iopub.status.busy":"2024-07-25T13:49:53.419469Z","iopub.execute_input":"2024-07-25T13:49:53.419800Z","iopub.status.idle":"2024-07-25T13:49:53.429025Z","shell.execute_reply.started":"2024-07-25T13:49:53.419769Z","shell.execute_reply":"2024-07-25T13:49:53.428180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# データを読み込むための関数を設定\ndef load_data(data_dir, sample_size=1000):\n    images = [] # 写真を入れるためのリストを設定\n    labels = [] # 上記写真のラベルを入れるためのリストを設定\n    files = os.listdir(data_dir) # ファイルを一覧化する\n\n    for file in files: # 繰り返し実行する\n        img_path = os.path.join(data_dir, file) # ディレクトリのパスとファイル名を合体させ、ファイルのパスを設定し、それをimg_pathに入れる\n\n        img = cv2.imread(img_path) # 写真を取得する\n        if img is not None: # 写真がある場合\n            img = cv2.resize(img, (IMG_SIZE, IMG_SIZE)) # 写真のサイズを調整\n            images.append(img) # リストの中に写真を一枚ずつ入れる\n            label = 1 if 'dog' in file else 0 # ラベル付け: イヌなら1,ネコなら0\n            labels.append(label) # そのラベルをリストに入れる\n        else: # 写真がない場合\n            print(f'（エラー）ファイル読み込めてない {img_path}') # デバッグ文を出す\n    return np.array(images) / 255.0, np.array(labels) # 写真とラベルのリスト","metadata":{"execution":{"iopub.status.busy":"2024-07-25T13:49:53.430134Z","iopub.execute_input":"2024-07-25T13:49:53.430612Z","iopub.status.idle":"2024-07-25T13:49:53.440172Z","shell.execute_reply.started":"2024-07-25T13:49:53.430586Z","shell.execute_reply":"2024-07-25T13:49:53.439350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# trainデータとtestデータを読み込む\nX_train, y_train = load_data(train_dir, sample_size=1000)\nX_test, y_test = load_data(test_dir, sample_size=500)","metadata":{"execution":{"iopub.status.busy":"2024-07-25T13:49:53.441260Z","iopub.execute_input":"2024-07-25T13:49:53.441581Z","iopub.status.idle":"2024-07-25T13:50:24.810398Z","shell.execute_reply.started":"2024-07-25T13:49:53.441549Z","shell.execute_reply":"2024-07-25T13:50:24.809525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# データの拡張\ndatagen = ImageDataGenerator(\n    rotation_range=20,\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'\n)","metadata":{"execution":{"iopub.status.busy":"2024-07-25T13:50:24.812139Z","iopub.execute_input":"2024-07-25T13:50:24.812440Z","iopub.status.idle":"2024-07-25T13:50:24.817177Z","shell.execute_reply.started":"2024-07-25T13:50:24.812414Z","shell.execute_reply":"2024-07-25T13:50:24.816297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# モデルを作成\ndef create_model(neuron):\n    # ライブラリや関数をインポート\n    Dense = keras.layers.Dense\n    Conv2D = keras.layers.Conv2D\n    MaxPooling2D = keras.layers.MaxPooling2D\n    Flatten = keras.layers.Flatten\n    Dropout = keras.layers.Dropout\n    \n    # モデルの定義\n    model = keras.models.Sequential()\n \n    # 畳み込む \n    model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(IMG_SIZE, IMG_SIZE, 3)))\n    # 2x2のプーリング層\n    model.add(MaxPooling2D((2, 2)))\n    \n    model.add(Conv2D(64, (3, 3), activation='relu'))\n    # 2x2のプーリング層\n    model.add(MaxPooling2D((2, 2)))\n    \n    model.add(Conv2D(128, (3, 3), activation='relu'))\n    # 2x2のプーリング層\n    model.add(MaxPooling2D((2, 2)))\n    \n    # 結合層を追加　　　　　　　　\n    model.add(Flatten())\n    model.add(Dense(neuron, activation='relu'))\n    model.add(Dropout(0.5))\n   \n    # 出力層シグモイド\n    model.add(Dense(1, activation='sigmoid'))\n\n    # モデルのコンパイル\n    # バイナリクロスエントロピー損失関数\n    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])\n    \n    # モデルを返す\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-07-25T13:50:24.818319Z","iopub.execute_input":"2024-07-25T13:50:24.818583Z","iopub.status.idle":"2024-07-25T13:50:24.831326Z","shell.execute_reply.started":"2024-07-25T13:50:24.818559Z","shell.execute_reply":"2024-07-25T13:50:24.830532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def save_model(model, filename):\n    model.save(filename)   ","metadata":{"execution":{"iopub.status.busy":"2024-07-25T13:50:24.832166Z","iopub.execute_input":"2024-07-25T13:50:24.832414Z","iopub.status.idle":"2024-07-25T13:50:24.849653Z","shell.execute_reply.started":"2024-07-25T13:50:24.832385Z","shell.execute_reply":"2024-07-25T13:50:24.848919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_existing_model(filename):\n    return load_model(filename)","metadata":{"execution":{"iopub.status.busy":"2024-07-25T13:50:24.850768Z","iopub.execute_input":"2024-07-25T13:50:24.851032Z","iopub.status.idle":"2024-07-25T13:50:24.860382Z","shell.execute_reply.started":"2024-07-25T13:50:24.851009Z","shell.execute_reply":"2024-07-25T13:50:24.859642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 学習し、テストデータで評価し、スコアを表示\ndef fit_epoch(neuron, batch, epochs, initial_epoch=0, model_filename='model.h5'):\n    if initial_epoch == 0: # もし、モデルを作るのが初めての場合\n        model = create_model(neuron) # 作ったモデルを作る関数を実行\n    else: # モデルを作ったことがある場合\n        model = load_existing_model(model_filename) #モデルを読み込む\n        \n    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])\n    \n    # モデルの学習\n    hist = model.fit(datagen.flow(X_train, y_train, batch_size=batch),\n                     steps_per_epoch=len(X_train) // batch,\n                     validation_data=(X_test, y_test),\n                     epochs=epochs,\n                     initial_epoch=initial_epoch,\n                     verbose=1)\n    \n    # モデルの評価をし、表示する\n    score = model.evaluate(X_test, y_test, verbose=1)\n    print('正解率=', score[1], 'loss=', score[0])\n    \n    # モデルの保存\n    save_model(model, model_filename)\n    \n    # 学習結果を可視化、グラフ化\n    \n    # trainデータの精度の流れをグラフ化\n    plt.plot(hist.history['accuracy'])\n    # testデータの精度の流れをグラフ化\n    plt.plot(hist.history['val_accuracy'])\n    # グラフのタイトルを設定\n    plt.title('Accuracy')\n    # 凡例を設定\n    plt.legend(['train', 'test'], loc='upper left')\n    # グラフの表示\n    plt.show()\n\n    # trainデータの損失値の流れをグラフ化\n    plt.plot(hist.history['loss'])\n    # testデータの損失値の流れをグラフ化\n    plt.plot(hist.history['val_loss'])\n    # グラフのタイトルを設定\n    plt.title('Loss')\n    # 凡例を設定\n    plt.legend(['train', 'test'], loc='upper left')\n    # グラフの表示\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-25T13:50:24.861406Z","iopub.execute_input":"2024-07-25T13:50:24.861635Z","iopub.status.idle":"2024-07-25T13:50:24.873856Z","shell.execute_reply.started":"2024-07-25T13:50:24.861614Z","shell.execute_reply":"2024-07-25T13:50:24.873107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# total_epochs = 30 # 全体のエポック数\nneuron = 512 # ニューロンの数\nbatch = 8 # バッチサイズ\nmodel_filename = 'model.h5' # モデルのファイル名","metadata":{"execution":{"iopub.status.busy":"2024-07-25T13:50:24.877576Z","iopub.execute_input":"2024-07-25T13:50:24.877936Z","iopub.status.idle":"2024-07-25T13:50:24.887896Z","shell.execute_reply.started":"2024-07-25T13:50:24.877913Z","shell.execute_reply":"2024-07-25T13:50:24.887054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 1回目の学習\nfit_epoch(neuron=neuron, batch=batch, epochs=10, initial_epoch=0, model_filename=model_filename)","metadata":{"execution":{"iopub.status.busy":"2024-07-25T13:50:24.888886Z","iopub.execute_input":"2024-07-25T13:50:24.889150Z","iopub.status.idle":"2024-07-25T13:54:01.786585Z","shell.execute_reply.started":"2024-07-25T13:50:24.889128Z","shell.execute_reply":"2024-07-25T13:54:01.785705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 2回目の学習\nfit_epoch(neuron=neuron, batch=batch, epochs=20, initial_epoch=10, model_filename=model_filename)","metadata":{"execution":{"iopub.status.busy":"2024-07-25T13:54:01.787881Z","iopub.execute_input":"2024-07-25T13:54:01.789092Z","iopub.status.idle":"2024-07-25T13:57:37.532523Z","shell.execute_reply.started":"2024-07-25T13:54:01.789040Z","shell.execute_reply":"2024-07-25T13:57:37.531457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 3回目の学習\nfit_epoch(neuron=neuron, batch=batch, epochs=30, initial_epoch=20, model_filename=model_filename)","metadata":{"execution":{"iopub.status.busy":"2024-07-25T13:57:37.533613Z","iopub.execute_input":"2024-07-25T13:57:37.533900Z","iopub.status.idle":"2024-07-25T14:01:14.454902Z","shell.execute_reply.started":"2024-07-25T13:57:37.533874Z","shell.execute_reply":"2024-07-25T14:01:14.453931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport cv2\nimport tensorflow.keras as keras\nfrom tensorflow.keras.models import load_model\n\nIMG_SIZE = 64\ntest_dir = '/kaggle/working/test/test'\nmodel_filename = 'model.h5'\n# 提出するCSVファイル\noutput_csv = '/kaggle/working/submission.csv'\n\n# 画像データの読み込み\ndef load_test_data(data_dir):\n    images = []\n    filenames = os.listdir(data_dir)\n    for file in filenames:\n        img_path = os.path.join(data_dir, file)\n        img = cv2.imread(img_path)\n        if img is not None:\n            img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))\n            images.append(img)\n        else:\n            print(f'Error reading {img_path}')\n    return np.array(images) / 255.0, filenames\n\n# テストデータの読み込み\nX_test, test_filenames = load_test_data(test_dir)\n\n# モデルの読み込み\nmodel = load_model(model_filename)\n\n# 予測の実行\npredictions = model.predict(X_test)\n\n# 予測結果を0または1に変換\npredictions = (predictions > 0.5).astype(int).flatten()\n\n# CSVファイルの作成\noutput_df = pd.DataFrame({\n    'id': [os.path.splitext(f)[0] for f in test_filenames],  # ファイル名から拡張子を消し、IDにする\n    'label': predictions\n})\noutput_df.to_csv(output_csv, index=False)\n\nprint(f'CSVファイル {output_csv} を作成しました')","metadata":{"execution":{"iopub.status.busy":"2024-07-25T14:01:47.443696Z","iopub.execute_input":"2024-07-25T14:01:47.444029Z","iopub.status.idle":"2024-07-25T14:02:01.063279Z","shell.execute_reply.started":"2024-07-25T14:01:47.444003Z","shell.execute_reply":"2024-07-25T14:02:01.062349Z"},"trusted":true},"execution_count":null,"outputs":[]}]}