{"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 tensorflow \nfrom tensorflow import keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Dense, Flatten\nfrom tensorflow.keras.layers import Dropout\nfrom tensorflow.keras.optimizers import Adam\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.models import Model\nimport os\nimport csv\nimport numpy as np\nfrom tensorflow.keras.models import load_model","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-10T04:01:46.574233Z","iopub.execute_input":"2021-11-10T04:01:46.574782Z","iopub.status.idle":"2021-11-10T04:01:52.093600Z","shell.execute_reply.started":"2021-11-10T04:01:46.574628Z","shell.execute_reply":"2021-11-10T04:01:52.092498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root_dir='../input/110-1-ntut-dl-app-hw2/tw_food_101/tw_food_101'\ntrain_dir = root_dir + '/train'\ntest_dir = root_dir + '/test'","metadata":{"execution":{"iopub.status.busy":"2021-11-10T04:01:52.095804Z","iopub.execute_input":"2021-11-10T04:01:52.096146Z","iopub.status.idle":"2021-11-10T04:01:52.101612Z","shell.execute_reply.started":"2021-11-10T04:01:52.096099Z","shell.execute_reply":"2021-11-10T04:01:52.100594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\nrows = 17\ncols = 6\nfig, ax = plt.subplots(rows, cols, frameon=False, figsize=(15, 25))\nfood_dirs = os.listdir(train_dir)\nfor i in range(rows):\n    for j in range(cols):\n        food_dir = food_dirs[(i*cols + j)%101]\n        all_files = os.listdir(os.path.join(train_dir, food_dir))\n        img = plt.imread(os.path.join(train_dir, food_dir, all_files[3]))\n        ax[i][j].imshow(img)\n        ax[i][j].text(0, -20, food_dir, size=10, rotation=0, ha=\"left\", va=\"top\", \n                bbox=dict(boxstyle=\"round\", ec=(0, .6, .1), fc=(0, .7, .2)))\nplt.setp(ax, xticks=[], yticks=[])\nplt.tight_layout(rect=[0, 0.03, 1, 0.95])","metadata":{"execution":{"iopub.status.busy":"2021-11-10T04:01:52.102913Z","iopub.execute_input":"2021-11-10T04:01:52.103470Z","iopub.status.idle":"2021-11-10T04:02:18.551701Z","shell.execute_reply.started":"2021-11-10T04:01:52.103427Z","shell.execute_reply":"2021-11-10T04:02:18.550553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n        rescale=1./255,\n        shear_range=0.2,\n        zoom_range=0.2,\n        horizontal_flip=True)","metadata":{"execution":{"iopub.status.busy":"2021-11-10T04:02:18.554443Z","iopub.execute_input":"2021-11-10T04:02:18.554745Z","iopub.status.idle":"2021-11-10T04:02:18.561343Z","shell.execute_reply.started":"2021-11-10T04:02:18.554706Z","shell.execute_reply":"2021-11-10T04:02:18.560389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = train_datagen.flow_from_directory(\n    train_dir, \n    target_size=(221, 221), \n    color_mode = \"rgb\",\n    batch_size= 32,\n    class_mode='categorical',\n    shuffle = True)","metadata":{"execution":{"iopub.status.busy":"2021-11-10T04:02:18.563243Z","iopub.execute_input":"2021-11-10T04:02:18.563911Z","iopub.status.idle":"2021-11-10T04:02:20.156537Z","shell.execute_reply.started":"2021-11-10T04:02:18.563872Z","shell.execute_reply":"2021-11-10T04:02:20.155554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./255)\ntest_generator = train_datagen.flow_from_directory(\n    train_dir,\n    target_size=(221, 221), \n    color_mode = \"rgb\",\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-10T04:33:18.631688Z","iopub.execute_input":"2021-11-10T04:33:18.631986Z","iopub.status.idle":"2021-11-10T04:33:20.368382Z","shell.execute_reply.started":"2021-11-10T04:33:18.631953Z","shell.execute_reply":"2021-11-10T04:33:20.366995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#base_model = keras.applications.densenet.DenseNet121(include_top=False, weights='imagenet', input_shape=(221, 221, 3))\nbase_model = keras.applications.inception_v3.InceptionV3(include_top=False, weights='imagenet', input_shape=(221, 221, 3))\n#base_model = keras.applications.vgg16.VGG16(include_top=False, weights='imagenet', input_shape=(150, 150, 3))\n#base_model = keras.applications.resnet.ResNet50(include_top=False, weights='imagenet', input_shape=(150, 150, 3))\n#base_model = keras.applications.mobilenet.MobileNet(include_top=False, weights='imagenet', input_shape=(150, 150, 3))","metadata":{"execution":{"iopub.status.busy":"2021-11-10T04:02:20.158496Z","iopub.execute_input":"2021-11-10T04:02:20.158934Z","iopub.status.idle":"2021-11-10T04:02:26.184508Z","shell.execute_reply.started":"2021-11-10T04:02:20.158889Z","shell.execute_reply":"2021-11-10T04:02:26.183318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = base_model.output\nx = Flatten()(x)\nx = Dense(2048, activation='relu')(x)\nx = Dense(1024, activation='relu')(x)\nx = Dropout(.4)(x)\n\npredictions = Dense(101, activation='softmax')(x)\n\nmodel = Model(base_model.input, predictions)\n\n#model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-10T04:02:26.186391Z","iopub.execute_input":"2021-11-10T04:02:26.186750Z","iopub.status.idle":"2021-11-10T04:02:26.251321Z","shell.execute_reply.started":"2021-11-10T04:02:26.186705Z","shell.execute_reply":"2021-11-10T04:02:26.250298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='Adam',\n            loss='categorical_crossentropy',\n            metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-11-10T04:02:26.252807Z","iopub.execute_input":"2021-11-10T04:02:26.253135Z","iopub.status.idle":"2021-11-10T04:02:26.276186Z","shell.execute_reply.started":"2021-11-10T04:02:26.253092Z","shell.execute_reply":"2021-11-10T04:02:26.275264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(\n      train_generator,\n      epochs=10\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-10T04:02:26.277674Z","iopub.execute_input":"2021-11-10T04:02:26.278012Z","iopub.status.idle":"2021-11-10T04:23:51.044477Z","shell.execute_reply.started":"2021-11-10T04:02:26.277973Z","shell.execute_reply":"2021-11-10T04:23:51.043305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create {ID, filename} dictionary {'1269': '1269.jpg', '3863': '3863.jpg',.....}\ntest_dict = {}\nfor root, dirs, files in os.walk(root_dir + '/test'):\n    for filename in files:\n        test_id, file_ext = os.path.splitext(filename)\n        test_dict[test_id] = filename","metadata":{"execution":{"iopub.status.busy":"2021-11-10T04:53:46.157940Z","iopub.execute_input":"2021-11-10T04:53:46.158261Z","iopub.status.idle":"2021-11-10T04:53:47.751019Z","shell.execute_reply.started":"2021-11-10T04:53:46.158222Z","shell.execute_reply":"2021-11-10T04:53:47.749944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport numpy as np\nfrom skimage import transform\ndef load_img(filename, target_w=150, target_h=150):\n   np_image = Image.open(filename)\n   np_image = np.array(np_image).astype('float32')/255\n   np_image = transform.resize(np_image, (target_w, target_h, 3))\n   np_image = np.expand_dims(np_image, axis=0)\n   return np_image\n","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:07:07.748119Z","iopub.execute_input":"2021-11-10T05:07:07.748466Z","iopub.status.idle":"2021-11-10T05:07:07.755424Z","shell.execute_reply.started":"2021-11-10T05:07:07.748420Z","shell.execute_reply":"2021-11-10T05:07:07.754222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read images in order and make predictions\nresults = []\nfor i in range(len(test_dict)):\n    img = load_img(root_dir + '/test/' + test_dict[str(i)], 221, 221)\n    ret = model.predict(img)\n    results.append(np.argmax(ret))","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:10:25.221009Z","iopub.execute_input":"2021-11-10T05:10:25.221486Z","iopub.status.idle":"2021-11-10T05:29:36.850265Z","shell.execute_reply.started":"2021-11-10T05:10:25.221453Z","shell.execute_reply":"2021-11-10T05:29:36.849207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Print results in CSV format and upload to Kaggle\nwith open('pred_results.csv', 'w') as f:\n    f.write('Id,Category\\n')\n    for i in range(len(results)):\n        f.write(str(i) + ',' + str(results[i]) + '\\n')","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:29:36.855039Z","iopub.execute_input":"2021-11-10T05:29:36.856029Z","iopub.status.idle":"2021-11-10T05:29:36.875562Z","shell.execute_reply.started":"2021-11-10T05:29:36.855980Z","shell.execute_reply":"2021-11-10T05:29:36.874590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Download your results!\nfrom IPython.display import FileLink\nFileLink('pred_results.csv')","metadata":{"execution":{"iopub.status.busy":"2021-11-10T05:29:36.877265Z","iopub.execute_input":"2021-11-10T05:29:36.877651Z","iopub.status.idle":"2021-11-10T05:29:36.886988Z","shell.execute_reply.started":"2021-11-10T05:29:36.877593Z","shell.execute_reply":"2021-11-10T05:29:36.885356Z"},"trusted":true},"execution_count":null,"outputs":[]}]}