{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":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)\nfrom keras.applications.densenet import DenseNet201 \nfrom keras.models import Model,load_model\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras import backend as K\nfrom keras.utils import to_categorical\nfrom keras.layers import Dense,Input\nimport seaborn as sns\nimport cv2\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\nimport matplotlib.pyplot as py\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n%matplotlib inline\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train=pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')\ntest=pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')\nval=pd.read_csv('/kaggle/input/Kannada-MNIST/Dig-MNIST.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train1=train.drop(['label'],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train1=train1.values.reshape(train.shape[0],28,28)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def visualize(train1):\n    l1=np.random.randint(1,60000,5)\n    l2=np.random.randint(1,60000,5)\n    fig,ax=py.subplots(2,5,figsize=(28,28))\n    for i in range(5):\n        ax[0][i].text(14,0.5,train.loc[l1[i],'label'],fontsize=12)\n        ax[1][i].text(14,0.5,train.loc[l2[i],'label'],fontsize=12)\n        ax[0][i].imshow(train1[l1[i]],cmap='cividis')\n        ax[1][i].imshow(train1[l2[i]],cmap='cividis')\n    #sns.heatmap(train1[l1[i]],ax=ax[0][i],)\n    #sns.heatmap(train1[l2[i]],ax=ax[1][i],)\n#py.tight_layout()\nvisualize(train1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train2=train1/255","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"visualize(train2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train,x_val,y_train,y_val=train_test_split(train2,to_categorical(train['label']),test_size=0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_rows=28\nimg_cols=28\nif K.image_data_format=='channels_first':\n    x_train = x_train.reshape(x_train.shape[0], 1,img_cols,img_rows)\n    x_val = x_val.reshape(x_val.shape[0], 1,img_cols,img_rows)\n    input_shape = (1,img_cols,img_rows)\nelse:\n    x_train = x_train.reshape(x_train.shape[0],img_cols,img_rows,1)\n    x_val = x_val.reshape(x_val.shape[0],img_cols,img_rows,1)\n    input_shape = (img_cols,img_rows,1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"datagen=ImageDataGenerator(horizontal_flip=True,\n    vertical_flip=True,\n    rotation_range=30,\n\tzoom_range=0.15,\n\twidth_shift_range=0.2,\n\theight_shift_range=0.2,\n\tshear_range=0.15,\n\tfill_mode=\"nearest\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_iter=datagen.flow(x_train,y_train,)\nval_iter=datagen.flow(x_val,y_val)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cache_dir = os.path.expanduser(os.path.join('~', '.keras'))\nif not os.path.exists(cache_dir):\n    os.makedirs(cache_dir)\nmodels_dir = os.path.join(cache_dir, 'models')\nif not os.path.exists(models_dir):\n    os.makedirs(models_dir)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!cp /kaggle/input/densenet201-imagenet-pretrained-weights/densenet201_weights_tf_dim_ordering_tf_kernels_notop.h5 ~/.keras/models/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"de=DenseNet201(include_top=False,weights='imagenet')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x=de.output","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x=Dense(units=10,activation='softmax')(x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod=Model(inputs=de.input,outputs=x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for layer in de.layers:\n    layer.trainable = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"in1=Input(batch_shape=(32,28,28,1),shape=(28,28,1),)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod.layers[3].weights\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod.layers[3].","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod.fit_generator(train_iter,len(train_iter),)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}