{"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":"markdown","source":"* <font size=5>In this tutorial I will show you how I extended **CIFAR10** dataset with 2000 extra images which are collected [here](https://www.kaggle.com/datasets/dunky11/cifar10-extra-10x1000-images)","metadata":{}},{"cell_type":"markdown","source":"# <center> Imports <center>","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport os\nimport numpy as np\nimport cv2\nfrom tensorflow import keras\nfrom PIL import Image\nimport random","metadata":{"execution":{"iopub.status.busy":"2022-03-22T11:41:35.897189Z","iopub.execute_input":"2022-03-22T11:41:35.897755Z","iopub.status.idle":"2022-03-22T11:41:38.354391Z","shell.execute_reply.started":"2022-03-22T11:41:35.897638Z","shell.execute_reply":"2022-03-22T11:41:38.353166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <center> Download original dataset<center>\n* We need to concatenate the original dataset with the extra dataset, and we are not going to use the downloaded test data as our final test data.","metadata":{}},{"cell_type":"code","source":"(x_train, y_train), (x_test, y_test) = keras.datasets.cifar10.load_data()\n\nprint('The size of x_train:', x_train.shape[0])","metadata":{"execution":{"iopub.status.busy":"2022-03-22T11:41:38.356163Z","iopub.execute_input":"2022-03-22T11:41:38.356418Z","iopub.status.idle":"2022-03-22T11:41:39.002279Z","shell.execute_reply.started":"2022-03-22T11:41:38.356381Z","shell.execute_reply":"2022-03-22T11:41:39.000574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* In original dataset, each image has a label, we need to make a \"pair\" (i.e. a list containing a image array and its corresponding label) so when we shuffle the dataset the images won't match a wrong label.","metadata":{}},{"cell_type":"code","source":"origin_train_pair_list = []\n\nx = list(x_train)\ny = list(y_train)\n\nfor i in range(x_train.shape[0]):\n    pair = []\n    pair.append(x[i])\n    pair.append(y[i])\n    origin_train_pair_list.append(pair)\n\nrandom.shuffle(origin_train_pair_list)","metadata":{"execution":{"iopub.status.busy":"2022-03-22T11:41:39.004998Z","iopub.execute_input":"2022-03-22T11:41:39.005286Z","iopub.status.idle":"2022-03-22T11:41:39.101008Z","shell.execute_reply.started":"2022-03-22T11:41:39.005244Z","shell.execute_reply":"2022-03-22T11:41:39.099285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* We need to get each extra folders' filenames which is also the name of classes. The classes' name and the labels have a same order (i.e. airplane to 0, horse to 1 etc.)","metadata":{}},{"cell_type":"code","source":"folder_name = os.listdir('../input/cifar10-extra-10x1000-images')\nfolder_name","metadata":{"execution":{"iopub.status.busy":"2022-03-22T11:41:39.103074Z","iopub.execute_input":"2022-03-22T11:41:39.10328Z","iopub.status.idle":"2022-03-22T11:41:39.11686Z","shell.execute_reply.started":"2022-03-22T11:41:39.103254Z","shell.execute_reply":"2022-03-22T11:41:39.115886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* This step creates a path list which contains the path of each folders, we will use them to load the extra images later.","metadata":{}},{"cell_type":"code","source":"NUM_CLASS = len(folder_name)\n\nfolder_path = []\nfor i in range(NUM_CLASS):\n    class_name = folder_name[i]\n    path = '../input/cifar10-extra-10x1000-images'\n    path = path + '/' + class_name\n    folder_path.append(path)\n    \nfolder_path","metadata":{"execution":{"iopub.status.busy":"2022-03-22T11:41:39.118439Z","iopub.execute_input":"2022-03-22T11:41:39.118789Z","iopub.status.idle":"2022-03-22T11:41:39.129604Z","shell.execute_reply.started":"2022-03-22T11:41:39.118744Z","shell.execute_reply":"2022-03-22T11:41:39.128004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_label_pair_list = []\n\n#This will decide how many extra images you want to add to the dataset\nNUM_EXTRA_IAMGES = 2000\n\n#The initial label is 0\nlabel = 0\n\nfor i in range(NUM_CLASS):\n    folder_name = folder_path[i]\n    image_name_list = os.listdir(folder_name)\n    for ids in range(NUM_EXTRA_IAMGES):\n        pair = []\n        file_path = folder_name + '/' + image_name_list[ids]\n        img = np.array(Image.open(file_path))\n        pair.append(img)\n        lab_list = []\n        lab_list.append(label)\n        pair.append(lab_list)\n        image_label_pair_list.append(pair)\n    label += 1\n    \n# We shuffle it again\nrandom.shuffle(image_label_pair_list)","metadata":{"execution":{"iopub.status.busy":"2022-03-22T11:41:39.131316Z","iopub.execute_input":"2022-03-22T11:41:39.132732Z","iopub.status.idle":"2022-03-22T11:42:07.148547Z","shell.execute_reply.started":"2022-03-22T11:41:39.132648Z","shell.execute_reply":"2022-03-22T11:42:07.147654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* We concatenated the extra images with original dataset and shuffle it.","metadata":{}},{"cell_type":"code","source":"origin_train_pair_list.extend(image_label_pair_list)\nrandom.shuffle(origin_train_pair_list)","metadata":{"execution":{"iopub.status.busy":"2022-03-22T11:42:07.149807Z","iopub.execute_input":"2022-03-22T11:42:07.150206Z","iopub.status.idle":"2022-03-22T11:42:07.26654Z","shell.execute_reply.started":"2022-03-22T11:42:07.150173Z","shell.execute_reply":"2022-03-22T11:42:07.265653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* We need to sperate the \"pair\" into two list, then transform it into a np.array type.","metadata":{}},{"cell_type":"code","source":"image_list = []\nlabel_list = []\n\nfor i in range(len(origin_train_pair_list)):\n    pair = origin_train_pair_list[i]\n    image_list.append(pair[0])\n    label_list.append(pair[1])\n\nx_train = np.array(image_list)\ny_train = np.array(label_list)\n\nprint('The size of x_train:', x_train.shape[0])","metadata":{"execution":{"iopub.status.busy":"2022-03-22T11:42:07.268023Z","iopub.execute_input":"2022-03-22T11:42:07.268369Z","iopub.status.idle":"2022-03-22T11:42:07.805448Z","shell.execute_reply.started":"2022-03-22T11:42:07.268337Z","shell.execute_reply":"2022-03-22T11:42:07.804669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* We create the test data from extended train data, so it will contains new-added images.","metadata":{}},{"cell_type":"code","source":"(x_train, y_train), (x_test, y_test) = ((x_train[:60000], y_train[:60000]),(x_train[60000:], y_train[60000:]))\n\nprint('The size of x_test:', x_test.shape[0])\nprint('The size of x_train:', x_train.shape[0])","metadata":{"execution":{"iopub.status.busy":"2022-03-22T11:42:07.80677Z","iopub.execute_input":"2022-03-22T11:42:07.807266Z","iopub.status.idle":"2022-03-22T11:42:07.851386Z","shell.execute_reply.started":"2022-03-22T11:42:07.807224Z","shell.execute_reply":"2022-03-22T11:42:07.850673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Run the cell below you will get the extended dataset in tfrecord format, and if you want to get a numpy-format dataset you can skip this cell.","metadata":{}},{"cell_type":"code","source":"TRAIN_DS_SIZE = x_train.shape[0]\nTEST_DS_SIZE = x_test.shape[0]\n\ndef Example(image, label):\n    feature = {\n        'image': tf.train.Feature(bytes_list = tf.train.BytesList(value = [image])),\n        'label': tf.train.Feature(int64_list = tf.train.Int64List(value = [label])),\n    }\n    return tf.train.Example(features = tf.train.Features(feature = feature))\n\nwith tf.io.TFRecordWriter(f'./train_ds') as writer:\n    for num in range(TRAIN_DS_SIZE):\n        image = cv2.imencode(\".jpg\", x_train[num])[1].tobytes()\n        label = y_train[num]\n        example = Example(image, label)\n        writer.write(example.SerializeToString())","metadata":{"execution":{"iopub.status.busy":"2022-03-22T11:42:07.853387Z","iopub.execute_input":"2022-03-22T11:42:07.853735Z","iopub.status.idle":"2022-03-22T11:42:12.54468Z","shell.execute_reply.started":"2022-03-22T11:42:07.85368Z","shell.execute_reply":"2022-03-22T11:42:12.54361Z"},"trusted":true},"execution_count":null,"outputs":[]}]}