{"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 # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-27T08:48:20.039989Z","iopub.execute_input":"2022-03-27T08:48:20.040325Z","iopub.status.idle":"2022-03-27T08:48:20.724138Z","shell.execute_reply.started":"2022-03-27T08:48:20.040295Z","shell.execute_reply":"2022-03-27T08:48:20.72315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport gc\nimport json\nimport math\nimport cv2\nimport PIL\nfrom PIL import Image\nimport numpy as np\nfrom keras import layers\nfrom keras.applications import DenseNet121\nfrom keras.callbacks import Callback, ModelCheckpoint\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential\nfrom keras.optimizers import Adam\nimport matplotlib.pyplot as plt\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import cohen_kappa_score, accuracy_score\nimport scipy\nimport tensorflow as tf\nfrom tqdm import tqdm\n%matplotlib inline\nfrom keras.preprocessing import image","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2022-03-27T08:49:13.748718Z","iopub.execute_input":"2022-03-27T08:49:13.749066Z","iopub.status.idle":"2022-03-27T08:49:22.288327Z","shell.execute_reply.started":"2022-03-27T08:49:13.749036Z","shell.execute_reply":"2022-03-27T08:49:22.287526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Config Settings","metadata":{}},{"cell_type":"code","source":"# Detect hardware, return appropriate distribution strategy\ntry:\n    # TPU detection. No parameters necessary if TPU_NAME environment variable is\n    # set: this is always the case on Kaggle.\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n    print('Running on TPU ', tpu.master())\nexcept ValueError:\n    tpu = None\n\nif tpu:\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.experimental.TPUStrategy(tpu)\nelse:\n    # Default distribution strategy in Tensorflow. Works on CPU and single GPU.\n    strategy = tf.distribute.get_strategy()\n\nprint(\"REPLICAS: \", strategy.num_replicas_in_sync)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T08:49:35.168876Z","iopub.execute_input":"2022-03-27T08:49:35.169167Z","iopub.status.idle":"2022-03-27T08:49:35.178435Z","shell.execute_reply.started":"2022-03-27T08:49:35.169138Z","shell.execute_reply":"2022-03-27T08:49:35.176997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fixed Constants","metadata":{}},{"cell_type":"code","source":"# class directories\ncover_dir = \"../input/alaska2-image-steganalysis/Cover/\"\nJMiPOD_dir = \"../input/alaska2-image-steganalysis/JMiPOD/\"\nJUNIWARD_dir = \"../input/alaska2-image-steganalysis/JUNIWARD/\"\nUERD_dir = \"../input/alaska2-image-steganalysis/UERD/\"\n# add to list\nclass_locs = [cover_dir, JMiPOD_dir, JUNIWARD_dir, UERD_dir]","metadata":{"execution":{"iopub.status.busy":"2022-03-27T08:49:38.566118Z","iopub.execute_input":"2022-03-27T08:49:38.566466Z","iopub.status.idle":"2022-03-27T08:49:38.57219Z","shell.execute_reply.started":"2022-03-27T08:49:38.566435Z","shell.execute_reply":"2022-03-27T08:49:38.570973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Variable Constants","metadata":{}},{"cell_type":"code","source":"BATCH_SIZE = 8\nTRAIN_VAL_SPLIT_RATIO = 0.50\nEPOCHS = 5\n# sample size for each class\ncN = 3000","metadata":{"execution":{"iopub.status.busy":"2022-03-27T08:49:51.487158Z","iopub.execute_input":"2022-03-27T08:49:51.487881Z","iopub.status.idle":"2022-03-27T08:49:51.495038Z","shell.execute_reply.started":"2022-03-27T08:49:51.487831Z","shell.execute_reply":"2022-03-27T08:49:51.493866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training Data","metadata":{}},{"cell_type":"code","source":"!pip install stegano ","metadata":{"execution":{"iopub.status.busy":"2022-03-27T08:49:55.417878Z","iopub.execute_input":"2022-03-27T08:49:55.418161Z","iopub.status.idle":"2022-03-27T08:50:06.087175Z","shell.execute_reply.started":"2022-03-27T08:49:55.418132Z","shell.execute_reply":"2022-03-27T08:50:06.086252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -q efficientnet_pytorch","metadata":{"execution":{"iopub.status.busy":"2022-03-27T08:50:08.140275Z","iopub.execute_input":"2022-03-27T08:50:08.140591Z","iopub.status.idle":"2022-03-27T08:50:18.758291Z","shell.execute_reply.started":"2022-03-27T08:50:08.14056Z","shell.execute_reply":"2022-03-27T08:50:18.757147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! git clone https://github.com/dwgoon/jpegio\n!pip install jpegio/.\n\nimport jpegio as jio","metadata":{"execution":{"iopub.status.busy":"2022-03-27T08:50:21.873798Z","iopub.execute_input":"2022-03-27T08:50:21.874259Z","iopub.status.idle":"2022-03-27T08:51:08.539182Z","shell.execute_reply.started":"2022-03-27T08:50:21.874223Z","shell.execute_reply":"2022-03-27T08:51:08.537824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import stegano\nfrom stegano import lsb\n\n# System\nimport cv2\nimport os, os.path\nfrom PIL import Image              # from RBG to YCbCr\n\n# Basics\nimport pandas as pd\nimport numpy as np\nfrom numpy import pi                # for DCT\nfrom numpy import r_                # for DCT\nimport scipy                        # for cosine similarity\nfrom scipy import fftpack           # for DCT\nimport random\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg    # to check images\n%matplotlib inline\nfrom tqdm.notebook import tqdm      # beautiful progression bar","metadata":{"execution":{"iopub.status.busy":"2022-03-27T08:51:56.090273Z","iopub.execute_input":"2022-03-27T08:51:56.0906Z","iopub.status.idle":"2022-03-27T08:51:56.101986Z","shell.execute_reply.started":"2022-03-27T08:51:56.090554Z","shell.execute_reply":"2022-03-27T08:51:56.100611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define 2D DCT\ndef dct2(a):\n    # Return the Discrete Cosine Transform of arbitrary type sequence x.\n    return fftpack.dct(fftpack.dct( a, axis=0, norm='ortho' ), axis=1, norm='ortho')","metadata":{"execution":{"iopub.status.busy":"2022-03-27T08:52:00.094358Z","iopub.execute_input":"2022-03-27T08:52:00.094656Z","iopub.status.idle":"2022-03-27T08:52:00.101237Z","shell.execute_reply.started":"2022-03-27T08:52:00.094626Z","shell.execute_reply":"2022-03-27T08:52:00.100113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"desired_size = 224\nim = Image.open(\"../input/alaska2-image-steganalysis/Cover/00004.jpg\")\nim = im.resize((desired_size, desired_size))\na = np.array(im)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T08:52:05.535889Z","iopub.execute_input":"2022-03-27T08:52:05.536195Z","iopub.status.idle":"2022-03-27T08:52:05.595454Z","shell.execute_reply.started":"2022-03-27T08:52:05.536166Z","shell.execute_reply":"2022-03-27T08:52:05.594674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a.shape[0]","metadata":{"execution":{"iopub.status.busy":"2022-03-27T08:52:09.741792Z","iopub.execute_input":"2022-03-27T08:52:09.74215Z","iopub.status.idle":"2022-03-27T08:52:09.751652Z","shell.execute_reply.started":"2022-03-27T08:52:09.742117Z","shell.execute_reply":"2022-03-27T08:52:09.750516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage import io\nfrom skimage.transform import rescale, resize, downscale_local_mean\n\nimg = io.imread(\"../input/alaska2-image-steganalysis/Cover/00004.jpg\")\nimg = resize(img,(224, 224))\nimg = img.reshape((224, 224))\nprint(img.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T08:53:29.874022Z","iopub.execute_input":"2022-03-27T08:53:29.874361Z","iopub.status.idle":"2022-03-27T08:53:30.75284Z","shell.execute_reply.started":"2022-03-27T08:53:29.874332Z","shell.execute_reply":"2022-03-27T08:53:30.751016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = mpimg.imread(\"../input/alaska2-image-steganalysis/Cover/00004.jpg\")\nimsize = image.shape\ndct = np.zeros(imsize)\nfor i in range(0,imsize[0],8):\n    for j in range(0,imsize[1],8):\n        dct[i:(i+8),j:(j+8)] = dct2(image[i:(i+8),j:(j+8)])","metadata":{"execution":{"iopub.status.busy":"2022-03-27T08:53:34.159878Z","iopub.execute_input":"2022-03-27T08:53:34.160167Z","iopub.status.idle":"2022-03-27T08:53:34.328198Z","shell.execute_reply.started":"2022-03-27T08:53:34.160128Z","shell.execute_reply":"2022-03-27T08:53:34.327422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = np.array(im)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T08:53:36.761439Z","iopub.execute_input":"2022-03-27T08:53:36.761878Z","iopub.status.idle":"2022-03-27T08:53:36.767247Z","shell.execute_reply.started":"2022-03-27T08:53:36.761777Z","shell.execute_reply":"2022-03-27T08:53:36.765965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#use dct of images as inputs in neural network\nimport cv2 \ndef preprocess_image(image_path,desired_size = 224):\n    image = cv2.imread(image_path,1) \n    image = cv2.resize(image, (desired_size, desired_size),  \n               interpolation = cv2.INTER_NEAREST) \n    dct = np.zeros(image.shape)\n    for i in range(0,image.shape[0],8):\n        for j in range(0,image.shape[1],8):\n            dct[i:(i+8),j:(j+8)] = dct2(image[i:(i+8),j:(j+8)])\n    return dct\n    ","metadata":{"execution":{"iopub.status.busy":"2022-03-27T08:53:41.783887Z","iopub.execute_input":"2022-03-27T08:53:41.784486Z","iopub.status.idle":"2022-03-27T08:53:41.793196Z","shell.execute_reply.started":"2022-03-27T08:53:41.784419Z","shell.execute_reply":"2022-03-27T08:53:41.792049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#def preprocess_image(image_path, desired_size=224):\n    im = Image.open(image_path)\n    im = im.convert('YCbCr') #lets convert ycbcr and train the model\n    im = im.resize((desired_size, desired_size))\n    im = np.array(im) / 255\n    return im","metadata":{"execution":{"iopub.status.busy":"2022-03-27T08:53:45.469726Z","iopub.execute_input":"2022-03-27T08:53:45.470101Z","iopub.status.idle":"2022-03-27T08:53:45.48003Z","shell.execute_reply.started":"2022-03-27T08:53:45.470072Z","shell.execute_reply":"2022-03-27T08:53:45.477843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#def preprocess_image(image_path,desired_size=224):\n    #im = Image.open(image_path)\n    #im = im.resize((desired_size, desired_size))\n    \n    jpeg = jio.read(image_path) #read image and convert into ycrbr\n    #jpeg = jpeg.resize((desired_size, desired_size)) #resize(224,224,3)\n    coverDCT = np.zeros([512,512,3])\n    coverDCT[:,:,0] = jpeg.coef_arrays[0]/255  #dct coeff of y\n    \n    \n    coverDCT[:,:,1] = jpeg.coef_arrays[1]/255  #dct of br\n    coverDCT[:,:,2] = jpeg.coef_arrays[2]/255   #dct of cr\n    return coverDCT\n    \n    \n    ","metadata":{"execution":{"iopub.status.busy":"2022-03-27T08:53:48.028444Z","iopub.execute_input":"2022-03-27T08:53:48.028761Z","iopub.status.idle":"2022-03-27T08:53:48.04068Z","shell.execute_reply.started":"2022-03-27T08:53:48.028731Z","shell.execute_reply":"2022-03-27T08:53:48.039007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"jpeg = jio.read(\"../input/alaska2-image-steganalysis/Cover/00004.jpg\")\na = np.array(jpeg.coef_arrays[0])","metadata":{"execution":{"iopub.status.busy":"2022-03-27T08:53:51.686194Z","iopub.execute_input":"2022-03-27T08:53:51.686576Z","iopub.status.idle":"2022-03-27T08:53:51.712807Z","shell.execute_reply.started":"2022-03-27T08:53:51.686529Z","shell.execute_reply":"2022-03-27T08:53:51.711872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = jpeg.coef_arrays[0]/255","metadata":{"execution":{"iopub.status.busy":"2022-03-27T08:53:53.949655Z","iopub.execute_input":"2022-03-27T08:53:53.949956Z","iopub.status.idle":"2022-03-27T08:53:53.957024Z","shell.execute_reply.started":"2022-03-27T08:53:53.949928Z","shell.execute_reply":"2022-03-27T08:53:53.955286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-27T08:53:57.037701Z","iopub.execute_input":"2022-03-27T08:53:57.038039Z","iopub.status.idle":"2022-03-27T08:53:57.044948Z","shell.execute_reply.started":"2022-03-27T08:53:57.038009Z","shell.execute_reply":"2022-03-27T08:53:57.043737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename","metadata":{"execution":{"iopub.status.busy":"2022-03-26T08:48:38.250683Z","iopub.execute_input":"2022-03-26T08:48:38.25101Z","iopub.status.idle":"2022-03-26T08:48:38.275387Z","shell.execute_reply.started":"2022-03-26T08:48:38.250981Z","shell.execute_reply":"2022-03-26T08:48:38.273825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\n# total samles \nN = cN * len(class_locs)\n# initialize y_train\ny_train = []\n# initalize x_train\nx_train = np.empty((N, 224, 224, 3), dtype=np.float16)\n# initialize class counter\nimClassNum = 0\n\n# run loop to grab training data\nfor imClass_dir in class_locs:\n    imClassNum += 1\n    print(\"running class\", str(imClassNum), \"...\")\n    for i, filename in enumerate(os.listdir(imClass_dir)):\n        x_train[i, :, :, :] = preprocess_image(imClass_dir+filename)\n        print(str(round(i/75000,2))+\"% of total images processed, \"+str(i)+\" images in total\", end=\"\\r\")\n        if i == cN:\n            print(\"\")\n            print(\"finished with class \" + str(imClassNum) + \"..\")\n            break\n    y_train.extend([imClassNum] * cN)\n\n# convert y_train to numpy    \ny_train = np.array(y_train)    \n\nprint(\"final training dataset shape..\")\nprint(\"x_train shape: \", x_train.shape)\nprint(\"y_train len: \", len(y_train))\nprint(str(round(N/(75000*4),2))+\"% of total images being used for training\")","metadata":{"execution":{"iopub.status.busy":"2022-03-27T08:54:00.524564Z","iopub.execute_input":"2022-03-27T08:54:00.524884Z","iopub.status.idle":"2022-03-27T09:03:13.268293Z","shell.execute_reply.started":"2022-03-27T08:54:00.524856Z","shell.execute_reply":"2022-03-27T09:03:13.26689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-27T09:09:16.073568Z","iopub.execute_input":"2022-03-27T09:09:16.073873Z","iopub.status.idle":"2022-03-27T09:09:16.081932Z","shell.execute_reply.started":"2022-03-27T09:09:16.073845Z","shell.execute_reply":"2022-03-27T09:09:16.080591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pre-processing the target (i.e. one-hot encoding the target)\ny_train = pd.get_dummies(y_train).values","metadata":{"execution":{"iopub.status.busy":"2022-03-27T09:09:23.268197Z","iopub.execute_input":"2022-03-27T09:09:23.268602Z","iopub.status.idle":"2022-03-27T09:09:23.307507Z","shell.execute_reply.started":"2022-03-27T09:09:23.268559Z","shell.execute_reply":"2022-03-27T09:09:23.306037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-27T09:09:31.775993Z","iopub.execute_input":"2022-03-27T09:09:31.776357Z","iopub.status.idle":"2022-03-27T09:09:31.783501Z","shell.execute_reply.started":"2022-03-27T09:09:31.776327Z","shell.execute_reply":"2022-03-27T09:09:31.782399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train","metadata":{"execution":{"iopub.status.busy":"2022-03-27T09:09:34.984973Z","iopub.execute_input":"2022-03-27T09:09:34.985301Z","iopub.status.idle":"2022-03-27T09:09:35.011335Z","shell.execute_reply.started":"2022-03-27T09:09:34.985271Z","shell.execute_reply":"2022-03-27T09:09:35.010441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train Validation Split","metadata":{}},{"cell_type":"code","source":"x_train, x_val, y_train, y_val = train_test_split(\n    x_train, y_train, \n    test_size=TRAIN_VAL_SPLIT_RATIO, \n    random_state=2020\n)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T09:09:50.183824Z","iopub.execute_input":"2022-03-27T09:09:50.184128Z","iopub.status.idle":"2022-03-27T09:09:51.462026Z","shell.execute_reply.started":"2022-03-27T09:09:50.184098Z","shell.execute_reply":"2022-03-27T09:09:51.461121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image Gen","metadata":{}},{"cell_type":"code","source":"# define gen\n\nimg_gen = ImageDataGenerator(\n        zoom_range=0.15,  # set range for random zoom\n        # set mode for filling points outside the input boundaries\n        fill_mode='constant',\n        cval=0.,  # value used for fill_mode = \"constant\"\n        horizontal_flip=True,  # randomly flip images\n        vertical_flip=True,  # randomly flip images\n       \n    )\n\n# create generator\ndata_generator = img_gen.flow(x_train, y_train, batch_size=BATCH_SIZE, seed=2019)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T09:09:54.521103Z","iopub.execute_input":"2022-03-27T09:09:54.521419Z","iopub.status.idle":"2022-03-27T09:09:58.963763Z","shell.execute_reply.started":"2022-03-27T09:09:54.521375Z","shell.execute_reply":"2022-03-27T09:09:58.962691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create Model","metadata":{}},{"cell_type":"code","source":"densenet = DenseNet121(\n    weights='../input/densenet-keras/DenseNet-BC-121-32-no-top.h5',\n    include_top=False,\n    input_shape=(224,224,3)\n)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T09:10:03.449343Z","iopub.execute_input":"2022-03-27T09:10:03.449631Z","iopub.status.idle":"2022-03-27T09:10:15.600969Z","shell.execute_reply.started":"2022-03-27T09:10:03.449601Z","shell.execute_reply":"2022-03-27T09:10:15.599836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndensenet = DenseNet121(\n    weights='../input/full-keras-pretrained-no-top/densenet121_weights_tf_dim_ordering_tf_kernels_notop.h5',\n    include_top=False,\n    input_shape=(224,224,3)\n)","metadata":{"execution":{"iopub.status.busy":"2022-03-26T09:05:09.678027Z","iopub.execute_input":"2022-03-26T09:05:09.678302Z","iopub.status.idle":"2022-03-26T09:05:09.715648Z","shell.execute_reply.started":"2022-03-26T09:05:09.678275Z","shell.execute_reply":"2022-03-26T09:05:09.714627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_model():\n    model = Sequential()\n    model.add(densenet)\n    model.add(layers.GlobalAveragePooling2D())\n    model.add(layers.Dropout(0.80))\n    model.add(layers.Dense(4, activation='sigmoid'))\n    \n    model.compile(\n        loss='binary_crossentropy',\n        optimizer=Adam(lr=0.00010509613402110064),\n        metrics=['accuracy']\n    )\n    \n    return model","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = build_model()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T09:14:15.313863Z","iopub.execute_input":"2022-03-27T09:14:15.314177Z","iopub.status.idle":"2022-03-27T09:14:21.854146Z","shell.execute_reply.started":"2022-03-27T09:14:15.314148Z","shell.execute_reply":"2022-03-27T09:14:21.85332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train Model","metadata":{}},{"cell_type":"code","source":"history = model.fit_generator(\n    data_generator,\n    steps_per_epoch=x_train.shape[0] / BATCH_SIZE,\n    epochs=15,\n    validation_data=(x_val, y_val)\n)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T09:14:28.255687Z","iopub.execute_input":"2022-03-27T09:14:28.256029Z","iopub.status.idle":"2022-03-27T10:25:37.690809Z","shell.execute_reply.started":"2022-03-27T09:14:28.255997Z","shell.execute_reply":"2022-03-27T10:25:37.689738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training Plots","metadata":{}},{"cell_type":"code","source":"history_df = pd.DataFrame(history.history)\nhistory_df[['loss', 'val_loss']].plot()\nhistory_df[['accuracy', 'val_accuracy']].plot()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T10:30:39.866871Z","iopub.execute_input":"2022-03-27T10:30:39.867227Z","iopub.status.idle":"2022-03-27T10:30:40.342496Z","shell.execute_reply.started":"2022-03-27T10:30:39.867193Z","shell.execute_reply":"2022-03-27T10:30:40.341182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"sub = pd.read_csv(\"../input/alaska2-image-steganalysis/sample_submission.csv\")\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T10:30:44.560146Z","iopub.execute_input":"2022-03-27T10:30:44.560593Z","iopub.status.idle":"2022-03-27T10:30:44.60874Z","shell.execute_reply.started":"2022-03-27T10:30:44.560554Z","shell.execute_reply":"2022-03-27T10:30:44.60786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test data directory\ntest_dir = \"../input/alaska2-image-steganalysis/Test/\"\n\n# do similar data loading we did on train but on the test\\holdout set\nN = len([name for name in os.listdir(test_dir)])\nx_test = np.empty((N, 224, 224, 3), dtype=np.uint8)\nfor i, filename in enumerate(tqdm(os.listdir(test_dir))):\n    x_test[i, :, :, :] = preprocess_image(test_dir+filename)\n\nx_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-27T10:30:47.52702Z","iopub.execute_input":"2022-03-27T10:30:47.52737Z","iopub.status.idle":"2022-03-27T10:34:26.346671Z","shell.execute_reply.started":"2022-03-27T10:30:47.527339Z","shell.execute_reply":"2022-03-27T10:34:26.34543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = model.predict(x_test)\ny_test = y_test[:,0]\nsub['Label'] = y_test","metadata":{"execution":{"iopub.status.busy":"2022-03-27T10:38:14.168253Z","iopub.execute_input":"2022-03-27T10:38:14.168557Z","iopub.status.idle":"2022-03-27T10:38:41.223059Z","shell.execute_reply.started":"2022-03-27T10:38:14.168514Z","shell.execute_reply":"2022-03-27T10:38:41.222101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['Label']","metadata":{"execution":{"iopub.status.busy":"2022-03-27T10:39:03.607872Z","iopub.execute_input":"2022-03-27T10:39:03.608174Z","iopub.status.idle":"2022-03-27T10:39:03.618763Z","shell.execute_reply.started":"2022-03-27T10:39:03.608144Z","shell.execute_reply":"2022-03-27T10:39:03.617444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv(\"submission_denep15_dct.csv\", index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-03-26T08:42:37.942808Z","iopub.status.idle":"2022-03-26T08:42:37.943369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test","metadata":{"execution":{"iopub.status.busy":"2022-03-26T08:42:37.944427Z","iopub.status.idle":"2022-03-26T08:42:37.945018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}