{"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\nimport tensorflow as tf\nimport requests\nimport sys\nsys.path.append(\"../input/ai-training-challenge-1/cifar10_input.py\")\nimport cifar10_input\nsys.path.append(\"../input/ai-training-challenge-1/model.py\")\nfrom model_challenge import Model_Plain_Net\nimport pandas as pd\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-02T16:45:12.57667Z","iopub.execute_input":"2022-04-02T16:45:12.577032Z","iopub.status.idle":"2022-04-02T16:45:17.963325Z","shell.execute_reply.started":"2022-04-02T16:45:12.576949Z","shell.execute_reply":"2022-04-02T16:45:17.962576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Model","metadata":{}},{"cell_type":"markdown","source":"Define Model Parameters","metadata":{}},{"cell_type":"code","source":"input_shape = (32,32,3)\nclass_num = 10\n# Construct model\nmodel = Model_Plain_Net(tf.keras.layers.Input(shape = input_shape),class_num)\n# The path for CIFAR-10 dataset\ndata_path = \"../input/ai-training-challenge-1/Dataset/\"\n# The url for downloading the pre-trained model\nurl = 'XXX' # Give url string for your trained network","metadata":{"execution":{"iopub.status.busy":"2022-04-02T16:45:17.964878Z","iopub.execute_input":"2022-04-02T16:45:17.965137Z","iopub.status.idle":"2022-04-02T16:45:21.431078Z","shell.execute_reply.started":"2022-04-02T16:45:17.965103Z","shell.execute_reply":"2022-04-02T16:45:21.430362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Download pre-trained model","metadata":{}},{"cell_type":"code","source":"# Create a requests variable with the given url of pre-trained model\nr = requests.get(url, allow_redirects=True)\n# Write the content of the request to 'pre_trained.hdf5' file for using in test\nwith open('./pre_trained.hdf5', 'wb') as f:\n    f.write(r.content)","metadata":{"execution":{"iopub.status.busy":"2022-04-02T16:45:21.432283Z","iopub.execute_input":"2022-04-02T16:45:21.434211Z","iopub.status.idle":"2022-04-02T16:45:23.191226Z","shell.execute_reply.started":"2022-04-02T16:45:21.434179Z","shell.execute_reply":"2022-04-02T16:45:23.190468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Load pre-trained model weights","metadata":{}},{"cell_type":"code","source":"model.net = tf.keras.models.load_model('./pre_trained.hdf5')","metadata":{"execution":{"iopub.status.busy":"2022-04-02T16:45:23.193026Z","iopub.execute_input":"2022-04-02T16:45:23.193262Z","iopub.status.idle":"2022-04-02T16:45:24.227393Z","shell.execute_reply.started":"2022-04-02T16:45:23.193229Z","shell.execute_reply":"2022-04-02T16:45:24.22663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Get Predictions","metadata":{}},{"cell_type":"code","source":"# Set the columns of submission csv file\nId = ['Trained_Net_Path']\nCategory = [url]\n# Get object for reading CIFAR-10 test datasset\nraw_cifar = cifar10_input.CIFAR10Data(data_path)\n# Set parameters for measuring test accuracy\nmean_test_acc = 0.0\ntest_batch = 100\ncyle_num = int(10000/test_batch)\n# Get predictions for submission and measure accuracy\nfor i in range(cyle_num):\n    testX = raw_cifar.eval_data.xs[i*test_batch:(i+1)*test_batch,:,:,:] / 255.0\n    testY = raw_cifar.eval_data.ys[i*test_batch:(i+1)*test_batch]  \n    predictions = model.net.predict(testX)\n    predict_label = np.argmax(predictions,axis=1)\n    # Write predictions on corresponding lists\n    for k in range(test_batch):\n        Id.append(str(i*test_batch+k))\n        Category.append(str(predict_label[k]))\n    # Measure accuracy for given batch\n    correct_predict = np.equal(predict_label,testY)\n    batch_test_acc = np.sum(correct_predict)/np.float32(test_batch)\n    \n    mean_test_acc += batch_test_acc/np.float32(cyle_num)\nprint(\"mean_test_acc = %f\"%mean_test_acc)\n\n# Write predictions on submission file\nsubmission = pd.DataFrame({'Id': Id, 'Category': Category})\nsubmission.to_csv('submission.csv', index=False)\n\n# Delete temporarily crerated pre-trained model file\nos.remove('./pre_trained.hdf5')","metadata":{"execution":{"iopub.status.busy":"2022-04-02T16:45:24.22871Z","iopub.execute_input":"2022-04-02T16:45:24.22896Z","iopub.status.idle":"2022-04-02T16:45:42.637059Z","shell.execute_reply.started":"2022-04-02T16:45:24.228916Z","shell.execute_reply":"2022-04-02T16:45:42.636361Z"},"trusted":true},"execution_count":null,"outputs":[]}]}