{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nfrom PIL import Image\nimport cv2\nimport os\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import preprocessing\nfrom keras.applications.vgg19 import VGG19\nfrom keras.layers import Dense, Dropout, Flatten\nfrom keras.models import Model\nprint(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2a75e418b09752e4ea8901430eda504e9cfd0fdc"},"cell_type":"code","source":"from matplotlib.pyplot import imshow\n%matplotlib inline\nimg = Image.open('../input/train/001513dfcb2ffafc82cccf4d8bbaba97.jpg','r')\nimshow(img)\nnp.array(img).shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"96e6d97d1d7384985caeef3bcf205ada7354219d","scrolled":false},"cell_type":"code","source":"images_list = ([ x for x in os.listdir('../input/train/') if x.endswith('.jpg')])\nlen(images_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3d033c97d4ec4793cfd2f6e1591d20ee0e0aaedd","scrolled":true},"cell_type":"code","source":"df_labels = pd.read_csv('../input/labels.csv')\ndf_labels.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7355065dbb11358c18815749003cf4fa5d688107"},"cell_type":"code","source":"# labels\n# lb_np = np.array(labels)\n# lb_np.shape\n# type(lb_np)\ntrain_slice = 2000\nnum_of_classes = 120","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b3ca018ff1e3fb9f068d1cc823c3511f8e7e8ea0"},"cell_type":"code","source":"labels = df_labels['breed']\nprint(labels.shape)\ntrain_im_lb = df_labels['id']+'.jpg'\nfor i in train_im_lb[:5]:\n    print(i)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b8de8d2510e3ceab84b24887afa898d4a63c65cf","_kg_hide-output":false,"_kg_hide-input":false},"cell_type":"code","source":"#train_images = np.array([cv2.imread('../input/train/'+x) for x in train_im_lb])\ntrain_images = []\nfrom tqdm import tqdm\nfor i in tqdm(train_im_lb[:train_slice]):\n    img = cv2.imread('../input/train/'+i).astype(np.float32)\n    img = cv2.resize(img, (500,300))\n    img = img / 255.0\n    train_images.append(img)\n    \nprint(train_images[0].shape, train_images[13].shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d0b7efcb966d78ddb4ea245197d889a958e3b68f","scrolled":true},"cell_type":"code","source":"x_train = train_images\ny_train = df_labels['breed']\n\ny_train = (pd.get_dummies(y_train))\nprint(y_train.shape)\ny_train = y_train[:train_slice]\nprint(len(y_train),len(x_train))\n#(y_train)[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0a3b9efde6af09f8936285d0429142285d350e12"},"cell_type":"code","source":"X_train, X_valid, Y_train, Y_valid = train_test_split(x_train, y_train, test_size=0.3, random_state=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e6a7a9e98d3c755cc130b7c4bbad75cb0c97b158"},"cell_type":"code","source":"base_model = VGG19(weights = 'imagenet', include_top=False, input_shape=(300, 500, 3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8c019efa97d0d2c6352886ece671d9782568dec5"},"cell_type":"code","source":"base_model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f60521b84fce84a433559cc280e4c16a416d300b"},"cell_type":"code","source":"LastConv = base_model.output\nLastConv = Flatten()(LastConv)\npredictions = Dense(num_of_classes, activation='softmax')(LastConv)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8c10348fae5437ac54157848e9736ccea1963688"},"cell_type":"code","source":"base_model.input\n#model.compile(optimizer='sgd' )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aedf9256765f534f9e3f504ad7889983bedc6294"},"cell_type":"code","source":"model = Model(inputs=base_model.input, outputs=predictions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"db9a012988bdbd96aa765163fa1c0b1347bbbdd7"},"cell_type":"code","source":"model.compile(loss='categorical_crossentropy', \n              optimizer='adam', \n              metrics=['accuracy'])\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"408255305480817ca60b81be3de31b2021f1b621"},"cell_type":"code","source":"model.fit((X_train), (Y_train), epochs=20, validation_data=((X_valid), (Y_valid)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"43dabdd7049c7c98d7fb8b78cae3f435c8cf9a50"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e4639b142de1bc7b328fbb4f42063ba971925a15"},"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}