{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport os\nprint(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"from tensorflow.python.keras.applications import InceptionV3\nfrom tensorflow.python.keras.models import Sequential\nfrom tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D, Activation, Flatten, Dropout, BatchNormalization\nfrom tensorflow.python.keras import optimizers, regularizers\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1395387c3341d53e9ca207245c9d8ff8d5492f10"},"cell_type":"code","source":"traindf=pd.read_csv('../input/dog-breed-identification/labels.csv')\ntestdf=pd.read_csv('../input/dog-breed-identification/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"660df060d7e06f762bce6cfa23898e04ca5020ba"},"cell_type":"code","source":"traindf.head(5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"78cba25909a86a4d574e699d4e95b1062f5db538"},"cell_type":"code","source":"num_classes = 120\ninception_weights_path = '../input/inceptionv3/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5'\n\nmy_new_model = Sequential()\nmy_new_model.add(InceptionV3(include_top=False, pooling='avg', weights=inception_weights_path))\nmy_new_model.add(Dense(512))\nmy_new_model.add(Activation('relu'))\nmy_new_model.add(Dropout(0.5))\nmy_new_model.add(Dense(num_classes, activation='softmax'))\n\n# Say not to train first layer model. It is already trained\nmy_new_model.layers[0].trainable = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6674c7169a88ab1a478881ae4ea1c1002d15e590"},"cell_type":"code","source":"my_new_model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])\n# my_new_model.compile(optimizers.rmsprop(lr=0.0001, decay=1e-6),loss=\"categorical_crossentropy\",metrics=[\"accuracy\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bd6ed38f62089f7dd93a428b28cb15e98916b547"},"cell_type":"code","source":"from tensorflow.python.keras.applications.inception_v3 import preprocess_input\nfrom tensorflow.python.keras.preprocessing.image import ImageDataGenerator\n\nimage_size = 299\ndatagen = ImageDataGenerator(preprocessing_function=preprocess_input, \n                             rescale=1./255.,\n                             horizontal_flip=True,\n                             width_shift_range = 0.2,\n                             height_shift_range = 0.2,\n                             validation_split=0.25)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"20b5d2cdb5718bbf9122fdac8272c10dcfc084f5"},"cell_type":"code","source":"train_generator=datagen.flow_from_dataframe(\n                        dataframe=traindf,\n                        directory=\"../input/dog-breed-identification/train/\",\n                        x_col=\"id\",\n                        y_col=\"breed\",\n                        has_ext=False,\n                        subset=\"training\",\n                        batch_size=32,\n                        seed=42,\n                        shuffle=True,\n                        class_mode=\"categorical\",\n                        target_size=(image_size, image_size))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9d654e592e5ff968475822c263c803c901026969"},"cell_type":"code","source":"valid_generator=datagen.flow_from_dataframe(\n                        dataframe=traindf,\n                        directory=\"../input/dog-breed-identification/train/\",\n                        x_col=\"id\",\n                        y_col=\"breed\",\n                        has_ext=False,\n                        subset=\"validation\",\n                        batch_size=1,\n                        seed=42,\n                        shuffle=True,\n                        class_mode=\"categorical\",\n                        target_size=(image_size, image_size))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a947c9822fa9a468649ebf7189d2a66c72c3c562"},"cell_type":"code","source":"test_datagen=ImageDataGenerator(preprocessing_function=preprocess_input,rescale=1./255.)\n\ntest_generator=test_datagen.flow_from_dataframe(\n                            dataframe=testdf,\n                            directory=\"../input/dog-breed-identification/test/\",\n                            x_col=\"id\",\n                            y_col=None,\n                            has_ext=False,\n                            batch_size=1,\n                            seed=42,\n                            shuffle=False,\n                            class_mode=None,\n                            target_size=(image_size, image_size))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1ae05aa2a5f25d39d576d2e1ef10a02b5b158d5d","scrolled":true},"cell_type":"code","source":"STEP_SIZE_TRAIN=train_generator.n\nSTEP_SIZE_VALID=valid_generator.n\n\nprint(STEP_SIZE_TRAIN)\nprint(STEP_SIZE_VALID)\n\nmy_new_model.fit_generator(generator=train_generator,\n                    steps_per_epoch=STEP_SIZE_TRAIN,\n                    validation_data=valid_generator,\n                    validation_steps=STEP_SIZE_VALID,\n                    epochs=1\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0ba3543c1fd1b64c3efd48ae7b3bee43b7f48010"},"cell_type":"code","source":"my_new_model.evaluate_generator(generator=valid_generator)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4a22b0a2c1e2173fd9327abf4dd78f58acf00ac0"},"cell_type":"code","source":"test_generator.reset()\npred=my_new_model.predict_generator(test_generator,verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"05d7c646e1685870d859c113518de36ef3aae0b9"},"cell_type":"code","source":"labels = (train_generator.class_indices)\nlabels = list(labels.keys())\ndf = pd.DataFrame(data=pred,\n                 columns=labels)\n\ncolumns = list(df)\ncolumns.sort()\ndf = df.reindex(columns=columns)\n\nfilenames = testdf[\"id\"]\ndf[\"id\"]  = filenames\n\ncols = df.columns.tolist()\ncols = cols[-1:] + cols[:-1]\ndf = df[cols]\ndf.head(5)\n\ndf.to_csv(\"results_tl_01.csv\",index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"295a583f9da0bbcf9b77ca955c1d2cd7a7edaeb5"},"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}