{"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 the libraries\nimport os\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport cv2 # computer vision library for reading images\nfrom glob import glob # finds all the pathnames matching a specified pattern according to the rules used by the Unix shell\nfrom tqdm import tqdm # progress bar library\n\nimport tensorflow as tf\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping\nfrom tensorflow.keras.optimizers import Adam\n\nfrom keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D\nfrom keras.layers import Dropout, Flatten, Dense\nfrom keras.models import Sequential\n\nfrom sklearn.model_selection import train_test_split\n\nfrom IPython.display import Image","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-10T08:38:32.340497Z","iopub.execute_input":"2021-06-10T08:38:32.340853Z","iopub.status.idle":"2021-06-10T08:38:38.054206Z","shell.execute_reply.started":"2021-06-10T08:38:32.340777Z","shell.execute_reply":"2021-06-10T08:38:38.05267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set the parameters for the Keras model\nsize = 224\nnum_classes = 120\nlr = 1e-4\nbatch = 8\nepochs = 50\nfilters = 16\nkernel_size = 2\nDIM = (224, 224)","metadata":{"execution":{"iopub.status.busy":"2021-06-08T10:16:38.197618Z","iopub.execute_input":"2021-06-08T10:16:38.198007Z","iopub.status.idle":"2021-06-08T10:16:38.20471Z","shell.execute_reply.started":"2021-06-08T10:16:38.197977Z","shell.execute_reply":"2021-06-08T10:16:38.203117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_model(size, num_classes):\n    '''\n    Builds the neural network classifier model\n    '''\n    model = Sequential()\n    model.add(Conv2D(filters=filters, kernel_size=kernel_size, padding='same',activation='relu',input_shape=(size,size,3)))\n    model.add(MaxPooling2D())\n    model.add(Conv2D(filters=filters, kernel_size=kernel_size, padding='same',activation='relu'))\n    model.add(MaxPooling2D())\n    model.add(Conv2D(filters=filters, kernel_size=kernel_size, padding='same',activation='relu'))\n    model.add(MaxPooling2D())\n    model.add(Flatten())\n    model.add(Dense(256, activation='relu'))\n    model.add(Dense(256, activation='relu'))\n    model.add(Dense(num_classes,activation='softmax'))\n    model.summary()\n    return model\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_image(path, size):\n    '''\n    Read the image.\n    The cv2.imread(path, flag) method returns a matrix,\n    the flag sets the color or grayscale mode\n    Resize the image.\n    Apply normalization.\n    '''\n    image = cv2.imread(path, cv2.IMREAD_COLOR)\n    image = cv2.resize(image, DIM)\n    image = image / 255.0\n    image = image.astype(np.float32)\n    return image\n\ndef parse_data(x, y):\n    x = x.decode()\n    num_class = num_classes\n    image = read_image(x, size)\n    label = [0] * num_class\n    label[y] = 1\n    label = np.array(label)\n    label = label.astype(np.int32)\n    return image, label\n\ndef tf_parse(x, y):\n    x, y = tf.numpy_function(parse_data, [x, y], [tf.float32, tf.int32])\n    x.set_shape((size, size, 3))\n    y.set_shape((num_classes))\n    return x, y\n\ndef tf_dataset(x, y, batch):\n    dataset = tf.data.Dataset.from_tensor_slices((x, y))\n    dataset = dataset.map(tf_parse)\n    dataset = dataset.batch(batch)\n    dataset = dataset.repeat()\n    return dataset","metadata":{"execution":{"iopub.status.busy":"2021-06-08T10:17:07.951309Z","iopub.execute_input":"2021-06-08T10:17:07.951677Z","iopub.status.idle":"2021-06-08T10:17:07.966936Z","shell.execute_reply.started":"2021-06-08T10:17:07.95163Z","shell.execute_reply":"2021-06-08T10:17:07.9658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Path to the data\npath = \"../input/dog-breed-identification\"\ntrain_path = os.path.join(path, 'train/*')\ntest_path = os.path.join(path, 'test/*')\nlabels_path = os.path.join(path, 'labels.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-08T08:12:20.355921Z","iopub.execute_input":"2021-06-08T08:12:20.357153Z","iopub.status.idle":"2021-06-08T08:12:20.368292Z","shell.execute_reply.started":"2021-06-08T08:12:20.357109Z","shell.execute_reply":"2021-06-08T08:12:20.367173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get the number of breeds\nlabels_df = pd.read_csv(labels_path)\nbreed = labels_df['breed'].unique()\nprint('Number of breeds: ', len(breed))","metadata":{"execution":{"iopub.status.busy":"2021-06-08T08:12:20.370041Z","iopub.execute_input":"2021-06-08T08:12:20.370543Z","iopub.status.idle":"2021-06-08T08:12:20.421043Z","shell.execute_reply.started":"2021-06-08T08:12:20.370484Z","shell.execute_reply":"2021-06-08T08:12:20.419357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set a unique id for each breed\nbreed_to_id = {name: i for i, name in enumerate(breed)}\nid_to_breed = {i: name for i, name in enumerate(breed)}","metadata":{"execution":{"iopub.status.busy":"2021-06-08T08:12:20.422729Z","iopub.execute_input":"2021-06-08T08:12:20.423183Z","iopub.status.idle":"2021-06-08T08:12:20.430438Z","shell.execute_reply.started":"2021-06-08T08:12:20.423108Z","shell.execute_reply":"2021-06-08T08:12:20.428881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a list of labels ids from each image\nids = glob(train_path)\nlabels = []\n\nfor image_id in ids:\n    image_id = image_id.split('/')[-1].split('.')[0]\n    breed_name = list(labels_df[labels_df.id == image_id]['breed'])[0]\n    breed_idx = breed_to_id[breed_name]\n    labels.append(breed_idx)","metadata":{"execution":{"iopub.status.busy":"2021-06-08T08:12:20.432494Z","iopub.execute_input":"2021-06-08T08:12:20.43328Z","iopub.status.idle":"2021-06-08T08:12:40.345571Z","shell.execute_reply.started":"2021-06-08T08:12:20.433214Z","shell.execute_reply":"2021-06-08T08:12:40.344426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the dataset\nx_train, x_valid, y_train, y_valid = train_test_split(ids, labels, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2021-06-08T08:12:40.359428Z","iopub.execute_input":"2021-06-08T08:12:40.360118Z","iopub.status.idle":"2021-06-08T08:12:40.376955Z","shell.execute_reply.started":"2021-06-08T08:12:40.360041Z","shell.execute_reply":"2021-06-08T08:12:40.375913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Instanciate the model\nmodel = build_model(size, num_classes)\nmodel.compile(loss='categorical_crossentropy', optimizer=Adam(lr), metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2021-06-08T08:12:40.378862Z","iopub.execute_input":"2021-06-08T08:12:40.379333Z","iopub.status.idle":"2021-06-08T08:12:43.104156Z","shell.execute_reply.started":"2021-06-08T08:12:40.37929Z","shell.execute_reply":"2021-06-08T08:12:43.102844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create the datasets\ntrain_dataset = tf_dataset(x_train, y_train, batch=batch)\nvalid_dataset = tf_dataset(x_valid, y_valid, batch=batch)","metadata":{"execution":{"iopub.status.busy":"2021-06-08T08:12:43.107567Z","iopub.execute_input":"2021-06-08T08:12:43.10784Z","iopub.status.idle":"2021-06-08T08:12:43.280616Z","shell.execute_reply.started":"2021-06-08T08:12:43.107813Z","shell.execute_reply":"2021-06-08T08:12:43.279613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set callbacks\ncallbacks = [\n    EarlyStopping(\n    patience=5,\n    min_delta=0.001,\n    restore_best_weights=True,\n    ),\n    ModelCheckpoint('model.h5', verbose=1, save_best_only=True),\n    ReduceLROnPlateau(factor=0.1, patience=5, min_lr=1e-6)\n]\ntrain_steps = (len(x_train)//batch) + 1\nvalid_steps = (len(x_valid)//batch) + 1\n\n# Training\nhistory = model.fit(train_dataset,\n          steps_per_epoch=train_steps,\n          validation_steps=valid_steps,\n          validation_data=valid_dataset,\n          epochs=epochs,\n          callbacks=callbacks\n         )","metadata":{"execution":{"iopub.status.busy":"2021-06-08T08:12:43.282208Z","iopub.execute_input":"2021-06-08T08:12:43.282593Z","iopub.status.idle":"2021-06-08T08:24:01.738131Z","shell.execute_reply.started":"2021-06-08T08:12:43.282552Z","shell.execute_reply":"2021-06-08T08:24:01.736382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_frame = pd.DataFrame(history.history)\nhistory_frame.loc[:, ['loss', 'val_loss']].plot()\nhistory_frame.loc[:, ['acc', 'val_acc']].plot();","metadata":{"execution":{"iopub.status.busy":"2021-06-08T08:24:01.74197Z","iopub.execute_input":"2021-06-08T08:24:01.74233Z","iopub.status.idle":"2021-06-08T08:24:02.152111Z","shell.execute_reply.started":"2021-06-08T08:24:01.742297Z","shell.execute_reply":"2021-06-08T08:24:02.15103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model.load_weights('model.h5')","metadata":{"execution":{"iopub.status.busy":"2021-06-08T08:24:02.155513Z","iopub.execute_input":"2021-06-08T08:24:02.155812Z","iopub.status.idle":"2021-06-08T08:24:02.164108Z","shell.execute_reply.started":"2021-06-08T08:24:02.155784Z","shell.execute_reply":"2021-06-08T08:24:02.162512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.chdir('./')","metadata":{"execution":{"iopub.status.busy":"2021-06-08T10:03:13.956251Z","iopub.execute_input":"2021-06-08T10:03:13.956601Z","iopub.status.idle":"2021-06-08T10:03:13.960736Z","shell.execute_reply.started":"2021-06-08T10:03:13.956565Z","shell.execute_reply":"2021-06-08T10:03:13.959626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not os.path.exists('./images'):\n    os.makedirs('./images')\n\n#os.chdir('./images')\n\nfor i, path in tqdm(enumerate(x_valid[:10])):\n\n    print(path)\n#    image = cv2.imread('../input/dog-breed-identification/train/000bec180eb18c7604dcecc8fe0dba07.jpg', cv2.IMREAD_COLOR)\n#   display(Image(filename=path, width=224, height=224))\n    try:\n        image = cv2.imread(path, size)\n        print(image.shape)\n        image = cv2.resize(image, DIM)\n        image = np.expand_dims(image, axis=0)\n        print(image)\n        pred = model.predict(image)[0]\n        label_idx = np.argmax(pred)\n        breed_name = id_to_breed[label_idx]\n\n        ori_breed = id_to_breed[y_valid[i]]\n        ori_image = cv2.imread(path, cv2.IMREAD_COLOR)\n\n        ori_image = cv2.putText(ori_image, breed_name, (0, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1)\n        ori_image = cv2.putText(ori_image, ori_breed, (0, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1)\n\n        cv2.imwrite(f\"images/valid_{i}.png\", ori_image)\n    except Exception as e:\n        print(str(e))\n\n\n# List files and directories  \n# in 'C:/Users / Rajnish / Desktop / GeeksforGeeks'  \nprint(\"After saving image:\")  \nprint(os.listdir('./images'))\n  \nprint('Successfully saved')","metadata":{"execution":{"iopub.status.busy":"2021-06-08T10:22:38.524148Z","iopub.execute_input":"2021-06-08T10:22:38.524693Z","iopub.status.idle":"2021-06-08T10:22:38.548673Z","shell.execute_reply.started":"2021-06-08T10:22:38.524663Z","shell.execute_reply":"2021-06-08T10:22:38.543335Z"},"trusted":true},"execution_count":null,"outputs":[]}]}