{"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":{"execution":{"iopub.status.busy":"2023-07-26T13:44:30.580911Z","iopub.execute_input":"2023-07-26T13:44:30.582012Z","iopub.status.idle":"2023-07-26T13:44:30.600270Z","shell.execute_reply.started":"2023-07-26T13:44:30.581972Z","shell.execute_reply":"2023-07-26T13:44:30.598847Z"},"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":"2023-07-26T13:44:30.602772Z","iopub.execute_input":"2023-07-26T13:44:30.604257Z","iopub.status.idle":"2023-07-26T13:44:48.252213Z","shell.execute_reply.started":"2023-07-26T13:44:30.604223Z","shell.execute_reply":"2023-07-26T13:44:48.250633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd train","metadata":{"execution":{"iopub.status.busy":"2023-07-26T13:44:48.253891Z","iopub.execute_input":"2023-07-26T13:44:48.254272Z","iopub.status.idle":"2023-07-26T13:44:48.268556Z","shell.execute_reply.started":"2023-07-26T13:44:48.254238Z","shell.execute_reply":"2023-07-26T13:44:48.265633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir cats","metadata":{"execution":{"iopub.status.busy":"2023-07-26T13:44:48.270621Z","iopub.execute_input":"2023-07-26T13:44:48.274368Z","iopub.status.idle":"2023-07-26T13:44:49.291488Z","shell.execute_reply.started":"2023-07-26T13:44:48.274335Z","shell.execute_reply":"2023-07-26T13:44:49.290232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir dogs","metadata":{"execution":{"iopub.status.busy":"2023-07-26T13:44:49.295391Z","iopub.execute_input":"2023-07-26T13:44:49.295736Z","iopub.status.idle":"2023-07-26T13:44:50.238804Z","shell.execute_reply.started":"2023-07-26T13:44:49.295709Z","shell.execute_reply":"2023-07-26T13:44:50.237493Z"},"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-26T13:44:50.240580Z","iopub.execute_input":"2023-07-26T13:44:50.240958Z","iopub.status.idle":"2023-07-26T13:44:51.443304Z","shell.execute_reply.started":"2023-07-26T13:44:50.240925Z","shell.execute_reply":"2023-07-26T13:44:51.442200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ../","metadata":{"execution":{"iopub.status.busy":"2023-07-26T13:44:51.444923Z","iopub.execute_input":"2023-07-26T13:44:51.445305Z","iopub.status.idle":"2023-07-26T13:44:51.451738Z","shell.execute_reply.started":"2023-07-26T13:44:51.445273Z","shell.execute_reply":"2023-07-26T13:44:51.450805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir validation","metadata":{"execution":{"iopub.status.busy":"2023-07-26T13:44:51.453398Z","iopub.execute_input":"2023-07-26T13:44:51.454131Z","iopub.status.idle":"2023-07-26T13:44:53.535509Z","shell.execute_reply.started":"2023-07-26T13:44:51.454100Z","shell.execute_reply":"2023-07-26T13:44:53.534237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd validation","metadata":{"execution":{"iopub.status.busy":"2023-07-26T13:44:53.539154Z","iopub.execute_input":"2023-07-26T13:44:53.539496Z","iopub.status.idle":"2023-07-26T13:44:53.546087Z","shell.execute_reply.started":"2023-07-26T13:44:53.539467Z","shell.execute_reply":"2023-07-26T13:44:53.545110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir cats","metadata":{"execution":{"iopub.status.busy":"2023-07-26T13:44:53.547474Z","iopub.execute_input":"2023-07-26T13:44:53.548533Z","iopub.status.idle":"2023-07-26T13:44:54.512655Z","shell.execute_reply.started":"2023-07-26T13:44:53.548499Z","shell.execute_reply":"2023-07-26T13:44:54.511310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir dogs","metadata":{"execution":{"iopub.status.busy":"2023-07-26T13:44:54.514448Z","iopub.execute_input":"2023-07-26T13:44:54.514820Z","iopub.status.idle":"2023-07-26T13:44:55.505474Z","shell.execute_reply.started":"2023-07-26T13:44:54.514792Z","shell.execute_reply":"2023-07-26T13:44:55.504016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ../","metadata":{"execution":{"iopub.status.busy":"2023-07-26T13:44:55.510905Z","iopub.execute_input":"2023-07-26T13:44:55.513347Z","iopub.status.idle":"2023-07-26T13:44:55.524221Z","shell.execute_reply.started":"2023-07-26T13:44:55.513307Z","shell.execute_reply":"2023-07-26T13:44:55.523237Z"},"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-26T13:44:55.527381Z","iopub.execute_input":"2023-07-26T13:44:55.528005Z","iopub.status.idle":"2023-07-26T13:44:55.958348Z","shell.execute_reply.started":"2023-07-26T13:44:55.527973Z","shell.execute_reply":"2023-07-26T13:44:55.957346Z"},"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-26T13:44:55.962809Z","iopub.execute_input":"2023-07-26T13:44:55.963463Z","iopub.status.idle":"2023-07-26T13:50:35.287725Z","shell.execute_reply.started":"2023-07-26T13:44:55.963427Z","shell.execute_reply":"2023-07-26T13:50:35.286569Z"},"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-26T13:50:35.289075Z","iopub.execute_input":"2023-07-26T13:50:35.289461Z","iopub.status.idle":"2023-07-26T13:50:35.526992Z","shell.execute_reply.started":"2023-07-26T13:50:35.289428Z","shell.execute_reply":"2023-07-26T13:50:35.526006Z"},"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-26T13:50:35.528446Z","iopub.execute_input":"2023-07-26T13:50:35.528809Z","iopub.status.idle":"2023-07-26T14:05:41.277840Z","shell.execute_reply.started":"2023-07-26T13:50:35.528775Z","shell.execute_reply":"2023-07-26T14:05:41.276756Z"},"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-26T14:05:41.280943Z","iopub.execute_input":"2023-07-26T14:05:41.281756Z","iopub.status.idle":"2023-07-26T14:05:41.288971Z","shell.execute_reply.started":"2023-07-26T14:05:41.281719Z","shell.execute_reply":"2023-07-26T14:05:41.288019Z"},"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-26T14:05:41.290428Z","iopub.execute_input":"2023-07-26T14:05:41.291278Z","iopub.status.idle":"2023-07-26T14:05:41.369287Z","shell.execute_reply.started":"2023-07-26T14:05:41.291243Z","shell.execute_reply":"2023-07-26T14:05:41.368371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2023-07-26T14:05:41.370627Z","iopub.execute_input":"2023-07-26T14:05:41.370949Z","iopub.status.idle":"2023-07-26T14:05:41.392765Z","shell.execute_reply.started":"2023-07-26T14:05:41.370917Z","shell.execute_reply":"2023-07-26T14:05:41.391548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-07-26T14:05:41.394356Z","iopub.execute_input":"2023-07-26T14:05:41.394981Z","iopub.status.idle":"2023-07-26T14:05:41.451159Z","shell.execute_reply.started":"2023-07-26T14:05:41.394944Z","shell.execute_reply":"2023-07-26T14:05:41.450289Z"},"trusted":true},"execution_count":null,"outputs":[]}]}