{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n'''\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n'''\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-27T02:13:35.370348Z","iopub.execute_input":"2021-07-27T02:13:35.370734Z","iopub.status.idle":"2021-07-27T02:13:35.387515Z","shell.execute_reply.started":"2021-07-27T02:13:35.370649Z","shell.execute_reply":"2021-07-27T02:13:35.386766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!apt-get install p7zip \n!apt-get install p7zip-full ","metadata":{"execution":{"iopub.status.busy":"2021-07-27T02:13:35.396024Z","iopub.execute_input":"2021-07-27T02:13:35.396265Z","iopub.status.idle":"2021-07-27T02:13:43.712559Z","shell.execute_reply.started":"2021-07-27T02:13:35.396241Z","shell.execute_reply":"2021-07-27T02:13:43.711681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls","metadata":{"execution":{"iopub.status.busy":"2021-07-27T02:13:43.714160Z","iopub.execute_input":"2021-07-27T02:13:43.714498Z","iopub.status.idle":"2021-07-27T02:13:44.346570Z","shell.execute_reply.started":"2021-07-27T02:13:43.714468Z","shell.execute_reply":"2021-07-27T02:13:44.345723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import csv\nimport pathlib\n\n\nfilename = 'submit.csv'\np_tmp = pathlib.Path('../input/test-img/test').glob('*.png')\nwith open(filename, 'w') as f:\n    writer = csv.writer(f)\n    writer.writerow(['id','label'])\nfor p in p_tmp:\n    with open(filename, 'a', newline=\"\")as f:\n        writer = csv.writer(f)\n        writer.writerow([p.name,\"cat\"])\n        \n","metadata":{"execution":{"iopub.status.busy":"2021-07-27T02:13:44.349982Z","iopub.execute_input":"2021-07-27T02:13:44.350249Z","iopub.status.idle":"2021-07-27T02:14:01.230090Z","shell.execute_reply.started":"2021-07-27T02:13:44.350220Z","shell.execute_reply":"2021-07-27T02:14:01.229183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!7za e ../input/cifar-10/train.7z","metadata":{"execution":{"iopub.status.busy":"2021-07-27T02:14:01.231814Z","iopub.execute_input":"2021-07-27T02:14:01.232425Z","iopub.status.idle":"2021-07-27T02:20:38.335799Z","shell.execute_reply.started":"2021-07-27T02:14:01.232385Z","shell.execute_reply":"2021-07-27T02:20:38.334770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install natsort","metadata":{"execution":{"iopub.status.busy":"2021-07-27T02:20:38.337286Z","iopub.execute_input":"2021-07-27T02:20:38.337547Z","iopub.status.idle":"2021-07-27T02:20:46.888462Z","shell.execute_reply.started":"2021-07-27T02:20:38.337518Z","shell.execute_reply":"2021-07-27T02:20:46.887395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import  load_img,img_to_array,array_to_img\nfrom glob import glob\nimport csv\nfrom natsort import natsorted\n\nCATEGORYS = ['airplane','automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']\n\ntrain_img = []\ntrain_label = []\nfiles = glob(\"./*.png\")\n\ncsv_file = open(\"../input/cifar-10/trainLabels.csv\", \"r\", encoding=\"utf_8\", errors=\"\", newline=\"\")\nlabel_read = csv.reader(csv_file, delimiter=\",\", doublequote=True, lineterminator=\"\\r\\n\", quotechar='\"', skipinitialspace=True)\nlabel_list = []\nfor row in label_read:\n    label_list.append(row)\n\nprint(label_list[1])\ni = 0\nfor file_name in natsorted(files):\n    # print(file_name)\n    img = load_img(file_name,color_mode=\"rgb\", target_size=(32,32))\n    array = img_to_array(img)\n    train_img.append(array)\n    categorys_num = CATEGORYS.index(label_list[i+1][1])\n    train_label.append(categorys_num)\n    i += 1\n\n","metadata":{"execution":{"iopub.status.busy":"2021-07-27T02:20:46.891760Z","iopub.execute_input":"2021-07-27T02:20:46.892028Z","iopub.status.idle":"2021-07-27T02:21:03.396147Z","shell.execute_reply.started":"2021-07-27T02:20:46.892000Z","shell.execute_reply":"2021-07-27T02:21:03.395291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nfrom tensorflow.keras.utils import to_categorical\ntrain_img = np.array(train_img)\ntrain_label = np.array(train_label)\ntrain_img = train_img.astype('float32') / 255\ntrain_label = to_categorical(train_label,10)\n\n\nfor i in range(len(train_img)):\n    plt.imshow(train_img[i])\n    print(CATEGORYS[np.argmax(train_label[i])])\n    plt.show()\n    if i == 9:\n        break","metadata":{"execution":{"iopub.status.busy":"2021-07-27T02:21:03.397472Z","iopub.execute_input":"2021-07-27T02:21:03.397836Z","iopub.status.idle":"2021-07-27T02:21:04.976299Z","shell.execute_reply.started":"2021-07-27T02:21:03.397797Z","shell.execute_reply":"2021-07-27T02:21:04.975539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.datasets import cifar10\n\n(a,b), (val_img, val_label) = cifar10.load_data()\nval_img = val_img.astype(\"float32\")/255\nval_label = to_categorical(val_label, 10)","metadata":{"execution":{"iopub.status.busy":"2021-07-27T02:21:04.979741Z","iopub.execute_input":"2021-07-27T02:21:04.979986Z","iopub.status.idle":"2021-07-27T02:21:11.154931Z","shell.execute_reply.started":"2021-07-27T02:21:04.979961Z","shell.execute_reply":"2021-07-27T02:21:11.154002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(11,21):\n    plt.imshow(val_img[i])\n    print(\"label\", CATEGORYS[np.argmax(val_label[i])])\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-27T02:21:11.156422Z","iopub.execute_input":"2021-07-27T02:21:11.156773Z","iopub.status.idle":"2021-07-27T02:21:12.321441Z","shell.execute_reply.started":"2021-07-27T02:21:11.156736Z","shell.execute_reply":"2021-07-27T02:21:12.320595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\ntrain_datagen = ImageDataGenerator(\n    featurewise_center = False,\n    featurewise_std_normalization=False,\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    horizontal_flip=True\n)\nval_datagen = ImageDataGenerator(\n    # featurewise_center = False,\n    # featurewise_std_normalization=False,\n    # rotation_range=20,\n    # width_shift_range=0.2,\n    # height_shift_range=0.2,\n    # horizontal_flip=True\n)\n\n# datagen.fit(train_img,augment=True, rounds=2, seed=None)\n\n\ntrain_img_augment = train_datagen.flow(train_img, train_label,batch_size=128)\nval_img_augment = val_datagen.flow(val_img, val_label,batch_size=128)","metadata":{"execution":{"iopub.status.busy":"2021-07-27T02:21:12.324435Z","iopub.execute_input":"2021-07-27T02:21:12.324761Z","iopub.status.idle":"2021-07-27T02:21:12.331225Z","shell.execute_reply.started":"2021-07-27T02:21:12.324701Z","shell.execute_reply":"2021-07-27T02:21:12.330181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def step_decay(epoch):\n    lr = 0.001\n    if(epoch >= 100):\n        lr/=5\n    if(epoch>=140):\n        lr/=2\n    return lr","metadata":{"execution":{"iopub.status.busy":"2021-07-27T02:21:12.332884Z","iopub.execute_input":"2021-07-27T02:21:12.333317Z","iopub.status.idle":"2021-07-27T02:21:12.344084Z","shell.execute_reply.started":"2021-07-27T02:21:12.333244Z","shell.execute_reply":"2021-07-27T02:21:12.343241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, Activation, Flatten, Dense,MaxPool2D, Dropout, BatchNormalization\nfrom tensorflow.keras.optimizers import SGD, Adam\nfrom tensorflow.keras.callbacks import LearningRateScheduler\n\nmodel = Sequential()\nmodel.add(Conv2D(64, (3,3), padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(Conv2D(64,(3,3), padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.25))\nmodel.add(MaxPool2D())\nmodel.add(Conv2D(128, (3,3), padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(Conv2D(128, (3,3), padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\n\nmodel.add(Dropout(0.25))\nmodel.add(MaxPool2D())\n# model.add(Conv2D(256, (3,3), padding='valid'))\n# model.add(Activation('relu'))\nmodel.add(Conv2D(256, (3,3), padding='same'))\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.25))\nmodel.add(MaxPool2D())\nmodel.add(Flatten())\nmodel.add(Dense(1024))\nmodel.add(Activation('relu'))\nmodel.add(Dense(512))\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.25))\nmodel.add(Dense(10))\nmodel.add(Activation('softmax'))\n\nlr_decay = LearningRateScheduler(step_decay)\nmodel.compile(loss='categorical_crossentropy', optimizer=Adam(lr=0.001),metrics=['accuracy'])\n# input_shape = train_img[0].shape\n# print(input_shape)\n# model.summary()\n# history = model.fit(train_img, train_label, batch_size=16, epochs=150,verbose=1,validation_data=(val_img,val_label))\nhistroy = model.fit_generator(train_img_augment,epochs=150,validation_data=val_img_augment,callbacks=[lr_decay])","metadata":{"execution":{"iopub.status.busy":"2021-07-27T02:21:12.346554Z","iopub.execute_input":"2021-07-27T02:21:12.347443Z","iopub.status.idle":"2021-07-27T03:29:38.524327Z","shell.execute_reply.started":"2021-07-27T02:21:12.347401Z","shell.execute_reply":"2021-07-27T03:29:38.523390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-27T03:29:38.525862Z","iopub.execute_input":"2021-07-27T03:29:38.526323Z","iopub.status.idle":"2021-07-27T03:29:38.546396Z","shell.execute_reply.started":"2021-07-27T03:29:38.526282Z","shell.execute_reply":"2021-07-27T03:29:38.545528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(histroy.history[\"accuracy\"])\nplt.plot(histroy.history[\"val_accuracy\"])\nplt.title(\"Model accuracy\")\nplt.xlabel(\"epoch\")\nplt.ylabel(\"accuracy\")\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()\n\nplt.plot(histroy.history[\"loss\"])\nplt.plot(histroy.history[\"val_loss\"])\nplt.title(\"model loss\")\nplt.xlabel(\"epoch\")\nplt.ylabel(\"loss\")\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-27T03:29:38.547769Z","iopub.execute_input":"2021-07-27T03:29:38.548332Z","iopub.status.idle":"2021-07-27T03:29:38.848972Z","shell.execute_reply.started":"2021-07-27T03:29:38.548295Z","shell.execute_reply":"2021-07-27T03:29:38.848233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls","metadata":{"execution":{"iopub.status.busy":"2021-07-27T03:29:38.850297Z","iopub.execute_input":"2021-07-27T03:29:38.850659Z","iopub.status.idle":"2021-07-27T03:29:39.729728Z","shell.execute_reply.started":"2021-07-27T03:29:38.850606Z","shell.execute_reply":"2021-07-27T03:29:39.728832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import csv\nimport pathlib\n\n\nfilename = 'submit.csv'\np_tmp = pathlib.Path('../input/test-img/test').glob('*.png')\nwith open(filename, 'w') as f:\n    writer = csv.writer(f)\n    writer.writerow(['id','label'])\ncnt = 0\nfor p in p_tmp:\n    \n    img = load_img(p,color_mode=\"rgb\", target_size=(32,32))\n    array = img_to_array(img)\n    np_array = np.array(array)\n    test_img = np_array.astype('float32') / 255\n    test_img = test_img.reshape(-1,32, 32, 3)\n    # print(test_img.shape)\n    predictions = model.predict(test_img)\n    predict_label = CATEGORYS[predictions[0].argmax()]\n    # print(predict_label)\n    \n    with open(filename, 'a', newline=\"\")as f:\n        writer = csv.writer(f)\n        writer.writerow([p.stem,predict_label])\n    cnt += 1\n    if cnt % 10000 == 0:\n        print('cnt',cnt)\n    \n        \n    \n\n        \n        \n'''   \nfiles = glob(\"./*.png\")\n\ncsv_file = open(\"../input/cifar-10/trainLabels.csv\", \"r\", encoding=\"utf_8\", errors=\"\", newline=\"\")\nlabel_read = csv.reader(csv_file, delimiter=\",\", doublequote=True, lineterminator=\"\\r\\n\", quotechar='\"', skipinitialspace=True)\nlabel_list = []\nfor row in label_read:\n    label_list.append(row)\n\nprint(label_list[1])\ni = 0\nfor file_name in natsorted(files):\n    # print(file_name)\n    img = load_img(file_name,color_mode=\"rgb\", target_size=(32,32))\n    array = img_to_array(img)\n    train_img.append(array)\n    categorys_num = CATEGORYS.index(label_list[i+1][1])\n    train_label.append(categorys_num)\n    i += 1\n'''    \n","metadata":{"execution":{"iopub.status.busy":"2021-07-27T07:00:16.835796Z","iopub.execute_input":"2021-07-27T07:00:16.836115Z","iopub.status.idle":"2021-07-27T09:52:53.653415Z","shell.execute_reply.started":"2021-07-27T07:00:16.836087Z","shell.execute_reply":"2021-07-27T09:52:53.652622Z"},"trusted":true},"execution_count":null,"outputs":[]}]}