{"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":"2022-07-22T12:32:10.030730Z","iopub.execute_input":"2022-07-22T12:32:10.031431Z","iopub.status.idle":"2022-07-22T12:32:10.069218Z","shell.execute_reply.started":"2022-07-22T12:32:10.031300Z","shell.execute_reply":"2022-07-22T12:32:10.067850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2022-07-22T12:32:10.072116Z","iopub.execute_input":"2022-07-22T12:32:10.072943Z","iopub.status.idle":"2022-07-22T12:32:31.546052Z","shell.execute_reply.started":"2022-07-22T12:32:10.072903Z","shell.execute_reply":"2022-07-22T12:32:31.544840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd train","metadata":{"execution":{"iopub.status.busy":"2022-07-22T12:32:31.548016Z","iopub.execute_input":"2022-07-22T12:32:31.548857Z","iopub.status.idle":"2022-07-22T12:32:31.567629Z","shell.execute_reply.started":"2022-07-22T12:32:31.548808Z","shell.execute_reply":"2022-07-22T12:32:31.566065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir cats","metadata":{"execution":{"iopub.status.busy":"2022-07-22T12:32:31.570643Z","iopub.execute_input":"2022-07-22T12:32:31.572788Z","iopub.status.idle":"2022-07-22T12:32:32.357472Z","shell.execute_reply.started":"2022-07-22T12:32:31.572744Z","shell.execute_reply":"2022-07-22T12:32:32.355765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir dogs","metadata":{"execution":{"iopub.status.busy":"2022-07-22T12:32:32.363742Z","iopub.execute_input":"2022-07-22T12:32:32.364289Z","iopub.status.idle":"2022-07-22T12:32:33.134330Z","shell.execute_reply.started":"2022-07-22T12:32:32.364252Z","shell.execute_reply":"2022-07-22T12:32:33.132732Z"},"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":"2022-07-22T12:32:33.136557Z","iopub.execute_input":"2022-07-22T12:32:33.137323Z","iopub.status.idle":"2022-07-22T12:32:35.224413Z","shell.execute_reply.started":"2022-07-22T12:32:33.137268Z","shell.execute_reply":"2022-07-22T12:32:35.223097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ../","metadata":{"execution":{"iopub.status.busy":"2022-07-22T12:32:35.231624Z","iopub.execute_input":"2022-07-22T12:32:35.236524Z","iopub.status.idle":"2022-07-22T12:32:35.249612Z","shell.execute_reply.started":"2022-07-22T12:32:35.236466Z","shell.execute_reply":"2022-07-22T12:32:35.247848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir validation","metadata":{"execution":{"iopub.status.busy":"2022-07-22T12:32:35.252747Z","iopub.execute_input":"2022-07-22T12:32:35.254271Z","iopub.status.idle":"2022-07-22T12:32:36.155429Z","shell.execute_reply.started":"2022-07-22T12:32:35.254204Z","shell.execute_reply":"2022-07-22T12:32:36.153600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd validation","metadata":{"execution":{"iopub.status.busy":"2022-07-22T12:32:36.157775Z","iopub.execute_input":"2022-07-22T12:32:36.158536Z","iopub.status.idle":"2022-07-22T12:32:36.166750Z","shell.execute_reply.started":"2022-07-22T12:32:36.158493Z","shell.execute_reply":"2022-07-22T12:32:36.165325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir cats","metadata":{"execution":{"iopub.status.busy":"2022-07-22T12:32:36.168730Z","iopub.execute_input":"2022-07-22T12:32:36.169554Z","iopub.status.idle":"2022-07-22T12:32:36.998641Z","shell.execute_reply.started":"2022-07-22T12:32:36.169515Z","shell.execute_reply":"2022-07-22T12:32:36.995739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir dogs","metadata":{"execution":{"iopub.status.busy":"2022-07-22T12:32:37.001491Z","iopub.execute_input":"2022-07-22T12:32:37.001943Z","iopub.status.idle":"2022-07-22T12:32:38.213855Z","shell.execute_reply.started":"2022-07-22T12:32:37.001897Z","shell.execute_reply":"2022-07-22T12:32:38.211785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ../","metadata":{"execution":{"iopub.status.busy":"2022-07-22T12:32:38.220945Z","iopub.execute_input":"2022-07-22T12:32:38.223302Z","iopub.status.idle":"2022-07-22T12:32:38.234894Z","shell.execute_reply.started":"2022-07-22T12:32:38.223252Z","shell.execute_reply":"2022-07-22T12:32:38.233577Z"},"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":"2022-07-22T12:32:38.236552Z","iopub.execute_input":"2022-07-22T12:32:38.237881Z","iopub.status.idle":"2022-07-22T12:32:38.601853Z","shell.execute_reply.started":"2022-07-22T12:32:38.237833Z","shell.execute_reply":"2022-07-22T12:32:38.599855Z"},"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=80,\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":"2022-07-22T12:32:38.607876Z","iopub.execute_input":"2022-07-22T12:32:38.608282Z","iopub.status.idle":"2022-07-22T18:43:22.547531Z","shell.execute_reply.started":"2022-07-22T12:32:38.608252Z","shell.execute_reply":"2022-07-22T18:43:22.546123Z"},"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 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":"2022-07-22T18:43:22.548950Z","iopub.execute_input":"2022-07-22T18:43:22.549613Z","iopub.status.idle":"2022-07-22T18:43:22.882446Z","shell.execute_reply.started":"2022-07-22T18:43:22.549571Z","shell.execute_reply":"2022-07-22T18:43:22.881104Z"},"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":"2022-07-22T18:43:22.888191Z","iopub.execute_input":"2022-07-22T18:43:22.889476Z","iopub.status.idle":"2022-07-22T18:55:48.829923Z","shell.execute_reply.started":"2022-07-22T18:43:22.889431Z","shell.execute_reply":"2022-07-22T18:55:48.828595Z"},"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":"2022-07-22T18:55:48.831684Z","iopub.execute_input":"2022-07-22T18:55:48.832088Z","iopub.status.idle":"2022-07-22T18:55:48.841554Z","shell.execute_reply.started":"2022-07-22T18:55:48.832046Z","shell.execute_reply":"2022-07-22T18:55:48.840097Z"},"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\n\ndf","metadata":{"execution":{"iopub.status.busy":"2022-07-22T18:55:48.843986Z","iopub.execute_input":"2022-07-22T18:55:48.844868Z","iopub.status.idle":"2022-07-22T18:55:48.926278Z","shell.execute_reply.started":"2022-07-22T18:55:48.844813Z","shell.execute_reply":"2022-07-22T18:55:48.924980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T18:55:48.928098Z","iopub.execute_input":"2022-07-22T18:55:48.928789Z","iopub.status.idle":"2022-07-22T18:55:48.979083Z","shell.execute_reply.started":"2022-07-22T18:55:48.928730Z","shell.execute_reply":"2022-07-22T18:55:48.977764Z"},"trusted":true},"execution_count":null,"outputs":[]}]}