{"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 sys\nsys.path.append('/kaggle/input/efficientnet-keras-dataset/efficientnet_kaggle')","metadata":{"execution":{"iopub.status.busy":"2021-11-17T16:34:26.452746Z","iopub.execute_input":"2021-11-17T16:34:26.453187Z","iopub.status.idle":"2021-11-17T16:34:26.475576Z","shell.execute_reply.started":"2021-11-17T16:34:26.453106Z","shell.execute_reply":"2021-11-17T16:34:26.474762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import MultiLabelBinarizer\nfrom tqdm.notebook import tqdm\nimport efficientnet.tfkeras as efn\nimport matplotlib.pyplot as plt\nimport tensorflow_addons as tfa\nimport tensorflow as tf\nimport pandas as pd\nimport numpy as np\nimport random\nimport os","metadata":{"execution":{"iopub.status.busy":"2021-11-17T16:34:26.477147Z","iopub.execute_input":"2021-11-17T16:34:26.477404Z","iopub.status.idle":"2021-11-17T16:34:32.774292Z","shell.execute_reply.started":"2021-11-17T16:34:26.477369Z","shell.execute_reply":"2021-11-17T16:34:32.773504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.mixed_precision.set_global_policy('mixed_float16')\n\n\n\nclass CFG:\n    \n    '''\n    keep these\n    '''\n    strategy = tf.distribute.get_strategy()\n    batch_size = 16 * strategy.num_replicas_in_sync\n    \n    img_size = 600\n    \n    classes = np.array([\n        'complex', \n        'frog_eye_leaf_spot', \n        'powdery_mildew', \n        'rust', \n        'scab'])\n    root = '../input/plant-pathology-2021-fgvc8/test_images'\n    \n    '''\n    tweak these\n    '''\n    seed = 42 # random seed we use for each operation\n#     tta_steps = 1 # number of TTA folds, run without TTA if 0","metadata":{"execution":{"iopub.status.busy":"2021-11-17T16:34:32.775474Z","iopub.execute_input":"2021-11-17T16:34:32.775971Z","iopub.status.idle":"2021-11-17T16:34:32.932402Z","shell.execute_reply.started":"2021-11-17T16:34:32.775933Z","shell.execute_reply":"2021-11-17T16:34:32.930448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def decode_image(image_data):\n    image = tf.image.decode_jpeg(image_data, channels=3)\n    image = tf.reshape(image, [CFG.img_size, CFG.img_size, 3])\n    image = tf.cast(image, tf.float32) / 255.#chuyển sang dạng này do tpu chỉ hộ trợ float32 \n    return image\n\n\ndef data_augment(image, label):\n    image = tf.image.random_flip_left_right(image, seed=CFG.seed)\n    image = tf.image.random_flip_up_down(image, seed=CFG.seed)\n    \n    k = tf.tf.random.uniform([], minval=0, maxval=4, dtype=tf.int64, seed=CFG.seed)\n    image = tf.image.rot90(image, k=k)\n    \n    image = tf.image.random_hue(image, .1, seed=CFG.seed)\n    image = tf.image.random_saturation(image, .8, 1.2, seed=CFG.seed)\n    image = tf.image.random_contrast(image, .8, 1.2, seed=CFG.seed)\n    image = tf.image.random_brightness(image, .1, seed=CFG.seed)\n    \n    return image, label\n\n#config lại dạng image\nfeature_map = {\n    'image': tf.io.FixedLenFeature([], tf.string),\n    'image_name': tf.io.FixedLenFeature([], tf.string)}\n\n#đọc fild tfrec\ndef read_tfrecord(example):\n    example = tf.io.parse_single_example(example, feature_map)\n    image = decode_image(example['image'])\n    label = example['image_name']\n    return image, label\n\n\ndef get_dataset(filenames, ordered=True, shuffled=False, repeated=False, \n                augmented=False, cached=False, distributed=False):\n    auto = tf.data.experimental.AUTOTUNE#hàm tối ưu\n    dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=auto)\n    if not ordered:\n        ignore_order = tf.data.Options()\n        ignore_order.experimental_deterministic = False\n        dataset = dataset.with_options(ignore_order)\n    dataset = dataset.map(read_tfrecord, num_parallel_calls=auto)\n    if shuffled:#xáo trộn\n        dataset = dataset.shuffle(2048, seed=CFG.seed)\n    if repeated:#repeat ảnh\n        dataset = dataset.repeat()\n    dataset = dataset.batch(CFG.batch_size)\n    if augmented:#tạo augumentation\n        dataset = dataset.map(data_augment, num_parallel_calls=auto)\n    if cached:#lưu bộ nhớ cached\n        dataset = dataset.cache()\n    dataset = dataset.prefetch(auto)\n    if distributed:#phân phối data set trên nhiều tpu\n        dataset = CFG.strategy.experimental_distribute_dataset(dataset)\n    return dataset\n\n\ndef get_model():\n    model = tf.keras.models.Sequential(name='EfficientNetB7')\n    \n    model.add(efn.EfficientNetB7(\n        include_top=False,\n        input_shape=(CFG.img_size, CFG.img_size, 3),\n        weights=None,\n        pooling='avg'))\n    \n    model.add(tf.keras.layers.Dense(len(CFG.classes), \n        kernel_initializer=tf.keras.initializers.RandomUniform(seed=CFG.seed),\n        bias_initializer=tf.keras.initializers.Zeros(), name='dense_top'))\n    model.add(tf.keras.layers.Activation('sigmoid', dtype='float32'))\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2021-11-17T16:34:32.934846Z","iopub.execute_input":"2021-11-17T16:34:32.935142Z","iopub.status.idle":"2021-11-17T16:34:32.954475Z","shell.execute_reply.started":"2021-11-17T16:34:32.935104Z","shell.execute_reply":"2021-11-17T16:34:32.953817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def _serialize_image(path):\n    image = tf.io.read_file(path)\n    \n    image = tf.image.decode_jpeg(image, channels=3)\n    image = tf.image.resize(image, [CFG.img_size, CFG.img_size])\n    image = tf.cast(image, tf.uint8)\n    return tf.image.encode_jpeg(image).numpy()\n\n\ndef _serialize_sample(image, name):\n    feature = {\n        'image': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image])),\n        'image_name': tf.train.Feature(bytes_list=tf.train.BytesList(value=[name]))}\n    sample = tf.train.Example(features=tf.train.Features(feature=feature))\n    return sample.SerializeToString()\n\n#lưu tập test du\ndef serialize_test():\n    samples = []\n    \n    for path in os.listdir(CFG.root):\n        image = _serialize_image(os.path.join(CFG.root, path))\n        name = path.encode()\n        samples.append(_serialize_sample(image, name))\n    \n    with tf.io.TFRecordWriter('test.tfrec') as writer:\n        [writer.write(x) for x in tqdm(samples, total=len(samples))]","metadata":{"execution":{"iopub.status.busy":"2021-11-17T16:34:32.955822Z","iopub.execute_input":"2021-11-17T16:34:32.956119Z","iopub.status.idle":"2021-11-17T16:34:32.966920Z","shell.execute_reply.started":"2021-11-17T16:34:32.956082Z","shell.execute_reply":"2021-11-17T16:34:32.966056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"serialize_test()","metadata":{"execution":{"iopub.status.busy":"2021-11-17T16:34:32.968213Z","iopub.execute_input":"2021-11-17T16:34:32.968563Z","iopub.status.idle":"2021-11-17T16:34:35.542874Z","shell.execute_reply.started":"2021-11-17T16:34:32.968528Z","shell.execute_reply":"2021-11-17T16:34:35.542198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"size = len(os.listdir(CFG.root)) #=3\n\nfilenames = tf.io.gfile.glob('*.tfrec')\n\n #tạo augmentation cho tập test\n# if CFG.tta_steps > 0:\n#     dataset = get_dataset(filenames, repeated=True, augmented=False)\n# else:\ndataset = get_dataset(filenames)   \n    \npredicts = np.zeros((size, len(CFG.classes)))#ma trận 3xCFG.classes, \npaths = tf.io.gfile.glob('../input/modelfgvc8efnb7/model_4.h5')\n\nfor path in tqdm(paths, total=len(paths)):\n\n    with CFG.strategy.scope():\n        model = get_model()\n        model.load_weights(path)\n   \n    #chạy cho tập test có augmentation và tính điểm  trug bình\n#     if CFG.tta_steps > 0:\n#         steps = CFG.tta_steps * (size / CFG.batch_size + 1)\n\n#         predict = model.predict(dataset, steps=steps)[:size * CFG.tta_steps] / len(paths)\n#         predicts += np.mean(\n#             predict.reshape(size, CFG.tta_steps, len(CFG.classes), order='F'), axis=1)\n\n#     else:\npredicts += model.predict(dataset) / len(paths)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T16:39:01.886426Z","iopub.execute_input":"2021-11-17T16:39:01.886730Z","iopub.status.idle":"2021-11-17T16:39:16.295144Z","shell.execute_reply.started":"2021-11-17T16:39:01.886698Z","shell.execute_reply":"2021-11-17T16:39:16.294443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicts1 = predicts.copy()\n# print(predicts1)\n# print(thresholds )\n# for i in range(len(predicts1)):\n#     predicts1[i] = predicts1[i] > thresholds #nếu > threshold thì bằng 1\n#     print(predicts1[i])\n\nfor i in range(len(predicts)):\n    predicts[i] = predicts[i] > 0.4 #nếu > threshold thì bằng 1\n    print(predicts[i])\n\n    \npredicts = predicts.astype('bool')\nlabels = []\n\n#1 class nếu true thì chuyển sang dang string\nfor i in range(len(predicts)):\n    labels.append(' '.join(CFG.classes[predicts[i]]))\n# print(labels) ['complex frog_eye_leaf_spot scab', 'frog_eye_leaf_spot', 'scab']            \n    \nlabels = ['healthy' if ('healthy' in x or x == '') else x for x in labels] #nếu xuất hiện nhãn heathy hay các nhãn trống thì gán nhãn healthy\n  \n\ndf = pd.DataFrame({\n    'image': os.listdir(CFG.root),\n    'labels': labels})\n# print(df)                   image                           labels\n#                 0  ad8770db05586b59.jpg  complex frog_eye_leaf_spot scab\n#                 1  c7b03e718489f3ca.jpg               frog_eye_leaf_spot\n#                 2  85f8cb619c66b863.jpg                             scab\n\ndf.to_csv('submission.csv', index=False)\ndisplay(df.head())","metadata":{"execution":{"iopub.status.busy":"2021-11-17T16:39:16.296911Z","iopub.execute_input":"2021-11-17T16:39:16.297308Z","iopub.status.idle":"2021-11-17T16:39:16.318292Z","shell.execute_reply.started":"2021-11-17T16:39:16.297271Z","shell.execute_reply":"2021-11-17T16:39:16.317615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}