{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\nimport glob \nimport os \nimport cv2\nimport math\nfrom keras import applications,layers\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D,MaxPooling2D,Convolution2D,Activation,Flatten,Dense,Dropout,MaxPool2D,BatchNormalization\nfrom keras.utils import to_categorical\nfrom keras.preprocessing.image import ImageDataGenerator\nimport numpy as np\nimport tensorflow as tf","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = applications.VGG19(include_top=False, weights=None,input_shape=(256, 256, 3))\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n# dimensions of our images.  \nimg_width, img_height = 256, 256  \n   \ntop_model_weights_path = 'bottleneck_fc_model.h5'  \ntrain_data_dir = '/kaggle/input/crack-identification-ce784a-2020-iitk/train'  \nvalidation_data_dir = '/kaggle/input/crack-identification-ce784a-2020-iitk/train'  \n   \n # number of epochs to train top model  \nepochs = 50  \n # batch size used by flow_from_directory and predict_generator  \nbatch_size = 16\n\ndatagen = ImageDataGenerator(rescale=1. / 255)  \n   \ngenerator = datagen.flow_from_directory(  \n     train_data_dir,  \n     target_size=(img_width, img_height),  \n     batch_size=batch_size,  \n     class_mode=None,  \n     shuffle=False)  \n\nnb_train_samples = len(generator.filenames)  \nnum_classes = len(generator.class_indices)  \n\npredict_size_train = int(math.ceil(nb_train_samples / batch_size))  \nbottleneck_features_train = model.predict_generator(  \n     generator, predict_size_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"np.save('bottleneck_features_train.npy', bottleneck_features_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bottleneck_features_train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"generator = datagen.flow_from_directory(  \n     '/kaggle/input/crack-identification-ce784a-2020-iitk/train',  \n     target_size=(100, 100),  \n     batch_size=batch_size,  \n     class_mode=None,  \n     shuffle=False)  \n   \nnb_validation_samples = len(generator.filenames)  \n   \npredict_size_validation = int(math.ceil(nb_validation_samples /batch_size))  \n   \nbottleneck_features_validation = model.predict_generator(  \n     generator, predict_size_validation)  \n   \nnp.save('bottleneck_features_validation.npy', bottleneck_features_validation)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ndatagen_top = ImageDataGenerator(rescale=1./255)  \ndata_augmentation = tf.keras.Sequential([\n  layers.experimental.preprocessing.RandomFlip(\"horizontal_and_vertical\"),\n  layers.experimental.preprocessing.RandomRotation(0.2),\n])\ngenerator_top = datagen_top.flow_from_directory(  \n         train_data_dir,  \n         target_size=(img_width, img_height),  \n         batch_size=batch_size,  \n         class_mode='categorical',  \n         shuffle=False)  \n   \nnb_train_samples = 10000\nnum_classes = len(generator_top.class_indices)  \n   \ntrain_data = np.load('bottleneck_features_train.npy')[:100000]  \ntrain_labels = generator_top.classes[:100000]   \ntrain_labels = to_categorical(train_labels, num_classes=num_classes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"generator_top = datagen_top.flow_from_directory(  \n         validation_data_dir,  \n         target_size=(img_width, img_height),  \n         batch_size=batch_size,  \n         class_mode='categorical',  \n         shuffle=False)  \n   \nnb_validation_samples =4968\n   \nvalidation_data = np.load('bottleneck_features_validation.npy')[10000:]\n   \nvalidation_labels = generator_top.classes[10000:] \nvalidation_labels = to_categorical(validation_labels, num_classes=num_classes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data.shape, train_labels.shape, validation_labels.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"initial_learning_rate = 0.001\ndef lr_step_decay(epoch, lr):\n    drop_rate = 0.5\n    epochs_drop = 10.0\n    return initial_learning_rate * math.pow(drop_rate, math.floor(epoch/epochs_drop))\n    #return 0.005","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nmodel = Sequential()  \nmodel.add(Flatten(input_shape=train_data.shape[1:]))  \nmodel.add(Dense(256, activation='relu'))  \nmodel.add(Dropout(0.5))  \nmodel.add(Dense(num_classes, activation='softmax'))  \n \nmodel.compile(optimizer='rmsprop',  \n              loss='categorical_crossentropy', metrics=['accuracy'])  \n   \nhistory = model.fit(train_data, train_labels,  \n          epochs=50,  \n          batch_size=32,callbacks=[tf.keras.callbacks.LearningRateScheduler(lr_step_decay, verbose=1)],validation_split = 0.1)  \n   \nmodel.save_weights('top_model_weights.h5')  \n   \n#(eval_loss, eval_accuracy) = model.evaluate(  validation_data, validation_labels, batch_size=batch_size, verbose=1)\n#print(\"[INFO] accuracy: {:.2f}%\".format(eval_accuracy * 100))  \n#print(\"[INFO] Loss: {}\".format(eval_loss))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def predict_img(image):\n    # build the VGG16 network  \n    model = applications.VGG16(include_top=False, weights=None)  \n\n    bottleneck_prediction = model.predict(image)  \n\n    model = Sequential()  \n    model.add(Flatten(input_shape=bottleneck_prediction.shape))  \n    model.add(Dense(256, activation='relu'))  \n    model.add(Dropout(0.5))  \n    model.add(Dense(num_classes, activation='sigmoid'))  \n\n    model.load_weights('./top_model_weights.h5')  \n\n    class_predicted = model.predict_classes(bottleneck_prediction)  \n    return label_dict[class_predicted[0]]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data_dir = '/kaggle/input/crack-identification-ce784a-2020-iitk/test'  \ncrack=[]\n#for dirname, _, filenames in os.walk('/kaggle/input/crack-identification-ce784a-2020-iitk/test'):\n#    for filename in filenames:\nimg = tf.keras.utils.get_file(origin='/kaggle/input/crack-identification-ce784a-2020-iitk/test', \n                                   fname='1.jpg')\nprint(predict_img(img))","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}