{"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 os \nfrom tqdm import tqdm\nfrom zipfile import ZipFile\nfrom PIL import Image\nfrom matplotlib import pyplot as plt\nimport numpy as np\nimport pandas as pd","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-23T13:09:21.467819Z","iopub.execute_input":"2022-07-23T13:09:21.468312Z","iopub.status.idle":"2022-07-23T13:09:21.475236Z","shell.execute_reply.started":"2022-07-23T13:09:21.468271Z","shell.execute_reply":"2022-07-23T13:09:21.473956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_zip = '../input/dogs-vs-cats/train.zip'\ntrain_dataset_path = 'dataset/train'\n\nval_zip = '../input/dogs-vs-cats/test1.zip'\nval_dataset_path = 'dataset/val'\n\nwith ZipFile(train_zip, 'r') as zipObj:\n    zipObj.extractall(train_dataset_path)\n\nwith ZipFile(val_zip, 'r') as zipObj:\n    zipObj.extractall(val_dataset_path)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T13:09:27.882174Z","iopub.execute_input":"2022-07-23T13:09:27.882720Z","iopub.status.idle":"2022-07-23T13:09:47.209351Z","shell.execute_reply.started":"2022-07-23T13:09:27.882673Z","shell.execute_reply":"2022-07-23T13:09:47.207954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# update path \ntrain_dataset_path = os.path.join('dataset/train', 'train')\nval_dataset_path = os.path.join('dataset/val', 'test1')\n# import shutil\n# shutil.rmtree('./train')","metadata":{"execution":{"iopub.status.busy":"2022-07-23T13:09:47.211740Z","iopub.execute_input":"2022-07-23T13:09:47.212118Z","iopub.status.idle":"2022-07-23T13:09:47.218364Z","shell.execute_reply.started":"2022-07-23T13:09:47.212085Z","shell.execute_reply":"2022-07-23T13:09:47.216987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# construct dataset\n# cat = 0\n# dog = 1\n\ndef preprocess(image):\n    # Resize image\n    img = image.resize((100, 100))\n    # Change type to float32\n    img = np.array(img).astype(np.float32)\n    # Normalization to 1.0\n    img /= 255\n    return img\n\n\nimages = []\nlabels = []\nwith tqdm(len(os.listdir(train_dataset_path))) as pb:\n    for filename in os.listdir(train_dataset_path):\n        label = (0.0 if filename.split('.')[0] == 'cat' else 1.0)\n        image = Image.open(os.path.join(train_dataset_path, filename))\n        labels.append(label)\n        images.append(preprocess(image))\n        pb.update(1)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T13:09:47.220245Z","iopub.execute_input":"2022-07-23T13:09:47.220780Z","iopub.status.idle":"2022-07-23T13:12:21.570528Z","shell.execute_reply.started":"2022-07-23T13:09:47.220733Z","shell.execute_reply":"2022-07-23T13:12:21.568956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_image(image, label, num=5):\n    choice = np.random.choice(len(image), num)\n    # create 1 row display image by num columns\n    fig, ax=plt.subplots(1, num, figsize=(20, 20))\n    for i in list(range(num)):\n        ax[i].title.set_text(label[choice[i]])\n#         ax[i].figure(figsize=(10, 10))\n        ax[i].imshow(image[choice[i]], )\nshow_image(images, labels)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T13:14:55.498050Z","iopub.execute_input":"2022-07-23T13:14:55.498742Z","iopub.status.idle":"2022-07-23T13:14:56.312308Z","shell.execute_reply.started":"2022-07-23T13:14:55.498686Z","shell.execute_reply":"2022-07-23T13:14:56.310854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_df = pd.DataFrame(labels)\nlabel_df[0].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T13:14:58.875704Z","iopub.execute_input":"2022-07-23T13:14:58.876820Z","iopub.status.idle":"2022-07-23T13:14:58.912671Z","shell.execute_reply.started":"2022-07-23T13:14:58.876766Z","shell.execute_reply":"2022-07-23T13:14:58.911430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make labels into categorical\nimport tensorflow as tf\n\ndef to_categorical(labels):\n    return tf.keras.utils.to_categorical(labels, 2)\n\n\nlabels_categorical = to_categorical(labels)\n# np datatype\nimages = np.array(images)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T13:15:00.285510Z","iopub.execute_input":"2022-07-23T13:15:00.285988Z","iopub.status.idle":"2022-07-23T13:15:03.387539Z","shell.execute_reply.started":"2022-07-23T13:15:00.285951Z","shell.execute_reply":"2022-07-23T13:15:03.386290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(images.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T13:15:03.393861Z","iopub.execute_input":"2022-07-23T13:15:03.394356Z","iopub.status.idle":"2022-07-23T13:15:03.401051Z","shell.execute_reply.started":"2022-07-23T13:15:03.394322Z","shell.execute_reply":"2022-07-23T13:15:03.399804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import *\nfrom tensorflow.keras import applications\nfrom tensorflow.keras.activations import *\nfrom tensorflow.keras.regularizers import *\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.losses import *\nfrom tensorflow.keras.optimizers import *\nfrom tensorflow.keras.callbacks import *\n\n\nbase_model = applications.mobilenet_v2.MobileNetV2(\n    input_shape=(100, 100, 3),\n    include_top=False,\n    weights=None,\n)\n\nx = Flatten()(base_model.layers[-1].output)\nx = Dense(256, activation='relu')(x)\nx = Dense(128, activation='relu')(x)\noutput = Dense(2, activation='softmax')(x)\nmodel_1 = Model(inputs=base_model.layers[0].output, outputs=output)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T13:15:03.403149Z","iopub.execute_input":"2022-07-23T13:15:03.404946Z","iopub.status.idle":"2022-07-23T13:15:04.776794Z","shell.execute_reply.started":"2022-07-23T13:15:03.404860Z","shell.execute_reply":"2022-07-23T13:15:04.775318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_2 = applications.mobilenet_v2.MobileNetV2(\n    input_shape=(100, 100, 3),\n    include_top=False,\n    weights='imagenet',\n)\nmodel_1.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-01T12:38:31.809953Z","iopub.execute_input":"2022-07-01T12:38:31.810925Z","iopub.status.idle":"2022-07-01T12:38:31.816436Z","shell.execute_reply.started":"2022-07-01T12:38:31.810883Z","shell.execute_reply":"2022-07-01T12:38:31.815696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_layer = InputLayer((100, 100, 3))\n\nx = Conv2D(32, (3, 3), activation='relu')(input_layer.output)\nx = BatchNormalization()(x)\nx = MaxPool2D()(x)\nx = Dropout(0.3)(x)\n\nx = Conv2D(64, (3, 3), activation='relu')(x)\nx = BatchNormalization()(x)\nx = MaxPool2D()(x)\nx = Dropout(0.3)(x)\n\nx = Conv2D(128, (3, 3), activation='relu')(x)\nx = BatchNormalization()(x)\nx = MaxPool2D()(x)\nx = Dropout(0.3)(x)\n\nx = Flatten()(x)\nx = Dense(512, activation='relu')(x)\nx = BatchNormalization()(x)\nx = Dropout(0.3)(x)\noutput = Dense(2, activation='softmax')(x)\n\nmodel_3 = Model(inputs=input_layer.output, outputs=output)","metadata":{"execution":{"iopub.status.busy":"2022-07-01T12:38:31.819851Z","iopub.execute_input":"2022-07-01T12:38:31.820191Z","iopub.status.idle":"2022-07-01T12:38:31.97024Z","shell.execute_reply.started":"2022-07-01T12:38:31.820162Z","shell.execute_reply":"2022-07-01T12:38:31.969221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_3.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-01T12:38:31.97214Z","iopub.execute_input":"2022-07-01T12:38:31.972419Z","iopub.status.idle":"2022-07-01T12:38:31.981597Z","shell.execute_reply.started":"2022-07-01T12:38:31.972392Z","shell.execute_reply":"2022-07-01T12:38:31.980195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# TRANSFER LEARNING 1\nbase_model = applications.mobilenet_v2.MobileNetV2(\n    input_shape=(100, 100, 3),\n    include_top=False,\n    weights='imagenet',\n)\nbase_model.trainable = False","metadata":{"execution":{"iopub.status.busy":"2022-07-23T13:15:09.429087Z","iopub.execute_input":"2022-07-23T13:15:09.429571Z","iopub.status.idle":"2022-07-23T13:15:11.395052Z","shell.execute_reply.started":"2022-07-23T13:15:09.429535Z","shell.execute_reply":"2022-07-23T13:15:11.393977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# TRANSFER LEARNING 2\nx = GlobalAveragePooling2D()(base_model.layers[-1].output)\nx = Dropout(0.2)(x)\noutput = Dense(2, activation='softmax')(x)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-23T13:15:11.396895Z","iopub.execute_input":"2022-07-23T13:15:11.398163Z","iopub.status.idle":"2022-07-23T13:15:11.421205Z","shell.execute_reply.started":"2022-07-23T13:15:11.398120Z","shell.execute_reply":"2022-07-23T13:15:11.420254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans_model = Model(inputs=base_model.layers[0].output, outputs=output)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T13:15:12.077457Z","iopub.execute_input":"2022-07-23T13:15:12.078287Z","iopub.status.idle":"2022-07-23T13:15:12.097852Z","shell.execute_reply.started":"2022-07-23T13:15:12.078244Z","shell.execute_reply":"2022-07-23T13:15:12.096478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# trans_model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T00:48:11.763469Z","iopub.execute_input":"2022-07-02T00:48:11.76399Z","iopub.status.idle":"2022-07-02T00:48:11.769487Z","shell.execute_reply.started":"2022-07-02T00:48:11.763944Z","shell.execute_reply":"2022-07-02T00:48:11.768468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reduce_lr = ReduceLROnPlateau(monitor='val_acc', \n                                patience=2, \n                                verbose=1, \n                                factor=0.5, \n                                min_lr=0.00001)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T13:15:16.308510Z","iopub.execute_input":"2022-07-23T13:15:16.309047Z","iopub.status.idle":"2022-07-23T13:15:16.315875Z","shell.execute_reply.started":"2022-07-23T13:15:16.308996Z","shell.execute_reply":"2022-07-23T13:15:16.314566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans_model.compile(optimizer=Adam(learning_rate=0.0001), \n                   loss='categorical_crossentropy',\n                  metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-07-23T13:15:16.682874Z","iopub.execute_input":"2022-07-23T13:15:16.683274Z","iopub.status.idle":"2022-07-23T13:15:16.707591Z","shell.execute_reply.started":"2022-07-23T13:15:16.683240Z","shell.execute_reply":"2022-07-23T13:15:16.706562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans_model.fit(x=images, y=labels_categorical,\n            batch_size=32, validation_split=0.3,\n            epochs=20, callbacks=[reduce_lr],\n           )","metadata":{"execution":{"iopub.status.busy":"2022-07-23T13:15:20.474293Z","iopub.execute_input":"2022-07-23T13:15:20.475894Z","iopub.status.idle":"2022-07-23T13:39:48.250516Z","shell.execute_reply.started":"2022-07-23T13:15:20.475823Z","shell.execute_reply":"2022-07-23T13:39:48.249269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# save weights\n# trans_model.save_weights('saved_model/trans_model-1.h5')\n# saved_model\nsave_dir = 'trans_model1'\ntf.saved_model.save(trans_model, save_dir)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:02:56.239980Z","iopub.execute_input":"2022-07-23T14:02:56.240475Z","iopub.status.idle":"2022-07-23T14:03:21.177915Z","shell.execute_reply.started":"2022-07-23T14:02:56.240439Z","shell.execute_reply":"2022-07-23T14:03:21.176684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans_model.save_weights('saved_model/trans_model-1.h5')","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:25:49.537243Z","iopub.execute_input":"2022-07-23T14:25:49.537668Z","iopub.status.idle":"2022-07-23T14:25:49.617603Z","shell.execute_reply.started":"2022-07-23T14:25:49.537635Z","shell.execute_reply":"2022-07-23T14:25:49.616428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert to tflite\nconverter = tf.lite.TFLiteConverter.from_keras_model(trans_model)\ntflite_model = converter.convert()\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-02T09:30:09.300858Z","iopub.execute_input":"2022-07-02T09:30:09.301273Z","iopub.status.idle":"2022-07-02T09:30:36.016145Z","shell.execute_reply.started":"2022-07-02T09:30:09.30124Z","shell.execute_reply":"2022-07-02T09:30:36.014477Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('trans_model1.tflite', 'wb') as f:\n    f.write(tflite_model)","metadata":{"execution":{"iopub.status.busy":"2022-07-02T09:30:56.708699Z","iopub.execute_input":"2022-07-02T09:30:56.709093Z","iopub.status.idle":"2022-07-02T09:30:56.72385Z","shell.execute_reply.started":"2022-07-02T09:30:56.709062Z","shell.execute_reply":"2022-07-02T09:30:56.722728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install tflite-runtime","metadata":{"execution":{"iopub.status.busy":"2022-07-03T13:02:50.407161Z","iopub.execute_input":"2022-07-03T13:02:50.407667Z","iopub.status.idle":"2022-07-03T13:03:00.953094Z","shell.execute_reply.started":"2022-07-03T13:02:50.407621Z","shell.execute_reply":"2022-07-03T13:03:00.952161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# download models in python \n!wget -O model/cat-dog.tflite \"https://drive.google.com/uc?id=16zfOJMoGdQId3gtJ7nivELKdWhSvUJTV&export=download\"","metadata":{"execution":{"iopub.status.busy":"2022-07-03T13:13:51.968003Z","iopub.execute_input":"2022-07-03T13:13:51.968419Z","iopub.status.idle":"2022-07-03T13:13:52.769321Z","shell.execute_reply.started":"2022-07-03T13:13:51.968386Z","shell.execute_reply":"2022-07-03T13:13:52.767661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Testing tflite_runtime\nimport tflite_runtime.interpreter as interpreter\n\nmodel_path = './cat-dog.tflite'\n\ndef setup_model(model_path):\n    model = interpreter.Interpreter(model_path=model_path)\n    model.allocate_tensors()\n    return model\n\nmodel = setup_model(model_path)","metadata":{"execution":{"iopub.status.busy":"2022-07-03T14:18:56.182576Z","iopub.execute_input":"2022-07-03T14:18:56.183984Z","iopub.status.idle":"2022-07-03T14:18:56.203754Z","shell.execute_reply.started":"2022-07-03T14:18:56.183934Z","shell.execute_reply":"2022-07-03T14:18:56.202600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prepare image\nimport requests\nfrom io import BytesIO\nim_url = 'https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftse2.mm.bing.net%2Fth%3Fid%3DOIP.udhtqEGoscMLdzC0BCejugHaF3%26pid%3DApi&f=1'\nim = BytesIO(requests.get(im_url).content)\nimg = Image.open(im)\n# preprocess\ndef preprocess(image, shape=(100,100)):\n    return np.array(image.resize(shape)).astype(np.float32)/255\n\nimage = preprocess(img)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:14:05.042445Z","iopub.execute_input":"2022-07-23T14:14:05.043082Z","iopub.status.idle":"2022-07-23T14:14:05.464061Z","shell.execute_reply.started":"2022-07-23T14:14:05.043039Z","shell.execute_reply":"2022-07-23T14:14:05.462534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:23:13.357043Z","iopub.execute_input":"2022-07-23T14:23:13.357450Z","iopub.status.idle":"2022-07-23T14:23:13.579848Z","shell.execute_reply.started":"2022-07-23T14:23:13.357416Z","shell.execute_reply":"2022-07-23T14:23:13.578715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.expand_dims(image, 0).shape","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:24:20.283094Z","iopub.execute_input":"2022-07-23T14:24:20.283553Z","iopub.status.idle":"2022-07-23T14:24:20.291151Z","shell.execute_reply.started":"2022-07-23T14:24:20.283514Z","shell.execute_reply":"2022-07-23T14:24:20.289958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans_model.predict(np.expand_dims(image, 0))","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:24:33.157796Z","iopub.execute_input":"2022-07-23T14:24:33.158952Z","iopub.status.idle":"2022-07-23T14:24:34.316442Z","shell.execute_reply.started":"2022-07-23T14:24:33.158897Z","shell.execute_reply":"2022-07-23T14:24:34.315709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def inference_img(model, image):    \n    model.set_tensor(model.get_input_details()[0]['index'], [image])\n    model.invoke()\n    result = model.get_tensor(model.get_output_details()[0]['index'])\n    return np.argmax(result)\n\nprint(inference_img(model, image))","metadata":{"execution":{"iopub.status.busy":"2022-07-03T14:19:26.712302Z","iopub.execute_input":"2022-07-03T14:19:26.712709Z","iopub.status.idle":"2022-07-03T14:19:26.726083Z","shell.execute_reply.started":"2022-07-03T14:19:26.712674Z","shell.execute_reply":"2022-07-03T14:19:26.724801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-07-03T14:05:47.074794Z","iopub.execute_input":"2022-07-03T14:05:47.075215Z","iopub.status.idle":"2022-07-03T14:05:47.080467Z","shell.execute_reply.started":"2022-07-03T14:05:47.075179Z","shell.execute_reply":"2022-07-03T14:05:47.079575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}