{"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\n#for 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 directo|\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":"from albumentations import (\nCompose,\nResize,\nOneOf,\nRandomBrightness,\nRandomContrast,\nMotionBlur,\nMedianBlur,\nGaussianBlur,\nVerticalFlip,\nHorizontalFlip,\nShiftScaleRotate,\nNormalize,\nCrop\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow.keras as tf\nfrom tensorflow.keras import models, layers\nfrom math import ceil\nimport matplotlib.pyplot as plt\nimport random\nimport cv2\nfrom sklearn.model_selection import StratifiedKFold\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, InputLayer, GlobalAveragePooling2D, Input, BatchNormalization\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.applications import EfficientNetB0\nfrom tensorflow.keras.applications.resnet50 import ResNet50\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.models import Model\nfrom albumentations import RandomCrop, Compose, HorizontalFlip, VerticalFlip, OneOf","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# def create_model(input_shape):\n#     input = Input(shape = input_shape)\n    \n#     #Create and complite model and show sumarry\n    \n#     x_model = ResNet50(weights = \"imagenet\", include_top = False, input_tensor = input, pooling = None,\n#                             classes = None)\n#     for layer in x_model.layers:\n#         layer.trainable = True\n        \n#     x = GlobalAveragePooling2D()(x_model.output)\n#     x = BatchNormalization()(x)\n    \n#     x = Dense(1024, activation = \"relu\")(x)\n#     x = BatchNormalization()(x)\n#     x = Dense(512, activation = \"relu\")(x)\n    \n#     healthy = Dense(5, activation = \"softmax\", name=\"output_layer\")(x)\n    \n#     model = Model(inputs = x_model.input, outputs = healthy)\n    \n#     return model ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def create_model(inp_shape):\n    conv_base = EfficientNetB0(include_top = False, weights = 'imagenet',\n                               input_shape = inp_shape)\n    model = conv_base.output\n    model = layers.GlobalAveragePooling2D()(model)\n    model = layers.Dense(5, activation = \"softmax\")(model)\n    model = models.Model(conv_base.input, model)\n\n    model.compile(optimizer = Adam(lr = 0.001),\n                  loss = \"sparse_categorical_crossentropy\",\n                  metrics = [\"acc\"])\n    return model","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"path = \"/kaggle/input/cassava-leaf-disease-classification/\"\nimg_shape = (512,512)\nimg_directory = path + \"train_images/\"\ntrain = pd.read_csv(path + \"train.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"### AUG\n\ndef augment(augment, image):\n    augment_image = augment(image=image)[\"image\"]\n    return augment_image\n\ndef vertical_horizontal_aug(image):\n    list_of_augmentations = [HorizontalFlip(p=1), VerticalFlip(p=1), Compose([HorizontalFlip(p=1), VerticalFlip(p=1)])]\n    aug = random.choice(list_of_augmentations)\n    print(\"I HATE EACH OF MY FUCKIN TEAMATES\")\n    aug_image = aug(image=image)[\"image\"]\n    return aug_image\n\ndef strong_aug(p=1.0):\n    return OneOf([\n        Compose([HorizontalFlip(p=1), VerticalFlip(p=1)],p=1),\n        RandomBrightness(p=1),\n        RandomContrast(p=1),\n        MotionBlur(p=1),\n        MedianBlur(p=1),\n        GaussianBlur(p=1),\n        VerticalFlip(p=1),\n        HorizontalFlip(p=1),\n        ShiftScaleRotate(p=1),\n        Normalize(p=1),\n        \n    ], p=0.5)\n\ndef four_D_augment(name):\n    name = str(name)\n    image = cv2.imread(img_directory + name)\n    aug_img_1 = augment(Compose([HorizontalFlip(p=1), VerticalFlip(p=1)],p=1), image)\n    aug_img_2 = augment(Resize(height=25,width=25,p=1), image) # I need to add values here; for test - 25\n    aug_img_3 = augment(RandomBrightness(p=1), image)\n    aug_img_4 = augment(RandomContrast(p=1), image)\n    aug_img_5 = augment(MotionBlur(p=1), image)\n    aug_img_6 = augment(MedianBlur(p=1), image)\n    aug_img_7 = augment(GaussianBlur(p=1), image)\n    aug_img_8 = augment(VerticalFlip(p=1), image)\n    aug_img_9 = augment(HorizontalFlip(p=1), image)\n    aug_img_10 = augment(ShiftScaleRotate(p=1), image)\n    aug_img_11 = augment(Normalize(p=1), image)\n#     aug_img_12 = augment(Crop(p=1), image)\n    fig, ax = plt.subplots(nrows = 3, ncols = 5, figsize = (25,25))\n\n    ax[0][0].imshow(aug_img_1[...,[2,1,0]])\n    ax[0][0].set_title('Compose', fontsize=14)\n    ax[0][1].imshow(image[...,[2,1,0]])\n    ax[0][1].set_title('Original image', fontsize=14)\n    ax[0][2].imshow(aug_img_2[...,[2,1,0]])\n    ax[0][2].set_title('Resize', fontsize=14) \n    ax[0][3].imshow(aug_img_3[...,[2,1,0]])\n    ax[0][3].set_title('RandomBrightness', fontsize=14)\n    ax[0][4].imshow(aug_img_4[...,[2,1,0]])\n    ax[0][4].set_title(\"RandomContrast\", fontsize=14)\n    ax[1][0].imshow(aug_img_5[...,[2,1,0]])\n    ax[1][0].set_title(\"MotionBlur\", fontsize=14)\n    ax[1][1].imshow(aug_img_6[...,[2,1,0]])\n    ax[1][1].set_title(\"MedianBlur\", fontsize=14)\n    ax[1][2].imshow(aug_img_7[...,[2,1,0]])\n    ax[1][2].set_title(\"GaussianBlur\", fontsize=14)\n    ax[1][3].imshow(aug_img_8[...,[2,1,0]])\n    ax[1][3].set_title(\"VerticalFlip\", fontsize=14)\n    ax[1][4].imshow(aug_img_9[...,[2,1,0]])\n    ax[1][4].set_title(\"HorizontalFlip\", fontsize=14)\n    ax[2][0].imshow(aug_img_10[...,[2,1,0]])\n    ax[2][0].set_title(\"ShiftScaleRotate\", fontsize=14)\n    ax[2][1].imshow(aug_img_11[...,[2,1,0]])\n    ax[2][1].set_title(\"Normalize\", fontsize=14)\n#     ax[2][2].imshow(aug_img_12[...,[2,1,0]])\n#     ax[2][2].set_title(\"Crop, from another nb\", fontsize=14)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cv2.imread(img_directory + train.image_id[0]).shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"i = 0\nimgs = []\nfor img in train.image_id:\n    image = cv2.imread(img_directory + img)\n    # four_D_augment(train.image_id[0])\n    plt.imshow(augment(strong_aug(p=1.0), image))\n    i += 1\n    if i > 5:\n        break","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class TrainDataGenerator(tf.utils.Sequence):\n    \n# img_dir deleted, restore if needed.    \n    \n    def __init__(self , X_set, Y_set, ids, batch_size = 8, img_size = [img_shape[0], img_shape[1], 3], augmentation = False, img_dir = img_directory):\n        self.X = X_set\n        self.Y = Y_set\n        self.batch_size = batch_size\n        self.ids = ids\n        self.img_size  = img_size  \n        self.img_dir = img_dir\n        self.on_epoch_end()\n        self.augmentation = augmentation\n        \n        #Split Data\n        self.x_indexed = X_set['image_id'][self.ids]\n        \n    def __len__(self):\n        return int(ceil(len(self.ids)/self.batch_size))\n\n    def __getitem__(self, index):\n        indices = self.ids[index*self.batch_size:(index+1)*self.batch_size]\n        X, Y = self.__generator__(indices)\n        return X, Y\n    \n    def on_epoch_end(self):\n        self.indices = np.arange(len(self.ids))\n        \n        \n    def __generator__(self, indices):\n        X = np.empty((self.batch_size, *self.img_size))\n        Y = np.empty((self.batch_size, 1))\n        for i, index in enumerate(indices):\n            ID = self.X['image_id'][index]\n            image = cv2.imread(self.img_dir + self.X['image_id'][index])\n            image = cv2.resize(image, (self.img_size[1], self.img_size[0]))\n#             if self.augmentation:\n#                 image = augment(strong_aug(p=1.0),image)\n            X[i,] = image/255\n            Y[i, ] = self.Y.loc[index]\n        return X,Y\n        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# ids_train = train.index\n# X_set = train\n# Y_set = pd.get_dummies(train.label)\n# ids_train = np.array(X_train.index)\n# for X,y in TrainDataGenerator(X_valid, y_valid, ids_valid):\n#     break\n    \n\n# print(X.shape)\n# print(y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"kf = StratifiedKFold(n_splits=3, shuffle=True, random_state=42)\n\nfor fold, (trn_, val_) in  enumerate(kf.split(X=train, y=train.label)):\n    print(len(trn_), len(val_))\n    train.loc[val_, 'kfold'] = fold","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"target = train.label\nimport time \nall_history = []\nresult_dict = []\ninput_shape = (img_shape[0], img_shape[1], 3)\nfor fold in range(0,3):\n    start = time.time()\n    X_train = train[train.kfold != fold].reset_index(drop=True)\n    X_valid = train[train.kfold == fold].reset_index(drop=True)\n    y_train = X_train.label\n    y_valid = X_valid.label\n    ids_train = np.array(X_train.index)\n    ids_valid = np.array(X_valid.index)\n    model = create_model(input_shape)\n    model.compile(optimizer= Adam(lr=0.001),loss=\"sparse_categorical_crossentropy\",\n                  metrics = ['sparse_categorical_accuracy'])\n    \n    \n    early_stopping = tf.callbacks.EarlyStopping(monitor = \"val_loss\", mode = \"min\", patience = 6, \n                                               verbose = 1, min_delta = 0.0001, restore_best_weights = True)\n    \n    history = model.fit_generator(TrainDataGenerator(X_train, y_train, ids_train, batch_size = 12, augmentation = True),\n                                 epochs=20,\n                                 verbose=1,\n                                 callbacks=[early_stopping],\n                                 validation_data=TrainDataGenerator(X_valid, y_valid, ids_valid),\n#                                 max_queue_size=100,\n                                 workers=-1,\n                                 shuffle=True)\n    model.save_weights(str(fold) + \"_Weed.h5\")\n    all_history.append(history)\n    end = time.time()\n    print (\"Время обучения одного сгиба\", end-start)","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}