{"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)\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":"2023-07-27T04:46:06.459783Z","iopub.execute_input":"2023-07-27T04:46:06.460345Z","iopub.status.idle":"2023-07-27T04:46:06.471242Z","shell.execute_reply.started":"2023-07-27T04:46:06.460288Z","shell.execute_reply":"2023-07-27T04:46:06.470101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install tensorflow","metadata":{"execution":{"iopub.status.busy":"2023-07-27T04:46:06.473554Z","iopub.execute_input":"2023-07-27T04:46:06.474382Z","iopub.status.idle":"2023-07-27T04:46:18.256921Z","shell.execute_reply.started":"2023-07-27T04:46:06.474349Z","shell.execute_reply":"2023-07-27T04:46:18.255594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip') as existing_zip:\n    existing_zip.extractall()\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip') as existing_zip:\n    existing_zip.extractall()","metadata":{"execution":{"iopub.status.busy":"2023-07-27T04:46:18.258662Z","iopub.execute_input":"2023-07-27T04:46:18.259644Z","iopub.status.idle":"2023-07-27T04:46:30.313831Z","shell.execute_reply.started":"2023-07-27T04:46:18.259597Z","shell.execute_reply":"2023-07-27T04:46:30.312546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd train","metadata":{"execution":{"iopub.status.busy":"2023-07-27T04:46:30.317124Z","iopub.execute_input":"2023-07-27T04:46:30.317836Z","iopub.status.idle":"2023-07-27T04:46:30.325042Z","shell.execute_reply.started":"2023-07-27T04:46:30.317798Z","shell.execute_reply":"2023-07-27T04:46:30.324167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir cats","metadata":{"execution":{"iopub.status.busy":"2023-07-27T04:46:30.326705Z","iopub.execute_input":"2023-07-27T04:46:30.328565Z","iopub.status.idle":"2023-07-27T04:46:31.378651Z","shell.execute_reply.started":"2023-07-27T04:46:30.328531Z","shell.execute_reply":"2023-07-27T04:46:31.377336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir dogs","metadata":{"execution":{"iopub.status.busy":"2023-07-27T04:46:31.380605Z","iopub.execute_input":"2023-07-27T04:46:31.381603Z","iopub.status.idle":"2023-07-27T04:46:32.373696Z","shell.execute_reply.started":"2023-07-27T04:46:31.381557Z","shell.execute_reply":"2023-07-27T04:46:32.372345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nimport glob\nimport os\n\n#移動関数\ndef move_glob(dst_path, pathname, recursive=True):\n    for p in glob.glob(pathname, recursive=recursive):\n        shutil.move(p, dst_path)\n        \n#画像の移動\nmove_glob('./dogs', 'dog*.jpg')\nmove_glob('./cats', 'cat*.jpg')","metadata":{"execution":{"iopub.status.busy":"2023-07-27T04:46:32.376096Z","iopub.execute_input":"2023-07-27T04:46:32.376552Z","iopub.status.idle":"2023-07-27T04:46:32.521436Z","shell.execute_reply.started":"2023-07-27T04:46:32.376510Z","shell.execute_reply":"2023-07-27T04:46:32.518390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ../","metadata":{"execution":{"iopub.status.busy":"2023-07-27T04:49:33.277020Z","iopub.execute_input":"2023-07-27T04:49:33.277437Z","iopub.status.idle":"2023-07-27T04:49:33.284660Z","shell.execute_reply.started":"2023-07-27T04:49:33.277405Z","shell.execute_reply":"2023-07-27T04:49:33.283620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir validation","metadata":{"execution":{"iopub.status.busy":"2023-07-27T04:49:36.395006Z","iopub.execute_input":"2023-07-27T04:49:36.397368Z","iopub.status.idle":"2023-07-27T04:49:37.381338Z","shell.execute_reply.started":"2023-07-27T04:49:36.397312Z","shell.execute_reply":"2023-07-27T04:49:37.379915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd validation","metadata":{"execution":{"iopub.status.busy":"2023-07-27T04:49:39.803684Z","iopub.execute_input":"2023-07-27T04:49:39.804093Z","iopub.status.idle":"2023-07-27T04:49:39.812575Z","shell.execute_reply.started":"2023-07-27T04:49:39.804057Z","shell.execute_reply":"2023-07-27T04:49:39.811518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir cats","metadata":{"execution":{"iopub.status.busy":"2023-07-27T04:49:41.277536Z","iopub.execute_input":"2023-07-27T04:49:41.277899Z","iopub.status.idle":"2023-07-27T04:49:42.269047Z","shell.execute_reply.started":"2023-07-27T04:49:41.277870Z","shell.execute_reply":"2023-07-27T04:49:42.267697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir dogs","metadata":{"execution":{"iopub.status.busy":"2023-07-27T04:49:44.187461Z","iopub.execute_input":"2023-07-27T04:49:44.187868Z","iopub.status.idle":"2023-07-27T04:49:45.204797Z","shell.execute_reply.started":"2023-07-27T04:49:44.187835Z","shell.execute_reply":"2023-07-27T04:49:45.203444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ../","metadata":{"execution":{"iopub.status.busy":"2023-07-27T04:49:46.267372Z","iopub.execute_input":"2023-07-27T04:49:46.268529Z","iopub.status.idle":"2023-07-27T04:49:46.276917Z","shell.execute_reply.started":"2023-07-27T04:49:46.268491Z","shell.execute_reply":"2023-07-27T04:49:46.275682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for num in range(2500):\n    move_glob('validation/cats', 'train/cats/cat.'+ str(num) +'.jpg')\n    move_glob('validation/dogs', 'train/dogs/dog.'+ str(num) +'.jpg')","metadata":{"execution":{"iopub.status.busy":"2023-07-27T04:49:47.772401Z","iopub.execute_input":"2023-07-27T04:49:47.772776Z","iopub.status.idle":"2023-07-27T04:49:48.058643Z","shell.execute_reply.started":"2023-07-27T04:49:47.772742Z","shell.execute_reply":"2023-07-27T04:49:48.057632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Model\nfrom keras.layers import Dense, GlobalAveragePooling2D,Input\nfrom keras.applications.vgg16 import VGG16\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.optimizers import SGD\nfrom keras.callbacks import CSVLogger\nimport csv\nimport json\n\nn_categories=2\nbatch_size=32\ntrain_dir='train'\nvalidation_dir='validation'\nfile_name='vgg16_dogvscat'\n\nbase_model=VGG16(weights='imagenet',include_top=False,\n                 input_tensor=Input(shape=(224,224,3)))\n\n#add new layers instead of FC networks\nx=base_model.output\nx=GlobalAveragePooling2D()(x)\nx=Dense(1024,activation='relu')(x)\nprediction=Dense(n_categories,activation='softmax')(x)\nmodel=Model(inputs=base_model.input,outputs=prediction)\n\n#fix weights before VGG16 14layers\nfor layer in base_model.layers[:15]:\n    layer.trainable=False\n\nmodel.compile(optimizer=SGD(lr=0.0001,momentum=0.9),\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n\nmodel.summary()\n\n#save model\njson_string=model.to_json()\nopen(file_name+'.json','w').write(json_string)\n\ntrain_datagen=ImageDataGenerator(\n    rescale=1.0/255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True)\n\nvalidation_datagen=ImageDataGenerator(rescale=1.0/255)\n\ntrain_generator=train_datagen.flow_from_directory(\n    train_dir,\n    target_size=(224,224),\n    batch_size=batch_size,\n    class_mode='categorical',\n    shuffle=True\n)\n\nvalidation_generator=validation_datagen.flow_from_directory(\n    validation_dir,\n    target_size=(224,224),\n    batch_size=batch_size,\n    class_mode='categorical',\n    shuffle=True\n)\n\nhist=model.fit_generator(train_generator,\n                         epochs=1,\n                         verbose=1,\n                         validation_data=validation_generator,\n                         callbacks=[CSVLogger(file_name+'.csv')])\n\n#save weights\nmodel.save(file_name+'.h5')","metadata":{"execution":{"iopub.status.busy":"2023-07-27T04:49:53.387622Z","iopub.execute_input":"2023-07-27T04:49:53.388059Z","iopub.status.idle":"2023-07-27T04:54:06.706227Z","shell.execute_reply.started":"2023-07-27T04:49:53.388026Z","shell.execute_reply":"2023-07-27T04:54:06.705146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import model_from_json\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport os,random\nfrom tensorflow.keras.preprocessing.image import img_to_array, load_img\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.optimizers import SGD\nfrom tensorflow.keras.preprocessing import image\nimport csv\n\nbatch_size=32\nfile_name='vgg16_dogvscat'\ntest_dir='test'\n\n#load model and weights\njson_string=open(file_name+'.json').read()\nmodel=model_from_json(json_string)\nmodel.load_weights(file_name+'.h5')","metadata":{"execution":{"iopub.status.busy":"2023-07-27T04:54:06.954237Z","iopub.execute_input":"2023-07-27T04:54:06.954596Z","iopub.status.idle":"2023-07-27T04:54:07.193550Z","shell.execute_reply.started":"2023-07-27T04:54:06.954563Z","shell.execute_reply":"2023-07-27T04:54:07.192028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = []\n\nfor num in range(1, 12501):\n    # 予測したい画像のパス\n    TESTPATH = \"test/\" + str(num) + \".jpg\"\n\n    # 予測モデルに入力できるように画像を配列に落とし込む\n    img = image.load_img(TESTPATH, target_size=(224, 224))\n    x = image.img_to_array(img)\n    x = np.expand_dims(x, axis=0)\n    x = x / 255.0\n    # 予測\n    result_predict = model.predict(x)\n    \n    result.append(result_predict)","metadata":{"execution":{"iopub.status.busy":"2023-07-27T04:54:13.980210Z","iopub.execute_input":"2023-07-27T04:54:13.981198Z","iopub.status.idle":"2023-07-27T05:07:02.934024Z","shell.execute_reply.started":"2023-07-27T04:54:13.981159Z","shell.execute_reply":"2023-07-27T05:07:02.932996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_num = []\n\nfor num in range(1, 12501):\n    result_num.append(num)","metadata":{"execution":{"iopub.status.busy":"2023-07-27T05:11:51.518838Z","iopub.execute_input":"2023-07-27T05:11:51.519200Z","iopub.status.idle":"2023-07-27T05:11:51.525857Z","shell.execute_reply.started":"2023-07-27T05:11:51.519170Z","shell.execute_reply":"2023-07-27T05:11:51.524875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lst = []\nnewlst= []\nn=0\n\nfor x in result:\n    lst.append(x.tolist())\n    \nfor n in range(12500):  \n    newlst.append(lst[n][0][1])\n    n+=1\n    \n#print(newlst)    \n\ndf = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')\ndf['label'] = newlst","metadata":{"execution":{"iopub.status.busy":"2023-07-27T05:12:07.576929Z","iopub.execute_input":"2023-07-27T05:12:07.577291Z","iopub.status.idle":"2023-07-27T05:12:07.634073Z","shell.execute_reply.started":"2023-07-27T05:12:07.577261Z","shell.execute_reply":"2023-07-27T05:12:07.633024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2023-07-27T05:12:11.534445Z","iopub.execute_input":"2023-07-27T05:12:11.535453Z","iopub.status.idle":"2023-07-27T05:12:11.556583Z","shell.execute_reply.started":"2023-07-27T05:12:11.535403Z","shell.execute_reply":"2023-07-27T05:12:11.555626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv(\"submission2.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-07-27T05:12:15.494223Z","iopub.execute_input":"2023-07-27T05:12:15.495103Z","iopub.status.idle":"2023-07-27T05:12:15.549132Z","shell.execute_reply.started":"2023-07-27T05:12:15.495060Z","shell.execute_reply":"2023-07-27T05:12:15.548038Z"},"trusted":true},"execution_count":null,"outputs":[]}]}