{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras import models, layers\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau\n#from tensorflow.keras.applications import ResNet50, DenseNet121, EfficientNetB3\nfrom keras.optimizers import Adam\nfrom keras.losses import CategoricalCrossentropy\nimport os, cv2, json\n\n# ignoring warnings\nimport warnings\nwarnings.simplefilter(\"ignore\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# For easy acces to files\nWORK_DIR = \"../input/iot-malware/IOT_Malware_dataset/\"\nos.listdir(WORK_DIR)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data = pd.read_csv(WORK_DIR + \"train.csv\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data.head(10)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data.dtypes","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data.shape[0]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data.label.value_counts()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#change for the ImageDatagen and flow_from_dataframe\n# data.label = data.label.astype(\"str\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = 224\nbatch = 16\nshuffle = True\ntrain_generator = ImageDataGenerator(#rescale = 1/255.0,\n                                    rotation_range = 4, \n                                    width_shift_range=2.0,\n                                    height_shift_range=2.0,\n                                    #featurewise_std_normalization=True,\n                                    #samplewise_std_normalization=True,\n                                    featurewise_center=True, samplewise_center=True,\n                                    #shear_range = 0.2, \n                                    zoom_range = 0.2, \n                                    vertical_flip=True,\n                                    fill_mode=\"nearest\",\n                                    horizontal_flip = True, \n                                    #brightness_range = (0.5, 1.5),#0.5, 1.5\n                                    #preprocessing_function=add_noise,\n                                    #data_format=None,\n                                    validation_split=0.2,\n                                    #dtype=tf.float32,\n) \nTrianImage=\"../input/iot-malware/IOT_Malware_dataset\"\ntrain_generator =train_generator.flow_from_directory(\n     TrianImage,\n     batch_size= batch,\n     shuffle=shuffle,\n     target_size=(IMG_SIZE, IMG_SIZE),\n     class_mode = \"categorical\",#input\n     color_mode='rgb',\n     subset = \"training\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_batch, y_batch = next(train_generator) \nfor i in range (0,2):\n    image = x_batch[i]\n    plt.imshow(image)\n    print(y_batch[i])\n    plt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator.class_indices","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nvalid_generator = ImageDataGenerator(#rescale = 1/255.0,\n                                    rotation_range = 4, \n                                    width_shift_range=2.0,\n                                    height_shift_range=2.0,\n                                    #featurewise_std_normalization=True,\n                                    #samplewise_std_normalization=True,\n                                    featurewise_center=True, samplewise_center=True,\n                                    shear_range = 0.2, \n                                    zoom_range = 0.2, \n                                    vertical_flip=True,\n                                    fill_mode=\"nearest\",\n                                    horizontal_flip = True, \n                                    #brightness_range = (0.5, 1.5),#0.5, 1.5\n                                    #preprocessing_function=add_noise,\n                                    #data_format=None,\n                                    validation_split=0.2,\n                                    #dtype=tf.float32,\n) \n\nvalid_generator =valid_generator.flow_from_directory(\n     TrianImage,\n     batch_size= batch,\n     shuffle=shuffle,\n     target_size=(IMG_SIZE, IMG_SIZE),\n     class_mode = \"categorical\",#input\n     #color_mode='rgb',\n     subset = \"validation\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_batch, y_batch = next(valid_generator) \nfor i in range (0,2):\n    image = x_batch[i]\n    plt.imshow(image)\n    print(y_batch[i])\n    plt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_generator.class_indices","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data_1 = pd.read_csv(WORK_DIR + \"sample_submission.csv\")#\n# #pd.read_csv('./Hand_Annotations_2.csv',dtype=str,names=colnames, header=None)\n# data_1.label = data_1.label.astype(\"str\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def add_noise(img):\n    '''Add random noise to an image'''\n    #img1 = img.astype(np.float32)/ 255.0#False\n    image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\n    return image","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_generator = ImageDataGenerator(\n# #                                     rotation_range = 4, \n# #                                     width_shift_range=2.0,\n# #                                     height_shift_range=2.0,\n# #                                     featurewise_std_normalization=True,\n# #                                     #samplewise_std_normalization=True,\n# #                                     featurewise_center=True, samplewise_center=True,\n# #                                     shear_range = 0.2, \n# #                                     zoom_range = 0.2, \n# #                                     vertical_flip=True,\n# #                                     fill_mode=\"nearest\",\n# #                                     horizontal_flip = True, \n# #                                     brightness_range = (0.5, 1.5),\n#                                     #validation_split = 0.2#,\n#                                     preprocessing_function=add_noise\n# ) \\\n#         .flow_from_dataframe(\n#                             data_1,\n#                             directory = WORK_DIR + \"test_images\",\n#                             x_col = \"image_id\",#image_id\n#                             y_col = \"label\",\n#                             target_size = (IMG_SIZE, IMG_SIZE),\n#                             class_mode = \"categorical\",\n#                             batch_size = batch,\n#                             #shuffle = True,\n#                             #seed = 34,\n#                             #interpolation = \"nearest\",\n#                             #subset = \"training\"\n# )","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import EfficientNetB1,EfficientNetB5,EfficientNetB7,DenseNet201,ResNet50, VGG19","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weight = '../input/applications-tensorflow/efficientnetb1_notop.h5'","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LR = 0.0001\ndef get_model():    \n    inp = tf.keras.layers.Input(shape = (IMG_SIZE,IMG_SIZE, 3))\n\n    x = DenseNet201(weights ='imagenet' , include_top = False)(inp)#'imagenet'\n    x = tf.keras.layers.GlobalAveragePooling2D()(x)\n    x = tf.keras.layers.Dense(512, activation = 'relu')(x)#256\n    x = tf.keras.layers.Dropout(0.20)(x)\n    x = tf.keras.layers.Dense(256, activation = 'relu')(x)\n    x = tf.keras.layers.Dropout(0.2)(x)\n    output = tf.keras.layers.Dense(2, activation = 'softmax')(x)\n        \n    model = tf.keras.models.Model(inputs = [inp], outputs = [output])\n\n    opt = tf.keras.optimizers.Adam(learning_rate = LR)\n\n    model.compile(\n        optimizer = opt,\n        loss = [tf.keras.losses.CategoricalCrossentropy(label_smoothing = 0.4)],#label_smoothing = 0.4\n        metrics = ['categorical_accuracy'])#[tf.keras.metrics.CategoricalAccuracy()])# ['categorical_accuracy']\n                    \n\n    return model\nmodel = get_model()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ReduceLROnPlateau , ModelCheckpoint, EarlyStopping\nmodel_check = ModelCheckpoint(\n                            \"./firstTry.h5\",\n                            monitor = \"val_loss\",\n                            verbose = 1,\n                            save_best_only = True,\n                            save_weights_only = False,\n                            #mode = \"min\"\n                                )\nearly_stop= EarlyStopping(\n                                monitor = \"val_loss\",\n                                #min_delta=0.001,\n                                patience=7,\n                                verbose=1,\n                                #mode=\"min\",\n                                #baseline=None,\n                                restore_best_weights=False)\n# reduce_lr = ReduceLROnPlateau(\n#                                 monitor=\"val_loss\",\n#                                 factor=0.1,\n#                                 patience=2,\n#                                 verbose=1,\n#                                 mode=\"min\",\n#                                 min_delta=0.0001,\n#                                 #cooldown=0,\n#                                 #min_lr=0\n# )\n\n\n#ModelCheckpoint means save best weights\n#model_chkpt = ModelCheckpoint('best_mod.h5', save_best_only=True, monitor='accuracy')\nlearning_rate_reduction1 = ReduceLROnPlateau(monitor='val_loss', \n                                             patience=2, verbose=1, factor=0.5,mode=\"min\", min_lr=0.000001)\nlearning_rate_reduction2 = ReduceLROnPlateau(monitor='loss', \n                                             patience=2, verbose=1, factor=0.5,mode=\"min\", min_lr=0.00001)\n\nearly_stopping = EarlyStopping(monitor='loss', restore_best_weights=False, patience=16)\n\ncallbacks1 = [early_stopping,learning_rate_reduction2,learning_rate_reduction1]#model_check\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"steps_per_epoch=train_generator.samples //batch\nprint(steps_per_epoch)\nvalidation_steps=valid_generator.samples // batch\nprint(validation_steps)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_generator,epochs = 128,#40\n                    steps_per_epoch=train_generator.samples //batch,\n                    validation_steps=valid_generator.samples // batch,\n                    validation_data = valid_generator,callbacks = callbacks1,\n                              #shuffle=True\n                             )","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1,2, figsize=(12, 3))\nax[0].plot(history.history['loss'], color='b', label=\"Training loss\")\nax[0].plot(history.history['val_loss'], color='r', label=\"validation loss\",axes =ax[0])\nlegend = ax[0].legend(loc='best', shadow=True)\n\nax[1].plot(history.history['categorical_accuracy'], color='b', label=\"Training accuracy\")\nax[1].plot(history.history['val_categorical_accuracy'], color='r',label=\"Validation accuracy\")\nlegend = ax[1].legend(loc='best', shadow=True) \n\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nimport pandas as pd\n\n\n\n\nmydict = {\"image_id\":[], 'label':[]}\n\nfor img in glob.glob(\"../input/cassava-leaf-disease-classification/test_images/*.jpg\"):\n        test_filename = os.path.basename(img)\n        print(img)\n        img = tf.keras.preprocessing.image.load_img(img)\n        img = tf.keras.preprocessing.image.img_to_array(img)\n        img = tf.keras.preprocessing.image.smart_resize(img, (IMG_SIZE, IMG_SIZE))\n        #img = tf.image.resize(img, IMG_SIZE)\n        #img = tf.cast(img, tf.float32)\n        img = tf.reshape(img, (-1, IMG_SIZE, IMG_SIZE, 3))\n        probabilities = model.predict(img)#/255\n        #mydict.append(np.argmax(prediction))\n        predictions = np.argmax(probabilities, axis=-1)\n        mydict[\"image_id\"].append(test_filename)\n        mydict[\"label\"].append(predictions[0])\n    \ndf = pd.DataFrame(mydict)\ndf.to_csv('submission.csv', index=False)\n!head submission.csv","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}