{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import cv2                 \nimport numpy as np         \nimport os                  \nfrom random import shuffle \nfrom tqdm import tqdm      \n\nTRAIN_DIR = '/potholeVSnormal/train'\nTEST_DIR = '/content/test'\nIMG_SIZE = 50\nLR = 1e-3\n\nMODEL_NAME = 'potholeVSnormal-{}-{}.model'.format(LR, '8conv-basic')\n\ndef label_img(img): \n    word_label = img.split('.')[-3] \n\n    if word_label == 'pothole': return [1, 0] \n    elif word_label == 'normal': return [0, 1] \ndef create_train_data(): \n    training_data = []  \n    for img in tqdm(os.listdir(TRAIN_DIR)):  \n      try:\n          label = label_img(img)  \n          path = os.path.join(TRAIN_DIR, img)  \n          img = cv2.imread(path, cv2.IMREAD_GRAYSCALE) \n          if img is not None:\n              img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))\n          else :\n            pass\n          training_data.append([np.array(img), np.array(label)]) \n      except:\n        pass\n  \n \n    shuffle(training_data) \n    np.save('train_data.npy', training_data) \n    return training_data \n  \n\ndef process_test_data(): \n    testing_data = [] \n    for img in tqdm(os.listdir(TEST_DIR)): \n        path = os.path.join(TEST_DIR, img) \n        img_num = img.split('.')[0] \n        img = cv2.imread(path, cv2.IMREAD_GRAYSCALE) \n        img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))\n        testing_data.append([np.array(img), img_num]) \n          \n    shuffle(testing_data) \n    np.save('test_data.npy', testing_data) \n    return testing_data \n\n\ntrain_data = create_train_data()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","collapsed":true,"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":false},"cell_type":"code","source":"train_data = np.load('train_data.npy',allow_pickle=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import torch\nimport tflearn\nfrom tflearn.layers.conv import conv_2d, max_pool_2d\nfrom tflearn.layers.core import input_data, dropout, fully_connected\nfrom tflearn.layers.estimator import regression\n\n#\n\nimport tensorflow as tf\ntf.reset_default_graph()\n\nconvnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')\n\nconvnet = conv_2d(convnet, 32, 5, activation='relu')\nconvnet = max_pool_2d(convnet, 5)\n\nconvnet = conv_2d(convnet, 64, 5, activation='relu')\nconvnet = max_pool_2d(convnet, 5)\n\nconvnet = conv_2d(convnet, 128, 5, activation='relu')\nconvnet = max_pool_2d(convnet, 5)\n\nconvnet = conv_2d(convnet, 512 ,5, activation='relu')\nconvnet = max_pool_2d(convnet, 5)\n\nconvnet = conv_2d(convnet, 128, 5, activation='relu')\nconvnet = max_pool_2d(convnet, 5)\n\nconvnet = conv_2d(convnet, 64, 5, activation='relu')\nconvnet = max_pool_2d(convnet, 5)\n\nconvnet = conv_2d(convnet, 32, 5, activation='relu')\nconvnet = max_pool_2d(convnet, 5)\n\nconvnet = fully_connected(convnet, 1024, activation='relu')\nconvnet = dropout(convnet, 0.8)\n\nconvnet = fully_connected(convnet, 2, activation='softmax')\nconvnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')\n\nmodel = tflearn.DNN(convnet, tensorboard_dir='log')\n\n\n\ntrain = train_data[:-500]\ntest = train_data[-500:]\n\nX = np.array([list(i[0]) for i in train]).reshape(train.shape[0], IMG_SIZE, IMG_SIZE, 1)\n\nY = [i[1] for i in train]\ntest_x = np.array([i[0] for i in test]).reshape(0,IMG_SIZE,IMG_SIZE, 1)\ntest_y = [i[1] for i in test]\n\nmodel.fit({'input': X}, {'targets': Y}, n_epoch=10, validation_set=({'input': test_x}, {'targets': test_y}), \n    snapshot_step=500, show_metric=True, run_id=MODEL_NAME)\n\nmodel.save(MODEL_NAME)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}