{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nimport numpy as np\nimport pandas as pd\nfrom matplotlib import pyplot as plt\nimport glob\nimport seaborn as sns\nimport random\nfrom sklearn.preprocessing import LabelBinarizer","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import os \nos.listdir('../input/dogcat-release/')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"导入图片和预处理","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train = glob.glob('../input/dogcat-release/train/*.jpg')\ntest =glob.glob('../input/dogcat-release/test/*.jpg')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = np.random.permutation(train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"label_names = set([p.split('/')[-1].split('.')[0] for p in train])\nlabel_names ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = [p.split('/')[-1].split('.')[0] for p in train]\nlabels[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = pd.DataFrame(labels,columns=['Type'])\nClass = labels['Type'].unique()\nClass_dict = dict(zip(Class, range(1,len(Class)+1)))\n\nlabels['str'] = labels['Type'].apply(lambda x: Class_dict[x])\nlb = LabelBinarizer()\nlb.fit(list(Class_dict.values()))\ntransformed_labels = lb.transform(labels['str'])\ny_bin_labels = []  \n\nfor i in range(transformed_labels.shape[1]):\n    y_bin_labels.append('str' + str(i))\n    labels['str' + str(i)] = transformed_labels[:, i]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Class_dict","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels.drop('str',axis=1,inplace=True)\nlabels.drop('Type',axis=1,inplace=True)\nlabels = labels.str0.values\nlabels[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#预处理函数\ndef preprocess_image(path,label):\n    image = tf.io.read_file(path)                           \n    image = tf.image.decode_jpeg(image,3)               \n    image = tf.image.resize(image,[224,224])       \n    image = tf.cast(image/127.5 -1,tf.float32)     \n\n    return image,label       ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataset = tf.data.Dataset.from_tensor_slices((train, labels)) \ndataset = dataset.shuffle(len(train))\ndataset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#创建数据集\nAUTO = tf.data.experimental.AUTOTUNE\ndataset = dataset.map(preprocess_image, num_parallel_calls = AUTO)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#切分数据集\ntest_count = int(len(train)*0.2)\ntrain_count = len(train) - test_count\ntrain_count,test_count","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_dataset = dataset.skip(test_count) \ntest_dataset = dataset.take(test_count)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"batch_size = 128","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_dataset = train_dataset.repeat().shuffle(800).batch(batch_size)\ntrain_dataset = train_dataset.prefetch(AUTO)\ntest_dataset = test_dataset.batch(batch_size)\ntrain_dataset","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"创建三种迁移模型结合测试","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"#Xception\nconv1 = keras.applications.xception.Xception(weights='imagenet',\n                                            include_top=False,\n                                            input_shape=(224,224,3),\n                                            pooling='avg')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#ResNet152\nconv2 = keras.applications.resnet.ResNet152(weights='imagenet',\n                                            include_top=False,\n                                            input_shape=(224,224,3),\n                                            pooling='avg')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#InceptionResNetV2\nconv3 = keras.applications.inception_v3.InceptionV3(weights='imagenet',\n                                                    include_top=False,\n                                                    input_shape=(224,224,3),\n                                                    pooling='avg')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"conv1.trainable = False\nconv2.trainable = False\nconv3.trainable = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"conv1.inputs,conv2.inputs,conv3.inputs","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"conv1.outputs,conv2.outputs,conv3.outputs","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def model():\n    inputs = tf.keras.layers.Input(shape=(224, 224, 3))\n    out1 = conv1(inputs)\n    out2 = conv2(inputs)\n    out3 = conv3(inputs)\n    out = tf.keras.layers.concatenate([out1,out2,out3],axis=1)\n    out = tf.keras.layers.Dropout(0.5)(out)\n    output = tf.keras.layers.Dense(1, activation='sigmoid')(out)\n    \n    return tf.keras.Model(inputs=inputs, outputs=output)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = model()\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tf.keras.utils.plot_model(model,show_shapes=True,dpi=300)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss='binary_crossentropy',\n              metrics=['acc'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"EPOCHS = 10","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit(train_dataset,\n                   steps_per_epoch=train_count//batch_size,\n                   epochs=EPOCHS,\n                   validation_data=test_dataset,\n                   validation_steps=test_count//batch_size,\n                   )","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"模型评估","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def plot_history(history):                \n    hist = pd.DataFrame(history.history)           \n    hist['epoch']=history.epoch\n    \n    plt.figure()                                     \n    plt.xlabel('Epoch')\n    plt.ylabel('Binary_crossentropy')               \n    plt.plot(hist['epoch'],hist['loss'],\n            label='Train Loss')\n    plt.plot(hist['epoch'],hist['val_loss'],\n            label='Val Loss')                           \n    plt.legend()\n    \n    plt.figure()                                      \n    plt.xlabel('Epoch')\n    plt.ylabel('Accuracy')               \n    plt.plot(hist['epoch'],hist['acc'],\n            label='Train Acc')\n    plt.plot(hist['epoch'],hist['val_acc'],\n            label='Val Acc')\n    plt.legend()      \n    \n    plt.show()\n    \nplot_history(history)          ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred = model.predict(test_dataset, verbose=1)\ny_pred = y_pred.clip(min=0.005, max=0.995)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def preprocess_test(path):\n    image = tf.io.read_file(path)                           \n    image = tf.image.decode_jpeg(image,3)               \n    image = tf.image.resize(image,[224,224])       \n    image = tf.cast(image/127.5 -1,tf.float32)     \n\n    return image  ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Dir = \"../input/dogcat-release/test/\"\nimgList = os.listdir(Dir)\nimgList.sort(key=lambda x: int(x.split('/')[-1].split('.')[0]))\nimgList[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_path = []\nfor count in range(0, len(imgList)):\n    im_name = imgList[count]\n    im_path = os.path.join(Dir,im_name)\n    test_path.append(im_path)\n    print(im_path)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_path[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"val_dataset = tf.data.Dataset.from_tensor_slices(test_path) \nval_dataset = val_dataset.map(preprocess_test, num_parallel_calls = AUTO)\nval_dataset = val_dataset.batch(batch_size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred = model.predict(val_dataset, verbose=1)\ny_pred[:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred = pd.DataFrame(y_pred).iloc[:,0].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def type_change(data):\n    for i in range(data.shape[0]):\n        if data[i] > 0.5:\n            data[i] = 0.005\n        else: data[i] = 0.995\n    return data\n\npredict_labels = type_change(pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predict_labels[:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"prediction = pd.DataFrame({\"label\":predict_labels})\nprediction.index += 1 \nprediction.to_csv('pred.csv',\n                  index_label='id')","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}