{"cells":[{"metadata":{"trusted":true,"_uuid":"db955685dfbc621f3707cd9b868a071ef1524e68","collapsed":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)\nfrom random import randint\nfrom tqdm import tqdm\nimport cv2\nimport matplotlib.pyplot as plt\n\nfrom subprocess import check_output\nprint(check_output([\"ls\",\"../input\"]).decode(\"utf8\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"49d860874e3318973d4f19c76526aed68f3adec5","collapsed":true},"cell_type":"code","source":"df_train=pd.read_csv(\"../input/labels.csv\")\ndf_train.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1946960c994ec5e6e6262da0c9ee19efb773c4ba","collapsed":true},"cell_type":"code","source":"ax= pd.value_counts(df_train['breed'],ascending=True).plot(kind='barh',\n                                                         fontsize ='20',\n                                                         title =\"Class Distribution\",\n                                                         figsize = (30,60))\nax.set(xlabel=\"Images per class\",ylabel ='Classes')\nax.xaxis.label.set_size(20)\nax.yaxis.label.set_size(20)\nax.title.set_size(80)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"e99c1ffa201b42f02837fb5b0ccbb7c757b4ae9c"},"cell_type":"code","source":"def plot_images(images, classes):\n    assert len(images) == len(classes) ==9\n    fig,axes = plt.subplots(3,3,figsize =(60,60),sharex =True)\n    fig.subplots_adjust(hspace =0.3, wspace = 0.3)\n    \n    for i,ax in enumerate(axes.flat):\n        ax.imshow(cv2.cvtColor(images[i],cv2.COLOR_BGR2RGB).reshape(img_width,img_height,3),cmap ='hsv')\n        xlabel = \"BreedL{0}\".format(classes[i])\n        ax.set_xlabel(xlabel)\n        ax.xaxis.label.set_size(60)\n        ax.set_xticks([])\n        ax.set_yticks([])\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"35b828a587332707399ea76376c5762c1f710a78","collapsed":true},"cell_type":"code","source":"img_width =250\nimg_height = 250\nimages=[]\nclasses =[]\n\nfor f,breed in tqdm(df_train.values):\n    img = cv2.imread('../input/train/{}.jpg'.format(f))\n    classes.append(breed)\n    images.append(cv2.resize(img,(img_width,img_height)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"af04c05526e047b16977ac605eaec47792fdf698","collapsed":true},"cell_type":"code","source":"random_numbers = [randint(0,len(images)) for p in range(0,9)]\nimages_to_show =[images[i] for i in random_numbers]\nclasses_to_show =[classes[i] for i in random_numbers]\nprint(\"Images to show:{0}\".format(len(images_to_show)))\nprint(\"Classes to show :{0}\".format(len(classes_to_show)))\n\nplot_images(images_to_show,classes_to_show)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5d60ef6745e1f76f250f138cf3ca2fd9d454f5ab","collapsed":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom random import randint\nfrom tqdm import tqdm\nimport cv2\nimport matplotlib.pyplot as plt\n\nfrom subprocess import check_output\nprint(check_output([\"ls\",\"../input\"]).decode(\"utf8\"))\n\ndf_train = pd.read_csv(\"../input/labels.csv\")\ndf_train.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4cfa1631aedd77d1c6ec132ab9a51492b2e2f919","collapsed":true},"cell_type":"code","source":"ax = pd.value_counts(df_train['breed'],ascending =True).plot(kind ='barh',\n                                                            fontsize ='40',\n                                                            title=\"Class Distribution\",\n                                                            figsize=(50,100))\nax.set(xlabel =\"Images per class\",ylabel =\"Classes\")\nax.xaxis.label.set_size(40)\nax.yaxis.label.set_size(40)\nax.title.set_size(60)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"fb20590a33d3342922ce18264494db792db9b7cc"},"cell_type":"code","source":"def plot_images(images,classes):\n    assert len(images) == len(classes) ==9\n    fig,axes = plt.subplots(3,3,figsize = (60,60),sharex=True)\n    fig.subplots_adjust(hspace=0.3,wspace=0.3)\n    \n    for i,ax in enumerate(axes.flat):\n        ax.imshow(cv2.cvtColor(images[i],cv2.COLOR_BGR2RGB).reshape(img_width,img_height,3),cmap='hsv')\n        xlabel =\"Breed:{0}\".format(classes[i])\n        \n        #show the classes as the label on the x_axis\n        ax.set_xlabel(xlabel)\n        ax.xaxis.label.set_size(60)\n        #remove ticks from the plot\n        ax.set_xticks([])\n        ax.set_yticks([])\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c13c65df8c7e8f5de284488002c02f122f0dc542","collapsed":true},"cell_type":"code","source":"img_width=250\nimg_height =250\nclasses =[]\nimages=[]\nfor f,breed in tqdm(df_train.values):\n    img = cv2.imread('../input/train/{}.jpg'.format(f))\n    classes.append(breed)\n    images.append(cv2.resize(img,(img_width,img_height)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e35ed09cc720e1981f94780d1ab9e84ddfc9f0a5","collapsed":true},"cell_type":"code","source":"random_numbers =[randint(0,len(images)) for p in range(0,9)]\nimages_to_show =[images[i] for i in random_numbers]\nclasses_to_show =[classes[i] for i in random_numbers]\nprint('Images to show:{0}'.format(len(images_to_show)))\nprint(\"classes to show:{0}\".format(len(classes_to_show)))\n\nplot_images(images_to_show,classes_to_show)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c3d03ab96165843e9a657e0d01dfd33ad9c63991"},"cell_type":"markdown","source":"**Keras starter***"},{"metadata":{"trusted":true,"_uuid":"dd58618c878a42bbd950a018eca9328bfec78182","collapsed":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport keras\nfrom keras.applications.vgg19 import VGG19\nfrom keras.models import Model\nfrom keras.layers import Dense,Dropout,Flatten\n\nimport os\nfrom tqdm import tqdm\nfrom sklearn import preprocessing\nfrom sklearn.model_selection import train_test_split\nimport cv2\n\nfrom subprocess import check_output\nprint(check_output([\"ls\",\"../input\"]).decode(\"utf8\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fde6fbe2122c762d9e9c5a0561b43db8bd735e2e","collapsed":true},"cell_type":"code","source":"df_train =pd.read_csv('../input/labels.csv')\ndf_test = pd.read_csv('../input/sample_submission.csv')\ndf_train.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"59c9fdef26d7d663f1b2b3a04c92dc02c2dd2c57"},"cell_type":"code","source":"# we can see that breed nees to be one-hot encoded for the final submission\ntargets_series = pd.Series(df_train['breed'])\none_hot = pd.get_dummies(targets_series,sparse= True)\n\none_hot_labels = np.asarray(one_hot)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d4c3352dbd025d4a77b272dd9fb732fe3d9cc803","collapsed":true},"cell_type":"code","source":"# read images for test and tain via for loop in csv files. resise image to 90 X 90px\n\nim_size =90\nx_train =[]\ny_train =[]\nx_test =[]\n\ni =0\nfor f,breed in tqdm(df_train.values):\n    img = cv2.imread('../input/train/{}.jpg'.format(f))\n    label = one_hot_labels[i]\n    x_train.append(cv2.resize(img,(im_size,im_size)))\n    y_train.append(label)\n    i +=1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2b2c2bcbb1c6aeffc82818afeee2d43d3d5624d6","collapsed":true},"cell_type":"code","source":"for f in tqdm(df_test['id'].values):\n    img = cv2.imread('../input/test/{}.jpg'.format(f))\n    x_test.append(cv2.resize(img,(im_size,im_size)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"65c1f3b3a9027d44c5da988a9eec336fb510e581"},"cell_type":"code","source":"y_train_raw = np.array(y_train,np.uint8)\nx_train_raw = np.array(x_train,np.float32)/255.\nx_test = np.array(x_test,np.float32)/255.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"454b2fffb2102113f82632453631e9cc7329b7aa","collapsed":true},"cell_type":"code","source":"#check the shape of the output to make sure everything went as expected\nprint(x_train_raw.shape)\nprint(y_train_raw.shape)\nprint(x_test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"060147b7614ed7ab3e2571b49e517bceac5cca84"},"cell_type":"code","source":"#put 120 breeds in a num_class variables\nnum_class = y_train_raw.shape[1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"f99b795ca9def67d50b3ebe9551d22e9d156c0ab"},"cell_type":"code","source":"#put validation sets 30% from training set. make sure it can contains images \n#from every class.\nX_train,X_valid,Y_train,Y_valid =train_test_split(x_train_raw,\n                                                 y_train_raw,\n                                                 test_size =0.3,\n                                                 random_state =1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4f06ed7e9e891a0d13836d031d8d878016be5733","collapsed":true},"cell_type":"code","source":"#CNN via VGG19\nbase_model = VGG19(weights = None, include_top =False, input_shape=(im_size,im_size,3))\n\n#add a new top layer\nx= base_model.output\nx =Flatten()(x)\npredictions =Dense(num_class,activation='softmax')(x)\n\n#This is the model we will train\nmodel =Model(inputs=base_model.input,outputs =predictions)\n\n# First: train only the top layers(which were randomly initialized)\nfor layer in base_model.layers:\n    layer.trainable = False\n\nmodel.compile(loss='categorical_crossentropy',\n             optimizer ='adam',\n             metrics=['accuracy'])\n\ncallbacks_list =[keras.callbacks.EarlyStopping(monitor ='val_acc',\n                                              patience =3,\n                                              verbose =1)]\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c54c70cee9e40020726f48c68365e764a7fd9290","collapsed":true},"cell_type":"code","source":"model.fit(X_train,Y_train,epochs =1, validation_data =(X_valid,Y_valid),\n         verbose =1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4564579a9237f7c238846ee0d78324309db11766","collapsed":true},"cell_type":"code","source":"# accuracy is low here because we are not taking advantage of the pre-trained weights as they cannot be downloaded in the kernel.\n#This means we are training the weights from scratch and we have only 1 epoch due to the hardware constrains in the kernel.\n\n#next we will make out predictions.\n\npreds = model.predict(X_test,verbose =1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"1d3eb7e20aa0e6afa73f4c4c4ada1349b6152887"},"cell_type":"code","source":"sub = pd.DataFrame(preds)\n#set column names to those generated by the one_hot encoding earlier\ncol_names =one_hot.columns.values\nsub,columns = col.names\n# Insert the column id from the sample_submission at the start of the data frame\nsub.insert(0,'id',df_test['id'])\nsub.head(5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"11999eb41fe835dc5f0a68a426a7eb1178790582","collapsed":true},"cell_type":"code","source":"from tqdm import tqdm\n\nfor i in tqdm(range(1000)):\n    pass\n\nfor char in tqdm([\"a\",\"b\",\"c\",\"d\"]):\n    pass\nfrom tqdm import trange\nfor i in trange(100):\n    pass\n\npbar = tqdm([\"a\",\"b\",\"c\",\"d\"])\nfor char in pbar:\n    print(char)\n    pbar.set_description(\"Processing %s\" %char)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"3d65397aed95a611f23e8c0831945b302426718a"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"8d6390650f2cc98af395f7b7dbb05c6eae8b3212"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"45d384ac1219e6e7ab59e6933f4aa851eecf74bd"},"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.5","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}