{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"scrolled":true},"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 in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\nimport keras\nfrom keras.applications import VGG16\nfrom keras.applications.vgg16 import preprocess_input\nfrom keras.models import Model\nfrom keras.layers import GlobalAveragePooling2D,Dense,Flatten,Dropout\nimport shutil\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8014885d3e0cecd1ff84b95aa6b0059e1a93208e"},"cell_type":"code","source":"df=pd.read_csv('../input/dog-breed-identification/labels.csv')\nlabels=df.as_matrix()\nprint(labels.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1369db92832d664b520e045ba28550b5120e0b72"},"cell_type":"code","source":"print(os.listdir('../input'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c9c75d1c7ef9ac44f096f8256851f22b35b57e91"},"cell_type":"code","source":"folder_path='../input/dog-breed-identification/train/'\ndirectory=os.fsencode(folder_path)\nnew_folder_path='./new/train_new/'\nnew_folder_directory=os.fsencode(new_folder_path)\nif not os.path.exists(new_folder_directory):\n        os.makedirs(new_folder_directory)\nfor file in os.listdir(folder_path):\n    file_name=os.fsdecode(file)\n    code=file_name[0:-4]\n    index_of_breed=np.where(labels[:,0]==code)\n    name_of_breed=labels[index_of_breed,1]\n    print(name_of_breed[0][0])\n    new_path=str(new_folder_path+name_of_breed[0][0])\n    new_path_directory=os.fsencode(new_path)\n    if not os.path.exists(new_path_directory):\n        os.makedirs(new_path_directory)\n    old_dir=os.fsencode(str(folder_path+file_name))\n    new_dir=os.fsencode(str(new_path))\n    shutil.copy(old_dir,new_dir)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"147a895d5fe5b0ef1255b259bd753424513dfec9"},"cell_type":"code","source":"base_model=VGG16(include_top=False,weights='imagenet')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5545927036240de2d4a65066f4a3764174f46da7"},"cell_type":"code","source":"for i,layer in enumerate(base_model.layers):\n    print(i,layer.name)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"717322d93654bcd40f8993c5f729dab847f50bdb"},"cell_type":"code","source":"x=base_model.output\nx=GlobalAveragePooling2D()(x)\nx=Dense(1024,activation='relu')(x)\nx=Dropout(0.1)(x)\nx=Dense(1024,activation='relu')(x)\nx=Dropout(0.1)(x)\nx=Dense(1024,activation='relu')(x)\nx=Dropout(0.1)(x)\nx=Dense(512,activation='relu')(x)\nx=Dropout(0.1)(x)\npreds=Dense(120,activation='softmax')(x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3e6adf3cef949dd20ca994e72f45fbdf25d629bc"},"cell_type":"code","source":"model=Model(inputs=base_model.input,outputs=preds)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5f8056e6b40f68d3235c3e46c9fe3324a7970e60"},"cell_type":"code","source":"for layer in base_model.layers[:18]:\n    layer.trainable=False\nfor layer in base_model.layers[18:]:\n    layer.trainable=True","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"66ce4cb6a94e58e988232cce3754dd13d4f93c5e"},"cell_type":"code","source":"model.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bd7a3b5a4c0d0f740d3621f0a16bcab49a470e93"},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\ndatagen=ImageDataGenerator(validation_split=0.10,\n                            preprocessing_function=preprocess_input,\n                           rotation_range=15,\n                            shear_range=0.1,\n                            zoom_range=0.1,\n                            horizontal_flip=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"790836227562d417fd544f6ddb56494729535e0c"},"cell_type":"code","source":"train_generator=datagen.flow_from_directory('./new/train_new/',\n                                                 target_size=(224,224),\n                                                 color_mode='rgb',\n                                                 batch_size=32,\n                                                 class_mode='categorical',\n                                                 shuffle=True,\n                                           subset='training')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b99e8e5f393a36011d1d5d5fa2a3dbaee7ac8ac7"},"cell_type":"code","source":"validation_generator=datagen.flow_from_directory('./new/train_new/',\n                                           target_size=(224,224),\n                                           color_mode='rgb',\n                                           batch_size=32,\n                                           class_mode='categorical',\n                                           shuffle=True,\n                                           subset='validation')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7138a43c152f24aa8fca3b4f7d6f76fbeb73d79f","scrolled":true},"cell_type":"code","source":"step_size_train=train_generator.n//train_generator.batch_size\nstep_size_valid=validation_generator.n//validation_generator.batch_size\nmodel.fit_generator(generator=train_generator,\n                   steps_per_epoch=step_size_train,\n                   validation_data=validation_generator,\n                   validation_steps=step_size_valid,\n                   epochs=10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"625a19b1dcb989ce90bf9d1e5ae30a1ee256dcac"},"cell_type":"code","source":"for i,layer in enumerate(model.layers):\n    print(i,layer.name)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"46db17265bb15bd1e9a16e4b622fc6081e76b27e"},"cell_type":"code","source":"for layer in model.layers[:14]:\n    layer.trainable=False\nfor layer in model.layers[14:]:\n    layer.trainable=True","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"484b61248a3da40765fee4ee1d280f7f382d6dff"},"cell_type":"code","source":"model.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e5b0a6135f830a61428d4355d19fe4ffd7ab7a47"},"cell_type":"code","source":"step_size_train=train_generator.n//train_generator.batch_size\nstep_size_valid=validation_generator.n//validation_generator.batch_size\nmodel.fit_generator(generator=train_generator,\n                   steps_per_epoch=step_size_train,\n                   validation_data=validation_generator,\n                   validation_steps=step_size_valid,\n                   epochs=10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"c9435135b04954ef17c3080f73febdb9d809b2fd"},"cell_type":"code","source":"test_datagen=ImageDataGenerator(rescale=1/255)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ca72d4b77f7efba52db5154b8b9d1e78246098e5","collapsed":true},"cell_type":"code","source":"test_generator=test_datagen.flow_from_directory('../input/dog-breed-identification/test/',\n                                               target_size=(224,224),\n                                               color_mode='rgb',\n                                               class_mode=None)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"90d0676cbdfd881feff8ae642b1be0ed6268ff7c"},"cell_type":"code","source":"test_path='../input/dog-breed-identification/test/'\ntest_dir=os.fsencode(test_path)\nsub=np.zeros([10357,121])\ni=0\nfor file in os.listdir(test_dir):\n    filename=os.fsdecode(file)\n    path=str(test_path+filename)\n    image=keras.preprocessing.image.img_to_array(keras.preprocessing.image.load_img(path,target_size=(224,224)))\n    pred=model.predict(np.expand_dims(image,axis=0))\n    np.append(sub,(filename[:-4],preds[0,:]))\n    print(i)\n    i+=1\n    if i==10:\n        break","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4f0256ac35410113d6ee974ae44a3b697dd0fe52"},"cell_type":"code","source":"print(sub.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"662a41c61ea7952a990194633ad3659f0a7162a2"},"cell_type":"code","source":"print(sub[0,0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"1f580b2c653c0ea1c90c0a7d081dcd32c96b270a"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}