{"nbformat": 4, "cells": [{"cell_type": "code", "execution_count": null, "outputs": [], "metadata": {"collapsed": true, "_uuid": "0574ae9cf202788587b74329b02e051ab88f005b", "_cell_guid": "6fd008ce-cdea-4908-945b-74676088fd3c"}, "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", "\n", "import numpy as np # linear algebra\n", "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n", "import os\n", "from PIL import Image\n", "from skimage.transform import resize\n", "from random import shuffle\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\n", "\n", "from subprocess import check_output\n", "print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n", "\n", "# Any results you write to the current directory are saved as output."]}, {"cell_type": "code", "execution_count": null, "outputs": [], "metadata": {"collapsed": true, "_uuid": "f9044c11e2b52edecd1a86574309e885094d3a20", "_cell_guid": "f59874a4-e8b4-47e1-b488-34bf0f2b42fb"}, "source": ["list_paths = []\n", "for subdir, dirs, files in os.walk(\"../input\"):\n", "    for file in files:\n", "        #print os.path.join(subdir, file)\n", "        filepath = subdir + os.sep + file\n", "        list_paths.append(filepath)"]}, {"cell_type": "code", "execution_count": null, "outputs": [], "metadata": {"collapsed": true, "_uuid": "982cf9b8f6b9b68b2a476a57d4846db22c76809f", "_cell_guid": "f4ceca4d-77df-4987-a20b-dbaae3ba89e0"}, "source": ["list_train = [filepath for filepath in list_paths if \"train/\" in filepath]\n", "shuffle(list_train)\n", "list_test = [filepath for filepath in list_paths if \"test/\" in filepath]\n", "\n", "list_train = list_train\n", "list_test = list_test\n", "index = [os.path.basename(filepath) for filepath in list_test]"]}, {"cell_type": "code", "execution_count": null, "outputs": [], "metadata": {"collapsed": true, "_uuid": "fcf5feb59fe568fd999ad95091e067b85e4cf1d2", "_cell_guid": "c6062575-203b-4062-ae57-7ffbc872f177"}, "source": ["list_classes = list(set([os.path.dirname(filepath).split(os.sep)[-1] for filepath in list_paths if \"train\" in filepath]))"]}, {"cell_type": "code", "execution_count": null, "outputs": [], "metadata": {"collapsed": true, "_uuid": "db99a95c9fd22162ccadc67835dc240a8874f838", "_cell_guid": "1737834f-8eea-4061-9210-53709a91d623"}, "source": ["list_classes = ['Sony-NEX-7',\n", " 'Motorola-X',\n", " 'HTC-1-M7',\n", " 'Samsung-Galaxy-Note3',\n", " 'Motorola-Droid-Maxx',\n", " 'iPhone-4s',\n", " 'iPhone-6',\n", " 'LG-Nexus-5x',\n", " 'Samsung-Galaxy-S4',\n", " 'Motorola-Nexus-6']"]}, {"cell_type": "code", "execution_count": null, "outputs": [], "metadata": {"collapsed": true, "_uuid": "75a608a742d3d3a1fae436aeca898f24dc174c09", "_cell_guid": "2e3ef3b1-9232-4f3e-aff3-771812633f69"}, "source": ["def get_class_from_path(filepath):\n", "    return os.path.dirname(filepath).split(os.sep)[-1]\n", "\n", "def read_and_resize(filepath):\n", "    im_array = np.array(Image.open((filepath)), dtype=\"uint8\")\n", "    pil_im = Image.fromarray(im_array)\n", "    new_array = np.array(pil_im.resize((256, 256)))\n", "    return new_array/255\n", "\n", "def label_transform(labels):\n", "    labels = pd.get_dummies(pd.Series(labels))\n", "    label_index = labels.columns.values\n", "\n", "    return labels, label_index\n"]}, {"cell_type": "code", "execution_count": null, "outputs": [], "metadata": {"collapsed": true, "_uuid": "5e12f38e36ae6d29f2edb1a1ece112bfc0008875", "_cell_guid": "0f45876d-1c70-4388-bd3c-47b67c7c140a"}, "source": ["X_train = np.array([read_and_resize(filepath) for filepath in list_train])\n", "X_test = np.array([read_and_resize(filepath) for filepath in list_test])\n", "\n", "labels = [get_class_from_path(filepath) for filepath in list_train]\n", "y, label_index = label_transform(labels)\n", "y = np.array(y)"]}, {"cell_type": "code", "execution_count": null, "outputs": [], "metadata": {"collapsed": true, "_uuid": "ab7de25beab727c6ca14ca24a9302538b70e8238", "_cell_guid": "9fe6b2ae-d956-49c6-9658-6f02bfab42b6"}, "source": ["ROWS=256\n", "COLS=256\n", "from keras.models import Sequential,model_from_json\n", "from keras.models import Model\n", "from keras.callbacks import ModelCheckpoint, LearningRateScheduler, EarlyStopping, ReduceLROnPlateau, TensorBoard\n", "from keras import optimizers, losses, activations, models\n", "from keras.layers import Convolution2D, Dense, Input, Flatten, Dropout, MaxPooling2D, BatchNormalization, GlobalMaxPool2D, Concatenate\n", "from keras import applications\n", "input_shape = (ROWS, COLS, 3)\n", "nclass = len(label_index)\n", "\n", "base_model = applications.VGG19(weights='imagenet', include_top=False, input_shape=(ROWS, COLS,3))\n", "\n", "add_model = Sequential()\n", "add_model.add(Flatten(input_shape=base_model.output_shape[1:]))\n", "add_model.add(Dense(256, activation='relu'))\n", "add_model.add(Dense(nclass, activation='softmax'))\n", "\n", "model = Model(inputs=base_model.input, outputs=add_model(base_model.output))\n", "model.compile(loss='categorical_crossentropy', optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),\n", "              metrics=['accuracy'])\n", "\n", "model_json = model.to_json()\n", "with open(\"model.json\", \"w\") as json_file:\n", "    json_file.write(model_json)"]}, {"cell_type": "code", "execution_count": null, "outputs": [], "metadata": {"collapsed": true, "_uuid": "086bdeb47ed7022e95b46e3c6c3c2ee0eda147d3", "_cell_guid": "d111f13d-c7e8-4b5a-a20e-34a1e67260c9"}, "source": ["\n", "#model = get_model()\n", "file_path=\"weights.best.hdf5\"\n", "\n", "checkpoint = ModelCheckpoint(file_path, monitor='val_acc', verbose=1, save_best_only=True, mode='max')\n", "\n", "early = EarlyStopping(monitor=\"val_acc\", mode=\"max\", patience=15)\n", "\n", "callbacks_list = [checkpoint, early] #early\n", "\n", "history = model.fit(X_train, y, validation_split=0.1, epochs=50, shuffle=True, verbose=2,\n", "                              callbacks=callbacks_list)\n", "\n", "#print(history)\n", "\n", "model.load_weights(file_path)"]}, {"cell_type": "code", "execution_count": null, "outputs": [], "metadata": {"collapsed": true, "_uuid": "641d27a2a045c06889dbcdf48ce835be40d5758c", "_cell_guid": "1bbc75a8-6711-4b37-af02-b1069eb69fbe"}, "source": ["predicts = model.predict(X_test)\n", "predicts = np.argmax(predicts, axis=1)\n", "predicts = [label_index[p] for p in predicts]\n", "\n", "df = pd.DataFrame(columns=['fname', 'camera'])\n", "df['fname'] = index\n", "df['camera'] = predicts\n", "df.to_csv(\"sub1.csv\", index=False)"]}], "metadata": {"kernelspec": {"display_name": "Python 3", "name": "python3", "language": "python"}, "language_info": {"pygments_lexer": "ipython3", "version": "3.6.4", "codemirror_mode": {"version": 3, "name": "ipython"}, "mimetype": "text/x-python", "nbconvert_exporter": "python", "file_extension": ".py", "name": "python"}}, "nbformat_minor": 1}