{"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":"# create my DataFrame dataset\n\nwidth = 64\nheight = 64\nsize = width*height\nNUM = 9\nsave_name = str(\"v21_gamma_64*64\")\n\nread_path = \"../input/numimgreduce722/num-img-reduce\"\nwrite_path = \"/kaggle/working/num_raw_aug\"\naug_factor = 2\npath = write_path + \"/\"\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:11:18.683317Z","iopub.execute_input":"2022-08-01T15:11:18.684100Z","iopub.status.idle":"2022-08-01T15:11:18.711704Z","shell.execute_reply.started":"2022-08-01T15:11:18.683988Z","shell.execute_reply":"2022-08-01T15:11:18.710867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cd /kaggle/working\n!rm -rf num_raw_aug\n!mkdir num_raw_aug\n!mkdir num_raw_aug/0\n!mkdir num_raw_aug/1\n!mkdir num_raw_aug/2\n!mkdir num_raw_aug/3\n!mkdir num_raw_aug/4\n!mkdir num_raw_aug/5\n!mkdir num_raw_aug/6\n!mkdir num_raw_aug/7\n!mkdir num_raw_aug/8\n!ls ./num_raw_aug\n!ls","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:11:18.714141Z","iopub.execute_input":"2022-08-01T15:11:18.714698Z","iopub.status.idle":"2022-08-01T15:11:29.051170Z","shell.execute_reply.started":"2022-08-01T15:11:18.714663Z","shell.execute_reply":"2022-08-01T15:11:29.050121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import imageio\nimport matplotlib as matplotlib\nfrom imgaug import augmenters as iaa\nfrom PIL import Image as im\nimport numpy as np\nimport imgaug as ia\n\nimport cv2\nimport os\n\n\ndef load_images_from_folder(folder):\n    images = []\n    for filename in os.listdir(folder):\n        img = cv2.imread(os.path.join(folder, filename), cv2.IMREAD_UNCHANGED)\n        if img is not None:\n            images.append(img)\n    return images\n\n\nseq = iaa.Sequential([\n    iaa.GaussianBlur(sigma=(0.0, 2.0)),\n    iaa.AdditiveGaussianNoise(scale=(0, 1)),\n    iaa.ScaleX((0.6, 1.4)),\n    iaa.ScaleY((0.8, 1.2)),\n    # iaa.imgcorruptlike.GaussianBlur(severity=(1, 3)),\n    # iaa.imgcorruptlike.Contrast(severity=(1, 3)),\n#     iaa.Affine(shear=(-15, 15)),\n#    iaa.Superpixels(p_replace=0.5, n_segments=30),\n#     iaa.CropAndPad(percent=(-0.25, 0.25)),\n#     iaa.CoarseDropout((0.0, 0.05), size_percent=(0.02, 0.25)),\n#     iaa.Affine(scale={\"x\": (0.7, 1.3), \"y\": (0.7, 1.3)}),\n#     iaa.Affine(shear=(-16, 16)),\n    iaa.PerspectiveTransform(scale=(0.01, 0.10)),\n#     iaa.Affine(rotate=(-18, 18)),\n#     iaa.GaussianBlur(sigma=(0.0, 0.7)),\n#     iaa.Resize((0.8, 1.2)),\n    iaa.AddToBrightness((-5, 2)),\n#     iaa.TranslateY(px=(-8, 8)), #Y平移\n#     iaa.TranslateX(px=(-3, 3))  #X平移\n    # iaa.TranslateX(px=(-5, 5)),\n    # iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6), per_channel=True)\n], random_order=True)\n\n\n\n\n# gray to rgb to gray\nfor num in range(NUM):\n    print(num)\n    images = load_images_from_folder(read_path+\"/\"+str(num)+\"/\")\n    count = 0\n    for c in range(aug_factor):\n        images_aug = seq(images=images)\n        for image in images_aug:\n#             image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n            cv2.imwrite(write_path+\"/\"+ str(num) + \"/\" + str(count) + \".jpg\", image)\n            # image = im.fromarray(image).convert('RGB').convert(\"L\")\n            # image.save(\"C://Users//LIUTAO//PycharmProjects//imgaug//armor_num_dataset_light_on//valid//\"+str(num+1)+\"_aug//\" + str(count) + \".jpg\")\n            if count % 1000 == 0:\n                print(str(count) + \" done\")\n            count += 1\n\n    for img in images:\n        cv2.imwrite(write_path+\"/\"+ str(num) + \"/\" + str(count) + \".jpg\", image)\n        count += 1\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:11:29.054823Z","iopub.execute_input":"2022-08-01T15:11:29.055429Z","iopub.status.idle":"2022-08-01T15:13:52.012524Z","shell.execute_reply.started":"2022-08-01T15:11:29.055389Z","shell.execute_reply":"2022-08-01T15:13:52.011526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! ls /kaggle/working/num_raw_aug/8","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:13:52.013912Z","iopub.execute_input":"2022-08-01T15:13:52.014375Z","iopub.status.idle":"2022-08-01T15:13:52.721719Z","shell.execute_reply.started":"2022-08-01T15:13:52.014335Z","shell.execute_reply":"2022-08-01T15:13:52.720830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nnp.random.seed(12)\n\nimport keras\nfrom keras.models import Model\nfrom keras.layers import *\nfrom keras import optimizers","metadata":{"_cell_guid":"55ec59e1-8a8c-42d2-8ccc-b6d95ea198b4","_uuid":"a6d625ede9e19ac1f146a5f63d6ca19c4b98e20c","_execution_state":"busy","execution":{"iopub.status.busy":"2022-08-01T15:13:52.724488Z","iopub.execute_input":"2022-08-01T15:13:52.724829Z","iopub.status.idle":"2022-08-01T15:13:57.586645Z","shell.execute_reply.started":"2022-08-01T15:13:52.724799Z","shell.execute_reply":"2022-08-01T15:13:57.585701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\ndef load_images_from_folder(folder):\n    images = []\n    for filename in os.listdir(folder):\n        img = cv2.imread(os.path.join(folder, filename), cv2.IMREAD_UNCHANGED)\n        if img is not None:\n            images.append(img)\n    return images","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:13:57.588160Z","iopub.execute_input":"2022-08-01T15:13:57.588885Z","iopub.status.idle":"2022-08-01T15:13:57.594937Z","shell.execute_reply.started":"2022-08-01T15:13:57.588846Z","shell.execute_reply":"2022-08-01T15:13:57.593656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def adjust_gamma(image, gamma=1.0):\n    invGamma =  gamma / 100.0\n    table = np.array([((i / 255.0) ** invGamma) * 255\n        for i in np.arange(0, 256)]).astype(\"uint8\")\n    return cv2.LUT(image, table)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:13:57.596603Z","iopub.execute_input":"2022-08-01T15:13:57.597443Z","iopub.status.idle":"2022-08-01T15:13:57.606382Z","shell.execute_reply.started":"2022-08-01T15:13:57.597403Z","shell.execute_reply":"2022-08-01T15:13:57.605480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot img\nimport matplotlib.pyplot as plt\nnum = 1\nsize = 50\nimages = load_images_from_folder(path+str(num)+\"/\")\nfor i in range(size):\n    img = images[i*100]\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n    img = adjust_gamma(img, 60.0)\n#     img = cv2.equalizeHist(img)\n    plt.imshow(img)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:13:57.607554Z","iopub.execute_input":"2022-08-01T15:13:57.608052Z","iopub.status.idle":"2022-08-01T15:14:07.230699Z","shell.execute_reply.started":"2022-08-01T15:13:57.608011Z","shell.execute_reply":"2022-08-01T15:14:07.229887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"size = width*height\n\nimport cv2\nimport numpy as np\n\n# features \ncolumns=['label']\nfor i in range(size):\n    columns.append('pixel'+str(i))\n# print(columns)\n\n# data\ndatas = []\n\n\nfor num in range(NUM):\n    print(num)\n    images = load_images_from_folder(path+str(num)+\"/\")\n    label = num\n    data = np.empty((len(images), size+1))\n    for img_idx in range(len(images)):\n        img = images[img_idx]\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n#         img = cv2.equalizeHist(img)\n        img = adjust_gamma(img, 60.0)\n        img = cv2.resize(img,(width,height))\n        img = img.reshape((1,size))\n        img = np.insert(img, (0), label)\n        data[img_idx] = img\n    datas.append(data)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:14:07.232122Z","iopub.execute_input":"2022-08-01T15:14:07.232477Z","iopub.status.idle":"2022-08-01T15:15:09.036649Z","shell.execute_reply.started":"2022-08-01T15:14:07.232441Z","shell.execute_reply":"2022-08-01T15:15:09.035823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data3s1 = np.vstack((datas[0],datas[1],datas[2]))\n# data3s2 = np.vstack((datas[3],datas[4],datas[5]))\n# data3s3 = np.vstack((datas[6],datas[7],datas[8]))\n\n# dataframe = np.vstack((data3s1,data3s2))\ndataframe = np.vstack((datas[0],datas[1],datas[2],datas[3],datas[4],datas[5],datas[6],datas[7],datas[8]))\nprint(dataframe.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:15:09.037979Z","iopub.execute_input":"2022-08-01T15:15:09.038382Z","iopub.status.idle":"2022-08-01T15:15:09.650945Z","shell.execute_reply.started":"2022-08-01T15:15:09.038343Z","shell.execute_reply":"2022-08-01T15:15:09.650120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"verify_num = 40000\n# verify\nfrom matplotlib import pyplot as plt\n%matplotlib inline\n#The line above is necesary to show Matplotlib's plots inside a Jupyter Notebook\n\nimg = dataframe[verify_num, 1:(size+1)]\nlabel = dataframe[verify_num, 0]\nimg = img.reshape((height,width))\nprint(label)\nplt.imshow(img)\nprint(dataframe.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:15:09.654347Z","iopub.execute_input":"2022-08-01T15:15:09.654627Z","iopub.status.idle":"2022-08-01T15:15:09.832743Z","shell.execute_reply.started":"2022-08-01T15:15:09.654602Z","shell.execute_reply":"2022-08-01T15:15:09.831848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\ndf_data = pd.DataFrame(dataframe, columns=columns)\ndf_data = df_data.sample(frac=1)\ndf_data.head()\nprint(df_data.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:15:09.834168Z","iopub.execute_input":"2022-08-01T15:15:09.834593Z","iopub.status.idle":"2022-08-01T15:15:10.343459Z","shell.execute_reply.started":"2022-08-01T15:15:09.834553Z","shell.execute_reply":"2022-08-01T15:15:10.342458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#split test\nfrom sklearn.model_selection import train_test_split\n\ndf_train, df_test = train_test_split(df_data, test_size=0.25)\nprint(df_train.shape, df_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:15:10.344807Z","iopub.execute_input":"2022-08-01T15:15:10.345341Z","iopub.status.idle":"2022-08-01T15:15:10.940966Z","shell.execute_reply.started":"2022-08-01T15:15:10.345303Z","shell.execute_reply":"2022-08-01T15:15:10.939372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Splitting into training and validation dataset","metadata":{"_cell_guid":"45bd232a-4000-4a06-a3ed-0be9f993746c","_uuid":"f0970dcbc0b6791dc44936d8d63e6985dfb6960c","_execution_state":"idle"}},{"cell_type":"code","source":"#split val\ndf_features = df_train.iloc[:, 1:(size+1)]\ndf_label = df_train.iloc[:, 0]\n\nX_test = df_test.iloc[:, 1:(size+1)]\ny_test = df_test.iloc[:, 0].values\n\nprint(y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:15:10.942382Z","iopub.execute_input":"2022-08-01T15:15:10.942795Z","iopub.status.idle":"2022-08-01T15:15:10.950735Z","shell.execute_reply.started":"2022-08-01T15:15:10.942755Z","shell.execute_reply":"2022-08-01T15:15:10.949971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_cv, y_train, y_cv = train_test_split(df_features, df_label, \n                                                test_size = 0.2,\n                                                random_state = 1212)\n\nprint(len(X_train))\n\nX_train = X_train.values.reshape(len(X_train), size) #(3144, 784)\nX_cv = X_cv.values.reshape(len(X_cv), size) #(786, 784)\n\nX_test = X_test.values.reshape(len(X_test), size)","metadata":{"_cell_guid":"bb78680a-ce50-4ee9-a77f-265b9ee38aee","_uuid":"16e39b5512ed29c8f9d7db51331693e4689ce306","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:10.951978Z","iopub.execute_input":"2022-08-01T15:15:10.952773Z","iopub.status.idle":"2022-08-01T15:15:12.110630Z","shell.execute_reply.started":"2022-08-01T15:15:10.952730Z","shell.execute_reply":"2022-08-01T15:15:12.109651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:15:12.112212Z","iopub.execute_input":"2022-08-01T15:15:12.112631Z","iopub.status.idle":"2022-08-01T15:15:12.117486Z","shell.execute_reply.started":"2022-08-01T15:15:12.112589Z","shell.execute_reply":"2022-08-01T15:15:12.116598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print((min(X_train[1]), max(X_train[1])))","metadata":{"_cell_guid":"4d0e07b5-28dd-42f1-b425-6414aea90cde","_uuid":"cc2daf9f099fb62e3b470f913349428ef396b75b","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:12.118670Z","iopub.execute_input":"2022-08-01T15:15:12.119587Z","iopub.status.idle":"2022-08-01T15:15:12.130449Z","shell.execute_reply.started":"2022-08-01T15:15:12.119430Z","shell.execute_reply":"2022-08-01T15:15:12.129711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(type(y_cv),y_cv.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:15:12.131737Z","iopub.execute_input":"2022-08-01T15:15:12.132130Z","iopub.status.idle":"2022-08-01T15:15:12.140187Z","shell.execute_reply.started":"2022-08-01T15:15:12.132095Z","shell.execute_reply":"2022-08-01T15:15:12.139239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import np_utils\n\n# Feature Normalization \nX_train = X_train.astype('float32'); X_cv= X_cv.astype('float32'); X_test = X_test.astype('float32')\nX_train /= 255; X_cv /= 255; X_test /= 255\n\nnum_digits = 10\ny_train = keras.utils.np_utils.to_categorical(y_train, num_digits)\ny_cv = keras.utils.np_utils.to_categorical(y_cv, num_digits)","metadata":{"_cell_guid":"e9f8eaa0-c77e-4510-9ff1-bb56e1d5f76d","_uuid":"c24261ffd2384078a836a909ea4839a31d51dd66","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:12.141683Z","iopub.execute_input":"2022-08-01T15:15:12.142049Z","iopub.status.idle":"2022-08-01T15:15:12.568355Z","shell.execute_reply.started":"2022-08-01T15:15:12.142014Z","shell.execute_reply":"2022-08-01T15:15:12.567498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_train[0]) # 2\nprint(y_train[3]) # 7","metadata":{"_cell_guid":"6667ca35-44ff-43f3-b88b-27b787be4ee0","_uuid":"3fdf37a538504f4e5288cbb8b70a719c2689caca","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:12.569708Z","iopub.execute_input":"2022-08-01T15:15:12.570091Z","iopub.status.idle":"2022-08-01T15:15:12.576023Z","shell.execute_reply.started":"2022-08-01T15:15:12.570039Z","shell.execute_reply":"2022-08-01T15:15:12.575092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Input Parameters\nn_input = size # number of features\nn_hidden_1 = 300\nn_hidden_2 = 100\nn_hidden_3 = 100\nn_hidden_4 = 200\nnum_digits = 10","metadata":{"_cell_guid":"be9af3d4-ddac-4f85-945f-bd1cdf09fc15","_uuid":"b88da407b6dab83e54b5380e0e62459577949932","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:12.577848Z","iopub.execute_input":"2022-08-01T15:15:12.578293Z","iopub.status.idle":"2022-08-01T15:15:12.586138Z","shell.execute_reply.started":"2022-08-01T15:15:12.578257Z","shell.execute_reply":"2022-08-01T15:15:12.585318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Inp = Input(shape=(size,))\nx = Dense(n_hidden_1, activation='relu', name = \"Hidden_Layer_1\")(Inp)\nx = Dense(n_hidden_2, activation='relu', name = \"Hidden_Layer_2\")(x)\nx = Dense(n_hidden_3, activation='relu', name = \"Hidden_Layer_3\")(x)\nx = Dense(n_hidden_4, activation='relu', name = \"Hidden_Layer_4\")(x)\noutput = Dense(num_digits, activation='softmax', name = \"Output_Layer\")(x)","metadata":{"_cell_guid":"74eac473-a6cb-4356-a982-ef3cb6c971de","_uuid":"f97a558010d88407363bbb08fa00e7c87a3d6ceb","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:12.587533Z","iopub.execute_input":"2022-08-01T15:15:12.588614Z","iopub.status.idle":"2022-08-01T15:15:15.539285Z","shell.execute_reply.started":"2022-08-01T15:15:12.588565Z","shell.execute_reply":"2022-08-01T15:15:15.538475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Model(Inp, output)\nmodel.summary() ","metadata":{"_cell_guid":"bf09781e-6376-4f2d-a872-6292d466f657","_uuid":"aaf8fe1cd6cb32675f0ed5009d7bc31d6f71e4ea","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:15.540590Z","iopub.execute_input":"2022-08-01T15:15:15.540954Z","iopub.status.idle":"2022-08-01T15:15:15.552863Z","shell.execute_reply.started":"2022-08-01T15:15:15.540906Z","shell.execute_reply":"2022-08-01T15:15:15.552110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learning_rate = 0.1\ntraining_epochs = 20\nbatch_size = 100\nsgd = tf.keras.optimizers.SGD(learning_rate=learning_rate)","metadata":{"_cell_guid":"906807f1-ed62-4dee-a423-5f15842fb28f","_uuid":"c4ea7858fbdf41308dcdb5d373af11484d5b5140","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:15.554218Z","iopub.execute_input":"2022-08-01T15:15:15.554859Z","iopub.status.idle":"2022-08-01T15:15:15.908128Z","shell.execute_reply.started":"2022-08-01T15:15:15.554818Z","shell.execute_reply":"2022-08-01T15:15:15.907352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',\n              optimizer='sgd',\n              metrics=['accuracy'])","metadata":{"_cell_guid":"5678ce6d-58eb-4394-9a77-b67c94c2ca4f","_uuid":"775193de659c41c949495a2afa3b14024a2536a7","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:15.909321Z","iopub.execute_input":"2022-08-01T15:15:15.909694Z","iopub.status.idle":"2022-08-01T15:15:15.927805Z","shell.execute_reply.started":"2022-08-01T15:15:15.909658Z","shell.execute_reply":"2022-08-01T15:15:15.927065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# history1 = model.fit(X_train, y_train,\n#                      batch_size = batch_size,\n#                      epochs = training_epochs,\n#                      verbose = 2,\n#                      validation_data=(X_cv, y_cv))","metadata":{"_cell_guid":"1a547d04-6aeb-4c76-8fbc-7fbca1b50b2a","_uuid":"4960dfa65394fb0314ce9cb7af63d48709a39d1a","_execution_state":"busy","execution":{"iopub.status.busy":"2022-08-01T15:15:15.930751Z","iopub.execute_input":"2022-08-01T15:15:15.931106Z","iopub.status.idle":"2022-08-01T15:15:15.935467Z","shell.execute_reply.started":"2022-08-01T15:15:15.931070Z","shell.execute_reply":"2022-08-01T15:15:15.934218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Inp = Input(shape=(size,))\nx = Dense(n_hidden_1, activation='relu', name = \"Hidden_Layer_1\")(Inp)\nx = Dense(n_hidden_2, activation='relu', name = \"Hidden_Layer_2\")(x)\nx = Dense(n_hidden_3, activation='relu', name = \"Hidden_Layer_3\")(x)\nx = Dense(n_hidden_4, activation='relu', name = \"Hidden_Layer_4\")(x)\noutput = Dense(num_digits, activation='softmax', name = \"Output_Layer\")(x)\n\nadam = tf.optimizers.Adam(learning_rate=learning_rate)\nmodel2 = Model(Inp, output)\n\nmodel2.compile(loss='categorical_crossentropy',\n              optimizer='adam',\n              metrics=['accuracy'])","metadata":{"_cell_guid":"4e7a2ba9-ac08-4355-a37f-2336220cfda7","_uuid":"3d9c3ffb9722b640ea0682e5826971045091db66","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:15.936572Z","iopub.execute_input":"2022-08-01T15:15:15.937209Z","iopub.status.idle":"2022-08-01T15:15:15.981735Z","shell.execute_reply.started":"2022-08-01T15:15:15.937175Z","shell.execute_reply":"2022-08-01T15:15:15.981087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# history2 = model2.fit(X_train, y_train,\n#                       batch_size = batch_size,\n#                       epochs = training_epochs,\n#                       verbose = 2,\n#                       validation_data=(X_cv, y_cv))","metadata":{"_cell_guid":"e4470ac7-6888-4b10-a4e5-8bd4e22301cf","_uuid":"7f94a2f9d7db89bf3fb28ecff64dbf7a496b0b9c","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:15.983126Z","iopub.execute_input":"2022-08-01T15:15:15.984000Z","iopub.status.idle":"2022-08-01T15:15:15.987632Z","shell.execute_reply.started":"2022-08-01T15:15:15.983964Z","shell.execute_reply":"2022-08-01T15:15:15.986941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Inp = Input(shape=(size,))\nx = Dense(n_hidden_1, activation='relu', name = \"Hidden_Layer_1\")(Inp)\nx = Dense(n_hidden_2, activation='relu', name = \"Hidden_Layer_2\")(x)\nx = Dense(n_hidden_3, activation='relu', name = \"Hidden_Layer_3\")(x)\nx = Dense(n_hidden_4, activation='relu', name = \"Hidden_Layer_4\")(x)\noutput = Dense(num_digits, activation='softmax', name = \"Output_Layer\")(x)\n\nlearning_rate = 0.01\nadam = tf.optimizers.Adam(learning_rate=learning_rate)\nmodel2a = Model(Inp, output)\n\nmodel2a.compile(loss='categorical_crossentropy',\n              optimizer='adam',\n              metrics=['accuracy'])","metadata":{"_cell_guid":"da528246-4223-42ff-af96-cd3419c34507","_uuid":"d2e88469ac4ed8ac19a195d71941178d8cdb7c32","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:15.993344Z","iopub.execute_input":"2022-08-01T15:15:15.994253Z","iopub.status.idle":"2022-08-01T15:15:16.038687Z","shell.execute_reply.started":"2022-08-01T15:15:15.994215Z","shell.execute_reply":"2022-08-01T15:15:16.038047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# history2a = model2a.fit(X_train, y_train,\n#                         batch_size = batch_size,\n#                         epochs = training_epochs,\n#                         verbose = 2,\n#                         validation_data=(X_cv, y_cv))","metadata":{"_cell_guid":"847391bb-4640-4117-a35e-5593d823bbc4","_uuid":"8e397c8d027a4f27b1f72ac9333de51cbc335cd6","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:16.039833Z","iopub.execute_input":"2022-08-01T15:15:16.040179Z","iopub.status.idle":"2022-08-01T15:15:16.043960Z","shell.execute_reply.started":"2022-08-01T15:15:16.040146Z","shell.execute_reply":"2022-08-01T15:15:16.042868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Inp = Input(shape=(size,))\nx = Dense(n_hidden_1, activation='relu', name = \"Hidden_Layer_1\")(Inp)\nx = Dense(n_hidden_2, activation='relu', name = \"Hidden_Layer_2\")(x)\nx = Dense(n_hidden_3, activation='relu', name = \"Hidden_Layer_3\")(x)\nx = Dense(n_hidden_4, activation='relu', name = \"Hidden_Layer_4\")(x)\noutput = Dense(num_digits, activation='softmax', name = \"Output_Layer\")(x)\n\nlearning_rate = 0.5\nadam = tf.optimizers.Adam(learning_rate=learning_rate)\nmodel2b = Model(Inp, output)\n\nmodel2b.compile(loss='categorical_crossentropy',\n              optimizer='adam',\n              metrics=['accuracy'])","metadata":{"_cell_guid":"18ef2f7d-f9a6-4609-8fd4-7d98cb7bc82f","_uuid":"fbc0a1e06296ada3553c9328e48861645680bbf9","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:16.045137Z","iopub.execute_input":"2022-08-01T15:15:16.045979Z","iopub.status.idle":"2022-08-01T15:15:16.090277Z","shell.execute_reply.started":"2022-08-01T15:15:16.045941Z","shell.execute_reply":"2022-08-01T15:15:16.089605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# history2b = model2b.fit(X_train, y_train,\n#                         batch_size = batch_size,\n#                         epochs = training_epochs,\n#                             validation_data=(X_cv, y_cv))","metadata":{"_cell_guid":"ad3ac83b-a040-42ba-9d62-2203e62a82ec","_uuid":"c2e1a75c4a95acdfd3d5ce81af0c20bce995ca24","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:16.091305Z","iopub.execute_input":"2022-08-01T15:15:16.091964Z","iopub.status.idle":"2022-08-01T15:15:16.095910Z","shell.execute_reply.started":"2022-08-01T15:15:16.091928Z","shell.execute_reply":"2022-08-01T15:15:16.095113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Input Parameters\nn_input = size # number of features\nn_hidden_1 = 300\nn_hidden_2 = 100\nn_hidden_3 = 100\nn_hidden_4 = 100\nn_hidden_5 = 200\nnum_digits = 10","metadata":{"_cell_guid":"25d57f65-533c-432b-bd4a-a3127101d4bb","_uuid":"c19270be516858cca76b3479417e4017877896b1","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:16.097319Z","iopub.execute_input":"2022-08-01T15:15:16.098365Z","iopub.status.idle":"2022-08-01T15:15:16.104997Z","shell.execute_reply.started":"2022-08-01T15:15:16.098329Z","shell.execute_reply":"2022-08-01T15:15:16.104337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Inp = Input(shape=(size,))\nx = Dense(n_hidden_1, activation='relu', name = \"Hidden_Layer_1\")(Inp)\nx = Dense(n_hidden_2, activation='relu', name = \"Hidden_Layer_2\")(x)\nx = Dense(n_hidden_3, activation='relu', name = \"Hidden_Layer_3\")(x)\nx = Dense(n_hidden_4, activation='relu', name = \"Hidden_Layer_4\")(x)\nx = Dense(n_hidden_5, activation='relu', name = \"Hidden_Layer_5\")(x)\noutput = Dense(num_digits, activation='softmax', name = \"Output_Layer\")(x)","metadata":{"_cell_guid":"d179df94-04b5-4d1e-ab7f-882a72442837","_uuid":"c919e5d20e55f0c86152efbb729feeb0292abf0a","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:16.106625Z","iopub.execute_input":"2022-08-01T15:15:16.106907Z","iopub.status.idle":"2022-08-01T15:15:16.150146Z","shell.execute_reply.started":"2022-08-01T15:15:16.106875Z","shell.execute_reply":"2022-08-01T15:15:16.149490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model3 = Model(Inp, output)\nmodel3.summary() ","metadata":{"_cell_guid":"04f2f013-41ee-48a7-9204-cfe8b748e09c","_uuid":"db189665726670192df8ed9e9758e908821f629b","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:16.151181Z","iopub.execute_input":"2022-08-01T15:15:16.151880Z","iopub.status.idle":"2022-08-01T15:15:16.160735Z","shell.execute_reply.started":"2022-08-01T15:15:16.151843Z","shell.execute_reply":"2022-08-01T15:15:16.159827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"adam = tf.optimizers.Adam(learning_rate=0.01)\n\nmodel3.compile(loss='categorical_crossentropy',\n              optimizer='adam',\n              metrics=['accuracy'])","metadata":{"_cell_guid":"3da4e128-5987-4fb3-a2c7-93420c844d1d","_uuid":"304ab7710bc6f2e56d698cc232fadbba6886b664","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:16.162118Z","iopub.execute_input":"2022-08-01T15:15:16.162817Z","iopub.status.idle":"2022-08-01T15:15:16.172161Z","shell.execute_reply.started":"2022-08-01T15:15:16.162779Z","shell.execute_reply":"2022-08-01T15:15:16.171455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# history3 = model3.fit(X_train, y_train,\n#                       batch_size = batch_size,\n#                       epochs = training_epochs,\n#                       validation_data=(X_cv, y_cv))","metadata":{"_cell_guid":"f7ac3e43-bf76-488d-8ec6-31ec98228ee6","_uuid":"986fc367bbd1fc9767c30a03324c4d18e3bfd0d5","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:16.173065Z","iopub.execute_input":"2022-08-01T15:15:16.175495Z","iopub.status.idle":"2022-08-01T15:15:16.179270Z","shell.execute_reply.started":"2022-08-01T15:15:16.175467Z","shell.execute_reply":"2022-08-01T15:15:16.178415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Input Parameters\nn_input = size # number of features\nn_hidden_1 = 300\nn_hidden_2 = 100\nn_hidden_3 = 100\nn_hidden_4 = 200\nnum_digits = 10","metadata":{"_cell_guid":"9054eb9b-e655-415c-93e7-3f0ca45d84ac","_uuid":"608b71d5bd8dc23df6d916e7371887fd52dd24d8","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:16.180817Z","iopub.execute_input":"2022-08-01T15:15:16.181240Z","iopub.status.idle":"2022-08-01T15:15:16.192806Z","shell.execute_reply.started":"2022-08-01T15:15:16.181207Z","shell.execute_reply":"2022-08-01T15:15:16.191767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Inp = Input(shape=(size,))\nx = Dense(n_hidden_1, activation='relu', name = \"Hidden_Layer_1\")(Inp)\nx = Dropout(0.3)(x)\nx = Dense(n_hidden_2, activation='relu', name = \"Hidden_Layer_2\")(x)\nx = Dropout(0.3)(x)\nx = Dense(n_hidden_3, activation='relu', name = \"Hidden_Layer_3\")(x)\nx = Dropout(0.3)(x)\nx = Dense(n_hidden_4, activation='relu', name = \"Hidden_Layer_4\")(x)\noutput = Dense(num_digits, activation='softmax', name = \"Output_Layer\")(x)","metadata":{"_cell_guid":"0541e860-b2f6-43cd-8d00-254e9daf775a","_uuid":"d6335bd113992a8898679d1a5d09ed2eda3fe5af","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:16.194236Z","iopub.execute_input":"2022-08-01T15:15:16.194533Z","iopub.status.idle":"2022-08-01T15:15:16.252639Z","shell.execute_reply.started":"2022-08-01T15:15:16.194504Z","shell.execute_reply":"2022-08-01T15:15:16.251962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model4 = Model(Inp, output)\nmodel4.summary() # We have 297,910 parameters to estimate","metadata":{"_cell_guid":"95f4299a-0e1c-4dca-b7d5-839e7a5e8fc8","_uuid":"08e56562d89bcd25756bc5bbbb4f33ceb87f106e","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:16.253982Z","iopub.execute_input":"2022-08-01T15:15:16.254309Z","iopub.status.idle":"2022-08-01T15:15:16.264411Z","shell.execute_reply.started":"2022-08-01T15:15:16.254277Z","shell.execute_reply":"2022-08-01T15:15:16.263411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model4.compile(loss='categorical_crossentropy',\n              optimizer='adam',\n              metrics=['accuracy'])","metadata":{"_cell_guid":"ed1c4a9d-730e-4e4d-8a25-4448d877d5b6","_uuid":"2328fa07b0f4c63448b0c30b7ae450df2b0fe3be","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:16.266011Z","iopub.execute_input":"2022-08-01T15:15:16.266514Z","iopub.status.idle":"2022-08-01T15:15:16.275894Z","shell.execute_reply.started":"2022-08-01T15:15:16.266478Z","shell.execute_reply":"2022-08-01T15:15:16.275246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model4.fit(X_train, y_train,\n                    batch_size = batch_size,\n                    epochs = training_epochs,\n                    validation_data=(X_cv, y_cv))","metadata":{"_cell_guid":"8543232e-da2b-46ce-9a5e-06e6283b854a","_uuid":"986af85bbaf1b212b00cf28dc9204339b717ee48","_execution_state":"busy","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-01T15:15:16.277036Z","iopub.execute_input":"2022-08-01T15:15:16.277373Z","iopub.status.idle":"2022-08-01T15:15:59.675984Z","shell.execute_reply.started":"2022-08-01T15:15:16.277340Z","shell.execute_reply":"2022-08-01T15:15:59.675114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_test.shape, y_test.shape)\ny_test_one_hot = tf.one_hot(y_test, depth=10)\n\n# Evaluate the model\nloss, acc = model4.evaluate(X_test, y_test_one_hot, verbose=2)\nprint(\"accuracy: {:5.2f}%\".format(100 * acc))","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:15:59.677703Z","iopub.execute_input":"2022-08-01T15:15:59.678103Z","iopub.status.idle":"2022-08-01T15:16:01.374990Z","shell.execute_reply.started":"2022-08-01T15:15:59.678049Z","shell.execute_reply":"2022-08-01T15:16:01.374087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 2\nprint(model4.predict(X_test[i].reshape((1,size))))\nimg = (X_test[i]*255).astype(int)\nimg = img.reshape((height,width))\nprint(img)\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:16:01.379170Z","iopub.execute_input":"2022-08-01T15:16:01.381546Z","iopub.status.idle":"2022-08-01T15:16:01.735522Z","shell.execute_reply.started":"2022-08-01T15:16:01.381507Z","shell.execute_reply":"2022-08-01T15:16:01.734332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:16:01.737179Z","iopub.execute_input":"2022-08-01T15:16:01.737565Z","iopub.status.idle":"2022-08-01T15:16:02.478741Z","shell.execute_reply.started":"2022-08-01T15:16:01.737526Z","shell.execute_reply":"2022-08-01T15:16:02.477699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -U tf2onnx","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:16:02.480346Z","iopub.execute_input":"2022-08-01T15:16:02.480649Z","iopub.status.idle":"2022-08-01T15:16:18.249165Z","shell.execute_reply.started":"2022-08-01T15:16:02.480620Z","shell.execute_reply":"2022-08-01T15:16:18.247720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #convert tf model to onnx\n# import tf2onnx\n# (onnx_model_proto, storage) = tf2onnx.convert.from_keras(model3)\n# with open(os.path.join(\"./\", 'model3.onnx'), \"wb\") as f:\n#     f.write(onnx_model_proto.SerializeToString())","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:16:18.250728Z","iopub.execute_input":"2022-08-01T15:16:18.251137Z","iopub.status.idle":"2022-08-01T15:16:18.255733Z","shell.execute_reply.started":"2022-08-01T15:16:18.251096Z","shell.execute_reply":"2022-08-01T15:16:18.255011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tf2onnx\nimport onnx\nonnx_model, _ = tf2onnx.convert.from_keras(model4)\nonnx.save(onnx_model, \"./model4_64*64_7_03_v7.onnx\")","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:16:18.257289Z","iopub.execute_input":"2022-08-01T15:16:18.257965Z","iopub.status.idle":"2022-08-01T15:16:19.782434Z","shell.execute_reply.started":"2022-08-01T15:16:18.257927Z","shell.execute_reply":"2022-08-01T15:16:19.781542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(type(model4))","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:16:19.784829Z","iopub.execute_input":"2022-08-01T15:16:19.785368Z","iopub.status.idle":"2022-08-01T15:16:19.814162Z","shell.execute_reply.started":"2022-08-01T15:16:19.785331Z","shell.execute_reply":"2022-08-01T15:16:19.812047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport seaborn as sns\n%matplotlib inline\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nimport itertools\n\nfrom keras.utils.np_utils import to_categorical # convert to one-hot-encoding\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom tensorflow.keras.optimizers import RMSprop\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau\n\n\nsns.set(style='white', context='notebook', palette='deep')\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:16:19.815840Z","iopub.execute_input":"2022-08-01T15:16:19.816992Z","iopub.status.idle":"2022-08-01T15:16:20.218436Z","shell.execute_reply.started":"2022-08-01T15:16:19.816952Z","shell.execute_reply":"2022-08-01T15:16:20.215040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)\nX_train = X_train.reshape(-1,width,height,1)\nX_cv = X_cv.reshape(-1,width,height,1)\nX_test = X_test.reshape(-1,width,height,1)\nprint(X_train.shape)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:16:20.219561Z","iopub.execute_input":"2022-08-01T15:16:20.219884Z","iopub.status.idle":"2022-08-01T15:16:20.236636Z","shell.execute_reply.started":"2022-08-01T15:16:20.219852Z","shell.execute_reply":"2022-08-01T15:16:20.233930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel = Sequential()\n\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu', input_shape = (width,height,1)))\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\nmodel.add(Dropout(0.25))\n\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation = \"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:16:20.237463Z","iopub.execute_input":"2022-08-01T15:16:20.237749Z","iopub.status.idle":"2022-08-01T15:16:20.526100Z","shell.execute_reply.started":"2022-08-01T15:16:20.237719Z","shell.execute_reply":"2022-08-01T15:16:20.525121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:16:20.531371Z","iopub.execute_input":"2022-08-01T15:16:20.533697Z","iopub.status.idle":"2022-08-01T15:16:20.548851Z","shell.execute_reply.started":"2022-08-01T15:16:20.533657Z","shell.execute_reply":"2022-08-01T15:16:20.548092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the optimizer\noptimizer = RMSprop(learning_rate=0.001, rho=0.9, epsilon=1e-08, decay=0.0)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:16:20.553700Z","iopub.execute_input":"2022-08-01T15:16:20.557251Z","iopub.status.idle":"2022-08-01T15:16:20.567304Z","shell.execute_reply.started":"2022-08-01T15:16:20.557213Z","shell.execute_reply":"2022-08-01T15:16:20.566357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nmodel.compile(optimizer = optimizer , loss = \"categorical_crossentropy\", metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:16:20.573597Z","iopub.execute_input":"2022-08-01T15:16:20.574978Z","iopub.status.idle":"2022-08-01T15:16:20.603251Z","shell.execute_reply.started":"2022-08-01T15:16:20.574941Z","shell.execute_reply":"2022-08-01T15:16:20.602401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set a learning rate annealer\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=3, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:16:20.608384Z","iopub.execute_input":"2022-08-01T15:16:20.611236Z","iopub.status.idle":"2022-08-01T15:16:20.621252Z","shell.execute_reply.started":"2022-08-01T15:16:20.611199Z","shell.execute_reply":"2022-08-01T15:16:20.620281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 30 \nbatch_size = 100","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:16:20.627003Z","iopub.execute_input":"2022-08-01T15:16:20.630669Z","iopub.status.idle":"2022-08-01T15:16:20.638298Z","shell.execute_reply.started":"2022-08-01T15:16:20.630605Z","shell.execute_reply":"2022-08-01T15:16:20.637438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(type(model))","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:16:20.644370Z","iopub.execute_input":"2022-08-01T15:16:20.645361Z","iopub.status.idle":"2022-08-01T15:16:20.662574Z","shell.execute_reply.started":"2022-08-01T15:16:20.645325Z","shell.execute_reply":"2022-08-01T15:16:20.660761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(model.input)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:16:20.663661Z","iopub.execute_input":"2022-08-01T15:16:20.664045Z","iopub.status.idle":"2022-08-01T15:16:20.698788Z","shell.execute_reply.started":"2022-08-01T15:16:20.664010Z","shell.execute_reply":"2022-08-01T15:16:20.695470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit the model\nhistory = model.fit(X_train,y_train, batch_size=batch_size,\n                              epochs = epochs, validation_data = (X_cv,y_cv),\n                              verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size\n                              , callbacks=[learning_rate_reduction])","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:16:20.700444Z","iopub.execute_input":"2022-08-01T15:16:20.705445Z","iopub.status.idle":"2022-08-01T15:19:56.137238Z","shell.execute_reply.started":"2022-08-01T15:16:20.705398Z","shell.execute_reply":"2022-08-01T15:19:56.136454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_test.shape, y_test.shape)\ny_test_one_hot = tf.one_hot(y_test, depth=10)\n\n# Evaluate the model\nloss, acc = model.evaluate(X_test, y_test_one_hot, verbose=2)\nprint(\"accuracy: {:5.2f}%\".format(100 * acc))","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:19:56.138824Z","iopub.execute_input":"2022-08-01T15:19:56.139293Z","iopub.status.idle":"2022-08-01T15:19:58.117127Z","shell.execute_reply.started":"2022-08-01T15:19:56.139254Z","shell.execute_reply":"2022-08-01T15:19:58.115945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tf2onnx\nimport onnx\nonnx_model, _ = tf2onnx.convert.from_keras(model)\nonnx.save(onnx_model, save_name+\"1.onnx\")","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:19:58.118581Z","iopub.execute_input":"2022-08-01T15:19:58.118987Z","iopub.status.idle":"2022-08-01T15:20:01.698600Z","shell.execute_reply.started":"2022-08-01T15:19:58.118950Z","shell.execute_reply":"2022-08-01T15:20:01.697657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#add residual connect","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:20:01.700601Z","iopub.execute_input":"2022-08-01T15:20:01.700981Z","iopub.status.idle":"2022-08-01T15:20:01.706645Z","shell.execute_reply.started":"2022-08-01T15:20:01.700933Z","shell.execute_reply":"2022-08-01T15:20:01.705441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import Tensor\nfrom tensorflow.keras.layers import Input, Conv2D, ReLU, BatchNormalization,\\\n                                    Add, AveragePooling2D, Flatten, Dense\nfrom tensorflow.keras.models import Model\n\ndef relu_bn(inputs: Tensor) -> Tensor:\n    relu = ReLU()(inputs)\n    bn = BatchNormalization()(relu)\n    return bn\n\ndef residual_block(x: Tensor, downsample: bool, filters: int, kernel_size: int = 3) -> Tensor:\n    y = Conv2D(kernel_size=kernel_size,\n               strides= (1 if not downsample else 2),\n               filters=filters,\n               padding=\"same\")(x)\n    y = relu_bn(y)\n    y = Conv2D(kernel_size=kernel_size,\n               strides=1,\n               filters=filters,\n               padding=\"same\")(y)\n\n    if downsample:\n        x = Conv2D(kernel_size=1,\n                   strides=2,\n                   filters=filters,\n                   padding=\"same\")(x)\n    out = Add()([x, y])\n    out = relu_bn(out)\n    return out\n\ndef create_res_net():\n    \n    inputs = Input(shape=(64, 64, 1))\n    num_filters = 64\n    \n    t = BatchNormalization()(inputs)\n    t = Conv2D(kernel_size=3,\n               strides=1,\n               filters=num_filters,\n               padding=\"same\")(t)\n    t = relu_bn(t)\n    \n    num_blocks_list = [2, 5, 5, 2]\n    for i in range(len(num_blocks_list)):\n        num_blocks = num_blocks_list[i]\n        for j in range(num_blocks):\n            t = residual_block(t, downsample=(j==0 and i!=0), filters=num_filters)\n        num_filters *= 2\n    \n    t = AveragePooling2D(4)(t)\n    t = Flatten()(t)\n    outputs = Dense(10, activation='softmax')(t)\n    \n    model = Model(inputs, outputs)\n\n    model.compile(\n        optimizer='adam',\n        loss='categorical_crossentropy',\n        metrics=['accuracy']\n    )\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:44:02.992641Z","iopub.execute_input":"2022-08-01T15:44:02.993346Z","iopub.status.idle":"2022-08-01T15:44:03.007777Z","shell.execute_reply.started":"2022-08-01T15:44:02.993309Z","shell.execute_reply":"2022-08-01T15:44:03.006974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1 = create_res_net() # or create_plain_net()\nmodel1.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:44:06.115163Z","iopub.execute_input":"2022-08-01T15:44:06.115718Z","iopub.status.idle":"2022-08-01T15:44:06.766348Z","shell.execute_reply.started":"2022-08-01T15:44:06.115671Z","shell.execute_reply":"2022-08-01T15:44:06.765511Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit the model\nmodel1.fit(\n    x=X_train,\n    y=y_train,\n    epochs=20,\n    verbose=1,\n    validation_data=(X_cv, y_cv),\n    batch_size=128,\n)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:44:12.582554Z","iopub.execute_input":"2022-08-01T15:44:12.583134Z","iopub.status.idle":"2022-08-01T16:10:39.063418Z","shell.execute_reply.started":"2022-08-01T15:44:12.583084Z","shell.execute_reply":"2022-08-01T16:10:39.061548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the model\nloss, acc = model1.evaluate(X_test, y_test_one_hot, verbose=2)\nprint(\"accuracy: {:5.2f}%\".format(100 * acc))","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:11:09.581864Z","iopub.execute_input":"2022-08-01T16:11:09.582327Z","iopub.status.idle":"2022-08-01T16:11:30.465452Z","shell.execute_reply.started":"2022-08-01T16:11:09.582286Z","shell.execute_reply":"2022-08-01T16:11:30.464369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install openvino","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:20:01.718251Z","iopub.execute_input":"2022-08-01T15:20:01.718723Z","iopub.status.idle":"2022-08-01T15:20:21.946864Z","shell.execute_reply.started":"2022-08-01T15:20:01.718680Z","shell.execute_reply":"2022-08-01T15:20:21.945886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#save model to openvino format\nimport time\nfrom pathlib import Path\n\nimport cv2\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom IPython.display import Markdown\nfrom openvino.runtime import Core","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:20:21.948732Z","iopub.execute_input":"2022-08-01T15:20:21.949136Z","iopub.status.idle":"2022-08-01T15:20:22.458516Z","shell.execute_reply.started":"2022-08-01T15:20:21.949095Z","shell.execute_reply":"2022-08-01T15:20:22.457716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"./model\")","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:20:22.459692Z","iopub.execute_input":"2022-08-01T15:20:22.460291Z","iopub.status.idle":"2022-08-01T15:20:24.413035Z","shell.execute_reply.started":"2022-08-01T15:20:22.460246Z","shell.execute_reply":"2022-08-01T15:20:24.412237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_path = Path(\"model/save_model.pb\")\nir_path = Path(model_path).with_suffix(\".xml\")","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:20:24.416242Z","iopub.execute_input":"2022-08-01T15:20:24.416519Z","iopub.status.idle":"2022-08-01T15:20:24.420761Z","shell.execute_reply.started":"2022-08-01T15:20:24.416493Z","shell.execute_reply":"2022-08-01T15:20:24.419961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Construct the command for Model Optimizer\nmo_command = f\"\"\"mo\n                 --input_model \"{model_path}\"\n                 --input_shape \"[1,64,64,1]\"\n                 --mean_values=\"[127.5,127.5,127.5]\"\n                 --scale_values=\"[127.5]\"\n                 --data_type FP16\n                 --output_dir \"{model_path.parent}\"\n                 \"\"\"\nmo_command = \" \".join(mo_command.split())\nprint(\"Model Optimizer command to convert TensorFlow to OpenVINO:\")\ndisplay(Markdown(f\"`{mo_command}`\"))","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:20:24.421827Z","iopub.execute_input":"2022-08-01T15:20:24.422363Z","iopub.status.idle":"2022-08-01T15:20:24.435846Z","shell.execute_reply.started":"2022-08-01T15:20:24.422325Z","shell.execute_reply":"2022-08-01T15:20:24.435106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/openvinotoolkit/openvino","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:20:24.437312Z","iopub.execute_input":"2022-08-01T15:20:24.437989Z","iopub.status.idle":"2022-08-01T15:20:45.656094Z","shell.execute_reply.started":"2022-08-01T15:20:24.437951Z","shell.execute_reply":"2022-08-01T15:20:45.655113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:20:45.657644Z","iopub.execute_input":"2022-08-01T15:20:45.658294Z","iopub.status.idle":"2022-08-01T15:20:45.667539Z","shell.execute_reply.started":"2022-08-01T15:20:45.658251Z","shell.execute_reply":"2022-08-01T15:20:45.666735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Run Model Optimizer if the IR model file does not exist\nif not ir_path.exists():\n    print(\"Exporting TensorFlow model to IR... This may take a few minutes.\")\n    ! $mo_command\nelse:\n    print(f\"IR model {ir_path} already exists.\")","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:20:45.668848Z","iopub.execute_input":"2022-08-01T15:20:45.669348Z","iopub.status.idle":"2022-08-01T15:20:46.548637Z","shell.execute_reply.started":"2022-08-01T15:20:45.669308Z","shell.execute_reply":"2022-08-01T15:20:46.547412Z"},"trusted":true},"execution_count":null,"outputs":[]}]}