{"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-24T17:53:02.148108Z","iopub.execute_input":"2023-07-24T17:53:02.148665Z","iopub.status.idle":"2023-07-24T17:53:02.167671Z","shell.execute_reply.started":"2023-07-24T17:53:02.148624Z","shell.execute_reply":"2023-07-24T17:53:02.166663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"shutil.rmtree('/kaggle/working/train')","metadata":{}},{"cell_type":"code","source":"import zipfile\nwith zipfile.ZipFile('../input/dogs-vs-cats-redux-kernels-edition/test.zip') as existing_zip:\n    existing_zip.extractall()\nwith zipfile.ZipFile('../input/dogs-vs-cats-redux-kernels-edition/train.zip') as existing_zip:\n    existing_zip.extractall()","metadata":{"execution":{"iopub.status.busy":"2023-07-24T17:53:02.170131Z","iopub.execute_input":"2023-07-24T17:53:02.170531Z","iopub.status.idle":"2023-07-24T17:53:18.086261Z","shell.execute_reply.started":"2023-07-24T17:53:02.170493Z","shell.execute_reply":"2023-07-24T17:53:18.081504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd train","metadata":{"execution":{"iopub.status.busy":"2023-07-24T17:53:18.087898Z","iopub.execute_input":"2023-07-24T17:53:18.088280Z","iopub.status.idle":"2023-07-24T17:53:18.096705Z","shell.execute_reply.started":"2023-07-24T17:53:18.088246Z","shell.execute_reply":"2023-07-24T17:53:18.095695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir cats","metadata":{"execution":{"iopub.status.busy":"2023-07-24T17:53:18.099938Z","iopub.execute_input":"2023-07-24T17:53:18.100301Z","iopub.status.idle":"2023-07-24T17:53:19.361062Z","shell.execute_reply.started":"2023-07-24T17:53:18.100269Z","shell.execute_reply":"2023-07-24T17:53:19.359791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir dogs","metadata":{"execution":{"iopub.status.busy":"2023-07-24T17:53:19.363494Z","iopub.execute_input":"2023-07-24T17:53:19.363894Z","iopub.status.idle":"2023-07-24T17:53:20.437662Z","shell.execute_reply.started":"2023-07-24T17:53:19.363854Z","shell.execute_reply":"2023-07-24T17:53:20.436390Z"},"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#画像の移動\nmove_glob('./dogs', 'dog*.jpg')\nmove_glob('./cats', 'cat*.jpg')","metadata":{"execution":{"iopub.status.busy":"2023-07-24T17:53:20.440113Z","iopub.execute_input":"2023-07-24T17:53:20.441400Z","iopub.status.idle":"2023-07-24T17:53:21.622477Z","shell.execute_reply.started":"2023-07-24T17:53:20.441361Z","shell.execute_reply":"2023-07-24T17:53:21.621374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ../","metadata":{"execution":{"iopub.status.busy":"2023-07-24T17:53:21.624127Z","iopub.execute_input":"2023-07-24T17:53:21.624504Z","iopub.status.idle":"2023-07-24T17:53:21.631774Z","shell.execute_reply.started":"2023-07-24T17:53:21.624470Z","shell.execute_reply":"2023-07-24T17:53:21.630605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir validation","metadata":{"execution":{"iopub.status.busy":"2023-07-24T17:53:21.633608Z","iopub.execute_input":"2023-07-24T17:53:21.633932Z","iopub.status.idle":"2023-07-24T17:53:22.605785Z","shell.execute_reply.started":"2023-07-24T17:53:21.633903Z","shell.execute_reply":"2023-07-24T17:53:22.603311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd validation","metadata":{"execution":{"iopub.status.busy":"2023-07-24T17:53:22.608888Z","iopub.execute_input":"2023-07-24T17:53:22.609630Z","iopub.status.idle":"2023-07-24T17:53:22.616777Z","shell.execute_reply.started":"2023-07-24T17:53:22.609590Z","shell.execute_reply":"2023-07-24T17:53:22.615855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir cats","metadata":{"execution":{"iopub.status.busy":"2023-07-24T17:53:22.621096Z","iopub.execute_input":"2023-07-24T17:53:22.622007Z","iopub.status.idle":"2023-07-24T17:53:23.860466Z","shell.execute_reply.started":"2023-07-24T17:53:22.621955Z","shell.execute_reply":"2023-07-24T17:53:23.858265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir dogs","metadata":{"execution":{"iopub.status.busy":"2023-07-24T17:53:23.862560Z","iopub.execute_input":"2023-07-24T17:53:23.863011Z","iopub.status.idle":"2023-07-24T17:53:25.519856Z","shell.execute_reply.started":"2023-07-24T17:53:23.862948Z","shell.execute_reply":"2023-07-24T17:53:25.518571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ../","metadata":{"execution":{"iopub.status.busy":"2023-07-24T17:53:25.521786Z","iopub.execute_input":"2023-07-24T17:53:25.522223Z","iopub.status.idle":"2023-07-24T17:53:25.532625Z","shell.execute_reply.started":"2023-07-24T17:53:25.522185Z","shell.execute_reply":"2023-07-24T17:53:25.531615Z"},"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-24T17:53:25.534280Z","iopub.execute_input":"2023-07-24T17:53:25.534868Z","iopub.status.idle":"2023-07-24T17:53:25.843947Z","shell.execute_reply.started":"2023-07-24T17:53:25.534834Z","shell.execute_reply":"2023-07-24T17:53:25.842093Z"},"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=89,\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-24T17:53:25.845411Z","iopub.execute_input":"2023-07-24T17:53:25.845747Z","iopub.status.idle":"2023-07-25T01:10:46.256999Z","shell.execute_reply.started":"2023-07-24T17:53:25.845715Z","shell.execute_reply":"2023-07-25T01:10:46.255852Z"},"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-25T01:10:46.258517Z","iopub.execute_input":"2023-07-25T01:10:46.258874Z","iopub.status.idle":"2023-07-25T01:10:46.502673Z","shell.execute_reply.started":"2023-07-25T01:10:46.258841Z","shell.execute_reply":"2023-07-25T01:10:46.501638Z"},"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-25T01:10:46.504368Z","iopub.execute_input":"2023-07-25T01:10:46.504730Z","iopub.status.idle":"2023-07-25T01:25:40.032741Z","shell.execute_reply.started":"2023-07-25T01:10:46.504696Z","shell.execute_reply":"2023-07-25T01:25:40.031109Z"},"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-25T01:25:40.041634Z","iopub.execute_input":"2023-07-25T01:25:40.046189Z","iopub.status.idle":"2023-07-25T01:25:40.056706Z","shell.execute_reply.started":"2023-07-25T01:25:40.046139Z","shell.execute_reply":"2023-07-25T01:25:40.055552Z"},"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('../input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')\ndf['label'] = newlst","metadata":{"execution":{"iopub.status.busy":"2023-07-25T01:25:40.058332Z","iopub.execute_input":"2023-07-25T01:25:40.058755Z","iopub.status.idle":"2023-07-25T01:25:40.155802Z","shell.execute_reply.started":"2023-07-25T01:25:40.058723Z","shell.execute_reply":"2023-07-25T01:25:40.154902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2023-07-25T01:25:40.159405Z","iopub.execute_input":"2023-07-25T01:25:40.159687Z","iopub.status.idle":"2023-07-25T01:25:40.178801Z","shell.execute_reply.started":"2023-07-25T01:25:40.159663Z","shell.execute_reply":"2023-07-25T01:25:40.177932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-07-25T01:25:40.180222Z","iopub.execute_input":"2023-07-25T01:25:40.180572Z","iopub.status.idle":"2023-07-25T01:25:40.228870Z","shell.execute_reply.started":"2023-07-25T01:25:40.180540Z","shell.execute_reply":"2023-07-25T01:25:40.228017Z"},"trusted":true},"execution_count":null,"outputs":[]}]}