{"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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../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\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n#Neural Network with TensorFlow and KERAS\nimport tensorflow as tf\nfrom tensorflow.keras.layers import Dense,Dropout,Conv2D\n# all  related libaray upload in  the given \n#model library \nfrom tensorflow.keras.models import Sequential\n#activation function \nfrom tensorflow.keras.activations import softmax,relu,sigmoid\n#layer \nfrom tensorflow.keras.layers import Flatten,Dense,Conv2D,MaxPooling2D,Dropout,Conv2D\n#losss \nfrom tensorflow.keras.losses import categorical_crossentropy,binary_crossentropy\n#optimizer \nfrom tensorflow.keras.optimizers import SGD,Adagrad,Adam,Adamax\n#calll back and checkpoint \nfrom tensorflow.keras.callbacks import ModelCheckpoint,EarlyStopping","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plant=pd.read_csv(\"../input/plant-pathology-2021-fgvc8/train.csv\")\nplant","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plant.labels.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(12, 6))\nplant.labels.value_counts().plot.bar()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications import vgg16, VGG16","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"idg=tf.keras.preprocessing.image.ImageDataGenerator(horizontal_flip=True, rotation_range=30,\n                                                    rescale=1/255,validation_split=0.3,\n                                                    preprocessing_function=tf.keras.applications.vgg16.preprocess_input)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#train \n\ntrain_idg= idg.flow_from_dataframe(plant,directory=\"../input/plant-pathology-2021-fgvc8/train_images\",x_col='image',y_col='labels',target_size=(224,224),batch_size=128,subset= \"training\")\n\n#validation \n\nval_idg= idg.flow_from_dataframe(plant,directory=\"../input/plant-pathology-2021-fgvc8/train_images\",x_col='image',y_col='labels',target_size=(224,224),batch_size=128,subset=\"validation\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#download the vgg model \n\nvggmodel=tf.keras.applications.vgg16.VGG16(include_top=False,input_shape=(224,224,3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for layer in vggmodel.layers:\n  layer.trainable=False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for layer in vggmodel.layers:\n  print(layer.name,layer.trainable)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#functional way two build the the model API\n\nvgg_output=vggmodel.output\nflatten_output=tf.keras.layers.Flatten()(vgg_output)\ndense1=tf.keras.layers.Dense(256,activation='relu')(flatten_output)\nbn1=tf.keras.layers.BatchNormalization()(dense1)\ndp1=tf.keras.layers.Dropout(0.3)(bn1)\ndense2=tf.keras.layers.Dense(128,activation='relu')(dp1)\ndp2=tf.keras.layers.Dropout(0.2)(dense2)\ndense3=tf.keras.layers.Dense(12,activation='softmax')(dp2)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"finalModel=tf.keras.models.Model(inputs=[vggmodel.input],outputs=[dense3])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#final model \n\nfinalModel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#compile the model \n\nfinalModel.compile(optimizer=tf.keras.optimizers.SGD(),loss=tf.keras.losses.categorical_crossentropy,metrics=['acc'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"finalModel_history=finalModel.fit(train_idg,epochs=50,validation_data=val_idg)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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":4}