{"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 in \n\n# import numpy as np # linear algebra\n# import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# # Input data files are available in the \"../input/\" directory.\n# # For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# # Any results you write to the current directory are saved as output.\nimport pandas as pd\nimport os\nimport json\nimport pylab\nimport imageio\nimport cv2\nimport time\nimport numpy as np","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Label_Path = \"../input/label/label/\"\nLabel_File = \"metadata.json\"\nface_root = '../input/train_frame_face_9/train_frame_face_9/'","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# **1. Load data label & Transformation**"},{"metadata":{"trusted":true},"cell_type":"code","source":"File = open(Label_Path+Label_File)\nLabel_data = json.load(File)\nLabel_df = pd.DataFrame(Label_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Label_df","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"Label_df = pd.DataFrame(Label_df.values.T, index=Label_df.columns, columns=Label_df.index)\nLabel_df = Label_df.reset_index()\nLabel_df.rename(columns={'index':'file'}, inplace = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Label_df","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# **2. Sift All Real And Fake**"},{"metadata":{"trusted":true},"cell_type":"code","source":"Label_df_REAL = Label_df[Label_df['label']=='REAL']\nLabel_df_FAKE = Label_df[Label_df['label']=='FAKE']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Label_df_REAL","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Label_df_FAKE","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# **3. Load Train Data**"},{"metadata":{"trusted":true},"cell_type":"code","source":"def read_frame(video_path):\n    capture = cv2.VideoCapture(video_path)\n    ret, frame = capture.read()\n    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n    frame = cv2.resize(frame, (384, 384))\n    capture.release()\n#     frame = Jet(frame)\n    return frame\n\ndef read_frame_ori(video_path):\n    capture = cv2.VideoCapture(video_path)\n    ret, frame = capture.read()\n    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n    capture.release()\n    return frame","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# 指定训练集\ntrain_nums = int(len(Label_df_REAL)* 0.8)\ntrain_index = range(0,1)\n# train_real_path_all = []\ntrain_frame = []\ntrain_label = []","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(0,len(train_index)):\n    train_sample_real = Label_df_REAL.iloc[train_index[i]]['file']\n    print(train_sample_real)\n    Label_df_FAKE_Train = Label_df_FAKE[Label_df_FAKE['original']==train_sample_real]\n    train_real_path = face_root + train_sample_real + '_face/'\n    train_image_names_real = os.listdir(train_real_path)\n    for j in range(0,len(train_image_names_real)):\n        frame = read_frame(train_real_path + train_image_names_real[j])\n        train_frame.append(frame)\n        train_label.append(1)\n    train_fake_path = []\n    for k in range(0,len(Label_df_FAKE_Train)):\n        train_fake_path.append(face_root + Label_df_FAKE_Train.iloc[k]['file'] + '_face/')   \n    for m in range(0,len(train_fake_path)):\n        train_image_names_fake = os.listdir(train_fake_path[m])\n        for n in range(0,len(train_image_names_fake)):\n            frame = read_frame(train_fake_path[m] + train_image_names_fake[n])\n            train_frame.append(frame)\n            train_label.append(0) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_frame = np.array(train_frame)\ntrain_frame.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_label = np.array(train_label)\ntrain_label.reshape((-1,1)).shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# **4. Load Test Data**"},{"metadata":{"trusted":true},"cell_type":"code","source":"# 指定test集\ntest_index = range(10,12)\n# train_real_path_all = []\ntest_frame = []\ntest_label = []","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(0,len(test_index)):\n    test_sample_real = Label_df_REAL.iloc[test_index[i]]['file']\n    print(test_sample_real)\n    Label_df_FAKE_Test = Label_df_FAKE[Label_df_FAKE['original']==test_sample_real]\n    test_real_path = face_root + test_sample_real + '_face/'\n    test_image_names_real = os.listdir(test_real_path)\n    \n\n    for j in range(0,len(test_image_names_real)):\n        frame = read_frame(test_real_path + test_image_names_real[j])\n        test_frame.append(frame)\n        test_label.append(1)\n    test_fake_path = []\n    for k in range(0,len(Label_df_FAKE_Test)):\n        test_fake_path.append(face_root + Label_df_FAKE_Test.iloc[k]['file'] + '_face/')\n    \n     \n    for m in range(0,len(test_fake_path)):\n        test_image_names_fake = os.listdir(test_fake_path[m])\n        for n in range(0,len(test_image_names_fake)):\n            frame = read_frame(test_fake_path[m] + test_image_names_fake[n])\n            test_frame.append(frame)\n            test_label.append(0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_frame = np.array(test_frame)\ntest_frame.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_label = np.array(test_label)\ntest_label.reshape((-1,1)).shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# **5. Train And Test**"},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.models import Model\nfrom keras import layers\nfrom keras.layers import Dense, Input, BatchNormalization, Activation\nfrom keras.layers import Conv2D, SeparableConv2D, MaxPooling2D, GlobalAveragePooling2D, GlobalMaxPooling2D\n#from keras.applications.imagenet_utils import _obtain_input_shape\nfrom keras.utils.data_utils import get_file\n\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping, LearningRateScheduler\nfrom keras.utils import np_utils\nfrom keras import regularizers, optimizers\nfrom keras.optimizers import SGD\nimport os\nimport h5py\nimport matplotlib\nimport matplotlib.pyplot as plt\nimport random\n#import cv2\nfrom sklearn.preprocessing import LabelEncoder\nfrom skimage import io, transform\nfrom sklearn.metrics import accuracy_score\nfrom scipy import misc\nimport numpy as np","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.4/xception_weights_tf_dim_ordering_tf_kernels.h5'\n\ndef Xception(nb_classes):\n\n    # Determine proper input shape\n#     input_shape = _obtain_input_shape(None, default_size=299, min_size=71, data_format='channels_last', include_top=False)\n\n#     img_input = Input(shape=input_shape)\n#     img_input = Input(shape=(227,227,3))\n    img_input = Input(shape=(384,384,3))\n\n    # Block 1\n    x = Conv2D(32, (3, 3), strides=(2, 2), use_bias=False)(img_input)\n    x = BatchNormalization()(x)\n    x = Activation('relu')(x)\n    x = Conv2D(64, (3, 3), use_bias=False)(x)\n    x = BatchNormalization()(x)\n    x = Activation('relu')(x)\n\n    residual = Conv2D(128, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x)\n    residual = BatchNormalization()(residual)\n\n    # Block 2\n    x = SeparableConv2D(128, (3, 3), padding='same', use_bias=False)(x)\n    x = BatchNormalization()(x)\n    x = Activation('relu')(x)\n    x = SeparableConv2D(128, (3, 3), padding='same', use_bias=False)(x)\n    x = BatchNormalization()(x)\n\n    # Block 2 Pool\n    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)\n    x = layers.add([x, residual])\n\n    residual = Conv2D(256, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x)\n    residual = BatchNormalization()(residual)\n\n    # Block 3\n    x = Activation('relu')(x)\n    x = SeparableConv2D(256, (3, 3), padding='same', use_bias=False)(x)\n    x = BatchNormalization()(x)\n    x = Activation('relu')(x)\n    x = SeparableConv2D(256, (3, 3), padding='same', use_bias=False)(x)\n    x = BatchNormalization()(x)\n\n    # Block 3 Pool\n    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)\n    x = layers.add([x, residual])\n\n    residual = Conv2D(728, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x)\n    residual = BatchNormalization()(residual)\n\n    # Block 4\n    x = Activation('relu')(x)\n    x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False)(x)\n    x = BatchNormalization()(x)\n    x = Activation('relu')(x)\n    x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False)(x)\n    x = BatchNormalization()(x)\n\n    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)\n    x = layers.add([x, residual])\n\n    # Block 5 - 12\n    for i in range(8):\n        residual = x\n\n        x = Activation('relu')(x)\n        x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False)(x)\n        x = BatchNormalization()(x)\n        x = Activation('relu')(x)\n        x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False)(x)\n        x = BatchNormalization()(x)\n        x = Activation('relu')(x)\n        x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False)(x)\n        x = BatchNormalization()(x)\n\n        x = layers.add([x, residual])\n\n    residual = Conv2D(1024, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x)\n    residual = BatchNormalization()(residual)\n\n    # Block 13\n    x = Activation('relu')(x)\n    x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False)(x)\n    x = BatchNormalization()(x)\n    x = Activation('relu')(x)\n    x = SeparableConv2D(1024, (3, 3), padding='same', use_bias=False)(x)\n    x = BatchNormalization()(x)\n\n    # Block 13 Pool\n    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)\n    x = layers.add([x, residual])\n\n    # Block 14\n    x = SeparableConv2D(1536, (3, 3), padding='same', use_bias=False)(x)\n    x = BatchNormalization()(x)\n    x = Activation('relu')(x)\n\n    # Block 14 part 2\n    x = SeparableConv2D(2048, (3, 3), padding='same', use_bias=False)(x)\n    x = BatchNormalization()(x)\n    x = Activation('relu')(x)\n\n    # Fully Connected Layer\n    x = GlobalAveragePooling2D()(x)\n    x = Dense(1000, activation='relu')(x)\n    x = Dense(nb_classes, activation='softmax')(x)\n\n    inputs = img_input\n\n    # Create model\n    model = Model(inputs, x, name='xception')\n\n    # Download and cache the Xception weights file\n    #weights_path = get_file('xception_weights.h5', WEIGHTS_PATH, cache_subdir='models')\n\n    # load weights\n    #model.load_weights(weights_path)\n\n    return model\n\n\n# \"\"\"\n#     Instantiate the model by using the following line of code\n\n#     model = Xception()\n\n# \"\"\"\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data = train_frame.astype(np.float16)/255\ntest_data = test_frame.astype(np.float16)/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#将标签量进行转化\ntrain_labels = np_utils.to_categorical(train_label)\ntest_labels = np_utils.to_categorical(test_label)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#设置模型\nnum_classes=2\nmodel=Xception(num_classes)\n#编译模型\nepochs = 50\nlearning_rate = 0.01\ndecay_rate = learning_rate / epochs\nmomentum = 0.9\nsgd = SGD(lr=learning_rate, momentum=momentum,  decay=decay_rate, nesterov=False)\nmodel.compile(loss='categorical_crossentropy',optimizer='sgd',metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history=model.fit(train_data, train_labels,validation_split=0.0, nb_epoch=epochs,batch_size=8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#测试模型\npreds = np.argmax(model.predict(test_data), axis=1)\ntest_labels = np.argmax(test_labels, axis=1)\nprint (accuracy_score(test_labels, preds))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(preds)","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}