{"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\ntf.compat.v1.disable_v2_behavior()\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":{"execution":{"iopub.status.busy":"2022-04-02T16:49:08.101058Z","iopub.execute_input":"2022-04-02T16:49:08.10143Z","iopub.status.idle":"2022-04-02T16:49:13.218934Z","shell.execute_reply.started":"2022-04-02T16:49:08.101344Z","shell.execute_reply":"2022-04-02T16:49:13.218132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Model","metadata":{}},{"cell_type":"markdown","source":"Define Model Parameters\n","metadata":{}},{"cell_type":"code","source":"class_num = 10\n# Construct model\nx_input_place_holder = tf.compat.v1.placeholder(tf.float32,shape=[None, 32, 32, 3],name='x_input')\ny_input_place_holder = tf.compat.v1.placeholder(tf.int64, shape=None,name=\"label\")\nlabelPlaceHolder = tf.one_hot(y_input_place_holder,class_num,on_value=1.0,off_value=0.0,dtype=tf.float32,name=\"label_one_hot\")\nmodel = Model_Plain_Net(x_input_place_holder,class_num,tf_ver=1)\n# The path for CIFAR-10 dataset\ndata_path = \"../input/ai-training-challenge-1/Dataset/\"","metadata":{"execution":{"iopub.status.busy":"2022-04-02T16:49:13.220391Z","iopub.execute_input":"2022-04-02T16:49:13.220631Z","iopub.status.idle":"2022-04-02T16:49:14.522354Z","shell.execute_reply.started":"2022-04-02T16:49:13.220597Z","shell.execute_reply":"2022-04-02T16:49:14.521624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read dataset for training and testing","metadata":{}},{"cell_type":"code","source":"# Get object for reading CIFAR-10 test datasset\nraw_cifar = cifar10_input.CIFAR10Data(data_path)\n# Get training dataset\nx_train = raw_cifar.train_data.xs / 255.0\ny_train = raw_cifar.train_data.ys\n# Get test dataset\nx_test = raw_cifar.eval_data.xs\ny_test = raw_cifar.eval_data.ys","metadata":{"execution":{"iopub.status.busy":"2022-04-02T16:49:14.523652Z","iopub.execute_input":"2022-04-02T16:49:14.523904Z","iopub.status.idle":"2022-04-02T16:49:17.182979Z","shell.execute_reply.started":"2022-04-02T16:49:14.523873Z","shell.execute_reply":"2022-04-02T16:49:17.182214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training\n**You should modify this section to define your optimizer method and train the given model with best accuracy.**\n\n**Good luck and have fun**","metadata":{}},{"cell_type":"code","source":"batch_size = 128\nepoch_size = int(50000/batch_size)\nmax_num_training_steps = epoch_size*5\narr = np.arange(50000)","metadata":{"execution":{"iopub.status.busy":"2022-04-02T16:49:17.186709Z","iopub.execute_input":"2022-04-02T16:49:17.186929Z","iopub.status.idle":"2022-04-02T16:49:17.191754Z","shell.execute_reply.started":"2022-04-02T16:49:17.186903Z","shell.execute_reply":"2022-04-02T16:49:17.190967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def define_accuracy(net_out,y_input):\n    predictions = tf.argmax(input=net_out, axis=1)\n    correct_prediction = tf.equal(predictions, y_input)\n    num_correct = tf.reduce_sum(input_tensor=tf.cast(correct_prediction, tf.int64))\n    accuracy = tf.reduce_mean(input_tensor=tf.cast(correct_prediction, tf.float32))\n\n    return accuracy","metadata":{"execution":{"iopub.status.busy":"2022-04-02T16:49:17.193148Z","iopub.execute_input":"2022-04-02T16:49:17.193508Z","iopub.status.idle":"2022-04-02T16:49:17.201163Z","shell.execute_reply.started":"2022-04-02T16:49:17.193472Z","shell.execute_reply":"2022-04-02T16:49:17.200298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def define_optimizer(out,label,lr,momentum):\n    trainOp = tf.compat.v1.train.MomentumOptimizer(learning_rate=lr, momentum=momentum) \n    loss_function = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=label, logits=out),name=\"Cross_Entropy_Loss\") \n    grads_and_vars = trainOp.compute_gradients(loss_function)\n    train_step = trainOp.apply_gradients(grads_and_vars)\n\n    return train_step,loss_function","metadata":{"execution":{"iopub.status.busy":"2022-04-02T16:49:17.202345Z","iopub.execute_input":"2022-04-02T16:49:17.202979Z","iopub.status.idle":"2022-04-02T16:49:17.209916Z","shell.execute_reply.started":"2022-04-02T16:49:17.202943Z","shell.execute_reply":"2022-04-02T16:49:17.209217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_step,loss_function = define_optimizer(model.output,labelPlaceHolder,0.01,0.9)\naccuracy_op = define_accuracy(model.output,y_input_place_holder)\nsaver = tf.compat.v1.train.Saver()\ntf1_save_dir = \"./model_tf1/\"\nif not os.path.exists(tf1_save_dir):\n    os.makedirs(tf1_save_dir)\nwith tf.compat.v1.Session() as sess:\n    sess.run(tf.compat.v1.global_variables_initializer())\n    for ii in range(max_num_training_steps):\n        # Read batch data\n        np.random.shuffle(arr)\n        x_batch = x_train[arr[0:batch_size],:,:,:]\n        y_batch = y_train[arr[0:batch_size]]\n        net_dict = {x_input_place_holder:x_batch,y_input_place_holder:y_batch}\n        _,acc,loss_value = sess.run([train_step,accuracy_op,loss_function], feed_dict=net_dict)\n            \n        if ii % epoch_size == 0:\n            print(\"Accuracy: %f, Training loss (for one batch): %f at step %d\"% (acc,float(loss_value),ii)) \n            # Save trained model\n            saver.save(sess,os.path.join(tf1_save_dir, 'checkpoint'),global_step=0)","metadata":{"execution":{"iopub.status.busy":"2022-04-02T16:49:17.210991Z","iopub.execute_input":"2022-04-02T16:49:17.211403Z","iopub.status.idle":"2022-04-02T16:51:26.415457Z","shell.execute_reply.started":"2022-04-02T16:49:17.211369Z","shell.execute_reply":"2022-04-02T16:51:26.414692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Restart kernel to enable eager execution","metadata":{}},{"cell_type":"code","source":"import IPython\n\nIPython.Application.instance().kernel.do_shutdown(True) #automatically restarts kernel","metadata":{"execution":{"iopub.status.busy":"2022-04-02T16:51:26.419809Z","iopub.execute_input":"2022-04-02T16:51:26.420028Z","iopub.status.idle":"2022-04-02T16:51:26.429399Z","shell.execute_reply.started":"2022-04-02T16:51:26.420002Z","shell.execute_reply":"2022-04-02T16:51:26.428465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Resave TF1 model as Keras model","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom model_challenge import Model_Plain_Net\n\nclass_num = 10\ntf1_save_dir = \"./model_tf1/\"\n\ntf.compat.v1.reset_default_graph()            \ninput_shape = (32,32,3)\nmodel = Model_Plain_Net(tf.keras.layers.Input(shape = input_shape),class_num)\n# Define scalar variables to be tracked on tensorboard\nvar_list = model.net.weights\nsaver = tf.compat.v1.train.Saver(var_list=var_list)\ncheckpoint = tf.train.latest_checkpoint(tf1_save_dir)\nsaver.restore(sess=tf.compat.v1.Session(), save_path=checkpoint)\ntf.keras.models.save_model(model.net,\"./my_trained_model.hdf5\",save_format=\"h5\")\n\n# Delete TF1 model\nfrom shutil import rmtree\nrmtree(tf1_save_dir)","metadata":{"execution":{"iopub.status.busy":"2022-04-02T16:51:26.43098Z","iopub.execute_input":"2022-04-02T16:51:26.431564Z","iopub.status.idle":"2022-04-02T16:51:30.617116Z","shell.execute_reply.started":"2022-04-02T16:51:26.431527Z","shell.execute_reply":"2022-04-02T16:51:30.616389Z"},"trusted":true},"execution_count":null,"outputs":[]}]}