{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np, pandas as pd, os\nimport matplotlib.pyplot as plt, cv2\nimport tensorflow as tf, re, math\nimport random\nimport os","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"Cover_PATH = '../input/alaska2-image-steganalysis/Cover/'\nJMiPOD_PATH = '../input/alaska2-image-steganalysis/JMiPOD/'\nJUNIWARD_PATH = '../input/alaska2-image-steganalysis/JUNIWARD/'\nUERD_PATH = '../input/alaska2-image-steganalysis/UERD/'\nTEST_PATH = \"../input/alaska2-image-steganalysis/Test/\"\n\nCover = [Cover_PATH + i for i in os.listdir(Cover_PATH)][:10000]\nJMiPOD = [JMiPOD_PATH +i for i in os.listdir(JMiPOD_PATH)][:10000]\nUERD = [UERD_PATH+i for i in os.listdir(UERD_PATH)][:10000]\nJUNIWARD = [JUNIWARD_PATH+i for i in os.listdir(JUNIWARD_PATH)][:10000]\n\nprint('There are %i images ' % len(Cover))\nprint('There are %i images ' % len(JMiPOD))\nprint('There are %i images ' % len(UERD))\nprint('There are %i images ' % len(JUNIWARD))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nall_paths = Cover + JMiPOD + UERD + JUNIWARD\n\npaths = pd.DataFrame(all_paths,columns=[\"image_path\"])\npaths['target'] = paths.image_path.apply(lambda x:x.split(\"/\")[3])\n\ndictt = {\n    'Cover':0,\n    'JMiPOD':1,\n    'JUNIWARD':2,\n    'UERD':3\n}\n\npaths['target'] = paths.target.apply(lambda x:dictt[x])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import seaborn as sns\nsns.countplot(paths['target'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.utils import shuffle\npaths = shuffle(paths).reset_index(drop=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def _bytes_feature(value):\n  \"\"\"Returns a bytes_list from a string / byte.\"\"\"\n  if isinstance(value, type(tf.constant(0))):\n    value = value.numpy() # BytesList won't unpack a string from an EagerTensor.\n  return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))\n\ndef _float_feature(value):\n  \"\"\"Returns a float_list from a float / double.\"\"\"\n  return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))\n\ndef _int64_feature(value):\n  \"\"\"Returns an int64_list from a bool / enum / int / uint.\"\"\"\n  return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"paths","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def serialize_example(feature0, feature1):\n  feature = {\n      'image': _bytes_feature(feature0),\n      'target': _int64_feature(feature1),\n  }\n  example_proto = tf.train.Example(features=tf.train.Features(feature=feature))\n  return example_proto.SerializeToString()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"SIZE = 10000\nCT = paths.shape[0]//SIZE + int(paths.shape[0]%SIZE!=0)\nfor j in range(CT):\n    print()\n    print('Writing TFRecord %i of %i...'%(j,CT))\n    CT2 = min(SIZE,paths.shape[0]-j*SIZE)\n    with tf.io.TFRecordWriter('train%.2i-%i.tfrec'%(j,CT2)) as writer:\n        for k in range(CT2):\n            img = cv2.imread(paths.iloc[k]['image_path'])\n            img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # Fix incorrect colors\n            img = cv2.imencode('.jpg', img, (cv2.IMWRITE_JPEG_QUALITY, 94))[1].tostring()\n            example = serialize_example(\n                img,\n                paths.iloc[k]['target']\n            )\n            writer.write(example)\n            if k%1000==0: print(k,', ',end='')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# test_paths = [TEST_PATH+i for i in os.listdir(TEST_PATH)]\n# test_paths[1].split(\"/\")[-1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# def serialize_example_test(feature0,feature1):\n#   feature = {\n#       'image': _bytes_feature(feature0),\n#       \"file_name\":_bytes_feature(feature1)\n#   }\n#   example_proto = tf.train.Example(features=tf.train.Features(feature=feature))\n#   return example_proto.SerializeToString()\n\n\n# SIZE = 5000\n# CT = len(test_paths)//SIZE + int(len(test_paths)%SIZE!=0)\n# for j in range(CT):\n#     print()\n#     print('Writing TFRecord %i of %i...'%(j,CT))\n#     CT2 = min(SIZE,len(test_paths)-j*SIZE)\n#     with tf.io.TFRecordWriter('test%.2i-%i.tfrec'%(j,CT2)) as writer:\n#         for k in range(CT2):\n#             img = cv2.imread(test_paths[k])\n#             img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # Fix incorrect colors\n#             img = cv2.imencode('.jpg', img, (cv2.IMWRITE_JPEG_QUALITY, 94))[1].tostring()\n#             example = serialize_example_test(\n#                 img,\n#                 str.encode(test_paths[k].split(\"/\")[-1])\n#             )\n#             writer.write(example)\n#             if k%1000==0: print(k,', ',end='')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}